Terse.pm

filter: Statementstmt 69.6% 197 / 283 Branchbran 58.7% 87 / 148 Conditioncond 46.0% 52 / 113 MC/DCmcdc 17.4% 11 / 63 Subroutinesub 80.0% 32 / 40 Podpod 81.8% 9 / 11 totaltotal 58.9% 388 / 658 CC 120 SCAR 36.3
CC
120
Cov
62%
run
127.7
TO_JSON
90.0
__ANON__
42.0
CRAP 37.9
« Terse/App.pm ↑ Summary Terse/Plugin.pm »
1 package Terse;
2 13 our $VERSION = '0.28';
3 13 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Terse::WebSocket;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Want qw/want/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use Struct::WOP qw/all/ => { type => ['UTF-8'], destruct => 1 };
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 13 our ($JSON, %PRIVATE);
19 13 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 $JSON = Cpanel::JSON::XS->new->utf8->canonical(1)->allow_blessed->convert_blessed;
21 %PRIVATE = (
22 13 map { $_ => 1 }
23 qw/new run logger logInfo logError websocket delayed_response build_terse content_type raiseError graft pretty serialize DESTROY TO_JSON AUTOLOAD to_app/
24 );
25 }
26  
27 44 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod documented
28 44 my ($pkg, %args) = @_;
29
30 44 ▶ $pkg = ref $pkg if ref $pkg;
Branch: 1 branch
BranchTrueFalseTotal
if ref $pkg 0 44 44
31
32 44 ▶ if (delete $args{private}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $args{"private"}) 6 38 44
33 6 for my $key (keys %args) {
34 41 ▶ if ($key !~ m/^_/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /^_/) 41 0 41
35 41 $args{"_$key"} = delete $args{$key};
36 }
37 }
38 }
39  
40 44 return bless \%args, $pkg;
41 }
42  
43 3 ▶sub run {
Subroutine run: called CC=31 SCAR=48.5
Pod documented
44 3 my ($pkg, %args) = @_;
45  
46 3 my $j = $pkg->new(
47 private => 1,
48 login => 'login',
49 logout => 'logout',
50 auth => 'auth',
51 insecure_session => 0,
52 content_type => 'application/json',
53 request_class => 'Plack::Request',
54 websocket_class => 'Terse::WebSocket',
55 sock => 'psgix.io',
56 stream_check => 'psgi.streaming',
57 favicon => 'favicon.ico',
58 %args
59 );
60  
61 3 $j->headers = {};
62  
63 3 $j->_build_terse();
64
65 3 $j->request = $j->{_request_class}->new($args{plack_env});
66 3 $j->response = $pkg->new(
67 authenticated => \0,
68 error => \0,
69 errors => [],
70 );
71
72 3 ▶ if ($j->request->env->{PATH_INFO} =~ m/favicon.ico$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j->request->env->{"PATH_INFO"} =~ /favicon.ico$/) 0 3 3
73 0 ▶ return [500, [], []] unless -f $j->_favicon;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $j->_favicon 0 0 0
74 0 open my $fh, '<', $j->_favicon;
75 0 my $favicon = do { local $/; <$fh> };
76 0 close $fh;
77 0 return [200, ['Content-Type', 'image/vnd.microsoft.icon'], [$favicon] ];
78 }
79  
80 3 my $content_type = $j->request->content_type;
81 3 ▶ if ($content_type && $content_type =~ m/application\/json/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type and $content_type =~ m[application/json]) { } 0 3 3
Condition33%1/3
$content_type and $content_type =~ m[application/json]
!ll&&!rl&&r
300
MC/DC0%0/2
$content_type and $content_type =~ m[application/json]
$content_type $content_type =~ m[application/json]
Truth table33%1/3
A and B
A= $content_typeB= $content_type =~ m[application/json]
ABresult
0X0
100
111
82 0 ▶ $j->graft('params', $j->request->raw_body || "{}");
Condition0%0/2
$j->request->raw_body || '{}'
l!l
00
MC/DC0%0/1
$j->request->raw_body || '{}'
$j->request->raw_body
Truth table0%0/2
A
A= $j->request->raw_body
Aresult
11
00
83 } else {
84 3 ▶ $j->params = {%{$j->request->parameters || {}}};
Condition50%1/2
$j->request->parameters || {}
l!l
30
MC/DC0%0/1
$j->request->parameters || {}
$j->request->parameters
Truth table50%1/2
A
A= $j->request->parameters
Aresult
11
00
85 }
86  
87 3 ▶ unless ((reftype($j->params) || "") eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless ((builtin::reftype($j->params) || "") eq "HASH") 0 3 3
Condition50%1/2
builtin::reftype($j->params) || ''
l!l
30
MC/DC0%0/1
builtin::reftype($j->params) || ''
builtin::reftype($j->params)
Truth table50%1/2
A
A= builtin::reftype($j->params)
Aresult
11
00
88 0 $j->response->raiseError('Invalid parameters', 400);
89 0 return $j->_response($j->response);
90 }
91
92 3 $j->sid = $j->request->cookies->{sid};
93
94 3 ▶ unless ($j->sid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($j->sid) 0 3 3
95 0 my $h = Digest::SHA->new(256);
96 0 my @us = gettimeofday;
97 0 push @us, map { $j->request->env->{$_} } grep {
98 0 $_ =~ /^HTTP(?:_|$)/;
99 0 } keys %{ $j->request->env };
100 0 $h->add(@us);
101 0 $j->sid = $h->hexdigest;
102 }
103  
104 $j->sid = {
105 value => $j->is_logout ? "" : $j->sid,
106 path => $j->{_sid_path} || $j->{_root_path} || "/",
107 secure => !$j->{_insecure_session},
108 3 ▶ samesite => 'none'
Branch: 1 branch
BranchTrueFalseTotal
$j->is_logout ? : 0 3 3
Condition40%2/5
$j->{'_sid_path'} || $j->{'_root_path'} || '/'
l!l
03
$j->{"_sid_path"} or $j->{"_root_path"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$j->{'_sid_path'} || $j->{'_root_path'} || '/'
$j->{"_sid_path"} $j->{"_root_path"}
Truth table33%1/3
A or B
A= $j->{"_sid_path"}B= $j->{"_root_path"}
ABresult
1X1
011
000
109 };
110  
111 3 my $auth = $j->{_auth};
112  
113 3 my ($session) = $j->_dispatch($auth, $pkg->new());
114
115 3 my $req = $j->params->req;
116 3 ▶ $req =~ /^([a-z][0-9a-zA-Z_]{1,31})$/ && do { $req = $1 // '' } if $req;
Branch: 1 branch
BranchTrueFalseTotal
if $req 3 0 3
Condition37%3/8
$req && ($req =~ /^([a-z][0-9a-zA-Z_]{1,31})$/ && do { $req = $1 // '' })
!ll&&!rl&&r
030
$req =~ /^([a-z][0-9a-zA-Z_]{1,31})$/ and do { $req = $1 // "" }
!ll&&!rl&&r
030
$1 // ""
l!l
30
MC/DC0%0/3
$req && ($req =~ /^([a-z][0-9a-zA-Z_]{1,31})$/ && do { $req = $1 // '' })
$req $req =~ /^([a-z][0-9a-zA-Z_]{1,31})$/ do { $req = $1 // "" }
MC/DC0%0/1
$1 // ""
$1
Truth table0%0/4
A && B and C
A= $reqB= $req =~ /^([a-z][0-9a-zA-Z_]{1,31})$/C= do { $req = $1 // "" }
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $1
Aresult
11
00
117 3 ▶ $req = $j->{_application}->preprocess_req($req, $j) if $j->{_application}->can('preprocess_req');
Branch: 1 branch
BranchTrueFalseTotal
if $j->{"_application"}->can("preprocess_req") 0 3 3
118 3 ▶ if (!$req || !$session || $PRIVATE{$req}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $req && $session or $Terse::PRIVATE{$req}) 0 3 3
Condition33%2/6
not $req && $session or $Terse::PRIVATE{$req}
l!l&&r!l&&!r
003
$req && $session
!ll&&!rl&&r
003
MC/DC0%0/3
not $req && $session or $Terse::PRIVATE{$req}
$req $session $Terse::PRIVATE{$req}
Truth table25%1/4
not(A && B) or C
A= $reqB= $sessionC= $Terse::PRIVATE{$req}
ABCresult
0XX1
10X1
1111
1100
119 0 $j->response->raiseError('Invalid request', 400);
120 0 return $j->_response($j->response);
121 }
122  
123 3 $j->req = $req;
124 3 $j->response->authenticated = \1;
125 3 $j->session = $session;
126  
127 3 ▶ $j->sid->expires = (ref $j->session && $j->session->expires) || (time + 24 * 60 * 60)
Branch: 1 branch
BranchTrueFalseTotal
unless $j->sid->expires 3 0 3
Condition33%2/6
ref $j->session && $j->session->expires || time + 86400
l!l&&r!l&&!r
030
ref $j->session and $j->session->expires
!ll&&!rl&&r
300
MC/DC0%0/3
ref $j->session && $j->session->expires || time + 86400
ref $j->session $j->session->expires time + 86400
Truth table0%0/5
A and B || C
A= ref $j->sessionB= $j->session->expiresC= time + 86400
ABCresult
11X1
0X11
1011
0X00
1000
128 if (!$j->sid->expires);
129  
130 ($j->is_login, $j->is_logout) = (
131 $j->{_login} eq $req,
132 3 $j->{_logout} eq $req
133 );
134  
135 3 my ($out) = $j->_dispatch($req);
136
137 3 ▶ return $j->_response($j->response) if $j->response->error;
Branch: 1 branch
BranchTrueFalseTotal
if $j->response->error 1 2 3
138  
139 2 ▶ $j->session = $out if ( $j->is_login || $j->is_logout );
Branch: 1 branch
BranchTrueFalseTotal
if $j->is_login or $j->is_logout 0 2 2
Condition33%1/3
$j->is_login or $j->is_logout
l!l&&r!l&&!r
002
MC/DC0%0/2
$j->is_login or $j->is_logout
$j->is_login $j->is_logout
Truth table33%1/3
A or B
A= $j->is_loginB= $j->is_logout
ABresult
1X1
011
000
140  
141 2 ▶ ($j->session) = $j->_dispatch($auth, $j->session) if $j->response->authenticated;
Branch: 1 branch
BranchTrueFalseTotal
if $j->response->authenticated 1 1 2
142  
143 2 ▶ if ((!$j->response->authenticated || !$j->session) && !($j->is_login || $j->is_logout)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($j->response->authenticated and $j->session or ($j->is_login or $j->is_logout)) 1 1 2
Condition55%5/9
$j->response->authenticated and $j->session or ($j->is_login or $j->is_logout)
l!l&&r!l&&!r
101
$j->response->authenticated and $j->session
!ll&&!rl&&r
101
$j->is_login or $j->is_logout
l!l&&r!l&&!r
001
MC/DC25%1/4
$j->response->authenticated and $j->session or ($j->is_login or $j->is_logout)
$j->response->authenticated $j->session $j->is_login $j->is_logout
Truth table28%2/7
A and B or C or D
A= $j->response->authenticatedB= $j->sessionC= $j->is_loginD= $j->is_logout
ABCDresult
11XX1
0X1X1
0X011
101X1
10011
0X000
10000
144 1 $j->response->raiseError('Unauthenticated during the request', 400);
145 1 return $j->_response($j->response);
146 }
147
148 1 return $j->_response($j->response, $j->sid, $j->content_type);
149 }
150  
151 0 ▶sub to_app {
Subroutine to_app: not called CC=2 SCAR=17.9
Pod undocumented
152 0 my ($self, $new, $run) = @_;
153 0 ▶ my $app = $self->new($new ? %{ $new } : ());
Branch: 1 branch
BranchTrueFalseTotal
$new ? : 0 0 0
154 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
155 0 my ($env) = (shift);
156 Terse->run(
157 plack_env => $env,
158 application => $app,
159 0 ▶ ($env->{'psgix.logger'} ? (logger => $env->{'psgix.logger'}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$env->{'psgix.logger'} ? : 0 0 0
160 );
161 0 };
162 };
163
164 4 ▶sub logger {
Subroutine logger: called CC=2 SCAR=6.9
Pod documented
165 4 my ($self, $logger) = @_;
166 4 ▶ $self->{_logger} = $logger if ($logger);
Branch: 1 branch
BranchTrueFalseTotal
if $logger 1 3 4
167 4 return $self->{_logger};
168 }
169  
170 4 ▶sub logError {
Subroutine logError: called CC=8 SCAR=20.8
Pod documented
171 4 my ($self, $message, $status, $no_response) = @_;
172 $self->{_application}
173 4 ▶ ? $self->response->raiseError($message, $status)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_application'} ? : 3 1 4
174 : $self->raiseError($message, $status);
175 4 ▶ $message = { message => $message } if (!ref $message);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $message 2 2 4
176 $message = $self->{_application}->_logError($message, $status)
177 4 ▶ if ($self->{_application} && $self->{_application}->can('_logError'));
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_application"} and $self->{"_application"}->can("_logError") 2 2 4
Condition100%3/3
$self->{"_application"} and $self->{"_application"}->can("_logError")
!ll&&!rl&&r
112
MC/DC100%2/2
$self->{"_application"} and $self->{"_application"}->can("_logError")
$self->{"_application"} $self->{"_application"}->can("_logError")
Truth table100%3/3
A and B
A= $self->{"_application"}B= $self->{"_application"}->can("_logError")
ABresult
0X0
100
111
178 ref $self->{_logger} eq 'CODE'
179 ? $self->{_logger}->('error', $message)
180 : $self->{_logger}->error($message)
181 4 ▶ if $self->{_logger};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"_logger"} 2 2 4
ref $self->{'_logger'} eq 'CODE' ? : 1 1 2
182 4 ▶ $self->response->no_response = 1 if $no_response;
Branch: 1 branch
BranchTrueFalseTotal
if $no_response 2 2 4
183 4 return $self;
184 }
185  
186 8 ▶sub logInfo {
Subroutine logInfo: called CC=6 SCAR=18.0
Pod documented
187 8 my ($self, $message) = @_;
188 8 ▶ $message = { message => $message } if (!ref $message);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $message 8 0 8
189 $message = $self->{_application}->_logInfo($message)
190 8 ▶ if ($self->{_application} && $self->{_application}->can('_logInfo'));
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_application"} and $self->{"_application"}->can("_logInfo") 6 2 8
Condition66%2/3
$self->{"_application"} and $self->{"_application"}->can("_logInfo")
!ll&&!rl&&r
206
MC/DC50%1/2
$self->{"_application"} and $self->{"_application"}->can("_logInfo")
$self->{"_application"} $self->{"_application"}->can("_logInfo")
Truth table66%2/3
A and B
A= $self->{"_application"}B= $self->{"_application"}->can("_logInfo")
ABresult
0X0
100
111
191 ref $self->{_logger} eq 'CODE'
192 ? $self->{_logger}->('info', $message)
193 : $self->{_logger}->info($message)
194 8 ▶ if $self->{_logger};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"_logger"} 4 4 8
ref $self->{'_logger'} eq 'CODE' ? : 2 2 4
195 8 return $self;
196 }
197  
198 6 ▶sub raiseError {
Subroutine raiseError: called CC=6 SCAR=20.5
Pod documented
199 6 my ($self, $message, $code) = @_;
200 6 ▶ return $self->response->raiseError($message, $code) if $self->{_application};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_application"} 0 6 6
201 6 $self->{error} = \1;
202 6 ▶ if ((reftype($message) || '') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($message) || '') eq 'ARRAY') { } 0 6 6
Condition100%2/2
builtin::reftype($message) || ''
l!l
24
MC/DC100%1/1
builtin::reftype($message) || ''
builtin::reftype($message)
Truth table100%2/2
A
A= builtin::reftype($message)
Aresult
11
00
203 0 push @{$self->{errors}}, @{$message};
204 } else {
205 6 push @{$self->{errors}}, $message;
206 }
207 6 ▶ $self->{status_code} = $code if ($code && !$self->{status_code});
Branch: 1 branch
BranchTrueFalseTotal
if $code and not $self->{"status_code"} 6 0 6
Condition33%1/3
$code and not $self->{"status_code"}
!ll&&!rl&&r
006
MC/DC0%0/2
$code and not $self->{"status_code"}
$code not $self->{"status_code"}
Truth table33%1/3
A and not(B)
A= $codeB= not $self->{"status_code"}
ABresult
0X0
100
111
208 6 return $self;
209 }
210  
211 5 ▶sub graft {
Subroutine graft: called CC=3 SCAR=11.0
Pod documented
212 5 my ($self, $name, $json) = @_;
213  
214 5 ▶ unless ($json =~ m/[\{\[]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json =~ /[\{\[]/) 1 4 5
215 1 $self->{$name} = $json;
216 1 return $self->{$name};
217 }
218  
219 4 $self->{$name} = eval {
220 4 $JSON->decode($json);
221 };
222  
223 4 ▶ return 0 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 3 4
224  
225 3 return $self->_bless_tree($self->{$name});
226 }
227  
228 1 ▶sub pretty { $_[0]->{_pretty} = 1; $_[0]; }
Subroutine pretty: called CC=1 SCAR=0.0
Pod documented
229  
230 6 ▶sub serialize {
Subroutine serialize: called CC=5 SCAR=16.7
Pod documented
231 6 my ($self, $die) = @_;
232 6 ▶ my $pretty = !!(reftype $self eq 'HASH' && $self->{_pretty});
Condition66%2/3
builtin::reftype($self) eq 'HASH' && $self->{'_pretty'}
!ll&&!rl&&r
150
MC/DC0%0/2
builtin::reftype($self) eq 'HASH' && $self->{'_pretty'}
builtin::reftype($self) eq 'HASH' $self->{'_pretty'}
Truth table66%2/3
A && B
A= builtin::reftype($self) eq 'HASH'B= $self->{'_pretty'}
ABresult
0X0
100
111
233 6 my $out = eval {
234 6 $JSON->pretty($pretty)->encode(maybe_decode($self));
235 };
236 6 ▶ die $@ if ($@ && $die);
Branch: 1 branch
BranchTrueFalseTotal
if $@ and $die 0 6 6
Condition66%2/3
$@ and $die
!ll&&!rl&&r
510
MC/DC0%0/2
$@ and $die
$@ $die
Truth table66%2/3
A and B
A= $@B= $die
ABresult
0X0
100
111
237 6 ▶ return $out || $@;
Condition66%2/3
$out || $@
l!l&&r!l&&!r
510
MC/DC0%0/2
$out || $@
$out $@
Truth table66%2/3
A || B
A= $outB= $@
ABresult
1X1
011
000
238 }
239  
240 3 ▶sub _build_terse {
Subroutine _build_terse: called CC=10 SCAR=33.5
241 3 my ($t) = @_;
242  
243 3 ▶ if (! $t->{_application}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->{"_application"}) 0 3 3
244 0 $t->response->raiseError('No application passed to run', 500);
245 0 return $t->_response($t->response);
246 }
247  
248 0 ▶ $t->{redirect} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
249 0 my ($self, $url, $response) = @_;
250 0 $url = URI->new($url);
251 0 ▶ $url->query_form( $url->query_form, %{$response || {}});
Condition0%0/2
$response || {}
l!l
00
MC/DC0%0/1
$response || {}
$response
Truth table0%0/2
A
A= $response
Aresult
11
00
252 0 $self->response->status_code = 302;
253 0 $self->response->message = 'Found';
254 0 $self->headers->Location = $url->as_string;
255 0 return $self;
256 3 };
257  
258 0 ▶ $t->{websocket} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
259 0 my ($self, %args) = @_;
260 0 my $websocket = $t->{_websocket_class}->new($self);
261 0 ▶ if (!ref $websocket) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $websocket) 0 0 0
262 0 $args{error}->($t, $websocket);
263 0 return;
264 }
265 0 ▶ $t->{_delayed_response} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
266 0 my $responder = shift;
267 0 $websocket->start($t, \%args, $responder);
268 0 };
269 0 return $websocket;
270 3 ▶ } unless $t->{websocket} || !$t->{_websocket_class};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"websocket"} or not $t->{"_websocket_class"} 3 0 3
Condition33%1/3
$t->{"websocket"} or not $t->{"_websocket_class"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$t->{"websocket"} or not $t->{"_websocket_class"}
$t->{"websocket"} not $t->{"_websocket_class"}
Truth table33%1/3
A or not(B)
A= $t->{"websocket"}B= not $t->{"_websocket_class"}
ABresult
1X1
011
000
271  
272 0 ▶ $t->{delayed_response} = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
273 0 my ($self, $response, $sid, $ct, $status) = @_;
274 0 ▶ $sid ||= $self->sid;
Condition0%0/3
$sid ||= $self->sid
l!l&&r!l&&!r
000
MC/DC0%0/2
$sid ||= $self->sid
$sid $self->sid
Truth table0%0/3
A ||= B
A= $sidB= $self->sid
ABresult
1X1
011
000
275 0 ▶ $status ||= 200;
Condition0%0/2
$status ||= 200
l!l
00
MC/DC0%0/1
$status ||= 200
$status
Truth table0%0/2
A
A= $status
Aresult
11
00
276 0 ▶ $ct ||= 'application/json';
Condition0%0/2
$ct ||= "application/json"
l!l
00
MC/DC0%0/1
$ct ||= "application/json"
$ct
Truth table0%0/2
A
A= $ct
Aresult
11
00
277 return $self->{_application}->delayed_response_handle(
278 $self, $response, $sid, $ct, $status
279 0 ▶ ) if $self->{_application_has_delayed_response_handler};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_application_has_delayed_response_handler"} 0 0 0
280 0 ▶ $self->{_delayed_response} = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
281 0 my $responder = shift;
282 0 my $res = $self->_build_response($sid, $ct, $status);
283 0 $res = [splice @{$res->finalize}, 0, 2];
284 0 my $writer = $responder->($res);
285 0 $response = eval { $response->($writer); };
286 0 ▶ if ($@ || $self->response->error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ or $self->response->error) { } 0 0 0
elsif ($response) { } 0 0 0
Condition0%0/3
$@ or $self->response->error
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or $self->response->error
$@ $self->response->error
Truth table0%0/3
A or B
A= $@B= $self->response->error
ABresult
1X1
011
000
287 0 ▶ $res->[0] = $self->response->status_code || 500;
Condition0%0/2
$self->response->status_code || 500
l!l
00
MC/DC0%0/1
$self->response->status_code || 500
$self->response->status_code
Truth table0%0/2
A
A= $self->response->status_code
Aresult
11
00
288 0 ▶ $self->raiseError($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
289 0 push @{$res}, [$self->response->serialize];
290 0 return $responder->($res);
291 }
292 elsif ($response) {
293 0 $writer->write($response->serialize);
294 }
295 0 $writer->close;
296 0 };
297 0 $self;
298 3 ▶ } unless $t->{delayed_response};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"delayed_response"} 3 0 3
299  
300 3 ▶ $t->{_application}->build_terse($t) if $t->{_application}->can('build_terse');
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"_application"}->can("build_terse") 0 3 3
301 3 $t->{_application_has_dispatcher} = !! $t->{_application}->can('dispatch');
302 3 $t->{_application_has_response_handler} = !! $t->{_application}->can('response_handle');
303 3 $t->{_application_has_delayed_response_handler} = !! $t->{_application}->can('delayed_response_handle');
304  
305 3 ▶ $t->{_build_response} = sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
306 3 my ($self, $sid, $content, $status) = @_;
307 3 ▶ my $res = $self->request->new_response($self->response->{status_code} ||= $status);
Condition66%2/3
$self->response->{"status_code"} ||= $status
l!l&&r!l&&!r
210
MC/DC0%0/2
$self->response->{"status_code"} ||= $status
$self->response->{"status_code"} $status
Truth table66%2/3
A ||= B
A= $self->response->{"status_code"}B= $status
ABresult
1X1
011
000
308 3 ▶ $res->cookies($self->cookies) if $self->cookies;
Branch: 1 branch
BranchTrueFalseTotal
if $self->cookies 0 3 3
309 3 ▶ $res->headers({%{$self->headers}}) if $self->headers;
Branch: 1 branch
BranchTrueFalseTotal
if $self->headers 3 0 3
310 3 ▶ $res->cookies->{sid} = {%{$sid}} if $sid;
Branch: 1 branch
BranchTrueFalseTotal
if $sid 1 2 3
311 3 $res->content_type($content);
312 3 return $res;
313 3 ▶ } unless $t->{_build_response};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"_build_response"} 3 0 3
314  
315 1 ▶ $t->{content_type} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
316 1 ▶ $_[0]->{_content_type} = $_[1] if $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 0 1 1
317 1 return $_[0]->{_content_type};
318 3 ▶ } unless $t->{content_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"content_type"} 3 0 3
319  
320 3 ▶ $t->{_response} = sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
321 3 my ($self, $response_body, $sid, $ct, $status) = @_;
322 3 ▶ return $self->{_application}->response_handle(@_) if $self->{_application_has_response_handler};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_application_has_response_handler"} 0 3 3
323 3 ▶ $ct ||= 'application/json';
Condition100%2/2
$ct ||= "application/json"
l!l
12
MC/DC100%1/1
$ct ||= "application/json"
$ct
Truth table100%2/2
A
A= $ct
Aresult
11
00
324 3 my $res = $self->{_delayed_response};
325 3 ▶ return $res if ($res);
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 3 3
326 3 ▶ $res = $self->_build_response($sid, $ct, $status || 200);
Condition50%1/2
$status || 200
l!l
03
MC/DC0%0/1
$status || 200
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
327 3 $res->body($response_body->serialize());
328 3 return $res->finalize;
329 3 ▶ } unless $t->{_response};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"_response"} 3 0 3
330  
331 7 ▶ $t->{_dispatch} = sub {
Subroutine __ANON__: called CC=4 SCAR=14.8
332 7 my ($self, $method, @params) = @_;
333 my @out = $self->{_application_has_dispatcher} ? eval {
334 0 $self->{_application}->dispatch($method, $self, @params)
335 7 ▶ } : eval {
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_application_has_dispatcher'} ? : 0 7 7
336 7 ▶ unless ($self->{_application}->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_application"}->can($method)) 1 6 7
337 1 $self->response->raiseError('Invalid request - ' . $method, 400);
338 1 return;
339 }
340 6 $self->{_application}->$method($self, @params);
341 };
342 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
343 0 $self->response->raiseError(['Error while dispatching the request', $@], 400);
344 0 return;
345 }
346 7 return @out;
347 3 ▶ } unless $t->{_dispatch};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"_dispatch"} 3 0 3
348
349 3 return $t;
350 }
351  
352 42 ▶sub _bless_tree {
Subroutine _bless_tree: called CC=8 SCAR=20.8
353 42 my ($self, $node) = @_;
354 42 my $refnode = ref $node;
355 42 ▶ return unless $refnode eq 'HASH' || $refnode eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $refnode eq "HASH" or $refnode eq "ARRAY" 22 20 42
Condition100%3/3
$refnode eq "HASH" or $refnode eq "ARRAY"
l!l&&r!l&&!r
18222
MC/DC100%2/2
$refnode eq "HASH" or $refnode eq "ARRAY"
$refnode eq "HASH" $refnode eq "ARRAY"
Truth table100%3/3
A or B
A= $refnode eq "HASH"B= $refnode eq "ARRAY"
ABresult
1X1
011
000
356 20 ▶ if ($refnode eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if ($refnode eq "HASH") 18 2 20
357 18 ▶ bless $node, $node->{_inherit} ? ref $self : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
$node->{'_inherit'} ? : 0 18 18
358 18 $self->_bless_tree($node->{$_}) for keys %$node;
359 }
360 20 ▶ if ($refnode eq 'ARRAY'){
Branch: 1 branch
BranchTrueFalseTotal
if ($refnode eq "ARRAY") 2 18 20
361 2 bless $node, ref $self;
362 2 $self->_bless_tree($_) for @$node;
363 }
364 20 $node;
365 }
366  
367 0 ▶sub TO_JSON {
Subroutine TO_JSON: not called CC=9 SCAR=45.0
Pod undocumented
368 0 my $self = shift;
369 0 my $ref = reftype $self;
370 0 ▶ return $self unless $ref && $ref =~ m/ARRAY|HASH/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref and $ref =~ /ARRAY|HASH/ 0 0 0
Condition0%0/3
$ref and $ref =~ /ARRAY|HASH/
!ll&&!rl&&r
000
MC/DC0%0/2
$ref and $ref =~ /ARRAY|HASH/
$ref $ref =~ /ARRAY|HASH/
Truth table0%0/3
A and B
A= $refB= $ref =~ /ARRAY|HASH/
ABresult
0X0
100
111
371 0 ▶ return [@$self] if $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 0 0 0
372 0 ▶ return 'cannot stringify application object' if $self->{_application};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_application"} 0 0 0
373 0 my $output = {};
374 0 my $nodebug = ! $self->{_debug};
375 0 for(keys %$self){
376 0 my $skip;
377 0 ▶ $skip++ if $_ =~ /^_/ && $nodebug;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^_/ and $nodebug 0 0 0
Condition0%0/3
$_ =~ /^_/ and $nodebug
!ll&&!rl&&r
000
MC/DC0%0/2
$_ =~ /^_/ and $nodebug
$_ =~ /^_/ $nodebug
Truth table0%0/3
A and B
A= $_ =~ /^_/B= $nodebug
ABresult
0X0
100
111
378 0 ▶ next if $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $skip 0 0 0
379 0 $output->{$_} = $self->{$_};
380 }
381 0 return $output;
382 }
383  
384 49 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
385 49 my ($self) = @_;
386 49 ▶ (reftype $self eq 'HASH' ? %{$self} : @{$self}) = ();
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($self) eq 'HASH' ? : 47 2 49
387 }
388  
389 179 ▶sub AUTOLOAD : lvalue {
Subroutine AUTOLOAD: called CC=12 SCAR=25.1
390 179 my $classname = ref $_[0];
391 179 my $validname = '[_a-zA-Z][\:a-zA-Z0-9_]*';
392 179 our $AUTOLOAD =~ /^${classname}::($validname)$/;
393 179 my $key = $1;
394 179 ▶ die "illegal key name, must be of $validname form\n$AUTOLOAD" unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 179 179
395 179 ▶ my $miss = Want::want('REF OBJECT') ? {} : '';
Branch: 1 branch
BranchTrueFalseTotal
Want::want('REF OBJECT') ? : 54 125 179
396 179 my $retval = $_[0]->{$key};
397 179 ▶ return $retval->(@_) if (ref $retval eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if ref $retval eq "CODE" 18 161 179
398 161 ▶ die "illegal use of AUTOLOAD $classname -> $key - too many arguments" if (scalar @_ > 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 2 0 161 161
399 161 ▶ my $isBool = Want::want('SCALAR BOOL') && ((reftype($retval) // '') eq 'SCALAR');
Condition100%5/5
Want::want('SCALAR BOOL') && (builtin::reftype($retval) // '') eq 'SCALAR'
!ll&&!rl&&r
132227
builtin::reftype($retval) // ""
l!l
1019
MC/DC100%2/2
Want::want('SCALAR BOOL') && (builtin::reftype($retval) // '') eq 'SCALAR'
Want::want('SCALAR BOOL') builtin::reftype($retval)
Truth table100%3/3
A && B
A= Want::want('SCALAR BOOL')B= builtin::reftype($retval)
ABresult
0X0
100
111
400 161 ▶ return $$retval if $isBool;
Branch: 1 branch
BranchTrueFalseTotal
if $isBool 7 154 161
401 154 ▶ $_[0]->{$key} = $_[1] // $retval // $miss;
Condition66%4/6
($_[1] // $retval) // $miss
l!l&&r!l&&!r
100540
$_[1] // $retval
l!l&&r!l&&!r
010054
MC/DC0%0/3
($_[1] // $retval) // $miss
$_[1] $retval $miss
Truth table50%2/4
A // B // C
A= $_[1]B= $retvalC= $miss
ABCresult
1XX1
01X1
0011
0000
402 154 ▶ $_[0]->_bless_tree($_[0]->{$key}) if ref $_[0]->{$key} eq 'HASH' || ref $_[0]->{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{$key} eq "HASH" or ref $_[0]{$key} eq "ARRAY" 17 137 154
Condition66%2/3
ref $_[0]{$key} eq "HASH" or ref $_[0]{$key} eq "ARRAY"
l!l&&r!l&&!r
170137
MC/DC50%1/2
ref $_[0]{$key} eq "HASH" or ref $_[0]{$key} eq "ARRAY"
ref $_[0]{$key} eq "HASH" ref $_[0]{$key} eq "ARRAY"
Truth table66%2/3
A or B
A= ref $_[0]{$key} eq "HASH"B= ref $_[0]{$key} eq "ARRAY"
ABresult
1X1
011
000
403 154 $_[0]->{$key};
404 }
405  
406 13 1;
407  
408 __END__
« Terse/App.pm ↑ Summary Terse/Plugin.pm »