updated
[catagits/Catalyst-Plugin-Session-State-Cookie.git] / FastMmap.pm
1 package Catalyst::Plugin::Session::FastMmap;
2
3 use strict;
4 use base qw/Class::Data::Inheritable Class::Accessor::Fast/;
5 use NEXT;
6 use Cache::FastMmap;
7 use Digest::MD5;
8 use URI;
9 use URI::Find;
10 use File::Temp 'tempdir';
11
12 our $VERSION = '0.07';
13
14 __PACKAGE__->mk_classdata('_session');
15 __PACKAGE__->mk_accessors('sessionid');
16
17 =head1 NAME
18
19 Catalyst::Plugin::Session::FastMmap - FastMmap sessions for Catalyst
20
21 =head1 SYNOPSIS
22
23     use Catalyst 'Session::FastMmap';
24     
25     MyApp->config->{session} = {
26         expires => 3600,
27         rewrite => 1,
28         storage => '/tmp/session'
29     };
30
31     $c->session->{foo} = 'bar';
32     print $c->sessionid;
33
34 =head1 DESCRIPTION
35
36 Fast sessions.
37
38 =head2 EXTENDED METHODS
39
40 =head3 finalize
41
42 =cut
43
44 sub finalize {
45     my $c = shift;
46     if ( $c->config->{session}->{rewrite} ) {
47         my $redirect = $c->response->redirect;
48         $c->response->redirect( $c->uri($redirect) ) if $redirect;
49     }
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         }
56         $c->response->cookies->{session} = { value => $sid } if $set;
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             );
67             $finder->find( \$c->res->{output} ) if $c->res->output;
68         }
69     }
70     return $c->NEXT::finalize(@_);
71 }
72
73 =head3 prepare_action
74
75 =cut
76
77 sub 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
92 sub session {
93     my $c = shift;
94     return $c->{session} if $c->{session};
95     my $sid = $c->sessionid;
96     if (   $sid
97         && $c->_session
98         && ( $c->{session} = $c->_session->get($sid) ) )
99     {
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
115 sub setup {
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
121     $self->_session(
122         Cache::FastMmap->new(
123             share_file  => $self->config->{session}->{storage},
124             expire_time => $self->config->{session}->{expires}
125         )
126     );
127
128     return $self->NEXT::setup(@_);
129 }
130
131 =head2 METHODS
132
133 =head3 session
134
135 =head3 uri
136
137 Extends an uri with session id if needed.
138
139     my $uri = $c->uri('http://localhost/foo');
140
141 =cut
142
143 sub 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
155 =head2 CONFIG OPTIONS
156
157 =head3 rewrite
158
159 To enable automatic storing of sessions in the url set this to a true value.
160
161 =head3 storage
162
163 File to mmap for sharing of data, defaults to /tmp/session.
164
165 =head3 expires
166
167 how many seconds until the session expires. defaults to 1 day
168
169 =head1 SEE ALSO
170
171 L<Catalyst> L<Cache::FastMmap>.
172
173 =head1 AUTHOR
174
175 Sebastian Riedel, C<sri@cpan.org>
176 Marcus Ramberg C<mramberg@cpan.org>
177
178 =head1 COPYRIGHT
179
180 This program is free software, you can redistribute it and/or modify it
181 under the same terms as Perl itself.
182
183 =cut
184
185 1;