Make it work the new Plack::Request (in Plack 1.0)
[catagits/Web-Session.git] / lib / Plack / Session / State / Cookie.pm
CommitLineData
bd992981 1package Plack::Session::State::Cookie;
2use strict;
3use warnings;
4
000c696e 5our $VERSION = '0.03';
30cc0a71 6our $AUTHORITY = 'cpan:STEVAN';
7
bd992981 8use parent 'Plack::Session::State';
92edbddf 9use Plack::Request;
10use Plack::Response;
bd992981 11
ac4892f4 12use Plack::Util::Accessor qw[
13 path
14 domain
15 expires
16 secure
17];
bd992981 18
4a0cb5a0 19sub get_session_id {
92edbddf 20 my ($self, $env) = @_;
2c5f34e8 21 Plack::Request->new($env)->cookies->{$self->session_key};
bd992981 22}
23
caf3bd90 24sub expire_session_id {
92edbddf 25 my ($self, $id, $res, $options) = @_;
2c5f34e8 26 $self->_set_cookie($id, $res, expires => time);
caf3bd90 27}
28
bd992981 29sub finalize {
92edbddf 30 my ($self, $id, $res, $options) = @_;
31 $self->_set_cookie($id, $res, (defined $self->expires ? (expires => $self->expires) : ()));
32}
33
34sub _set_cookie {
35 my($self, $id, $res, %options) = @_;
36
37 # TODO: Do not use Plack::Response
38 my $response = Plack::Response->new($res);
bd992981 39 $response->cookies->{ $self->session_key } = +{
40 value => $id,
41 path => ($self->path || '/'),
05b5f99d 42 ( defined $self->domain ? ( domain => $self->domain ) : () ),
05b5f99d 43 ( defined $self->secure ? ( secure => $self->secure ) : () ),
92edbddf 44 %options,
bd992981 45 };
92edbddf 46
47 my $final_r = $response->finalize;
48 $res->[1] = $final_r->[1]; # headers
bd992981 49}
50
fe1bfe7d 511;
ac4892f4 52
53__END__
54
55=pod
56
57=head1 NAME
58
59Plack::Session::State::Cookie - Basic cookie-based session state
60
3d92cf47 61=head1 SYNOPSIS
62
63 use Plack::Builder;
64 use Plack::Middleware::Session;
65
66 my $app = sub {
67 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
68 };
69
70 builder {
71 enable 'Session'; # Cookie is the default state
72 $app;
73 };
74
ac4892f4 75=head1 DESCRIPTION
76
43f34c01 77This is a subclass of L<Plack::Session::State> and implements it's
3d92cf47 78full interface. This is the default state used in
79L<Plack::Middleware::Session>.
43f34c01 80
ac4892f4 81=head1 METHODS
82
83=over 4
84
85=item B<new ( %params )>
86
3d92cf47 87The C<%params> can include I<path>, I<domain>, I<expires> and
88I<secure> options, as well as all the options accepted by
89L<Plack::Session::Store>.
90
ac4892f4 91=item B<path>
92
3d92cf47 93Path of the cookie, this defaults to "/";
94
ac4892f4 95=item B<domain>
96
3d92cf47 97Domain of the cookie, if nothing is supplied then it will not
98be included in the cookie.
99
ac4892f4 100=item B<expires>
101
3d92cf47 102Expiration time of the cookie, if nothing is supplied then it will
103not be included in the cookie.
104
ac4892f4 105=item B<secure>
106
3d92cf47 107Secure flag for the cookie, if nothing is supplied then it will not
108be included in the cookie.
109
ac4892f4 110=back
111
ac4892f4 112=head1 BUGS
113
114All complex software has bugs lurking in it, and this module is no
115exception. If you find a bug please either email me, or add the bug
116to cpan-RT.
117
118=head1 AUTHOR
119
120Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
121
122=head1 COPYRIGHT AND LICENSE
123
000c696e 124Copyright 2009, 2010 Infinity Interactive, Inc.
ac4892f4 125
126L<http://www.iinteractive.com>
127
128This library is free software; you can redistribute it and/or modify
129it under the same terms as Perl itself.
130
131=cut
132
133