X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Plugin-Session-State-Cookie.git;a=blobdiff_plain;f=lib%2FCatalyst%2FPlugin%2FSession%2FState%2FCookie.pm;h=51727e2d6288ac613db3c977d5ea901ff3c9e1a3;hp=698063959a6630adb02721aeff8fc94769d8e3d8;hb=58730edcb664fc621cbb907b477d037c5c8576fa;hpb=cc88d7267f661665aca3f3dda7bb2f0342c237c9 diff --git a/lib/Catalyst/Plugin/Session/State/Cookie.pm b/lib/Catalyst/Plugin/Session/State/Cookie.pm index 6980639..51727e2 100644 --- a/lib/Catalyst/Plugin/Session/State/Cookie.pm +++ b/lib/Catalyst/Plugin/Session/State/Cookie.pm @@ -1,212 +1,166 @@ -package Catalyst::Plugin::Session::FastMmap; +package Catalyst::Plugin::Session::State::Cookie; +use base qw/Catalyst::Plugin::Session::State/; use strict; -use base qw/Class::Data::Inheritable Class::Accessor::Fast/; -use NEXT; -use Cache::FastMmap; -use Digest::MD5; -use URI; -use URI::Find; -use File::Temp 'tempdir'; - -our $VERSION = '0.13'; +use warnings; -__PACKAGE__->mk_classdata('_session'); -__PACKAGE__->mk_accessors('sessionid'); - -=head1 NAME - -Catalyst::Plugin::Session::FastMmap - FastMmap sessions for Catalyst +use NEXT; -=head1 SYNOPSIS +our $VERSION = "0.01"; - use Catalyst 'Session::FastMmap'; - - MyApp->config->{session} = { - expires => 3600, - rewrite => 1, - storage => '/tmp/session' - }; +sub setup_session { + my $c = shift; - $c->session->{foo} = 'bar'; - print $c->sessionid; + $c->NEXT::setup_session(@_); -=head1 DESCRIPTION + $c->config->{session}{cookie_name} ||= "session"; +} -C is a fast session plugin for -Catalyst that uses an mmap'ed file to act as a shared memory -interprocess cache. It is based on C. +sub finalize_cookies { + my $c = shift; + if ( $c->sessionid ) { + $c->update_session_cookie( $c->make_session_cookie ); + } -=head2 EXTENDED METHODS + return $c->NEXT::finalize_cookies(@_); +} -=over 4 +sub update_session_cookie { + my ( $c, $updated ) = @_; + my $cookie_name = $c->config->{session}{cookie_name}; + $c->response->cookies->{$cookie_name} = $updated; +} -=item finalize +sub make_session_cookie { + my $c = shift; -=cut + my $cfg = $c->config->{session}; + my $cookie = { + value => $c->sessionid, + ( $cfg->{cookie_domain} ? ( domain => $cfg->{cookie_domain} ) : () ), + }; -sub finalize { - my $c = shift; - if ( $c->config->{session}->{rewrite} ) { - my $redirect = $c->response->redirect; - $c->response->redirect( $c->uri($redirect) ) if $redirect; + if ( exists $cfg->{cookie_expires} ) { + if ( my $ttl = $cfg->{cookie_expires} ) { + $cookie->{expires} = time() + $ttl; + } # else { cookie is non-persistent } } - if ( my $sid = $c->sessionid ) { - $c->_session->set( $sid, $c->session ); - my $set = 1; - if ( my $cookie = $c->request->cookies->{session} ) { - $set = 0 if $cookie->value eq $sid; - } - if ( $set ) { - $c->response->cookies->{session} = { - value => $sid - }; - } - if ( $c->config->{session}->{rewrite} ) { - my $finder = URI::Find->new( - sub { - my ( $uri, $orig ) = @_; - my $base = $c->request->base; - return $orig unless $orig =~ /^$base/; - return $orig if $uri->path =~ /\/-\//; - return $c->uri($orig); - } - ); - $finder->find( \$c->res->{body} ) if $c->res->body; - } + else { + $cookie->{expires} = $c->session->{__expires}; } - return $c->NEXT::finalize(@_); + + return $cookie; } -=item prepare_action +sub prepare_cookies { + my $c = shift; -=cut + my $ret = $c->NEXT::prepare_cookies(@_); -sub prepare_action { - my $c = shift; - if ( $c->request->path =~ /^(.*)\/\-\/(.+)$/ ) { - $c->request->path($1); - $c->sessionid($2); - $c->log->debug(qq/Found sessionid "$2" in path/) if $c->debug; - } - if ( my $cookie = $c->request->cookies->{session} ) { + my $cookie_name = $c->config->{session}{cookie_name}; + + if ( my $cookie = $c->request->cookies->{$cookie_name} ) { my $sid = $cookie->value; $c->sessionid($sid); $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug; } - $c->NEXT::prepare_action(@_); -} -sub session { - my $c = shift; - return $c->{session} if $c->{session}; - my $sid = $c->sessionid; - if ( $sid - && $c->_session - && ( $c->{session} = $c->_session->get($sid) ) ) - { - $c->log->debug(qq/Found session "$sid"/) if $c->debug; - return $c->{session}; - } - else { - my $sid = Digest::MD5::md5_hex( time, rand, $$, 'catalyst' ); - $c->sessionid($sid); - $c->log->debug(qq/Created session "$sid"/) if $c->debug; - return $c->{session} = {}; - } + return $ret; } -=item setup +__PACKAGE__ -Sets up the session cache file. +__END__ -=cut +=pod -sub setup { - my $self = shift; - $self->config->{session}->{storage} ||= '/tmp/session'; - $self->config->{session}->{expires} ||= 60 * 60 * 24; - $self->config->{session}->{rewrite} ||= 0; +=head1 NAME - $self->_session( - Cache::FastMmap->new( - share_file => $self->config->{session}->{storage}, - expire_time => $self->config->{session}->{expires} - ) - ); +Catalyst::Plugin::Session::State::Cookie - A session ID - return $self->NEXT::setup(@_); -} +=head1 SYNOPSIS + + use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/; + +=head1 DESCRIPTION + +In order for L to work the session ID needs to be +stored on the client, and the session data needs to be stored on the server. + +This plugin stores the session ID on the client using the cookie mechanism. + +=head1 METHODS + +=over 4 + +=item make_session_cookie + +Returns a hash reference with the default values for new cookies. + +=item update_session_cookie $hash_ref + +Sets the cookie based on C in the response object. =back -=head2 METHODS +=head1 EXTENDED METHODS =over 4 -=item session +=item prepare_cookies -=item uri +Will restore if an appropriate cookie is found. -Extends an uri with session id if needed. +=item finalize_cookies - my $uri = $c->uri('http://localhost/foo'); +Will set a cookie called C if it doesn't exist or if it's value is not +the current session id. -=cut +=item setup_session -sub uri { - my ( $c, $uri ) = @_; - if ( my $sid = $c->sessionid ) { - $uri = URI->new($uri); - my $path = $uri->path; - $path .= '/' unless $path =~ /\/$/; - $uri->path( $path . "-/$sid" ); - return $uri->as_string; - } - return $uri; -} +Will set the C parameter to it's default value if it isn't set. =back -=head2 CONFIG OPTIONS +=head1 CONFIGURATION =over 4 -=item rewrite +=item cookie_name -If set to a true value sessions are automatically stored in the url; -defaults to false. +The name of the cookie to store (defaults to C). -=item storage +=item cookie_domain -Specifies the file to be used for the sharing of session data; -defaults to C. +The name of the domain to store in the cookie (defaults to current host) -Note that the file will be created with mode 0640, which means that it -will only be writeable by processes running with the same uid as the -process that creates the file. If this may be a problem, for example -if you may try to debug the program as one user and run it as another, -specify a filename like C<< /tmp/session-$> >>, which includes the -UID of the process in the filename. +=back +=head1 CAVEATS -=item expires +Sessions have to be created before the first write to be saved. For example: -Specifies the session expiry time in seconds; defaults to 86,400, -i.e. one day. + sub action : Local { + my ( $self, $c ) = @_; + $c->res->write("foo"); + $c->session( ... ); + ... + } -=back +Will cause a session ID to not be set, because by the time a session is +actually created the headers have already been sent to the client. =head1 SEE ALSO -L, L. +L, L. =head1 AUTHOR Sebastian Riedel ECE, Marcus Ramberg ECE, -Andrew Ford ECE +Andrew Ford ECE, +Yuval Kogman ECE =head1 COPYRIGHT