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