Commit | Line | Data |
bf2bce67 |
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 | |
0850f14b |
12 | our $VERSION = '0.09'; |
bf2bce67 |
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'; |
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 | |
36 | Fast sessions. |
37 | |
38 | =head2 EXTENDED METHODS |
39 | |
40 | =head3 finalize |
41 | |
42 | =cut |
43 | |
44 | sub 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 | } |
5e0e17c8 |
56 | $c->response->cookies->{session} = { |
57 | value => $sid, |
58 | expires => "+" . $self->config->{session}->{expires} . "s" |
59 | } |
60 | if $set; |
b2f8df5e |
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 | ); |
0850f14b |
71 | $finder->find( \$c->res->{body} ) if $c->res->body; |
58c05d1a |
72 | } |
bf2bce67 |
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; |
617a86d6 |
100 | if ( $sid |
101 | && $c->_session |
102 | && ( $c->{session} = $c->_session->get($sid) ) ) |
103 | { |
bf2bce67 |
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 { |
b2f8df5e |
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 | |
bf2bce67 |
125 | $self->_session( |
126 | Cache::FastMmap->new( |
b2f8df5e |
127 | share_file => $self->config->{session}->{storage}, |
128 | expire_time => $self->config->{session}->{expires} |
bf2bce67 |
129 | ) |
130 | ); |
b2f8df5e |
131 | |
bf2bce67 |
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 | |
58c05d1a |
159 | =head2 CONFIG OPTIONS |
160 | |
b2f8df5e |
161 | =head3 rewrite |
58c05d1a |
162 | |
b2f8df5e |
163 | To enable automatic storing of sessions in the url set this to a true value. |
58c05d1a |
164 | |
b2f8df5e |
165 | =head3 storage |
58c05d1a |
166 | |
b2f8df5e |
167 | File to mmap for sharing of data, defaults to /tmp/session. |
58c05d1a |
168 | |
b2f8df5e |
169 | =head3 expires |
58c05d1a |
170 | |
171 | how many seconds until the session expires. defaults to 1 day |
172 | |
bf2bce67 |
173 | =head1 SEE ALSO |
174 | |
58c05d1a |
175 | L<Catalyst> L<Cache::FastMmap>. |
bf2bce67 |
176 | |
177 | =head1 AUTHOR |
178 | |
179 | Sebastian Riedel, C<sri@cpan.org> |
0c806459 |
180 | Marcus Ramberg C<mramberg@cpan.org> |
bf2bce67 |
181 | |
182 | =head1 COPYRIGHT |
183 | |
bfeb5ca0 |
184 | This program is free software, you can redistribute it and/or modify it |
185 | under the same terms as Perl itself. |
bf2bce67 |
186 | |
187 | =cut |
188 | |
189 | 1; |