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