removed cookie expiriation
[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
c4390601 12our $VERSION = '0.13';
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 }
c9fb61f3 56 if ( $set ) {
57 $c->response->cookies->{session} = {
c4390601 58 value => $sid
c9fb61f3 59 };
60 }
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
81sub 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
96sub 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
119sub setup {
b2f8df5e 120 my $self = shift;
121 $self->config->{session}->{storage} ||= '/tmp/session';
6f9d2cbb 122 $self->config->{session}->{expires} ||= 60 * 60 * 24;
b2f8df5e 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
141Extends an uri with session id if needed.
142
143 my $uri = $c->uri('http://localhost/foo');
144
145=cut
146
147sub 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 163To enable automatic storing of sessions in the url set this to a true value.
58c05d1a 164
b2f8df5e 165=head3 storage
58c05d1a 166
b2f8df5e 167File to mmap for sharing of data, defaults to /tmp/session.
58c05d1a 168
b2f8df5e 169=head3 expires
58c05d1a 170
171how many seconds until the session expires. defaults to 1 day
172
bf2bce67 173=head1 SEE ALSO
174
58c05d1a 175L<Catalyst> L<Cache::FastMmap>.
bf2bce67 176
177=head1 AUTHOR
178
179Sebastian Riedel, C<sri@cpan.org>
0c806459 180Marcus Ramberg C<mramberg@cpan.org>
bf2bce67 181
182=head1 COPYRIGHT
183
bfeb5ca0 184This program is free software, you can redistribute it and/or modify it
185under the same terms as Perl itself.
bf2bce67 186
187=cut
188
1891;