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 | |
0c806459 |
12 | our $VERSION = '0.05'; |
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'; |
24 | |
25 | $c->session->{foo} = 'bar'; |
26 | print $c->sessionid; |
27 | |
28 | =head1 DESCRIPTION |
29 | |
30 | Fast sessions. |
31 | |
32 | =head2 EXTENDED METHODS |
33 | |
34 | =head3 finalize |
35 | |
36 | =cut |
37 | |
38 | sub finalize { |
39 | my $c = shift; |
40 | my $redirect = $c->response->redirect; |
41 | $c->response->redirect( $c->uri($redirect) ) if $redirect; |
42 | if ( my $sid = $c->sessionid ) { |
43 | $c->_session->set( $sid, $c->session ); |
44 | my $set = 1; |
45 | if ( my $cookie = $c->request->cookies->{session} ) { |
46 | $set = 0 if $cookie->value eq $sid; |
47 | } |
48 | $c->response->cookies->{session} = { value => $sid } if $set; |
49 | my $finder = URI::Find->new( |
50 | sub { |
51 | my ( $uri, $orig ) = @_; |
52 | my $base = $c->request->base; |
53 | return $orig unless $orig =~ /^$base/; |
54 | return $orig if $uri->path =~ /\/-\//; |
55 | return $c->uri($orig); |
56 | } |
57 | ); |
58 | $finder->find( \$c->response->{output} ); |
59 | } |
60 | return $c->NEXT::finalize(@_); |
61 | } |
62 | |
63 | =head3 prepare_action |
64 | |
65 | =cut |
66 | |
67 | sub prepare_action { |
68 | my $c = shift; |
69 | if ( $c->request->path =~ /^(.*)\/\-\/(.+)$/ ) { |
70 | $c->request->path($1); |
71 | $c->sessionid($2); |
72 | $c->log->debug(qq/Found sessionid "$2" in path/) if $c->debug; |
73 | } |
74 | if ( my $cookie = $c->request->cookies->{session} ) { |
75 | my $sid = $cookie->value; |
76 | $c->sessionid($sid); |
77 | $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug; |
78 | } |
79 | $c->NEXT::prepare_action(@_); |
80 | } |
81 | |
82 | sub session { |
83 | my $c = shift; |
84 | return $c->{session} if $c->{session}; |
85 | my $sid = $c->sessionid; |
0c806459 |
86 | if ( $sid && $c->_session && |
87 | ( $c->{session} = $c->_session->get($sid) ) ) { |
bf2bce67 |
88 | $c->log->debug(qq/Found session "$sid"/) if $c->debug; |
89 | return $c->{session}; |
90 | } |
91 | else { |
92 | my $sid = Digest::MD5::md5_hex( time, rand, $$, 'catalyst' ); |
93 | $c->sessionid($sid); |
94 | $c->log->debug(qq/Created session "$sid"/) if $c->debug; |
95 | return $c->{session} = {}; |
96 | } |
97 | } |
98 | |
99 | =head3 setup |
100 | |
101 | =cut |
102 | |
103 | sub setup { |
104 | my $self = shift; |
105 | my $cache_root = $self->config->{cache_root} || tempdir; |
106 | my $default_expires_in = $self->config->{default_expires_in} |
107 | || 60 * 60 * 24; |
108 | my $auto_purge_interval = $self->config->{auto_purge_interval} |
109 | || 60 * 60 * 24; |
110 | my $auto_purge_on_set = $self->config->{auto_purge_on_set} || 1; |
111 | $self->_session( |
112 | Cache::FastMmap->new( |
113 | cache_root => $cache_root, |
114 | default_expires_in => $default_expires_in, |
115 | auto_purge_interval => $auto_purge_interval, |
116 | auto_purge_on_set => $auto_purge_on_set |
117 | ) |
118 | ); |
119 | return $self->NEXT::setup(@_); |
120 | } |
121 | |
122 | =head2 METHODS |
123 | |
124 | =head3 session |
125 | |
126 | =head3 uri |
127 | |
128 | Extends an uri with session id if needed. |
129 | |
130 | my $uri = $c->uri('http://localhost/foo'); |
131 | |
132 | =cut |
133 | |
134 | sub uri { |
135 | my ( $c, $uri ) = @_; |
136 | if ( my $sid = $c->sessionid ) { |
137 | $uri = URI->new($uri); |
138 | my $path = $uri->path; |
139 | $path .= '/' unless $path =~ /\/$/; |
140 | $uri->path( $path . "-/$sid" ); |
141 | return $uri->as_string; |
142 | } |
143 | return $uri; |
144 | } |
145 | |
146 | =head1 SEE ALSO |
147 | |
148 | L<Catalyst>. |
149 | |
150 | =head1 AUTHOR |
151 | |
152 | Sebastian Riedel, C<sri@cpan.org> |
0c806459 |
153 | Marcus Ramberg C<mramberg@cpan.org> |
bf2bce67 |
154 | |
155 | =head1 COPYRIGHT |
156 | |
157 | This program is free software, you can redistribute it and/or modify it under |
158 | the same terms as Perl itself. |
159 | |
160 | =cut |
161 | |
162 | 1; |