Moved Plack::Request and Plack::Response use to individual State:: subclasses.
Tatsuhiko Miyagawa [Sat, 9 Jan 2010 20:50:10 +0000 (12:50 -0800)]
Refactored Cookie State to remove code duplicates.

lib/Plack/Middleware/Session.pm
lib/Plack/Session/State.pm
lib/Plack/Session/State/Cookie.pm

index d022f70..1a325f3 100644 (file)
@@ -5,8 +5,6 @@ use warnings;
 our $VERSION   = '0.03';
 our $AUTHORITY = 'cpan:STEVAN';
 
-use Plack::Request;
-use Plack::Response;
 use Plack::Util;
 use Scalar::Util;
 
@@ -44,13 +42,11 @@ sub call {
     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'} = {};
     }
 
@@ -61,27 +57,21 @@ sub call {
     }
 
     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 {
@@ -98,16 +88,16 @@ 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);
     }
 }
 
index b369cd3..9082e60 100644 (file)
@@ -26,7 +26,7 @@ sub new {
 }
 
 sub expire_session_id {
-    my ($self, $id, $response) = @_;
+    my ($self, $id, $res) = @_;
 }
 
 sub validate_session_id {
@@ -35,14 +35,14 @@ 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 );
@@ -56,7 +56,7 @@ sub generate {
 
 
 sub finalize {
-    my ($self, $id, $response, $options) = @_;
+    my ($self, $id, $res, $options) = @_;
     ();
 }
 
@@ -127,9 +127,9 @@ This is a regex used to validate requested session id.
 
 =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.
 
@@ -138,14 +138,12 @@ 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 )>
 
index 36728bf..2b33edb 100644 (file)
@@ -6,6 +6,8 @@ our $VERSION   = '0.03';
 our $AUTHORITY = 'cpan:STEVAN';
 
 use parent 'Plack::Session::State';
+use Plack::Request;
+use Plack::Response;
 
 use Plack::Util::Accessor qw[
     path
@@ -15,30 +17,35 @@ use Plack::Util::Accessor qw[
 ];
 
 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;