tidying up
[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.09';
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} = { 
57             value => $sid, 
58             expires => "+" . $self->config->{session}->{expires} . "s" 
59                                            } 
60           if $set;
61         if ( $c->config->{session}->{rewrite} ) {
62             my $finder = URI::Find->new(
63                 sub {
64                     my ( $uri, $orig ) = @_;
65                     my $base = $c->request->base;
66                     return $orig unless $orig =~ /^$base/;
67                     return $orig if $uri->path =~ /\/-\//;
68                     return $c->uri($orig);
69                 }
70             );
71             $finder->find( \$c->res->{body} ) if $c->res->body;
72         }
73     }
74     return $c->NEXT::finalize(@_);
75 }
76
77 =head3 prepare_action
78
79 =cut
80
81 sub prepare_action {
82     my $c = shift;
83     if ( $c->request->path =~ /^(.*)\/\-\/(.+)$/ ) {
84         $c->request->path($1);
85         $c->sessionid($2);
86         $c->log->debug(qq/Found sessionid "$2" in path/) if $c->debug;
87     }
88     if ( my $cookie = $c->request->cookies->{session} ) {
89         my $sid = $cookie->value;
90         $c->sessionid($sid);
91         $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
92     }
93     $c->NEXT::prepare_action(@_);
94 }
95
96 sub session {
97     my $c = shift;
98     return $c->{session} if $c->{session};
99     my $sid = $c->sessionid;
100     if (   $sid
101         && $c->_session
102         && ( $c->{session} = $c->_session->get($sid) ) )
103     {
104         $c->log->debug(qq/Found session "$sid"/) if $c->debug;
105         return $c->{session};
106     }
107     else {
108         my $sid = Digest::MD5::md5_hex( time, rand, $$, 'catalyst' );
109         $c->sessionid($sid);
110         $c->log->debug(qq/Created session "$sid"/) if $c->debug;
111         return $c->{session} = {};
112     }
113 }
114
115 =head3 setup
116
117 =cut
118
119 sub setup {
120     my $self = shift;
121     $self->config->{session}->{storage} ||= '/tmp/session';
122     $self->config->{session}->{expires} ||= 60 * 60 * 24;
123     $self->config->{session}->{rewrite} ||= 0;
124
125     $self->_session(
126         Cache::FastMmap->new(
127             share_file  => $self->config->{session}->{storage},
128             expire_time => $self->config->{session}->{expires}
129         )
130     );
131
132     return $self->NEXT::setup(@_);
133 }
134
135 =head2 METHODS
136
137 =head3 session
138
139 =head3 uri
140
141 Extends an uri with session id if needed.
142
143     my $uri = $c->uri('http://localhost/foo');
144
145 =cut
146
147 sub uri {
148     my ( $c, $uri ) = @_;
149     if ( my $sid = $c->sessionid ) {
150         $uri = URI->new($uri);
151         my $path = $uri->path;
152         $path .= '/' unless $path =~ /\/$/;
153         $uri->path( $path . "-/$sid" );
154         return $uri->as_string;
155     }
156     return $uri;
157 }
158
159 =head2 CONFIG OPTIONS
160
161 =head3 rewrite
162
163 To enable automatic storing of sessions in the url set this to a true value.
164
165 =head3 storage
166
167 File to mmap for sharing of data, defaults to /tmp/session.
168
169 =head3 expires
170
171 how many seconds until the session expires. defaults to 1 day
172
173 =head1 SEE ALSO
174
175 L<Catalyst> L<Cache::FastMmap>.
176
177 =head1 AUTHOR
178
179 Sebastian Riedel, C<sri@cpan.org>
180 Marcus Ramberg C<mramberg@cpan.org>
181
182 =head1 COPYRIGHT
183
184 This program is free software, you can redistribute it and/or modify it
185 under the same terms as Perl itself.
186
187 =cut
188
189 1;