blib/lib/ASP4/SessionStateManager.pm

filter: Statementstmt 44.0% 59 / 134 Branchbran 35.7% 5 / 14 Conditioncond 15.3% 2 / 13 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 66.6% 18 / 27 Podpod 23.0% 3 / 13 totaltotal 42.1% 88 / 209 CC 15 SCAR 13.5
CC
15
Cov
41%
new
12.0
retrieve
12.0
verify_session_id
6.0
CRAP 3.8
« blib/lib/ASP4/UserAgent.pm ↑ Summary blib/lib/ASP4/Server.pm »
1  
2 package ASP4::SessionStateManager;
3  
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use base 'Ima::DBI::Contextual';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use HTTP::Date qw( time2iso time2str str2time );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Time::HiRes 'gettimeofday';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Digest::MD5 'md5_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Storable qw( freeze thaw );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use ASP4::ConfigLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 sub new
16 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
17 0 my ($class, $r) = @_;
18 0 my $s = bless { }, $class;
19 0 my $conn = context()->config->data_connections->session;
20
21 0 local $^W = 0;
22 0 $class->set_db('Session',
23 $conn->dsn,
24 $conn->username,
25 $conn->password
26 );
27
28 0 my $id = $s->parse_session_id();
29 0 ▶ unless( $id && $s->verify_session_id( $id, $conn->session_timeout ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($id and $s->verify_session_id($id, $conn->session_timeout)) 0 0 0
Condition0%0/3
$id and $s->verify_session_id($id, $conn->session_timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
$id and $s->verify_session_id($id, $conn->session_timeout)
$id $s->verify_session_id($id, $conn->session_timeout)
Truth table0%0/3
A and B
A= $idB= $s->verify_session_id($id, $conn->session_timeout)
ABresult
0X0
100
111
30 {
31 0 $s->{SessionID} = $s->new_session_id();
32 0 $s->write_session_cookie($r);
33 0 return $s->create( $s->{SessionID} );
34 }# end unless()
35
36 0 return $s->retrieve( $id );
37 }# end new()
38  
39 3228 ▶sub context { ASP4::HTTPContext->current }
Subroutine context: called CC=1 SCAR=0.0
Pod undocumented
40  
41 sub is_read_only
42 8249 ▶{
Subroutine is_read_only: called CC=2 SCAR=7.6
Pod documented
43 8249 my ($s, $val) = @_;
44
45 8249 ▶ if( defined($val) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 8249 8249
46 {
47 0 $s->{____is_read_only} = $val;
48 }
49 else
50 {
51 8249 return $s->{____is_read_only};
52 }# end if()
53 }# end is_readonly()
54  
55  
56 sub parse_session_id
57 3144 ▶{
Subroutine parse_session_id: called CC=2 SCAR=6.9
Pod undocumented
58 3144 my $session_config = context()->config->data_connections->session;
59 3144 my $cookie_name = $session_config->cookie_name;
60 3144 ▶ my ($id) = ($ENV{HTTP_COOKIE}||'') =~ m/\b\Q$cookie_name\E\=([a-f0-9]{32,32})/s;
Condition100%2/2
$ENV{'HTTP_COOKIE'} || ''
l!l
311628
MC/DC100%1/1
$ENV{'HTTP_COOKIE'} || ''
$ENV{'HTTP_COOKIE'}
Truth table100%2/2
A
A= $ENV{'HTTP_COOKIE'}
Aresult
11
00
61  
62 3144 return $id;
63 }# end parse_session_id()
64  
65  
66 28 ▶sub new_session_id { md5_hex( join ':', ( context()->config->web->www_root, $$, gettimeofday() ) ) }
Subroutine new_session_id: called CC=1 SCAR=0.0
Pod undocumented
67  
68  
69 sub write_session_cookie
70 28 ▶{
Subroutine write_session_cookie: called CC=3 SCAR=12.3
Pod undocumented
71 28 my ($s, $r) = @_;
72
73 28 my $config = context()->config->data_connections->session;
74 28 my $domain = "";
75 28 ▶ unless( $config->cookie_domain eq '*' )
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->cookie_domain eq "*") 0 28 28
76 {
77 0 ▶ $domain = "domain=" . ( $config->cookie_domain || $ENV{HTTP_HOST} ) . ";";
Condition0%0/3
$config->cookie_domain || $ENV{'HTTP_HOST'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->cookie_domain || $ENV{'HTTP_HOST'}
$config->cookie_domain $ENV{'HTTP_HOST'}
Truth table0%0/3
A || B
A= $config->cookie_domainB= $ENV{'HTTP_HOST'}
ABresult
1X1
011
000
78 }# end unless()
79 28 my $name = $config->cookie_name;
80
81 28 my @cookie = (
82 'Set-Cookie' => "$name=$s->{SessionID}; path=/; $domain"
83 );
84 28 context()->headers_out->push_header( @cookie );
85 28 @cookie;
86 }# end write_session_cookie()
87  
88  
89 sub verify_session_id
90 0 ▶{
Subroutine verify_session_id: not called CC=2 SCAR=17.9
Pod undocumented
91 0 my ($s, $id, $timeout ) = @_;
92
93 0 my $is_active;
94 0 ▶ if( $timeout eq '*' )
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout eq '*') { } 0 0 0
95 {
96 0 local $s->db_Session->{AutoCommit} = 1;
97 0 my $sth = $s->db_Session->prepare(<<"");
98 SELECT count(*)
99 FROM asp_sessions
100 WHERE session_id = ?
101  
102 0 $sth->execute( $id );
103 0 ($is_active) = $sth->fetchrow();
104 0 $sth->finish();
105 }
106 else
107 {
108 0 my $range_start = time() - ( $timeout * 60 );
109 0 local $s->db_Session->{AutoCommit} = 1;
110 0 my $sth = $s->db_Session->prepare(<<"");
111 SELECT count(*)
112 FROM asp_sessions
113 WHERE session_id = ?
114 AND modified_on - created_on < ?
115  
116 0 $sth->execute( $id, $timeout );
117 0 ($is_active) = $sth->fetchrow();
118 0 $sth->finish();
119 }# end if()
120  
121 0 return $is_active;
122 }# end verify_session_id()
123  
124  
125 sub create
126 0 ▶{
Subroutine create: not called CC=1 SCAR=6.9
Pod undocumented
127 0 my ($s, $id) = @_;
128
129 0 local $s->db_Session->{AutoCommit} = 1;
130 0 my $sth = $s->db_Session->prepare_cached(<<"");
131 delete from asp_sessions
132 where session_id = ?
133  
134 0 $sth->execute( $id );
135  
136 0 $sth = $s->db_Session->prepare_cached(<<"");
137 INSERT INTO asp_sessions (
138 session_id,
139 session_data,
140 created_on,
141 modified_on
142 )
143 VALUES (
144 ?, ?, ?, ?
145 )
146  
147 0 my $time = time();
148 0 my $now = time2iso($time);
149 0 $s->{__lastMod} = $time;
150
151 0 $s->sign();
152
153 0 my %clone = %$s;
154
155 0 $sth->execute(
156 $id,
157 freeze( \%clone ),
158 $now,
159 $now,
160 );
161 0 $sth->finish();
162
163 0 return $s->retrieve( $id );
164 }# end create()
165  
166  
167 sub retrieve
168 0 ▶{
Subroutine retrieve: not called CC=3 SCAR=24.8
Pod undocumented
169 0 my ($s, $id) = @_;
170  
171 0 local $s->db_Session->{AutoCommit} = 1;
172 0 my $sth = $s->db_Session->prepare_cached(<<"");
173 SELECT session_data, modified_on
174 FROM asp_sessions
175 WHERE session_id = ?
176  
177 0 my $now = time2iso();
178 0 $sth->execute( $id );
179 0 my ($data, $modified_on) = $sth->fetchrow;
180 0 ▶ $data = thaw($data) || { SessionID => $id };
Condition0%0/2
thaw($data) || {'SessionID', $id}
l!l
00
MC/DC0%0/1
thaw($data) || {'SessionID', $id}
thaw($data)
Truth table0%0/2
A
A= thaw($data)
Aresult
11
00
181 0 $sth->finish();
182
183 0 $s->{$_} = $data->{$_} for keys %$data;
184
185 0 return $s;
186 }# end retrieve()
187  
188  
189 sub save
190 0 ▶{
Subroutine save: not called CC=2 SCAR=14.2
Pod documented
191 0 my ($s) = @_;
192
193 0 ▶ return unless $s->{SessionID};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"SessionID"} 0 0 0
194 15 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
195 # $s->{__lastMod} = time();
196 0 $s->sign;
197
198 0 local $s->db_Session->{AutoCommit} = 1;
199 0 my $sth = $s->db_Session->prepare_cached(<<"");
200 UPDATE asp_sessions SET
201 session_data = ?,
202 modified_on = ?
203 WHERE session_id = ?
204  
205 0 my %clone = %$s;
206 0 delete $clone{____is_read_only};
207 0 my $data = freeze( \%clone );
208
209 0 $sth->execute( $data, time2iso(), $s->{SessionID} );
210 0 $sth->finish();
211
212 0 1;
213 }# end save()
214  
215  
216 sub sign
217 0 ▶{
Subroutine sign: not called CC=1 SCAR=6.9
Pod undocumented
218 0 my $s = shift;
219
220 0 $s->{__signature} = $s->_hash;
221 }# end sign()
222  
223  
224 sub _hash
225 0 ▶{
Subroutine _hash: not called CC=2 SCAR=12.2
226 0 my $s = shift;
227
228 15 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
229 md5_hex(
230 join ":",
231 0 map { "$_:$s->{$_}" }
232 0 ▶ grep { $_ ne '__signature' && $_ ne '____is_read_only' } sort keys(%$s)
Condition0%0/3
$_ ne '__signature' && $_ ne '____is_read_only'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne '__signature' && $_ ne '____is_read_only'
$_ ne '__signature' $_ ne '____is_read_only'
Truth table0%0/3
A && B
A= $_ ne '__signature'B= $_ ne '____is_read_only'
ABresult
0X0
100
111
233 );
234 }# end _hash()
235  
236  
237 sub is_changed
238 0 ▶{
Subroutine is_changed: not called CC=1 SCAR=0.6
Pod undocumented
239 0 my $s = shift;
240
241 15 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
242 0 $s->_hash ne $s->{__signature};
243 }# end is_changed()
244  
245  
246 sub reset
247 0 ▶{
Subroutine reset: not called CC=2 SCAR=17.9
Pod documented
248 0 my $s = shift;
249
250 0 delete($s->{$_}) for grep { $_ ne 'SessionID' } keys %$s;
251 0 $s->save;
252 0 return;
253 }# end reset()
254  
255  
256 sub DESTROY
257 5114 ▶{
Subroutine DESTROY: called CC=3 SCAR=11.0
258 5114 my $s = shift;
259
260 5114 ▶ return undef(%$s) unless $s->{SessionID};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"SessionID"} 1998 3116 5114
261
262 3116 ▶ unless( $s->is_read_only )
Branch: 1 branch
BranchTrueFalseTotal
unless ($s->is_read_only) 3116 0 3116
263 {
264 3116 $s->save;# if $s->is_changed;
265 }# end unless()
266 3116 undef(%$s);
267 }# end DESTROY()
268  
269 15 1;# return true:
270  
271 =pod
272  
273 =head1 NAME
274  
275 ASP4::SessionStateManager - Per-user state persistence
276  
277 =head1 SYNOPSIS
278  
279 You've seen this page <%= $Session->{counter}++ %> times before.
280  
281 =head1 DESCRIPTION
282  
283 Web applications require session state management - and the simpler, the better.
284  
285 C<ASP4::SessionStateManager> is a simple blessed hash. When it goes out of scope,
286 it is saved to the database (or whatever).
287  
288 If no changes were made to the session, it is not saved.
289  
290 =head1 PUBLIC PROPERTIES
291  
292 =head2 is_read_only( 1:0 )
293  
294 Starting with version 1.044, setting this property to a true value will prevent
295 any changes made to the contents of the session during the current request from
296 being saved at the end of the request.
297  
298 B<NOTE:> A side-effect is that calling C<< $Session->save() >> after calling C<< $Session->is_read_only(1) >>
299 will B<*NOT*> prevent changes from being saved B<ON PURPOSE>. Explicitly calling C<< $Session->save() >>
300 will still cause the session data to be stored. Setting C<< $Session->is_read_only(1) >> will only
301 prevent the default behavior of saving session state at the end of each successful request.
302  
303 =head1 PUBLIC METHODS
304  
305 =head2 save( )
306  
307 Causes the session data to be saved. (Unless C<< $Session->is_read_only(1) >> is set.)
308  
309 =head2 reset( )
310  
311 Causes the session data to be emptied.
312  
313 =head1 BUGS
314  
315 It's possible that some bugs have found their way into this release.
316  
317 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports.
318  
319 =head1 HOMEPAGE
320  
321 Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples
322 of ASP4 in action.
323  
324 =cut
325  
« blib/lib/ASP4/UserAgent.pm ↑ Summary blib/lib/ASP4/Server.pm »