File Coverage

blib/lib/Plack/Middleware/Session/Simple.pm
Criterion Covered Total %
statement 49 117 41.8
branch 7 52 13.4
condition 3 61 4.9
subroutine 14 25 56.0
pod 2 4 50.0
total 75 259 28.9


line stmt bran cond sub pod time code
1             package Plack::Middleware::Session::Simple;
2              
3 3     3   2080 use 5.008005;
  3         8  
  3         110  
4 3     3   15 use strict;
  3         6  
  3         91  
5 3     3   20 use warnings;
  3         6  
  3         94  
6 3     3   451 use parent qw/Plack::Middleware/;
  3         265  
  3         21  
7 3     3   11947 use Digest::SHA1 qw//;
  3         1785  
  3         65  
8 3     3   1407 use Cookie::Baker;
  3         4695  
  3         166  
9 3     3   16 use Plack::Util;
  3         5  
  3         59  
10 3     3   11 use Scalar::Util qw/blessed/;
  3         4  
  3         161  
11 3         23 use Plack::Util::Accessor qw/
12             store
13             cookie_name
14             sid_generator
15             sid_validator
16             keep_empty
17             path
18             domain
19             expires
20             secure
21             httponly
22             serializer
23 3     3   15 /;
  3         3  
24              
25             our $VERSION = "0.03";
26              
27             sub prepare_app {
28 2     2 1 570 my $self = shift;
29              
30 2         6 my $store = $self->store;
31 2 50 33     204 die('store require get, set and remove method.')
      33        
      33        
32             unless blessed $store
33             && $store->can('get')
34             && $store->can('set')
35             && $store->can('remove');
36              
37 2 50       8 $self->cookie_name('simple_session') unless $self->cookie_name;
38 2 50       13 $self->path('/') unless defined $self->path;
39 2 100       25 $self->keep_empty(1) unless defined $self->keep_empty;
40              
41 2 50       17 if ( !$self->sid_generator ) {
42             $self->sid_generator(sub{
43 0     0   0 Digest::SHA1::sha1_hex(rand() . $$ . {} . time)
44 2         14 });
45             }
46 2 50       11 if ( !$self->sid_validator ) {
47 2         16 $self->sid_validator(
48             qr/\A[0-9a-f]{40}\Z/
49             );
50             }
51              
52             }
53              
54             sub call {
55 0     0 1   my ($self,$env) = @_;
56              
57 0           my($id, $session) = $self->get_session($env);
58              
59 0           my $tied;
60 0 0 0       if ($id && $session) {
61 0           $tied = tie my %session,
62             'Plack::Middleware::Session::Simple::Session', %$session;
63 0           $env->{'psgix.session'} = \%session;
64 0           $env->{'psgix.session.options'} = {
65             id => $id,
66             };
67             } else {
68 0           my $id = $self->{sid_generator}->();
69 0           $tied = tie my %session,
70             'Plack::Middleware::Session::Simple::Session';
71 0           $env->{'psgix.session'} = \%session;
72 0           $env->{'psgix.session.options'} = {
73             id => $id,
74             new_session => 1,
75             };
76             }
77              
78 0           my $res = $self->app->($env);
79              
80             $self->response_cb(
81             $res, sub {
82 0     0     $self->finalize($env, $_[0], $tied)
83             }
84 0           );
85             }
86              
87             sub get_session {
88 0     0 0   my ($self, $env) = @_;
89 0   0       my $cookie = crush_cookie($env->{HTTP_COOKIE} || '')->{$self->{cookie_name}};
90 0 0         return unless defined $cookie;
91 0 0         return unless $cookie =~ $self->{sid_validator};
92              
93 0 0         my $session = $self->{store}->get($cookie) or return;
94 0 0         $session = $self->{serializer}->[1]->($session) if $self->{serializer};
95 0           return ($cookie, $session);
96             }
97              
98             sub finalize {
99 0     0 0   my ($self, $env, $res, $session) = @_;
100 0           my $options = $env->{'psgix.session.options'};
101 0           my $new_session = delete $options->{new_session};
102              
103 0           my $need_store;
104 0 0 0       if ( ($new_session && $self->{keep_empty} && ! $session->has_key )
      0        
      0        
      0        
      0        
105             || $session->[1] || $options->{expire} || $options->{change_id}) {
106 0           $need_store = 1;
107             }
108 0 0         $need_store = 0 if $options->{no_store};
109              
110 0           my $set_cookie;
111 0 0 0       if ( ($new_session && $self->{keep_empty} && ! $session->has_key )
      0        
      0        
      0        
      0        
      0        
112             || ($new_session && $session->[1] )
113             || $options->{expire} || $options->{change_id}) {
114 0           $set_cookie = 1;
115             }
116              
117 0 0         if ( $need_store ) {
118 0 0         if ($options->{expire}) {
    0          
119 0           $self->{store}->remove($options->{id});
120             } elsif ($options->{change_id}) {
121 0           $self->{store}->remove($options->{id});
122 0           $options->{id} = $self->{sid_generator}->();
123 0           my $val = $session->[0];
124 0 0         $val = $self->{serializer}->[0]->($val) if $self->{serializer};
125 0           $self->{store}->set($options->{id}, $val);
126             } else {
127 0           my $val = $session->[0];
128 0 0         $val = $self->{serializer}->[0]->($val) if $self->{serializer};
129 0           $self->{store}->set($options->{id}, $val);
130             }
131             }
132              
133 0 0         if ( $set_cookie ) {
134 0 0         if ($options->{expire}) {
135 0           $self->_set_cookie(
136             $options->{id}, $res, %$options, expires => 'now');
137             } else {
138 0           $self->_set_cookie(
139             $options->{id}, $res, %$options);
140             }
141             }
142             }
143              
144             sub _set_cookie {
145 0     0     my($self, $id, $res, %options) = @_;
146              
147 0           delete $options{id};
148              
149 0 0 0       $options{path} = $self->{path} || '/' if !exists $options{path};
150 0 0 0       $options{domain} = $self->{domain} if !exists $options{domain} && defined $self->{domain};
151 0 0 0       $options{secure} = $self->{secure} if !exists $options{secure} && defined $self->{secure};
152 0 0 0       $options{httponly} = $self->{httponly} if !exists $options{httponly} && defined $self->{httponly};
153              
154 0 0 0       if (!exists $options{expires} && defined $self->{expires}) {
155 0           $options{expires} = $self->{expires};
156             }
157              
158 0           my $cookie = bake_cookie(
159             $self->{cookie_name}, {
160             value => $id,
161             %options,
162             }
163             );
164 0           Plack::Util::header_push($res->[1], 'Set-Cookie', $cookie);
165             }
166              
167             1;
168              
169             package Plack::Middleware::Session::Simple::Session;
170              
171 3     3   2365 use strict;
  3         5  
  3         78  
172 3     3   11 use warnings;
  3         3  
  3         71  
173 3     3   500 use Tie::Hash;
  3         654  
  3         118  
174 3     3   12 use base qw/Tie::ExtraHash/;
  3         4  
  3         1396  
175              
176             sub TIEHASH {
177 0     0     my $class = shift;
178 0           bless [{@_},0, scalar @_], $class;
179             }
180              
181             sub STORE {
182 0     0     $_[0]->[1]++;
183 0           $_[0]->[0]{$_[1]} = $_[2]
184             }
185              
186             sub DELETE {
187 0     0     $_[0]->[1]++;
188 0           delete $_[0]->[0]->{$_[1]}
189             }
190              
191             sub CLEAR {
192 0     0     $_[0]->[1]++;
193 0           %{$_[0]->[0]} = ()
  0            
194             }
195              
196             sub has_key {
197 0     0     scalar keys %{$_[0]->[0]}
  0            
198             }
199              
200             1;
201              
202             __END__