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

filter: Statementstmt 85.2% 167 / 196 Branchbran 69.6% 39 / 56 Conditioncond 48.1% 13 / 27 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 82.9% 34 / 41 Podpod 17.6% 3 / 17 totaltotal 73.7% 261 / 354 CC 40 SCAR 13.2
CC
40
Cov
78%
__ANON__
12.0
session_apply_cookie_defaults
7.1
session_state_extract_id
6.3
CRAP 3.7
« misc/cpan/MyBuilder.pm ↑ Summary blib/lib/YATT/Lite/XHF/Dumper.pm »
1 package YATT::Lite::WebMVC0::Partial::Session2;
2 sub MY () {__PACKAGE__}
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use constant DEBUG => ($ENV{DEBUG_YATT_SESSION2} // 0);
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"DEBUG_YATT_SESSION2"} // 0
l!l
04
MC/DC0%0/1
$ENV{"DEBUG_YATT_SESSION2"} // 0
$ENV{"DEBUG_YATT_SESSION2"}
Truth table50%1/2
A
A= $ENV{"DEBUG_YATT_SESSION2"}
Aresult
11
00
8 4 ▶use YATT::Lite::Util qw/dputs
Subroutine BEGIN: called CC=1 SCAR=0.0
9 lexpand
10 4 /;
11  
12 4 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 #========================================
15  
16 # This version of YATT::Lite::WebMVC0::Partial::Session2 directly
17 # calls *internal* methods of Plack::Middleware::Session.
18  
19 4 ▶use Plack::Middleware::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶sub default_session_middleware_class {'Plack::Middleware::Session'}
Subroutine default_session_middleware_class: called CC=1 SCAR=0.0
Pod undocumented
21  
22 #========================================
23  
24 4 ▶use YATT::Lite::PSGIEnv;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 use YATT::Lite::Partial
27 4 ▶ (requires => [qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
28 error
29 /]
30 , fields => [qw/
31 _session_middleware
32 session_middleware_class
33 session_state
34 session_store
35 /]
36 , -Entity, -CON
37 4 );
38  
39 #========================================
40  
41 6 ▶Entity psgix_session => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
42 6 my ($this, @opts) = @_;
43 6 my Env $env = $CON->env;
44 6 ▶ unless ($env->{'psgix.session.options'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($env->{"psgix.session.options"}) 1 5 6
45 1 $CON->cget('system')->session_start($CON, @opts);
46 }
47 6 $env->{'psgix.session'};
48 4 };
49  
50 0 ▶Entity psgix_session_or_dummy => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
51 0 my ($this) = @_;
52 0 ▶ if ($this->entity_session_state_exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->entity_session_state_exists) { } 0 0 0
53 0 $this->entity_psgix_session;
54 } else {
55 0 +{};
56 }
57 4 };
58  
59 0 ▶Entity psgix_session_options => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 my ($this) = @_;
61 0 my Env $env = $CON->env;
62 0 $env->{'psgix.session.options'};
63 4 };
64  
65 10 ▶Entity psgix_session_exists => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
66 10 my ($this) = @_;
67 10 my Env $env = $CON->env;
68 defined $env->{'psgix.session.options'}
69 10 ▶ and $env->{'psgix.session.options'}{'id'};
Branch: 1 branch
BranchTrueFalseTotal
defined $env->{"psgix.session.options"} and $env->{"psgix.session.options"}{"id"} 7 3 10
70 4 };
71  
72 #----------------------------------------
73 22 ▶Entity session_start => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 22 my ($this, @opts) = @_;
75 22 $CON->cget('system')->session_start($CON, @opts);
76 22 "";
77 4 };
78  
79 17 ▶Entity session_state_id => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
80 17 my ($this) = @_;
81 17 $CON->cget('system')->session_state_extract_id($CON);
82 4 };
83  
84 23 ▶Entity session_state_exists => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 23 $CON->cget('system')->session_state_exists($CON);
86 4 };
87  
88 2 ▶Entity session_change_id => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
89 2 my ($this) = @_;
90 2 $CON->cget('system')->session_change_id($CON);
91 4 };
92  
93 4 ▶Entity session_expire => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
94 4 my ($this) = @_;
95 4 $CON->cget('system')->session_expire($CON);
96 4 "";
97 4 };
98  
99 0 ▶Entity session_middleware => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
100 0 my ($this) = @_;
101 0 my MY $self = $CON->cget('system');
102 0 ▶ my $mw = $self->{_session_middleware} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $mw = $self->{"_session_middleware"} or do { Carp::croak("Session middleware is not initialized!") } 0 0 0
103 0 Carp::croak("Session middleware is not initialized!");
104 };
105 0 $mw;
106 4 };
107  
108 0 ▶Entity session_csrf_token => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
109 0 my ($this, $name) = @_;
110 0 ▶ $name //= "csrf_token"; # XXX: default name should be configurable.
Condition0%0/2
$name //= "csrf_token"
l!l
00
MC/DC0%0/1
$name //= "csrf_token"
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
111 0 ▶ $this->entity_psgix_session->{$name} //= do {
Condition0%0/3
$this->entity_psgix_session->{$name} //= do { $this->entity_session_middleware->state->generate }
l!l&&r!l&&!r
000
MC/DC0%0/2
$this->entity_psgix_session->{$name} //= do { $this->entity_session_middleware->state->generate }
$this->entity_psgix_session->{$name} do { $this->entity_session_middleware->state->generate }
Truth table0%0/3
A //= B
A= $this->entity_psgix_session->{$name}B= do { $this->entity_session_middleware->state->generate }
ABresult
1X1
011
000
112 0 $this->entity_session_middleware->state->generate;
113 };
114 4 };
115  
116 #----------------------------------------
117 28 ▶sub default_session_class {'Plack::Session'}
Subroutine default_session_class: called CC=1 SCAR=0.0
Pod undocumented
118  
119 0 ▶Entity session => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 my ($this) = @_;
121 0 my Env $env = $CON->env;
122 0 $env->{'plack.session'};
123 4 };
124  
125 {
126 4 foreach my $meth (qw(id get keys)) {
127 20 ▶ Entity "session_$meth" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
128 20 my $this = shift;
129 20 my Env $env = $CON->env;
130 20 $env->{'plack.session'}->$meth(@_);
131 12 };
132 }
133 4 foreach my $meth (qw(set remove)) {
134 11 ▶ Entity "session_$meth" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
135 11 my $this = shift;
136 11 my Env $env = $CON->env;
137 11 $env->{'plack.session'}->$meth(@_);
138 11 "";
139 8 };
140 }
141 }
142  
143 #========================================
144  
145 #
146 # Stolen from the top half of Plack::Middleware::Session->call
147 #
148 23 ▶sub session_start {
Subroutine session_start: called CC=3 SCAR=11.1
Pod documented
149 23 (my MY $self, my ($CON, @opts)) = @_;
150  
151 23 ▶ my $mw = $self->{_session_middleware} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $mw = $self->{"_session_middleware"} or do { Carp::croak("Session middleware is not initialized!") } 23 0 23
152 0 Carp::croak("Session middleware is not initialized!");
153 };
154  
155 23 my Env $env = $CON->env;
156  
157 23 my $id = $self->session_state_extract_id($CON);
158 23 ▶ my $session; $session = $self->session_store_fetch($CON, $id) if $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id 6 17 23
159  
160 23 $self->session_init_env($env, $session, $id, @opts);
161 }
162  
163 4 ▶sub session_expire {
Subroutine session_expire: called CC=3 SCAR=12.0
Pod documented
164 4 (my MY $self, my ($CON)) = @_;
165  
166 4 ▶ my $mw = $self->{_session_middleware} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $mw = $self->{"_session_middleware"} or do { Carp::croak("Session middleware is not initialized!") } 4 0 4
167 0 Carp::croak("Session middleware is not initialized!");
168 };
169  
170 4 my Env $env = $CON->env;
171  
172 # XXX: How about renaming $options->{expire} to $options->{do_expire}
173 # to avoid confusing with $options->{expires}.
174 #
175 4 ▶ if (my $options = $env->{'psgix.session.options'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $options = $env->{'psgix.session.options'}) { } 0 4 4
176  
177 0 $options->{expire} = 1;
178  
179 } else {
180 4 my $id = $self->session_state_extract_id($CON);
181 4 $self->session_init_env($env, {}, $id, expire => 1);
182 }
183  
184 }
185  
186 28 ▶sub session_init_env {
Subroutine session_init_env: called CC=5 SCAR=16.4
Pod undocumented
187 28 (my MY $self, my Env $env, my ($session, $id, %opts)) = @_;
188  
189 28 ▶ my $mw = $self->{_session_middleware} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $mw = $self->{"_session_middleware"} or do { Carp::croak("Session middleware is not initialized!") } 28 0 28
190 0 Carp::croak("Session middleware is not initialized!");
191 };
192  
193 28 ▶ if ($id && $session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id and $session) { } 9 19 28
Condition66%2/3
$id and $session
!ll&&!rl&&r
1909
MC/DC50%1/2
$id and $session
$id $session
Truth table66%2/3
A and B
A= $idB= $session
ABresult
0X0
100
111
194 9 $env->{'psgix.session'} = $session;
195 } else {
196 19 $id = $mw->generate_id($env);
197 19 $env->{'psgix.session'} = {};
198 }
199  
200 28 ▶ $opts{path} //= $env->{'yatt.script_dir'};
Condition66%2/3
$opts{"path"} //= $env->{"yatt.script_dir"}
l!l&&r!l&&!r
0271
MC/DC50%1/2
$opts{"path"} //= $env->{"yatt.script_dir"}
$opts{"path"} $env->{"yatt.script_dir"}
Truth table66%2/3
A //= B
A= $opts{"path"}B= $env->{"yatt.script_dir"}
ABresult
1X1
011
000
201  
202 28 $env->{'psgix.session.options'} = { id => $id, %opts };
203  
204 28 $self->session_apply_cookie_defaults($env, $env->{'psgix.session.options'});
205  
206 28 $env->{'plack.session'}
207 = Plack::Util::load_class($self->default_session_class)->new($env);
208  
209 }
210  
211 #
212 # Cookie attribute defaults for the session cookie, applied to
213 # psgix.session.options so that both State::Cookie#finalize and
214 # #expire_session_id see them (merge_options only fills what is absent):
215 #
216 # secure - 1 whenever the request came over TLS (psgi.url_scheme,
217 # X-Forwarded-Proto or HTTPS), not only when $env->{HTTPS} is set.
218 # httponly - 1, unless the configured session_state defines it.
219 # samesite - Lax, unless the configured session_state defines it.
220 #
221 28 ▶sub session_apply_cookie_defaults {
Subroutine session_apply_cookie_defaults: called CC=7 SCAR=19.7
Pod undocumented
222 28 (my MY $self, my Env $env, my $options) = @_;
223 28 my $state = $self->{_session_middleware}->state;
224  
225 28 ▶ if (Env->is_https($env)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("YATT::Lite::WebMVC0::Partial::Session2::Env"->is_https($env)) 3 25 28
226 3 ▶ $options->{secure} //= 1;
Condition50%1/2
$options->{"secure"} //= 1
l!l
03
MC/DC0%0/1
$options->{"secure"} //= 1
$options->{"secure"}
Truth table50%1/2
A
A= $options->{"secure"}
Aresult
11
00
227 }
228 28 foreach my $spec ([httponly => 1], [samesite => 'Lax']) {
229 56 my ($attr, $default) = @$spec;
230 56 ▶ next if exists $options->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{$attr} 0 56 56
231 56 ▶ next if $state->can($attr) and defined $state->$attr;
Branch: 1 branch
BranchTrueFalseTotal
if $state->can($attr) and defined $state->$attr 11 45 56
Condition66%2/3
$state->can($attr) and defined $state->$attr
!ll&&!rl&&r
04511
MC/DC50%1/2
$state->can($attr) and defined $state->$attr
$state->can($attr) defined $state->$attr
Truth table66%2/3
A and B
A= $state->can($attr)B= defined $state->$attr
ABresult
0X0
100
111
232 45 $options->{$attr} = $default;
233 }
234 28 $options;
235 }
236  
237 23 ▶sub session_state_exists {
Subroutine session_state_exists: called CC=1 SCAR=0.0
Pod documented
238 23 (my MY $self, my $CON) = @_;
239 23 defined $self->session_state_extract_id($CON);
240 }
241  
242 #
243 # The session id as claimed by the client's cookie. Only an id that
244 # passes the state's validator (Plack::Session::State: /\A[0-9a-f]{40}\Z/)
245 # is returned; anything else is treated as "no cookie" so it never
246 # reaches $store->fetch/remove (with Plack::Session::Store::File, an
247 # unvalidated id is a file path).
248 #
249 68 ▶sub session_state_extract_id {
Subroutine session_state_extract_id: called CC=6 SCAR=18.4
Pod undocumented
250 68 (my MY $self, my $CON) = @_;
251 68 ▶ my $mw = $self->{_session_middleware} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $mw = $self->{"_session_middleware"} or do { Carp::croak("Session middleware is not initialized!") } 68 0 68
252 0 Carp::croak("Session middleware is not initialized!");
253 };
254  
255 68 my $state = $mw->state;
256 68 my $id = $CON->cookies_in->{$state->session_key};
257 68 ▶ return undef unless defined $id and length $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and length $id 18 50 68
Condition66%2/3
defined $id and length $id
!ll&&!rl&&r
18050
MC/DC50%1/2
defined $id and length $id
defined $id length $id
Truth table66%2/3
A and B
A= defined $idB= length $id
ABresult
0X0
100
111
258 50 ▶ if ($state->can('validate_session_id')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->can("validate_session_id")) 50 0 50
259 50 ▶ return undef unless $state->validate_session_id($id);
Branch: 1 branch
BranchTrueFalseTotal
unless $state->validate_session_id($id) 20 30 50
260 }
261 30 $id;
262 }
263  
264 #
265 # In some case, you may want to clear previous session explicitly
266 # even when previous cookie state exists.
267 #
268 2 ▶sub session_change_id {
Subroutine session_change_id: called CC=4 SCAR=14.1
Pod undocumented
269 2 (my MY $self, my $CON) = @_;
270 2 ▶ my $mw = $self->{_session_middleware} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $mw = $self->{"_session_middleware"} or do { Carp::croak("Session middleware is not initialized!") } 2 0 2
271 0 Carp::croak("Session middleware is not initialized!");
272 };
273  
274 2 my Env $env = $CON->env;
275  
276 2 ▶ if (not $env->{'psgix.session'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $env->{'psgix.session'}) { } 1 1 2
277 1 ▶ if (my $prev_sid = $self->session_state_extract_id($CON)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev_sid = $self->session_state_extract_id($CON)) 1 0 1
278 1 $self->session_store_remove($CON, $prev_sid);
279 }
280  
281 1 $self->session_init_env($env);
282  
283 1 return $env->{'psgix.session.options'}{'id'};
284 } else {
285 1 $mw->change_id($env);
286 }
287 }
288  
289 6 ▶sub session_store_fetch {
Subroutine session_store_fetch: called CC=2 SCAR=7.6
Pod undocumented
290 6 (my MY $self, my ($CON, $id)) = @_;
291 6 ▶ my $mw = $self->{_session_middleware} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $mw = $self->{"_session_middleware"} or do { Carp::croak("Session middleware is not initialized!") } 6 0 6
292 0 Carp::croak("Session middleware is not initialized!");
293 };
294  
295 6 $mw->store->fetch($id);
296 }
297  
298 1 ▶sub session_store_remove {
Subroutine session_store_remove: called CC=2 SCAR=7.6
Pod undocumented
299 1 (my MY $self, my ($CON, $id)) = @_;
300 1 ▶ my $mw = $self->{_session_middleware} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $mw = $self->{"_session_middleware"} or do { Carp::croak("Session middleware is not initialized!") } 1 0 1
301 0 Carp::croak("Session middleware is not initialized!");
302 };
303  
304 1 $mw->store->remove($id);
305 }
306  
307 #
308 # Stolen from the bottom half of Plack::Middleware::Session->call
309 #
310 30 ▶sub finalize_response {
Subroutine finalize_response: called CC=4 SCAR=13.9
Pod undocumented
311 30 (my MY $self, my ($env, $res)) = @_;
312  
313 30 dputs('START') if DEBUG >= 4;
314  
315 30 $self->maybe::next::method($env, $res);
316  
317 30 my $mw = $self->{_session_middleware};
318 30 my $session = $env->{'psgix.session'};
319 30 my $options = $env->{'psgix.session.options'};
320  
321 30 ▶ if (not $session) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session) 3 27 30
322 3 return;
323 }
324  
325 # XXX: See comment in session_expire() too.
326 #
327 27 ▶ if ($options->{expire}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'expire'}) { } 4 23 27
328 4 dputs('EXPIRE') if DEBUG >= 4;
329 4 $mw->expire_session($options->{id}, $res, $env);
330 } else {
331 23 ▶ $mw->change_id($env) if $options->{change_id};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"change_id"} 0 23 23
332 23 $mw->commit($env);
333 23 $mw->save_state($options->{id}, $res, $env);
334 23 dputs('SAVED') if DEBUG >= 4;
335 }
336  
337 27 dputs('DONE') if DEBUG >= 4;
338 }
339  
340 #
341 # This prepare_app is called very late of inheritance chain.
342 #
343 7 ▶sub prepare_app {
Subroutine prepare_app: called CC=4 SCAR=14.2
Pod undocumented
344 7 (my MY $self) = @_;
345  
346 7 dputs('START') if DEBUG >= 3;
347  
348 7 my $mw = $self->{_session_middleware} = do {
349 my $class = $self->{session_middleware_class}
350 7 ▶ || $self->default_session_middleware_class;
Condition33%1/3
$self->{'session_middleware_class'} || $self->default_session_middleware_class
l!l&&r!l&&!r
070
MC/DC0%0/2
$self->{'session_middleware_class'} || $self->default_session_middleware_class
$self->{'session_middleware_class'} $self->default_session_middleware_class
Truth table33%1/3
A || B
A= $self->{'session_middleware_class'}B= $self->default_session_middleware_class
ABresult
1X1
011
000
351  
352 my $state = ($self->{session_state}
353 ? $self->create_session_backend(State => $self->{session_state})
354 7 ▶ : Plack::Util::load_class($self->default_session_state)->new());
Branch: 1 branch
BranchTrueFalseTotal
$self->{'session_state'} ? : 4 3 7
355  
356 0 ▶ $class->new({app => sub {[200, [], []]}
Subroutine __ANON__: not called CC=1 SCAR=6.9
357 , state => $state
358 , ($self->{session_store}
359 7 ▶ ? (store => $self->create_session_backend(Store => $self->{session_store})) : ())
Branch: 1 branch
BranchTrueFalseTotal
$self->{'session_store'} ? : 7 0 7
360 });
361 };
362  
363 7 dputs('session_middleware is created') if DEBUG >= 3;
364  
365 7 $mw->prepare_app;
366  
367 7 dputs('after session_middleware->prepare_app') if DEBUG >= 3;
368  
369 7 dputs('begin maybe::next::method') if DEBUG >= 3;
370  
371 7 $self->maybe::next::method;
372  
373 7 dputs('DONE') if DEBUG >= 3;
374 }
375  
376 3 ▶sub default_session_state {'Plack::Session::State::Cookie'}
Subroutine default_session_state: called CC=1 SCAR=0.0
Pod undocumented
377 0 ▶sub default_session_store {'Plack::Session::Store'}
Subroutine default_session_store: not called CC=1 SCAR=6.9
Pod undocumented
378 7 ▶sub default_session_class_prefix {'Plack::Session'}
Subroutine default_session_class_prefix: called CC=1 SCAR=0.0
Pod undocumented
379  
380 # From Session::inflate_backend
381 11 ▶sub create_session_backend {
Subroutine create_session_backend: called CC=3 SCAR=11.0
Pod undocumented
382 11 (my MY $self, my ($kind, $spec)) = @_;
383  
384 # When $spec is not [$backend => @opts], just return it.
385 11 ▶ return $spec if defined $spec and ref $spec ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec and ref $spec ne "ARRAY" 4 7 11
Condition66%2/3
defined $spec and ref $spec ne "ARRAY"
!ll&&!rl&&r
074
MC/DC50%1/2
defined $spec and ref $spec ne "ARRAY"
defined $spec ref $spec ne "ARRAY"
Truth table66%2/3
A and B
A= defined $specB= ref $spec ne "ARRAY"
ABresult
0X0
100
111
386  
387 7 my ($backend, @args) = lexpand($spec);
388  
389 7 my $prefix = join("::", $self->default_session_class_prefix, $kind);
390  
391 7 my $class = Plack::Util::load_class($backend, $prefix);
392  
393 7 $class->new(@args);
394 }
395  
396 4 1;
« misc/cpan/MyBuilder.pm ↑ Summary blib/lib/YATT/Lite/XHF/Dumper.pm »