C::P::S::FastMmap: expire cookie at same time as session
[catagits/Catalyst-Plugin-Session-State-Cookie.git] / FastMmap.pm
CommitLineData
bf2bce67 1package Catalyst::Plugin::Session::FastMmap;
2
3use strict;
4use base qw/Class::Data::Inheritable Class::Accessor::Fast/;
5use NEXT;
6use Cache::FastMmap;
7use Digest::MD5;
8use URI;
9use URI::Find;
10use File::Temp 'tempdir';
11
0850f14b 12our $VERSION = '0.09';
bf2bce67 13
14__PACKAGE__->mk_classdata('_session');
15__PACKAGE__->mk_accessors('sessionid');
16
17=head1 NAME
18
19Catalyst::Plugin::Session::FastMmap - FastMmap sessions for Catalyst
20
21=head1 SYNOPSIS
22
23 use Catalyst 'Session::FastMmap';
b2f8df5e 24
25 MyApp->config->{session} = {
26 expires => 3600,
27 rewrite => 1,
28 storage => '/tmp/session'
29 };
bf2bce67 30
31 $c->session->{foo} = 'bar';
32 print $c->sessionid;
33
34=head1 DESCRIPTION
35
36Fast sessions.
37
38=head2 EXTENDED METHODS
39
40=head3 finalize
41
42=cut
43
44sub finalize {
b2f8df5e 45 my $c = shift;
46 if ( $c->config->{session}->{rewrite} ) {
ad142f4c 47 my $redirect = $c->response->redirect;
48 $c->response->redirect( $c->uri($redirect) ) if $redirect;
49 }
bf2bce67 50 if ( my $sid = $c->sessionid ) {
51 $c->_session->set( $sid, $c->session );
52 my $set = 1;
53 if ( my $cookie = $c->request->cookies->{session} ) {
54 $set = 0 if $cookie->value eq $sid;
55 }
964ca63e 56 $c->response->cookies->{session} = { value => $sid, expires => "+" . $self->config->{session}->{expires} . "s" } if $set;
b2f8df5e 57 if ( $c->config->{session}->{rewrite} ) {
58 my $finder = URI::Find->new(
59 sub {
60 my ( $uri, $orig ) = @_;
61 my $base = $c->request->base;
62 return $orig unless $orig =~ /^$base/;
63 return $orig if $uri->path =~ /\/-\//;
64 return $c->uri($orig);
65 }
66 );
0850f14b 67 $finder->find( \$c->res->{body} ) if $c->res->body;
58c05d1a 68 }
bf2bce67 69 }
70 return $c->NEXT::finalize(@_);
71}
72
73=head3 prepare_action
74
75=cut
76
77sub prepare_action {
78 my $c = shift;
79 if ( $c->request->path =~ /^(.*)\/\-\/(.+)$/ ) {
80 $c->request->path($1);
81 $c->sessionid($2);
82 $c->log->debug(qq/Found sessionid "$2" in path/) if $c->debug;
83 }
84 if ( my $cookie = $c->request->cookies->{session} ) {
85 my $sid = $cookie->value;
86 $c->sessionid($sid);
87 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
88 }
89 $c->NEXT::prepare_action(@_);
90}
91
92sub session {
93 my $c = shift;
94 return $c->{session} if $c->{session};
95 my $sid = $c->sessionid;
617a86d6 96 if ( $sid
97 && $c->_session
98 && ( $c->{session} = $c->_session->get($sid) ) )
99 {
bf2bce67 100 $c->log->debug(qq/Found session "$sid"/) if $c->debug;
101 return $c->{session};
102 }
103 else {
104 my $sid = Digest::MD5::md5_hex( time, rand, $$, 'catalyst' );
105 $c->sessionid($sid);
106 $c->log->debug(qq/Created session "$sid"/) if $c->debug;
107 return $c->{session} = {};
108 }
109}
110
111=head3 setup
112
113=cut
114
115sub setup {
b2f8df5e 116 my $self = shift;
117 $self->config->{session}->{storage} ||= '/tmp/session';
118 $self->config->{session}->{expires} ||= 60 * 60 * 24;
119 $self->config->{session}->{rewrite} ||= 0;
120
bf2bce67 121 $self->_session(
122 Cache::FastMmap->new(
b2f8df5e 123 share_file => $self->config->{session}->{storage},
124 expire_time => $self->config->{session}->{expires}
bf2bce67 125 )
126 );
b2f8df5e 127
bf2bce67 128 return $self->NEXT::setup(@_);
129}
130
131=head2 METHODS
132
133=head3 session
134
135=head3 uri
136
137Extends an uri with session id if needed.
138
139 my $uri = $c->uri('http://localhost/foo');
140
141=cut
142
143sub uri {
144 my ( $c, $uri ) = @_;
145 if ( my $sid = $c->sessionid ) {
146 $uri = URI->new($uri);
147 my $path = $uri->path;
148 $path .= '/' unless $path =~ /\/$/;
149 $uri->path( $path . "-/$sid" );
150 return $uri->as_string;
151 }
152 return $uri;
153}
154
58c05d1a 155=head2 CONFIG OPTIONS
156
b2f8df5e 157=head3 rewrite
58c05d1a 158
b2f8df5e 159To enable automatic storing of sessions in the url set this to a true value.
58c05d1a 160
b2f8df5e 161=head3 storage
58c05d1a 162
b2f8df5e 163File to mmap for sharing of data, defaults to /tmp/session.
58c05d1a 164
b2f8df5e 165=head3 expires
58c05d1a 166
167how many seconds until the session expires. defaults to 1 day
168
bf2bce67 169=head1 SEE ALSO
170
58c05d1a 171L<Catalyst> L<Cache::FastMmap>.
bf2bce67 172
173=head1 AUTHOR
174
175Sebastian Riedel, C<sri@cpan.org>
0c806459 176Marcus Ramberg C<mramberg@cpan.org>
bf2bce67 177
178=head1 COPYRIGHT
179
bfeb5ca0 180This program is free software, you can redistribute it and/or modify it
181under the same terms as Perl itself.
bf2bce67 182
183=cut
184
1851;