Accept expires as seconds since new Plack::Response ditches CGI::Simple::Cookie.
[catagits/Web-Session.git] / lib / Plack / Session / State / Cookie.pm
index 85c5908..36639db 100644 (file)
@@ -2,10 +2,12 @@ package Plack::Session::State::Cookie;
 use strict;
 use warnings;
 
-our $VERSION   = '0.02';
+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
@@ -14,34 +16,36 @@ use Plack::Util::Accessor qw[
     secure
 ];
 
-sub expire_session_id {
-    my ($self, $id) = @_;
-    $self->SUPER::expire_session_id( $id );
-    $self->expires( 0 );
+sub get_session_id {
+    my ($self, $env) = @_;
+    Plack::Request->new($env)->cookies->{$self->session_key};
 }
 
-sub get_session_id_from_request {
-    my ($self, $request) = @_;
-    ( $request->cookie( $self->session_key ) || return )->value;
+sub expire_session_id {
+    my ($self, $id, $res, $options) = @_;
+    $self->_set_cookie($id, $res, expires => time);
 }
 
 sub finalize {
-    my ($self, $id, $response) = @_;
+    my ($self, $id, $res, $options) = @_;
+    $self->_set_cookie($id, $res, (defined $self->expires ? (expires => time + $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,
     };
 
-    # clear the expires after
-    # finalization if the session
-    # has been expired - SL
-    $self->expires( undef )
-        if defined $self->expires
-        && $self->expires == 0
-        && $self->is_session_expired( $id );
+    my $final_r = $response->finalize;
+    $res->[1] = $final_r->[1]; # headers
 }
 
 1;
@@ -95,8 +99,9 @@ be included in the cookie.
 
 =item B<expires>
 
-Expiration time of the cookie, if nothing is supplied then it will
-not be included in the cookie.
+Expiration time of the cookie in seconds, if nothing is supplied then
+it will not be included in the cookie, which means the session expires
+per browser session.
 
 =item B<secure>
 
@@ -117,7 +122,7 @@ Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2009 Infinity Interactive, Inc.
+Copyright 2009, 2010 Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>