Refactored Cookie State to remove code duplicates.
our $VERSION = '0.03';
our $AUTHORITY = 'cpan:STEVAN';
-use Plack::Request;
-use Plack::Response;
use Plack::Util;
use Scalar::Util;
my $self = shift;
my $env = shift;
- my $request = Plack::Request->new($env);
-
- my($id, $session) = $self->get_session($request);
+ my($id, $session) = $self->get_session($env);
if ($id && $session) {
$env->{'psgix.session'} = $session;
} else {
- $id = $self->generate_id($request);
+ $id = $self->generate_id($env);
$env->{'psgix.session'} = {};
}
}
my $res = $self->app->($env);
- $self->response_cb($res, sub {
- my $res = Plack::Response->new(@{$_[0]});
- $self->finalize($env, $res);
- $res = $res->finalize;
- $_[0]->[0] = $res->[0];
- $_[0]->[1] = $res->[1];
- });
+ $self->response_cb($res, sub { $self->finalize($env, $_[0]) });
}
sub get_session {
- my($self, $request) = @_;
+ my($self, $env) = @_;
- my $id = $self->state->extract($request) or return;
- my $session = $self->store->fetch($id) or return;
+ my $id = $self->state->extract($env) or return;
+ my $session = $self->store->fetch($id) or return;
return ($id, $session);
}
sub generate_id {
- my($self, $request) = @_;
- $self->state->generate($request);
+ my($self, $env) = @_;
+ $self->state->generate($env);
}
sub commit {
}
sub finalize {
- my($self, $env, $response) = @_;
+ my($self, $env, $res) = @_;
my $session = $env->{'psgix.session'};
my $options = $env->{'psgix.session.options'};
$self->commit($env) unless $options->{no_store};
if ($options->{expire}) {
- $self->expire_session($options->{id}, $response, $env);
+ $self->expire_session($options->{id}, $res, $env);
} else {
- $self->save_state($options->{id}, $response, $env);
+ $self->save_state($options->{id}, $res, $env);
}
}
}
sub expire_session_id {
- my ($self, $id, $response) = @_;
+ my ($self, $id, $res) = @_;
}
sub validate_session_id {
}
sub get_session_id {
- my ($self, $request) = @_;
- return $request->param( $self->session_key );
+ my ($self, $env) = @_;
+ return Plack::Request->new($env)->param( $self->session_key );
}
sub extract {
- my ($self, $request) = @_;
+ my ($self, $env) = @_;
- my $id = $self->get_session_id( $request );
+ my $id = $self->get_session_id( $env );
return unless defined $id;
return $id if $self->validate_session_id( $id );
sub finalize {
- my ($self, $id, $response, $options) = @_;
+ my ($self, $id, $res, $options) = @_;
();
}
=over 4
-=item B<get_session_id ( $request )>
+=item B<get_session_id ( $env )>
-This is the method used to extract the session id from a C<$request>.
+This is the method used to extract the session id from a C<$env>.
Subclasses will often only need to override this method and the
C<finalize> method.
This will use the C<sid_validator> regex and confirm that the
C<$session_id> is valid.
-=item B<extract ( $request )>
+=item B<extract ( $env )>
-This will attempt to extract the session from a C<$request> by looking
-for the C<session_key> in the C<$request> params. It will then check to
+This will attempt to extract the session from a C<$env> by looking
+for the C<session_key> in the request params. It will then check to
see if the session is valid and that it has not expired. It will return
-the session id if everything is good or undef otherwise. The C<$request>
-is expected to be a L<Plack::Request> instance or an object with an
-equivalent interface.
+the session id if everything is good or undef otherwise.
=item B<generate ( $request )>
our $AUTHORITY = 'cpan:STEVAN';
use parent 'Plack::Session::State';
+use Plack::Request;
+use Plack::Response;
use Plack::Util::Accessor qw[
path
];
sub get_session_id {
- my ($self, $request) = @_;
- ( $request->cookie( $self->session_key ) || return )->value;
+ my ($self, $env) = @_;
+ ( Plack::Request->new($env)->cookie( $self->session_key ) || return )->value;
}
sub expire_session_id {
- my ($self, $id, $response) = @_;
- $response->cookies->{ $self->session_key } = +{
- value => $id,
- path => ($self->path || '/'),
- expires => 0,
- ( defined $self->domain ? ( domain => $self->domain ) : () ),
- ( defined $self->secure ? ( secure => $self->secure ) : () ),
- };
+ my ($self, $id, $res, $options) = @_;
+ $self->_set_cookie($id, $res, expires => 0);
}
sub finalize {
- my ($self, $id, $response, $options) = @_;
+ my ($self, $id, $res, $options) = @_;
+ $self->_set_cookie($id, $res, (defined $self->expires ? (expires => $self->expires) : ()));
+}
+
+sub _set_cookie {
+ my($self, $id, $res, %options) = @_;
+
+ # TODO: Do not use Plack::Response
+ my $response = Plack::Response->new($res);
$response->cookies->{ $self->session_key } = +{
value => $id,
path => ($self->path || '/'),
( defined $self->domain ? ( domain => $self->domain ) : () ),
- ( defined $self->expires ? ( expires => $self->expires ) : () ),
( defined $self->secure ? ( secure => $self->secure ) : () ),
+ %options,
};
+
+ my $final_r = $response->finalize;
+ $res->[1] = $final_r->[1]; # headers
}
1;