Commit | Line | Data |
1a776a0c |
1 | package Catalyst::Plugin::Session::State::Cookie; |
2 | use base qw/Catalyst::Plugin::Session::State/; |
bf2bce67 |
3 | |
4 | use strict; |
1a776a0c |
5 | use warnings; |
bf2bce67 |
6 | |
1a776a0c |
7 | use NEXT; |
74586782 |
8 | use Catalyst::Utils (); |
bf2bce67 |
9 | |
00fa6d61 |
10 | our $VERSION = "0.02"; |
81eb8ebf |
11 | |
5e50008f |
12 | sub setup_session { |
20e33791 |
13 | my $c = shift; |
5e50008f |
14 | |
20e33791 |
15 | $c->NEXT::setup_session(@_); |
7022ec4c |
16 | $c->config->{session}{cookie_name} |
17 | ||= Catalyst::Utils::appprefix($c) . '_session'; |
5e50008f |
18 | } |
19 | |
d52e5079 |
20 | sub finalize_cookies { |
b2f8df5e |
21 | my $c = shift; |
1a776a0c |
22 | |
58730edc |
23 | if ( $c->sessionid ) { |
24 | $c->update_session_cookie( $c->make_session_cookie ); |
25 | } |
db1cda22 |
26 | |
27 | return $c->NEXT::finalize_cookies(@_); |
28 | } |
29 | |
30 | sub update_session_cookie { |
58730edc |
31 | my ( $c, $updated ) = @_; |
20e33791 |
32 | my $cookie_name = $c->config->{session}{cookie_name}; |
58730edc |
33 | $c->response->cookies->{$cookie_name} = $updated; |
db1cda22 |
34 | } |
5e50008f |
35 | |
db1cda22 |
36 | sub make_session_cookie { |
58730edc |
37 | my $c = shift; |
38 | |
39 | my $cfg = $c->config->{session}; |
40 | my $cookie = { |
41 | value => $c->sessionid, |
42 | ( $cfg->{cookie_domain} ? ( domain => $cfg->{cookie_domain} ) : () ), |
43 | }; |
44 | |
1e986fd5 |
45 | $cookie->{expires}=$c->calc_expiry(); |
46 | |
47 | return $cookie; |
48 | } |
49 | |
50 | sub calc_expiry { |
51 | my $c=shift; |
52 | my $cfg = $c->config->{session}; |
53 | my $value= $c->NEXT::calc_expiry(@_); |
54 | return $value if $value; |
58730edc |
55 | if ( exists $cfg->{cookie_expires} ) { |
7022ec4c |
56 | if ( $cfg->{cookie_expires} > 0 ) { |
1e986fd5 |
57 | return time() + $cfg->{cookie_expires}; |
7022ec4c |
58 | } |
59 | else { |
1e986fd5 |
60 | return undef; |
7022ec4c |
61 | } |
58730edc |
62 | } |
63 | else { |
1e986fd5 |
64 | return $c->session_expires; |
58730edc |
65 | } |
bf2bce67 |
66 | } |
67 | |
1a776a0c |
68 | sub prepare_cookies { |
bf2bce67 |
69 | my $c = shift; |
1a776a0c |
70 | |
7acb108b |
71 | my $ret = $c->NEXT::prepare_cookies(@_); |
72 | |
20e33791 |
73 | my $cookie_name = $c->config->{session}{cookie_name}; |
5e50008f |
74 | |
75 | if ( my $cookie = $c->request->cookies->{$cookie_name} ) { |
bf2bce67 |
76 | my $sid = $cookie->value; |
77 | $c->sessionid($sid); |
78 | $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug; |
79 | } |
bf2bce67 |
80 | |
20e33791 |
81 | return $ret; |
bf2bce67 |
82 | } |
83 | |
1a776a0c |
84 | __PACKAGE__ |
57dbf608 |
85 | |
1a776a0c |
86 | __END__ |
bf2bce67 |
87 | |
1a776a0c |
88 | =pod |
b2f8df5e |
89 | |
1a776a0c |
90 | =head1 NAME |
bf2bce67 |
91 | |
75d3560d |
92 | Catalyst::Plugin::Session::State::Cookie - Maintain session IDs using cookies. |
bf2bce67 |
93 | |
1a776a0c |
94 | =head1 SYNOPSIS |
bf2bce67 |
95 | |
20e33791 |
96 | use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/; |
bf2bce67 |
97 | |
1a776a0c |
98 | =head1 DESCRIPTION |
bf2bce67 |
99 | |
1a776a0c |
100 | In order for L<Catalyst::Plugin::Session> to work the session ID needs to be |
101 | stored on the client, and the session data needs to be stored on the server. |
bf2bce67 |
102 | |
1a776a0c |
103 | This plugin stores the session ID on the client using the cookie mechanism. |
57dbf608 |
104 | |
724a6173 |
105 | =head1 METHODS |
106 | |
107 | =over 4 |
108 | |
109 | =item make_session_cookie |
110 | |
111 | Returns a hash reference with the default values for new cookies. |
112 | |
113 | =item update_session_cookie $hash_ref |
114 | |
115 | Sets the cookie based on C<cookie_name> in the response object. |
116 | |
117 | =back |
118 | |
1a776a0c |
119 | =head1 EXTENDED METHODS |
58c05d1a |
120 | |
57dbf608 |
121 | =over 4 |
122 | |
1a776a0c |
123 | =item prepare_cookies |
57dbf608 |
124 | |
1a776a0c |
125 | Will restore if an appropriate cookie is found. |
58c05d1a |
126 | |
d52e5079 |
127 | =item finalize_cookies |
58c05d1a |
128 | |
19c2baa1 |
129 | Will set a cookie called C<session> if it doesn't exist or if it's value is not |
130 | the current session id. |
131 | |
132 | =item setup_session |
133 | |
134 | Will set the C<cookie_name> parameter to it's default value if it isn't set. |
58c05d1a |
135 | |
57dbf608 |
136 | =back |
58c05d1a |
137 | |
5e50008f |
138 | =head1 CONFIGURATION |
139 | |
140 | =over 4 |
141 | |
142 | =item cookie_name |
143 | |
ae33e13f |
144 | The name of the cookie to store (defaults to C<Catalyst::Utils::apprefix($c) . '_session'>). |
5e50008f |
145 | |
41b4b15c |
146 | =item cookie_domain |
147 | |
148 | The name of the domain to store in the cookie (defaults to current host) |
149 | |
7022ec4c |
150 | =item cookie_expires |
151 | |
152 | Number of seconds from now you want to elapse before cookie will expire. |
153 | Set to 0 to create a session cookie, ie one which will die when the |
154 | user's browser is shut down. |
155 | |
5e50008f |
156 | =back |
157 | |
724a6173 |
158 | =head1 CAVEATS |
db1cda22 |
159 | |
160 | Sessions have to be created before the first write to be saved. For example: |
161 | |
162 | sub action : Local { |
163 | my ( $self, $c ) = @_; |
164 | $c->res->write("foo"); |
165 | $c->session( ... ); |
166 | ... |
167 | } |
168 | |
169 | Will cause a session ID to not be set, because by the time a session is |
170 | actually created the headers have already been sent to the client. |
171 | |
bf2bce67 |
172 | =head1 SEE ALSO |
173 | |
1a776a0c |
174 | L<Catalyst>, L<Catalyst::Plugin::Session>. |
bf2bce67 |
175 | |
47f47da5 |
176 | =head1 AUTHORS |
bf2bce67 |
177 | |
47f47da5 |
178 | This module is derived from L<Catalyst::Plugin::Session::FastMmap> code, and |
179 | has been heavily modified since. |
180 | |
181 | Andrew Ford |
182 | Andy Grundman |
183 | Christian Hansen |
184 | Yuval Kogman, C<nothingmuch@woobling.org> |
185 | Marcus Ramberg |
186 | Sebastian Riedel |
bf2bce67 |
187 | |
188 | =head1 COPYRIGHT |
189 | |
bfeb5ca0 |
190 | This program is free software, you can redistribute it and/or modify it |
191 | under the same terms as Perl itself. |
bf2bce67 |
192 | |
193 | =cut |
194 | |
195 | 1; |