blib/lib/YATT/Lite/WebMVC0/Partial/Session.pm

filter: Statementstmt 18.6% 31 / 166 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 22.5% 7 / 31 Podpod 0.0% 0 / 19 totaltotal 10.7% 38 / 354 CC 58 SCAR 39.2
CC
58
Cov
11%
session_resume
156.0
session_start
90.0
session_init
90.0
CRAP 50.4
« blib/lib/YATT/Lite/Inspector.pm ↑ Summary blib/lib/YATT/Lite/LanguageServer/Generic.pm »
1 package YATT::Lite::WebMVC0::Partial::Session;
2 sub MY () {__PACKAGE__}
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use YATT::Lite::Partial
8 1 ▶ (requires => [qw/error
Subroutine BEGIN: called CC=1 SCAR=0.0
9 app_path_ensure_existing
10 app_path_var_tmp
11 /]
12 , fields => [qw/session_driver
13 session_config
14 session_debug
15 session_path
16 csrftok_name
17 tmpdir
18 /]
19 , -Entity, -CON
20 1 );
21  
22 1 ▶use YATT::Lite::Util qw/lexpand escape nonempty symtab
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 num_is_ge/;
24  
25 1 ▶use YATT::Lite::Types [Config => fields => [qw/name expire/]];
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #========================================
28  
29 1 ▶use YATT::Lite::WebMVC0::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 sub Connection () {'YATT::Lite::WebMVC0::Connection'}
31 sub ConnProp () {Connection}
32  
33 #========================================
34 # Session support, based on CGI::Session.
35 #========================================
36  
37 0 ▶Entity sess => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
38 0 my ($this) = shift;
39  
40 # This will call MY->session_resume.
41 0 ▶ my $sess = $CON->get_session
Branch: 1 branch
BranchTrueFalseTotal
my $sess = $YATT::Lite::WebMVC0::Partial::Session::CON->get_session or return undef 0 0 0
42 or return undef;
43  
44 0 $sess->param(@_);
45 1 };
46  
47 0 ▶Entity csrf_token_input => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
48 0 my ($this, $name) = @_;
49 0 ▶ $name ||= $CON->cget('system')->csrftok_name;
Condition0%0/3
$name ||= $YATT::Lite::WebMVC0::Partial::Session::CON->cget("system")->csrftok_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$name ||= $YATT::Lite::WebMVC0::Partial::Session::CON->cget("system")->csrftok_name
$name $YATT::Lite::WebMVC0::Partial::Session::CON->cget("system")->csrftok_name
Truth table0%0/3
A ||= B
A= $nameB= $YATT::Lite::WebMVC0::Partial::Session::CON->cget("system")->csrftok_name
ABresult
1X1
011
000
50  
51 0 \ sprintf <<END, $name, escape($CON->get_session_sid);
52 <input type="hidden" name="%s" value="%s">
53 END
54 1 };
55  
56 0 ▶Entity csrf_token_check => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
57 0 my ($this, $name) = @_;
58 0 ▶ $name ||= $CON->cget('system')->csrftok_name;
Condition0%0/3
$name ||= $YATT::Lite::WebMVC0::Partial::Session::CON->cget("system")->csrftok_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$name ||= $YATT::Lite::WebMVC0::Partial::Session::CON->cget("system")->csrftok_name
$name $YATT::Lite::WebMVC0::Partial::Session::CON->cget("system")->csrftok_name
Truth table0%0/3
A ||= B
A= $nameB= $YATT::Lite::WebMVC0::Partial::Session::CON->cget("system")->csrftok_name
ABresult
1X1
011
000
59  
60 0 ▶ nonempty(my $sid = $CON->get_session_sid)
Branch: 1 branch
BranchTrueFalseTotal
nonempty(my $sid = $YATT::Lite::WebMVC0::Partial::Session::CON->get_session_sid) or return undef 0 0 0
61 or return undef;
62 0 ▶ nonempty(my $got = $CON->param($name))
Branch: 1 branch
BranchTrueFalseTotal
nonempty(my $got = $YATT::Lite::WebMVC0::Partial::Session::CON->param($name)) or return undef 0 0 0
63 or return undef;
64  
65 0 $sid eq $got;
66 1 };
67  
68 {
69 1 my $symtab = symtab(MY);
70 1 foreach my $meth (grep {/^session_/} keys %$symtab) {
71 11 my $sub = MY->can($meth);
72 0 ▶ Entity $meth => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 my $this = shift;
74 0 my ConnProp $prop = $CON->prop;
75 0 my MY $self = $prop->{system};
76 0 $self->$meth($CON, @_);
77 11 };
78 }
79 }
80  
81 #========================================
82  
83 # This will be called back from $CON->get_session_sid.
84 0 ▶sub session_sid {
Subroutine session_sid: not called CC=3 SCAR=24.8
Pod undocumented
85 0 (my MY $self, my ($con)) = @_;
86 0 my ConnProp $prop = $con->prop;
87 0 my $sid_name = $self->session_sid_name;
88 0 ▶ my $ck = $con->cookies_in->{$sid_name}
Branch: 1 branch
BranchTrueFalseTotal
my $ck = $con->cookies_in->{$sid_name} or return undef 0 0 0
89 or return undef;
90 0 ▶ ref $ck ? $ck->value : $ck;
Branch: 1 branch
BranchTrueFalseTotal
ref $ck ? : 0 0 0
91 }
92  
93 0 ▶sub session_regenerate_id {
Subroutine session_regenerate_id: not called CC=2 SCAR=17.9
Pod undocumented
94 0 (my MY $self, my ($con, @with_init)) = @_;
95 0 my ConnProp $prop = $con->prop;
96  
97 0 ▶ if (defined $prop->{_session}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prop->{"_session"}) 0 0 0
98 0 $self->session_delete($con);
99 }
100  
101 0 $self->session_start($con, @with_init);
102 }
103  
104 # This will be called back from $CON->get_session.
105 # usually called from before_dirhandler
106 0 ▶sub session_resume {
Subroutine session_resume: not called CC=12 SCAR=50.5
Pod undocumented
107 0 (my MY $self, my ($con)) = @_;
108 $con->logbacktrace("session.resume")
109 0 ▶ if num_is_ge($self->{session_debug}, 2);
Branch: 1 branch
BranchTrueFalseTotal
if num_is_ge($self->{"session_debug"}, 2) 0 0 0
110  
111 0 my ConnProp $prop = $con->prop;
112  
113 0 ▶ if (exists $prop->{_session}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prop->{"_session"}) 0 0 0
114 $con->logdump("session.resume" => "session is already loaded")
115 0 ▶ if $self->{session_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"session_debug"} 0 0 0
116 0 return $prop->{_session};
117 }
118 0 $prop->{_session} = undef;
119  
120 0 ▶ my $sid = $self->session_sid($con) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $sid = $self->session_sid($con) or do { $con->logdump("session.resume", "sid is empty") if $self->{"session_debug"}; return undef } 0 0 0
121 $con->logdump("session.resume" => "sid is empty")
122 0 ▶ if $self->{session_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"session_debug"} 0 0 0
123 0 return undef;
124 };
125  
126 0 ▶ my $sess = $self->session_create_by(load => $con, $sid)
Branch: 1 branch
BranchTrueFalseTotal
my $sess = $self->session_create_by("load", $con, $sid) or $self->error("Can't load session for sid='%s': %s", $sid, "CGI::Session"->errstr) 0 0 0
127 or $self->error("Can't load session for sid='%s': %s"
128 , $sid, CGI::Session->errstr);
129  
130 CHK: {
131 0 ▶ if ($sess->is_expired) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sess->is_expired) { } 0 0 0
elsif (not $sess->id) { } 0 0 0
132 $con->logdump("session.resume" => expired => $sid)
133 0 ▶ if $self->{session_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"session_debug"} 0 0 0
134 } elsif (not $sess->id) {
135 $con->logdump("session.resume" => "id is empty"
136 , claimed_sid => $sid, sessobj => $sess)
137 0 ▶ if $self->{session_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"session_debug"} 0 0 0
138 } else {
139 0 last CHK;
140 }
141 # not ok.
142 0 delete $prop->{_session}; # To allow calling session_start.
143 0 return undef; # XXX: Should we notify?
144 };
145  
146 $con->logdump("session.resume" => OK => sid => $sid)
147 0 ▶ if $self->{session_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"session_debug"} 0 0 0
148  
149 0 $prop->{_session} = $sess;
150 }
151  
152 # This will be called back from $CON->start_session.
153 0 ▶sub session_start {
Subroutine session_start: not called CC=9 SCAR=45.0
Pod undocumented
154 0 (my MY $self, my ($con, @with_init)) = @_;
155  
156 0 ▶ my $opts = shift @with_init if @with_init and ref $with_init[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @with_init and ref $with_init[0] eq "HASH" 0 0 0
Condition0%0/3
@with_init and ref $with_init[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@with_init and ref $with_init[0] eq "HASH"
@with_init ref $with_init[0] eq "HASH"
Truth table0%0/3
A and B
A= @with_initB= ref $with_init[0] eq "HASH"
ABresult
0X0
100
111
157 0 ▶ my $path = delete $opts->{path} || $self->session_path($con);
Condition0%0/3
delete $opts->{'path'} || $self->session_path($con)
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $opts->{'path'} || $self->session_path($con)
delete $opts->{'path'} $self->session_path($con)
Truth table0%0/3
A || B
A= delete $opts->{'path'}B= $self->session_path($con)
ABresult
1X1
011
000
158 0 ▶ if (keys %$opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$opts) 0 0 0
159 0 $self->error("Invalid option for session_start: %s"
160 , join ", ", keys %$opts);
161 }
162  
163 $con->logbacktrace("session.start")
164 0 ▶ if num_is_ge($self->{session_debug}, 2);
Branch: 1 branch
BranchTrueFalseTotal
if num_is_ge($self->{"session_debug"}, 2) 0 0 0
165  
166 0 my ConnProp $prop = $con->prop;
167  
168 0 ▶ if (defined $prop->{_session}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prop->{"_session"}) 0 0 0
169 0 $self->error("session is called twice! sid=%s", $prop->{_session}->id);
170 }
171 0 $prop->{_session} = undef;
172  
173 0 ▶ my $sess = $self->session_create_by(new => $con)
Branch: 1 branch
BranchTrueFalseTotal
my $sess = $self->session_create_by("new", $con) or $self->error("Can't create new session: %s", "CGI::Session"->errstr) 0 0 0
174 or $self->error("Can't create new session: %s", CGI::Session->errstr);
175  
176 0 $con->set_cookie($self->session_sid_name
177 , $sess->id
178 , -path => $path);
179 0 $sess->clear;
180 0 ▶ $self->session_init($con, $sess, @with_init) if @with_init;
Branch: 1 branch
BranchTrueFalseTotal
if @with_init 0 0 0
181  
182 0 $prop->{_session} = $sess;
183 }
184  
185 0 ▶sub session_create_by {
Subroutine session_create_by: not called CC=4 SCAR=30.0
Pod undocumented
186 0 (my MY $self, my ($method, $con, $sid)) = @_;
187 0 require CGI::Session;
188  
189 0 my ($type, %driver_opts) = $self->session_driver;
190 0 my Config $opts = $self->{session_config};
191  
192 0 ▶ my $expire = delete($opts->{expire}) // $self->default_session_expire;
Condition0%0/3
delete $opts->{"expire"} // $self->default_session_expire
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $opts->{"expire"} // $self->default_session_expire
delete $opts->{"expire"} $self->default_session_expire
Truth table0%0/3
A // B
A= delete $opts->{"expire"}B= $self->default_session_expire
ABresult
1X1
011
000
193 0 ▶ if (my $sess = CGI::Session->$method($type, $sid, \%driver_opts, $opts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sess = "CGI::Session"->$method($type, $sid, \%driver_opts, $opts)) 0 0 0
194 0 ▶ unless ($sess->is_expired) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sess->is_expired) 0 0 0
195 0 $sess->expire($expire);
196 }
197 0 return $sess;
198 }
199 }
200  
201 0 ▶sub session_init {
Subroutine session_init: not called CC=9 SCAR=45.0
Pod undocumented
202 0 my MY $self = shift;
203 0 my ConnProp $prop = (my $con = shift)->prop;
204 0 my ($sess, @with_init) = @_;
205  
206 0 foreach my $spec (@with_init) {
207 0 ▶ unless (defined $spec) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $spec) { } 0 0 0
if (do { ref $spec eq 'ARRAY' }) { } 0 0 0
elsif (not ref $spec or ref $spec eq 'Regexp') { } 0 0 0
208 0 $self->error("Undefined session initializer");
209 } elsif (ref $spec eq 'ARRAY') {
210 0 my ($name, @value) = @$spec;
211 0 $sess->param($name, @value);
212 ▶ } elsif (not ref $spec or ref $spec eq 'Regexp') {
Condition0%0/3
not ref $spec or ref $spec eq "Regexp"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $spec or ref $spec eq "Regexp"
not ref $spec ref $spec eq "Regexp"
Truth table0%0/3
not(A) or B
A= not ref $specB= ref $spec eq "Regexp"
ABresult
1X1
011
000
213 0 ▶ $spec = qr{^\Q$spec} unless ref $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec 0 0 0
214 0 foreach my $name ($con->param) {
215 0 ▶ next unless $name =~ $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$spec/ 0 0 0
216 0 my (@value) = $con->param($name);
217 0 $sess->param($name, @value);
218 }
219 } else {
220 0 $self->error("Invalid session initializer: %s"
221 , terse_dump($spec));
222 }
223 }
224 }
225  
226 0 ▶sub session_driver {
Subroutine session_driver: not called CC=2 SCAR=17.9
Pod undocumented
227 0 (my MY $self) = @_;
228 $self->{session_driver}
229 ? lexpand($self->{session_driver})
230 0 ▶ : $self->default_session_driver;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'session_driver'} ? : 0 0 0
231 }
232  
233 0 ▶sub session_delete {
Subroutine session_delete: not called CC=7 SCAR=40.3
Pod undocumented
234 0 my MY $self = shift;
235 0 my ConnProp $prop = (my $con = shift)->prop;
236 0 my $opts = shift;
237 0 ▶ my $path = delete $opts->{path} || $self->session_path($con);
Condition0%0/3
delete $opts->{'path'} || $self->session_path($con)
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $opts->{'path'} || $self->session_path($con)
delete $opts->{'path'} $self->session_path($con)
Truth table0%0/3
A || B
A= delete $opts->{'path'}B= $self->session_path($con)
ABresult
1X1
011
000
238 0 ▶ if (keys %$opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$opts) 0 0 0
239 0 $self->error("Invalid option for session_delete: %s"
240 , join ", ", keys %$opts);
241 }
242  
243 $con->logbacktrace("session.delete")
244 0 ▶ if num_is_ge($self->{session_debug}, 2);
Branch: 1 branch
BranchTrueFalseTotal
if num_is_ge($self->{"session_debug"}, 2) 0 0 0
245  
246 0 ▶ if (my $sess = delete $prop->{_session}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sess = delete $prop->{'_session'}) { } 0 0 0
247 0 my $sid = $sess->id;
248 0 $sess->delete;
249 0 $sess->flush;
250 $con->logdump("session.delete" => OK => sid => $sid)
251 0 ▶ if $self->{session_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"session_debug"} 0 0 0
252 } else {
253 $con->logdump("session.delete" => 'NOP')
254 0 ▶ if $self->{session_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"session_debug"} 0 0 0
255 }
256 0 my $name = $self->session_sid_name;
257 0 my @rm = ($name, '', -expires => '-10y', -path => $path);
258 0 $con->set_cookie(@rm);
259 }
260  
261 0 ▶sub session_flush {
Subroutine session_flush: not called CC=4 SCAR=30.0
Pod undocumented
262 0 my MY $self = shift;
263 0 my ConnProp $prop = (my $glob = shift)->prop;
264 my $sess = $prop->{_session}
265 0 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sess = $prop->{"_session"} or return 0 0 0
266 0 ▶ return if $sess->errstr;
Branch: 1 branch
BranchTrueFalseTotal
if $sess->errstr 0 0 0
267 0 $sess->flush;
268 0 ▶ if (my $err = $sess->errstr) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $sess->errstr) 0 0 0
269 0 local $prop->{_session};
270 0 $self->error("Can't flush session: %s", $err);
271 }
272 }
273  
274 0 ▶sub configure_use_session {
Subroutine configure_use_session: not called CC=5 SCAR=34.0
Pod undocumented
275 0 (my MY $self, my $value) = @_;
276 0 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 0 0 0
277 $self->{session_config}
278 0 ▶ //= ref $value ? +{lexpand($value)} : +{$self->default_session_config};
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 0 0
Condition0%0/3
$self->{"session_config"} //= ref $value ? {lexpand($value)} : {$self->default_session_config}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"session_config"} //= ref $value ? {lexpand($value)} : {$self->default_session_config}
$self->{"session_config"} ref $value ? {lexpand($value)} : {$self->default_session_config}
Truth table0%0/3
A //= B
A= $self->{"session_config"}B= ref $value ? {lexpand($value)} : {$self->default_session_config}
ABresult
1X1
011
000
279 0 ▶ $self->{session_driver} //= [$self->default_session_driver];
Condition0%0/2
$self->{"session_driver"} //= [$self->default_session_driver]
l!l
00
MC/DC0%0/1
$self->{"session_driver"} //= [$self->default_session_driver]
$self->{"session_driver"}
Truth table0%0/2
A
A= $self->{"session_driver"}
Aresult
11
00
280 }
281 }
282  
283 0 ▶sub session_path {
Subroutine session_path: not called CC=2 SCAR=17.9
Pod undocumented
284 0 (my MY $self, my ($con)) = @_;
285 0 ▶ $self->{session_path} || $con->site_location;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"session_path"} or $con->site_location 0 0 0
286 }
287  
288 0 ▶sub session_sid_name {
Subroutine session_sid_name: not called CC=2 SCAR=17.9
Pod undocumented
289 0 (my MY $self) = @_;
290 0 my Config $opts = $self->{session_config};
291 0 ▶ $opts->{name} || $self->default_session_sid_name;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{"name"} or $self->default_session_sid_name 0 0 0
292 }
293  
294 0 ▶sub default_session_expire { '1d' }
Subroutine default_session_expire: not called CC=1 SCAR=6.9
Pod undocumented
295 0 ▶sub default_session_sid_name { 'SID' }
Subroutine default_session_sid_name: not called CC=1 SCAR=6.9
Pod undocumented
296 0 ▶sub default_session_config {}
Subroutine default_session_config: not called CC=1 SCAR=0.0
Pod undocumented
297  
298 0 ▶sub default_session_driver {
Subroutine default_session_driver: not called CC=2 SCAR=17.9
Pod undocumented
299 0 (my MY $self) = @_;
300 0 ▶ my $tmpdir = $self->{tmpdir} //= $self->app_path_var_tmp('sess');
Condition0%0/3
$self->{"tmpdir"} //= $self->app_path_var_tmp("sess")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"tmpdir"} //= $self->app_path_var_tmp("sess")
$self->{"tmpdir"} $self->app_path_var_tmp("sess")
Truth table0%0/3
A //= B
A= $self->{"tmpdir"}B= $self->app_path_var_tmp("sess")
ABresult
1X1
011
000
301 0 ("driver:file"
302 , Directory => $tmpdir
303 )
304 }
305  
306 0 ▶sub cmd_session_list {
Subroutine cmd_session_list: not called CC=1 SCAR=6.9
Pod undocumented
307 0 (my MY $self, my @param) = @_;
308 0 print join("\t", qw(id created accessed), @param), "\n";
309 0 require CGI::Session;
310 0 my ($type, %driver_opts) = $self->session_driver;
311 0 ▶ CGI::Session->find($type, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
312 0 my ($sess) = @_;
313 0 ▶ print join("\t", map {defined $_ ? $_ : "(undef)"}
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
314 $sess->id, $sess->ctime, $sess->atime
315 0 , map {$sess->param($_)} @param), "\n";
316 0 }, \%driver_opts);
317 }
318  
319  
320 0 ▶sub csrftok_name {
Subroutine csrftok_name: not called CC=2 SCAR=17.9
Pod undocumented
321 0 (my MY $self) = @_;
322 0 ▶ $self->{csrftok_name} || $self->default_csrftok_name;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"csrftok_name"} or $self->default_csrftok_name 0 0 0
323 }
324  
325 0 ▶sub default_csrftok_name { '--csrftok' }
Subroutine default_csrftok_name: not called CC=1 SCAR=6.9
Pod undocumented
326  
327 1 1;
« blib/lib/YATT/Lite/Inspector.pm ↑ Summary blib/lib/YATT/Lite/LanguageServer/Generic.pm »