Releng, test fixes, bug fixes for Session plugins
[catagits/Catalyst-Plugin-Session-State-Cookie.git] / lib / Catalyst / Plugin / Session / State / Cookie.pm
CommitLineData
1a776a0c 1package Catalyst::Plugin::Session::State::Cookie;
2use base qw/Catalyst::Plugin::Session::State/;
bf2bce67 3
4use strict;
1a776a0c 5use warnings;
bf2bce67 6
1a776a0c 7use NEXT;
bf2bce67 8
81eb8ebf 9our $VERSION = "0.01";
10
5e50008f 11sub setup_session {
12 my $c = shift;
13
14 $c->NEXT::setup_session(@_);
15
16 $c->config->{session}{cookie_name} ||= "session";
17}
18
bf2bce67 19sub finalize {
b2f8df5e 20 my $c = shift;
1a776a0c 21
5e50008f 22 my $cookie_name = $c->config->{session}{cookie_name};
23
bf2bce67 24 if ( my $sid = $c->sessionid ) {
5e50008f 25 my $cookie = $c->request->cookies->{$cookie_name};
1a776a0c 26 if ( !$cookie or $cookie->value ne $sid ) {
5e50008f 27 $c->response->cookies->{$cookie_name} = { value => $sid };
1a776a0c 28 $c->log->debug(qq/A cookie with the session id "$sid" was saved/)
29 if $c->debug;
58c05d1a 30 }
bf2bce67 31 }
1a776a0c 32
bf2bce67 33 return $c->NEXT::finalize(@_);
34}
35
1a776a0c 36sub prepare_cookies {
bf2bce67 37 my $c = shift;
1a776a0c 38
7acb108b 39 my $ret = $c->NEXT::prepare_cookies(@_);
40
5e50008f 41 my $cookie_name = $c->config->{session}{cookie_name};
42
43 if ( my $cookie = $c->request->cookies->{$cookie_name} ) {
bf2bce67 44 my $sid = $cookie->value;
45 $c->sessionid($sid);
46 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
47 }
bf2bce67 48
7acb108b 49 return $ret;
bf2bce67 50}
51
1a776a0c 52__PACKAGE__
57dbf608 53
1a776a0c 54__END__
bf2bce67 55
1a776a0c 56=pod
b2f8df5e 57
1a776a0c 58=head1 NAME
bf2bce67 59
1a776a0c 60Catalyst::Plugin::Session::State::Cookie - A session ID
bf2bce67 61
1a776a0c 62=head1 SYNOPSIS
bf2bce67 63
1a776a0c 64 use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/;
bf2bce67 65
1a776a0c 66=head1 DESCRIPTION
bf2bce67 67
1a776a0c 68In order for L<Catalyst::Plugin::Session> to work the session ID needs to be
69stored on the client, and the session data needs to be stored on the server.
bf2bce67 70
1a776a0c 71This plugin stores the session ID on the client using the cookie mechanism.
57dbf608 72
1a776a0c 73=head1 EXTENDED METHODS
58c05d1a 74
57dbf608 75=over 4
76
1a776a0c 77=item prepare_cookies
57dbf608 78
1a776a0c 79Will restore if an appropriate cookie is found.
58c05d1a 80
1a776a0c 81=item finalize
58c05d1a 82
1a776a0c 83Will set a cookie called C<session> if it doesn't exist or if it's value is not the current session id.
58c05d1a 84
57dbf608 85=back
58c05d1a 86
5e50008f 87=head1 CONFIGURATION
88
89=over 4
90
91=item cookie_name
92
93The name of the cookie to store (defaults to C<session>).
94
95=back
96
bf2bce67 97=head1 SEE ALSO
98
1a776a0c 99L<Catalyst>, L<Catalyst::Plugin::Session>.
bf2bce67 100
101=head1 AUTHOR
102
57dbf608 103Sebastian Riedel E<lt>C<sri@cpan.org>E<gt>,
104Marcus Ramberg E<lt>C<mramberg@cpan.org>E<gt>,
1a776a0c 105Andrew Ford E<lt>C<andrewf@cpan.org>E<gt>,
106Yuval Kogman E<lt>C<nothingmuch@woobling.org>E<gt>
bf2bce67 107
108=head1 COPYRIGHT
109
bfeb5ca0 110This program is free software, you can redistribute it and/or modify it
111under the same terms as Perl itself.
bf2bce67 112
113=cut
114
1151;