1 package Plack::Middleware::Session;
6 our $AUTHORITY = 'cpan:STEVAN';
14 use parent 'Plack::Middleware';
16 use Plack::Util::Accessor qw(
25 $self->session_class( 'Plack::Session' ) unless $self->session_class;
26 $self->state( 'Cookie' ) unless $self->state;
28 $self->state( $self->inflate_backend('Plack::Session::State', $self->state) );
29 $self->store( $self->inflate_backend('Plack::Session::Store', $self->store) );
33 my($self, $prefix, $backend) = @_;
35 return $backend if defined $backend && Scalar::Util::blessed $backend;
38 push @class, $backend if defined $backend; # undef means the root class
41 Plack::Util::load_class(@class)->new();
48 $env->{'psgix.session'} = $env->{'plack.session'} = $self->session_class->new(
49 state => $self->state,
50 store => $self->store,
51 request => Plack::Request->new( $env )
54 my $res = $self->app->($env);
55 $self->response_cb($res, sub {
56 my $res = Plack::Response->new(@{$_[0]});
57 $env->{'psgix.session'}->finalize( $res );
58 $res = $res->finalize;
59 $_[0]->[0] = $res->[0];
60 $_[0]->[1] = $res->[1];
72 Plack::Middleware::Session - Middleware for session management
82 [ 'Content-Type' => 'text/plain' ],
83 [ 'Hello, your Session ID is ' . $env->{'psgix.session'}->id ]
92 # Or, use the File store backend (great if you use multiprocess server)
93 # For more options, see perldoc Plack::Session::Store::File
95 enable 'Session', store => 'File';
101 This is a Plack Middleware component for session management. By
102 default it will use cookies to keep session state and store data in
103 memory. This distribution also comes with other state and store
104 solutions. See perldoc for these backends how to use them.
106 It should be noted that we store the current session in the
107 C<psgix.session> key inside the C<$env> where you can access it
108 as needed. Additionally, as of version 0.09, you can call the
109 C<session> method of a L<Plack::Request> instance to fetch
110 whatever is stored in C<psgix.session>.
112 B<NOTE:> As of version 0.02 the session is stored in C<psgix.session>
113 instead of C<plack.session>. We still keep a copy of it in
114 C<plack.session>, but this is deprecated and will be removed
121 =item L<Plack::Session::State>
123 This will maintain session state by passing the session through
124 the request params. It does not do this automatically though,
125 you are responsible for passing the session param.
127 =item L<Plack::Session::State::Cookie>
129 This will maintain session state using browser cookies.
137 =item L<Plack::Session::Store>
139 This is your basic in-memory session data store. It is volatile storage
140 and not recommended for multiprocessing environments. However it is
141 very useful for development and testing.
143 =item L<Plack::Session::Store::File>
145 This will persist session data in a file. By default it uses
146 L<Storable> but it can be configured to have a custom serializer and
149 =item L<Plack::Session::Store::Cache>
151 This will persist session data using the L<Cache> interface.
153 =item L<Plack::Session::Store::Null>
155 Sometimes you don't care about storing session data, in that case
156 you can use this noop module.
162 The following are options that can be passed to this mdoule.
168 This is expected to be an instance of L<Plack::Session::State> or an
169 object that implements the same interface. If no option is provided
170 the default L<Plack::Session::State::Cookie> will be used.
174 This is expected to be an instance of L<Plack::Session::Store> or an
175 object that implements the same interface. If no option is provided
176 the default L<Plack::Session::Store> will be used.
178 It should be noted that this default is an in-memory volatile store
179 is only suitable for development (or single process servers). For a
180 more robust solution see L<Plack::Session::Store::File> or
181 L<Plack::Session::Store::Cache>.
183 =item I<session_class>
185 This can be used to override the actual session class. It currently
186 defaults to L<Plack::Session> but you can substitute any class which
187 implements the same interface.
193 All complex software has bugs lurking in it, and this module is no
194 exception. If you find a bug please either email me, or add the bug
201 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
203 =head1 COPYRIGHT AND LICENSE
205 Copyright 2009, 2010 Infinity Interactive, Inc.
207 L<http://www.iinteractive.com>
209 This library is free software; you can redistribute it and/or modify
210 it under the same terms as Perl itself.