Commit | Line | Data |
bd992981 |
1 | package Plack::Middleware::Session; |
2 | use strict; |
3 | use warnings; |
4 | |
5 | use Plack::Session; |
6 | use Plack::Request; |
7 | use Plack::Response; |
ad80e445 |
8 | use Plack::Util; |
9 | use Scalar::Util; |
bd992981 |
10 | |
11 | use parent 'Plack::Middleware'; |
12 | |
d6af4aa8 |
13 | use Plack::Util::Accessor qw( |
14 | state |
15 | store |
16 | session_class |
17 | ); |
bd992981 |
18 | |
fe1bfe7d |
19 | sub prepare_app { |
20 | my $self = shift; |
fe1bfe7d |
21 | |
d6af4aa8 |
22 | $self->session_class( 'Plack::Session' ) unless $self->session_class; |
23 | $self->state( 'Cookie' ) unless $self->state; |
ad80e445 |
24 | |
25 | $self->state( $self->inflate_backend('Plack::Session::State', $self->state) ); |
26 | $self->store( $self->inflate_backend('Plack::Session::Store', $self->store) ); |
27 | } |
28 | |
29 | sub inflate_backend { |
30 | my($self, $prefix, $backend) = @_; |
31 | |
32 | return $backend if defined $backend && Scalar::Util::blessed $backend; |
33 | |
34 | my @class; |
35 | push @class, $backend if defined $backend; # undef means the root class |
36 | push @class, $prefix; |
37 | |
38 | Plack::Util::load_class(@class)->new(); |
fe1bfe7d |
39 | } |
40 | |
bd992981 |
41 | sub call { |
42 | my $self = shift; |
43 | my $env = shift; |
44 | |
d6af4aa8 |
45 | $env->{'plack.session'} = $self->session_class->new( |
3dbe8dfa |
46 | state => $self->state, |
bd992981 |
47 | store => $self->store, |
48 | request => Plack::Request->new( $env ) |
49 | ); |
50 | |
fe1bfe7d |
51 | my $res = $self->app->($env); |
52 | $self->response_cb($res, sub { |
53 | my $res = Plack::Response->new(@{$_[0]}); |
726a37c1 |
54 | $env->{'plack.session'}->finalize( $res ); |
fe1bfe7d |
55 | @{$_[0]} = @{$res->finalize}; |
56 | }); |
bd992981 |
57 | } |
58 | |
59 | 1; |
60 | |
61 | __END__ |
ac4892f4 |
62 | |
63 | =pod |
64 | |
65 | =head1 NAME |
66 | |
67 | Plack::Middleware::Session - Middleware for session management |
68 | |
69 | =head1 SYNOPSIS |
70 | |
3d92cf47 |
71 | use Plack::Builder; |
ac4892f4 |
72 | |
3d92cf47 |
73 | my $app = sub { |
74 | return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ]; |
75 | }; |
76 | |
77 | builder { |
78 | enable 'Session'; |
79 | $app; |
80 | }; |
81 | |
ad80e445 |
82 | # Or, use the File store backend (great if you use multiprocess server) |
83 | # For more options, see perldoc Plack::Session::Store::File |
84 | builder { |
85 | enable 'Session', store => 'File'; |
86 | $app; |
87 | }; |
88 | |
ac4892f4 |
89 | =head1 DESCRIPTION |
90 | |
3d92cf47 |
91 | This is a Plack Middleware component for session management. By |
ad80e445 |
92 | default it will use cookies to keep session state and store data in |
93 | memory. This distribution also comes with other state and store |
94 | solutions. See perldoc for these backends how to use them. |
3d92cf47 |
95 | |
96 | =head2 State |
97 | |
98 | =over 4 |
99 | |
100 | =item L<Plack::Session::State> |
101 | |
102 | This will maintain session state by passing the session through |
103 | the request params. It does not do this automatically though, |
104 | you are responsible for passing the session param. |
105 | |
106 | =item L<Plack::Session::State::Cookie> |
107 | |
108 | This will maintain session state using browser cookies. |
109 | |
110 | =back |
111 | |
112 | =head2 Store |
113 | |
114 | =over 4 |
115 | |
116 | =item L<Plack::Session::Store> |
117 | |
118 | This is your basic in-memory session data store. It is volatile storage |
119 | and not recommended for multiprocessing environments. However it is |
120 | very useful for development and testing. |
121 | |
122 | =item L<Plack::Session::Store::File> |
123 | |
124 | This will persist session data in a file. By default it uses |
125 | L<Storable> but it can be configured to have a custom serializer and |
126 | deserializer. |
127 | |
20ede533 |
128 | =item L<Plack::Session::Store::Cache> |
3d92cf47 |
129 | |
20ede533 |
130 | This will persist session data using the L<Cache> interface. |
3d92cf47 |
131 | |
132 | =item L<Plack::Session::Store::Null> |
133 | |
134 | Sometimes you don't care about storing session data, in that case |
135 | you can use this noop module. |
136 | |
137 | =back |
138 | |
ac4892f4 |
139 | =head1 BUGS |
140 | |
141 | All complex software has bugs lurking in it, and this module is no |
142 | exception. If you find a bug please either email me, or add the bug |
143 | to cpan-RT. |
144 | |
145 | =head1 AUTHOR |
146 | |
147 | Tatsuhiko Miyagawa |
148 | |
149 | Stevan Little E<lt>stevan.little@iinteractive.comE<gt> |
150 | |
151 | =head1 COPYRIGHT AND LICENSE |
152 | |
153 | Copyright 2009 Infinity Interactive, Inc. |
154 | |
155 | L<http://www.iinteractive.com> |
156 | |
157 | This library is free software; you can redistribute it and/or modify |
158 | it under the same terms as Perl itself. |
159 | |
160 | =cut |
161 | |
162 | |