blib/lib/PAGI/Server/EventValidator.pm

filter: Statementstmt 99.1% 224 / 226 Branchbran 93.0% 240 / 258 Conditioncond 72.2% 120 / 166 MC/DCmcdc 53.1% 51 / 96 Subroutinesub 100% 36 / 36 Podpod 100% 11 / 11 totaltotal 86.0% 682 / 793 CC 192 SCAR 23.1
CC
192
Cov
90%
advance_sse
20.0
advance_http
17.0
advance_websocket
16.1
CRAP 10.1
« blib/lib/PAGI/Server/Protocol/HTTP2.pm ↑ Summary blib/lib/PAGI/Server/AppNormalizer.pm »
1 package PAGI::Server::EventValidator;
2  
3 156 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 156 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 156 our $VERSION = '0.002013';
7  
8 156 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 156 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # --- Primitive shape checks (anchored; undef and refs are always false) ---
12  
13 488 ▶sub _is_nonneg_int {
Subroutine _is_nonneg_int: called CC=3 SCAR=11.0
14 488 my ($v) = @_;
15 488 ▶ return defined $v && !ref $v && $v =~ /\A[0-9]+\z/;
Condition100%6/6
defined $v && !ref($v) && $v =~ /\A[0-9]+\z/
!ll&&!rl&&r
315470
defined $v and not ref $v
!ll&&!rl&&r
12485
MC/DC100%3/3
defined $v && !ref($v) && $v =~ /\A[0-9]+\z/
defined $v not ref $v $v =~ /\A[0-9]+\z/
Truth table100%4/4
A and not(B) && C
A= defined $vB= not ref $vC= $v =~ /\A[0-9]+\z/
ABCresult
0XX0
10X0
1100
1111
16 }
17  
18 408 ▶sub _is_bool01 {
Subroutine _is_bool01: called CC=3 SCAR=12.5
19 408 my ($v) = @_;
20 408 ▶ return defined $v && !ref $v && $v =~ /\A[01]\z/;
Condition50%3/6
defined $v && !ref($v) && $v =~ /\A[01]\z/
!ll&&!rl&&r
07401
defined $v and not ref $v
!ll&&!rl&&r
00408
MC/DC33%1/3
defined $v && !ref($v) && $v =~ /\A[01]\z/
defined $v not ref $v $v =~ /\A[01]\z/
Truth table50%2/4
A and not(B) && C
A= defined $vB= not ref $vC= $v =~ /\A[01]\z/
ABCresult
0XX0
10X0
1100
1111
21 }
22  
23 48 ▶sub _is_nonneg_number {
Subroutine _is_nonneg_number: called CC=3 SCAR=12.5
24 48 my ($v) = @_;
25 48 ▶ return defined $v && !ref $v && $v =~ /\A[0-9]+(?:\.[0-9]+)?\z/;
Condition50%3/6
defined $v && !ref($v) && $v =~ /\A[0-9]+(?:\.[0-9]+)?\z/
!ll&&!rl&&r
0840
defined $v and not ref $v
!ll&&!rl&&r
0048
MC/DC33%1/3
defined $v && !ref($v) && $v =~ /\A[0-9]+(?:\.[0-9]+)?\z/
defined $v not ref $v $v =~ /\A[0-9]+(?:\.[0-9]+)?\z/
Truth table50%2/4
A and not(B) && C
A= defined $vB= not ref $vC= $v =~ /\A[0-9]+(?:\.[0-9]+)?\z/
ABCresult
0XX0
10X0
1100
1111
26 }
27  
28 # --- Shared header validation (byte safety per PAGI Www.pod "Header byte safety") ---
29 # Names: no CR/LF/NUL and no other control characters. Values: no CR/LF/NUL.
30  
31 626 ▶sub check_header_value {
Subroutine check_header_value: called CC=2 SCAR=6.9
Pod documented
32 626 my ($value) = @_;
33 626 ▶ die "Invalid header value: contains CR, LF, or null byte\n"
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[\r\n\0]/ 5 621 626
34 if $value =~ /[\r\n\0]/;
35 621 return $value;
36 }
37  
38 627 ▶sub check_header_name {
Subroutine check_header_name: called CC=3 SCAR=11.0
Pod documented
39 627 my ($name) = @_;
40 627 ▶ die "Invalid header name: contains CR, LF, or null byte\n"
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /[\r\n\0]/ 0 627 627
41 if $name =~ /[\r\n\0]/;
42 627 ▶ die "Invalid header name: contains control characters\n"
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /[[:cntrl:]]/ 1 626 627
43 if $name =~ /[[:cntrl:]]/;
44 626 return $name;
45 }
46  
47 418 ▶sub validate_headers {
Subroutine validate_headers: called CC=9 SCAR=22.0
Pod documented
48 418 my ($headers, $event_type) = @_;
49 418 ▶ croak "$event_type 'headers' must be an array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $headers eq "ARRAY" 2 416 418
50 unless ref $headers eq 'ARRAY';
51 416 for my $h (@$headers) {
52 459 ▶ croak "$event_type headers: each header must be a 2-element array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h eq "ARRAY" and @$h == 2 2 457 459
Condition100%3/3
ref $h eq "ARRAY" and @$h == 2
!ll&&!rl&&r
11457
MC/DC100%2/2
ref $h eq "ARRAY" and @$h == 2
ref $h eq "ARRAY" @$h == 2
Truth table100%3/3
A and B
A= ref $h eq "ARRAY"B= @$h == 2
ABresult
0X0
100
111
53 unless ref $h eq 'ARRAY' && @$h == 2;
54 457 ▶ croak "$event_type headers: header name and value must be defined strings"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $h->[0] && defined $h->[1] or ref $h->[0] or ref $h->[1] 2 455 457
Condition77%7/9
not defined $h->[0] && defined $h->[1] or ref $h->[0] or ref $h->[1]
l!l&&r!l&&!r
20455
not defined $h->[0] && defined $h->[1] or ref $h->[0]
l!l&&r!l&&!r
11455
defined $h->[0] && defined $h->[1]
!ll&&!rl&&r
01456
MC/DC50%2/4
not defined $h->[0] && defined $h->[1] or ref $h->[0] or ref $h->[1]
defined $h->[0] defined $h->[1] ref $h->[0] ref $h->[1]
Truth table60%3/5
not(A && B) or C or D
A= defined $h->[0]B= defined $h->[1]C= ref $h->[0]D= ref $h->[1]
ABCDresult
0XXX1
10XX1
111X1
11011
11000
55 if !defined $h->[0] || !defined $h->[1] || ref $h->[0] || ref $h->[1];
56 455 check_header_name($h->[0]);
57 454 check_header_value($h->[1]);
58 }
59 406 return;
60 }
61  
62 # =============================================================================
63 # PAGI::Server::EventValidator - Mandatory outgoing-event validation
64 #
65 # Per main.mkdn: Servers must raise exceptions if events are missing required
66 # fields, event fields are of the wrong type, the type is unrecognized, or
67 # events arrive out of sequence.
68 #
69 # This module is the shared authority for both event shape and send-sequence
70 # validation. Every send path in PAGI::Server::Connection and the lifespan
71 # send path in PAGI::Server call it unconditionally, in every environment;
72 # there is no way to disable it.
73 # =============================================================================
74  
75 # =============================================================================
76 # HTTP Event Validation
77 # =============================================================================
78  
79 823 ▶sub validate_http_send {
Subroutine validate_http_send: called CC=9 SCAR=22.0
Pod documented
80 823 my ($event, $opts) = @_;
81 823 ▶ my $ext = ($opts && $opts->{extensions}) || {};
Condition80%4/5
$opts && $opts->{'extensions'} || {}
l!l
77647
$opts and $opts->{"extensions"}
!ll&&!rl&&r
470776
MC/DC50%1/2
$opts && $opts->{'extensions'} || {}
$opts $opts->{"extensions"}
Truth table66%2/3
A and B
A= $optsB= $opts->{"extensions"}
ABresult
111
0X0
100
82 823 ▶ my $type = $event->{type} // '';
Condition100%2/2
$event->{"type"} // ""
l!l
8221
MC/DC100%1/1
$event->{"type"} // ""
$event->{"type"}
Truth table100%2/2
A
A= $event->{"type"}
Aresult
11
00
83  
84 823 ▶ if ($type eq 'http.response.start') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'http.response.start') { } 347 476 823
elsif ($type eq 'http.response.body') { } 440 36 476
elsif ($type eq 'http.response.trailers') { } 24 12 36
elsif ($type eq 'http.fullflush') { } 7 5 12
85 347 _validate_http_response_start($event);
86 }
87 elsif ($type eq 'http.response.body') {
88 440 _validate_http_response_body($event);
89 }
90 elsif ($type eq 'http.response.trailers') {
91 24 _validate_http_response_trailers($event);
92 }
93 elsif ($type eq 'http.fullflush') {
94 croak "Extension not enabled: fullflush"
95 7 ▶ unless exists $ext->{fullflush};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ext->{"fullflush"} 3 4 7
96 }
97 else {
98 5 croak "Unrecognized event type '$type' for http protocol";
99 }
100 780 return;
101 }
102  
103 347 ▶sub _validate_http_response_start {
Subroutine _validate_http_response_start: called CC=8 SCAR=20.9
104 347 my ($event) = @_;
105  
106 # status is required (Int)
107 croak "http.response.start requires 'status' field"
108 347 ▶ unless exists $event->{status};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $event->{"status"} 1 346 347
109 croak "http.response.start 'status' must be a non-negative integer"
110 346 ▶ unless _is_nonneg_int($event->{status});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_nonneg_int($event->{"status"}) 14 332 346
111  
112 # headers must be a valid tuple list if present
113 validate_headers($event->{headers}, 'http.response.start')
114 332 ▶ if exists $event->{headers} && defined $event->{headers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $event->{"headers"} and defined $event->{"headers"} 327 5 332
Condition66%2/3
exists $event->{"headers"} and defined $event->{"headers"}
!ll&&!rl&&r
50327
MC/DC50%1/2
exists $event->{"headers"} and defined $event->{"headers"}
exists $event->{"headers"} defined $event->{"headers"}
Truth table66%2/3
A and B
A= exists $event->{"headers"}B= defined $event->{"headers"}
ABresult
0X0
100
111
115  
116 # trailers must be 0 or 1 if present
117 324 ▶ if (exists $event->{trailers} && defined $event->{trailers}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $event->{"trailers"} and defined $event->{"trailers"}) 24 300 324
Condition66%2/3
exists $event->{"trailers"} and defined $event->{"trailers"}
!ll&&!rl&&r
300024
MC/DC50%1/2
exists $event->{"trailers"} and defined $event->{"trailers"}
exists $event->{"trailers"} defined $event->{"trailers"}
Truth table66%2/3
A and B
A= exists $event->{"trailers"}B= defined $event->{"trailers"}
ABresult
0X0
100
111
118 croak "http.response.start 'trailers' must be 0 or 1"
119 24 ▶ unless _is_bool01($event->{trailers});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_bool01($event->{"trailers"}) 1 23 24
120 }
121 }
122  
123 440 ▶sub _validate_http_response_body {
Subroutine _validate_http_response_body: called CC=11 SCAR=24.0
124 440 my ($event) = @_;
125  
126 # Exactly one of body, file, or fh must be present
127 440 my $has_body = exists $event->{body};
128 440 my $has_file = exists $event->{file};
129 440 my $has_fh = exists $event->{fh};
130 440 my $count = $has_body + $has_file + $has_fh;
131  
132 440 ▶ croak "http.response.body requires exactly one of body/file/fh (got $count)"
Branch: 1 branch
BranchTrueFalseTotal
unless $count <= 1 2 438 440
133 unless $count <= 1; # 0 is OK - defaults to empty body
134  
135 # offset must be a non-negative integer if present
136 438 ▶ if (exists $event->{offset} && defined $event->{offset}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $event->{"offset"} and defined $event->{"offset"}) 15 423 438
Condition66%2/3
exists $event->{"offset"} and defined $event->{"offset"}
!ll&&!rl&&r
423015
MC/DC50%1/2
exists $event->{"offset"} and defined $event->{"offset"}
exists $event->{"offset"} defined $event->{"offset"}
Truth table66%2/3
A and B
A= exists $event->{"offset"}B= defined $event->{"offset"}
ABresult
0X0
100
111
137 croak "http.response.body 'offset' must be a non-negative integer"
138 15 ▶ unless _is_nonneg_int($event->{offset});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_nonneg_int($event->{"offset"}) 1 14 15
139 }
140  
141 # length must be a non-negative integer if present
142 437 ▶ if (exists $event->{length} && defined $event->{length}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $event->{"length"} and defined $event->{"length"}) 7 430 437
Condition66%2/3
exists $event->{"length"} and defined $event->{"length"}
!ll&&!rl&&r
43007
MC/DC50%1/2
exists $event->{"length"} and defined $event->{"length"}
exists $event->{"length"} defined $event->{"length"}
Truth table66%2/3
A and B
A= exists $event->{"length"}B= defined $event->{"length"}
ABresult
0X0
100
111
143 croak "http.response.body 'length' must be a non-negative integer"
144 7 ▶ unless _is_nonneg_int($event->{length});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_nonneg_int($event->{"length"}) 1 6 7
145 }
146  
147 # more must be 0 or 1 if present
148 436 ▶ if (exists $event->{more} && defined $event->{more}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $event->{"more"} and defined $event->{"more"}) 351 85 436
Condition66%2/3
exists $event->{"more"} and defined $event->{"more"}
!ll&&!rl&&r
850351
MC/DC50%1/2
exists $event->{"more"} and defined $event->{"more"}
exists $event->{"more"} defined $event->{"more"}
Truth table66%2/3
A and B
A= exists $event->{"more"}B= defined $event->{"more"}
ABresult
0X0
100
111
149 croak "http.response.body 'more' must be 0 or 1"
150 351 ▶ unless _is_bool01($event->{more});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_bool01($event->{"more"}) 6 345 351
151 }
152 }
153  
154 24 ▶sub _validate_http_response_trailers {
Subroutine _validate_http_response_trailers: called CC=3 SCAR=11.1
155 24 my ($event) = @_;
156  
157 # headers must be a valid tuple list if present
158 validate_headers($event->{headers}, 'http.response.trailers')
159 24 ▶ if exists $event->{headers} && defined $event->{headers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $event->{"headers"} and defined $event->{"headers"} 21 3 24
Condition66%2/3
exists $event->{"headers"} and defined $event->{"headers"}
!ll&&!rl&&r
3021
MC/DC50%1/2
exists $event->{"headers"} and defined $event->{"headers"}
exists $event->{"headers"} defined $event->{"headers"}
Truth table66%2/3
A and B
A= exists $event->{"headers"}B= defined $event->{"headers"}
ABresult
0X0
100
111
160 }
161  
162 # =============================================================================
163 # WebSocket Event Validation
164 # =============================================================================
165  
166 174 ▶sub validate_websocket_send {
Subroutine validate_websocket_send: called CC=12 SCAR=24.9
Pod documented
167 174 my ($event, $opts) = @_;
168 174 ▶ my $ext = ($opts && $opts->{extensions}) || {};
Condition80%4/5
$opts && $opts->{'extensions'} || {}
l!l
15816
$opts and $opts->{"extensions"}
!ll&&!rl&&r
160158
MC/DC50%1/2
$opts && $opts->{'extensions'} || {}
$opts $opts->{"extensions"}
Truth table66%2/3
A and B
A= $optsB= $opts->{"extensions"}
ABresult
111
0X0
100
169 174 ▶ my $type = $event->{type} // '';
Condition50%1/2
$event->{"type"} // ""
l!l
1740
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table50%1/2
A
A= $event->{"type"}
Aresult
11
00
170  
171 174 ▶ if ($type eq 'websocket.accept') {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'websocket.accept') { } 71 103 174
elsif ($type eq 'websocket.send') { } 43 60 103
elsif ($type eq 'websocket.close') { } 16 44 60
elsif ($type eq 'websocket.keepalive') { } 19 25 44
elsif ($type eq 'websocket.http.response.start') { } 12 13 25
elsif ($type eq 'websocket.http.response.body') { } 12 1 13
172 71 _validate_websocket_accept($event);
173 }
174 elsif ($type eq 'websocket.send') {
175 43 _validate_websocket_send_event($event);
176 }
177 elsif ($type eq 'websocket.close') {
178 16 _validate_websocket_close($event);
179 }
180 elsif ($type eq 'websocket.keepalive') {
181 19 _validate_websocket_keepalive($event);
182 }
183 elsif ($type eq 'websocket.http.response.start') {
184 croak "Extension not enabled: websocket.http.response"
185 12 ▶ unless exists $ext->{'websocket.http.response'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ext->{"websocket.http.response"} 1 11 12
186 11 _validate_ws_denial_start($event);
187 }
188 elsif ($type eq 'websocket.http.response.body') {
189 croak "Extension not enabled: websocket.http.response"
190 12 ▶ unless exists $ext->{'websocket.http.response'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ext->{"websocket.http.response"} 0 12 12
191 12 _validate_ws_denial_body($event);
192 }
193 else {
194 1 croak "Unrecognized event type '$type' for websocket protocol";
195 }
196 162 return;
197 }
198  
199 71 ▶sub _validate_websocket_accept {
Subroutine _validate_websocket_accept: called CC=3 SCAR=11.1
200 71 my ($event) = @_;
201  
202 # headers must be a valid tuple list if present
203 validate_headers($event->{headers}, 'websocket.accept')
204 71 ▶ if exists $event->{headers} && defined $event->{headers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $event->{"headers"} and defined $event->{"headers"} 3 68 71
Condition66%2/3
exists $event->{"headers"} and defined $event->{"headers"}
!ll&&!rl&&r
6803
MC/DC50%1/2
exists $event->{"headers"} and defined $event->{"headers"}
exists $event->{"headers"} defined $event->{"headers"}
Truth table66%2/3
A and B
A= exists $event->{"headers"}B= defined $event->{"headers"}
ABresult
0X0
100
111
205 }
206  
207 43 ▶sub _validate_websocket_send_event {
Subroutine _validate_websocket_send_event: called CC=4 SCAR=13.9
208 43 my ($event) = @_;
209  
210 # Exactly one of bytes or text must be present and defined
211 43 ▶ my $count = (defined $event->{bytes} ? 1 : 0) + (defined $event->{text} ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
defined $event->{'bytes'} ? : 8 35 43
defined $event->{'text'} ? : 35 8 43
212  
213 43 ▶ croak "websocket.send requires exactly one of bytes/text (got $count)"
Branch: 1 branch
BranchTrueFalseTotal
unless $count == 1 4 39 43
214 unless $count == 1;
215 }
216  
217 16 ▶sub _validate_websocket_close {
Subroutine _validate_websocket_close: called CC=4 SCAR=13.9
218 16 my ($event) = @_;
219  
220 # code must be a non-negative integer if present
221 16 ▶ if (exists $event->{code} && defined $event->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $event->{"code"} and defined $event->{"code"}) 7 9 16
Condition66%2/3
exists $event->{"code"} and defined $event->{"code"}
!ll&&!rl&&r
907
MC/DC50%1/2
exists $event->{"code"} and defined $event->{"code"}
exists $event->{"code"} defined $event->{"code"}
Truth table66%2/3
A and B
A= exists $event->{"code"}B= defined $event->{"code"}
ABresult
0X0
100
111
222 croak "websocket.close 'code' must be a non-negative integer"
223 7 ▶ unless _is_nonneg_int($event->{code});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_nonneg_int($event->{"code"}) 1 6 7
224 }
225 }
226  
227 19 ▶sub _validate_websocket_keepalive {
Subroutine _validate_websocket_keepalive: called CC=6 SCAR=17.9
228 19 my ($event) = @_;
229  
230 # interval is required (Number)
231 croak "websocket.keepalive requires 'interval' field"
232 19 ▶ unless exists $event->{interval};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $event->{"interval"} 1 18 19
233 croak "websocket.keepalive 'interval' must be a non-negative number"
234 18 ▶ unless _is_nonneg_number($event->{interval});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_nonneg_number($event->{"interval"}) 1 17 18
235  
236 # timeout must be a non-negative number if present
237 17 ▶ if (exists $event->{timeout} && defined $event->{timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $event->{"timeout"} and defined $event->{"timeout"}) 7 10 17
Condition66%2/3
exists $event->{"timeout"} and defined $event->{"timeout"}
!ll&&!rl&&r
1007
MC/DC50%1/2
exists $event->{"timeout"} and defined $event->{"timeout"}
exists $event->{"timeout"} defined $event->{"timeout"}
Truth table66%2/3
A and B
A= exists $event->{"timeout"}B= defined $event->{"timeout"}
ABresult
0X0
100
111
238 croak "websocket.keepalive 'timeout' must be a non-negative number"
239 7 ▶ unless _is_nonneg_number($event->{timeout});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_nonneg_number($event->{"timeout"}) 1 6 7
240 }
241 }
242  
243 11 ▶sub _validate_ws_denial_start {
Subroutine _validate_ws_denial_start: called CC=5 SCAR=16.7
244 11 my ($event) = @_;
245  
246 # status is required (Int)
247 croak "websocket.http.response.start requires 'status' field"
248 11 ▶ unless exists $event->{status};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $event->{"status"} 0 11 11
249 croak "websocket.http.response.start 'status' must be a non-negative integer"
250 11 ▶ unless _is_nonneg_int($event->{status});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_nonneg_int($event->{"status"}) 0 11 11
251  
252 # headers must be a valid tuple list if present
253 validate_headers($event->{headers}, 'websocket.http.response.start')
254 11 ▶ if exists $event->{headers} && defined $event->{headers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $event->{"headers"} and defined $event->{"headers"} 10 1 11
Condition66%2/3
exists $event->{"headers"} and defined $event->{"headers"}
!ll&&!rl&&r
1010
MC/DC50%1/2
exists $event->{"headers"} and defined $event->{"headers"}
exists $event->{"headers"} defined $event->{"headers"}
Truth table66%2/3
A and B
A= exists $event->{"headers"}B= defined $event->{"headers"}
ABresult
0X0
100
111
255 }
256  
257 12 ▶sub _validate_ws_denial_body {
Subroutine _validate_ws_denial_body: called CC=4 SCAR=14.2
258 12 my ($event) = @_;
259  
260 # more must be 0 or 1 if present
261 12 ▶ if (exists $event->{more} && defined $event->{more}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $event->{"more"} and defined $event->{"more"}) 6 6 12
Condition66%2/3
exists $event->{"more"} and defined $event->{"more"}
!ll&&!rl&&r
606
MC/DC50%1/2
exists $event->{"more"} and defined $event->{"more"}
exists $event->{"more"} defined $event->{"more"}
Truth table66%2/3
A and B
A= exists $event->{"more"}B= defined $event->{"more"}
ABresult
0X0
100
111
262 croak "websocket.http.response.body 'more' must be 0 or 1"
263 6 ▶ unless _is_bool01($event->{more});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_bool01($event->{"more"}) 0 6 6
264 }
265 }
266  
267 # =============================================================================
268 # SSE Event Validation
269 # =============================================================================
270  
271 286 ▶sub validate_sse_send {
Subroutine validate_sse_send: called CC=13 SCAR=25.7
Pod documented
272 286 my ($event, $opts) = @_;
273 286 ▶ my $ext = ($opts && $opts->{extensions}) || {};
Condition80%4/5
$opts && $opts->{'extensions'} || {}
l!l
25927
$opts and $opts->{"extensions"}
!ll&&!rl&&r
270259
MC/DC50%1/2
$opts && $opts->{'extensions'} || {}
$opts $opts->{"extensions"}
Truth table66%2/3
A and B
A= $optsB= $opts->{"extensions"}
ABresult
111
0X0
100
274 286 ▶ my $type = $event->{type} // '';
Condition50%1/2
$event->{"type"} // ""
l!l
2860
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table50%1/2
A
A= $event->{"type"}
Aresult
11
00
275  
276 286 ▶ if ($type eq 'sse.start') {
Branch: 8 branches
BranchTrueFalseTotal
if ($type eq 'sse.start') { } 72 214 286
elsif ($type eq 'sse.send') { } 106 108 214
elsif ($type eq 'sse.comment') { } 8 100 108
elsif ($type eq 'sse.keepalive') { } 24 76 100
elsif ($type eq 'sse.close') { } 16 60 76
elsif ($type eq 'sse.http.response.start') { } 28 32 60
elsif ($type eq 'sse.http.response.body') { } 27 5 32
elsif ($type eq 'http.fullflush') { } 4 1 5
277 72 _validate_sse_start($event);
278 }
279 elsif ($type eq 'sse.send') {
280 106 _validate_sse_send_event($event);
281 }
282 elsif ($type eq 'sse.comment') {
283 8 _validate_sse_comment($event);
284 }
285 elsif ($type eq 'sse.keepalive') {
286 24 _validate_sse_keepalive($event);
287 }
288 elsif ($type eq 'sse.close') {
289 16 _validate_sse_close($event);
290 }
291 elsif ($type eq 'sse.http.response.start') {
292 28 _validate_sse_decline_start($event);
293 }
294 elsif ($type eq 'sse.http.response.body') {
295 27 _validate_sse_decline_body($event);
296 }
297 elsif ($type eq 'http.fullflush') {
298 croak "Extension not enabled: fullflush"
299 4 ▶ unless exists $ext->{fullflush};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ext->{"fullflush"} 1 3 4
300 }
301 else {
302 1 croak "Unrecognized event type '$type' for sse protocol";
303 }
304 264 return;
305 }
306  
307 28 ▶sub _validate_sse_decline_start {
Subroutine _validate_sse_decline_start: called CC=6 SCAR=22.0
308 28 my ($event) = @_;
309  
310 croak "sse.http.response.start requires 'status' field"
311 28 ▶ unless exists $event->{status} && defined $event->{status};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $event->{"status"} and defined $event->{"status"} 0 28 28
Condition33%1/3
exists $event->{"status"} and defined $event->{"status"}
!ll&&!rl&&r
0028
MC/DC0%0/2
exists $event->{"status"} and defined $event->{"status"}
exists $event->{"status"} defined $event->{"status"}
Truth table33%1/3
A and B
A= exists $event->{"status"}B= defined $event->{"status"}
ABresult
0X0
100
111
312 croak "sse.http.response.start 'status' must be a non-negative integer"
313 28 ▶ unless _is_nonneg_int($event->{status});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_nonneg_int($event->{"status"}) 0 28 28
314 validate_headers($event->{headers}, 'sse.http.response.start')
315 28 ▶ if exists $event->{headers} && defined $event->{headers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $event->{"headers"} and defined $event->{"headers"} 28 0 28
Condition33%1/3
exists $event->{"headers"} and defined $event->{"headers"}
!ll&&!rl&&r
0028
MC/DC0%0/2
exists $event->{"headers"} and defined $event->{"headers"}
exists $event->{"headers"} defined $event->{"headers"}
Truth table33%1/3
A and B
A= exists $event->{"headers"}B= defined $event->{"headers"}
ABresult
0X0
100
111
316 }
317  
318 27 ▶sub _validate_sse_decline_body {
Subroutine _validate_sse_decline_body: called CC=4 SCAR=16.1
319 27 my ($event) = @_;
320  
321 27 ▶ if (exists $event->{more} && defined $event->{more}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $event->{"more"} and defined $event->{"more"}) 27 0 27
Condition33%1/3
exists $event->{"more"} and defined $event->{"more"}
!ll&&!rl&&r
0027
MC/DC0%0/2
exists $event->{"more"} and defined $event->{"more"}
exists $event->{"more"} defined $event->{"more"}
Truth table33%1/3
A and B
A= exists $event->{"more"}B= defined $event->{"more"}
ABresult
0X0
100
111
322 croak "sse.http.response.body 'more' must be 0 or 1"
323 27 ▶ unless _is_bool01($event->{more});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_bool01($event->{"more"}) 0 27 27
324 }
325 }
326  
327 16 ▶sub _validate_sse_close {
Subroutine _validate_sse_close: called CC=4 SCAR=14.2
328 16 my ($event) = @_;
329  
330 # reason is optional and server-side only; if present it must be a string
331 16 ▶ if (exists $event->{reason} && defined $event->{reason}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $event->{"reason"} and defined $event->{"reason"}) 2 14 16
Condition66%2/3
exists $event->{"reason"} and defined $event->{"reason"}
!ll&&!rl&&r
1402
MC/DC50%1/2
exists $event->{"reason"} and defined $event->{"reason"}
exists $event->{"reason"} defined $event->{"reason"}
Truth table66%2/3
A and B
A= exists $event->{"reason"}B= defined $event->{"reason"}
ABresult
0X0
100
111
332 croak "sse.close 'reason' must be a string"
333 2 ▶ if ref $event->{reason};
Branch: 1 branch
BranchTrueFalseTotal
if ref $event->{"reason"} 0 2 2
334 }
335 }
336  
337 72 ▶sub _validate_sse_start {
Subroutine _validate_sse_start: called CC=6 SCAR=18.3
338 72 my ($event) = @_;
339  
340 # status must be a non-negative integer if present
341 72 ▶ if (exists $event->{status} && defined $event->{status}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $event->{"status"} and defined $event->{"status"}) 70 2 72
Condition66%2/3
exists $event->{"status"} and defined $event->{"status"}
!ll&&!rl&&r
2070
MC/DC50%1/2
exists $event->{"status"} and defined $event->{"status"}
exists $event->{"status"} defined $event->{"status"}
Truth table66%2/3
A and B
A= exists $event->{"status"}B= defined $event->{"status"}
ABresult
0X0
100
111
342 croak "sse.start 'status' must be a non-negative integer"
343 70 ▶ unless _is_nonneg_int($event->{status});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_nonneg_int($event->{"status"}) 0 70 70
344 }
345  
346 # headers must be a valid tuple list if present
347 validate_headers($event->{headers}, 'sse.start')
348 72 ▶ if exists $event->{headers} && defined $event->{headers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $event->{"headers"} and defined $event->{"headers"} 29 43 72
Condition66%2/3
exists $event->{"headers"} and defined $event->{"headers"}
!ll&&!rl&&r
43029
MC/DC50%1/2
exists $event->{"headers"} and defined $event->{"headers"}
exists $event->{"headers"} defined $event->{"headers"}
Truth table66%2/3
A and B
A= exists $event->{"headers"}B= defined $event->{"headers"}
ABresult
0X0
100
111
349 }
350  
351 106 ▶sub _validate_sse_send_event {
Subroutine _validate_sse_send_event: called CC=12 SCAR=25.1
352 106 my ($event) = @_;
353  
354 # data is required (String)
355 croak "sse.send requires 'data' field"
356 106 ▶ unless exists $event->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $event->{"data"} 1 105 106
357 croak "sse.send 'data' must be a string"
358 105 ▶ unless defined $event->{data} && !ref $event->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $event->{"data"} and not ref $event->{"data"} 1 104 105
Condition66%2/3
defined $event->{"data"} and not ref $event->{"data"}
!ll&&!rl&&r
01104
MC/DC50%1/2
defined $event->{"data"} and not ref $event->{"data"}
defined $event->{"data"} not ref $event->{"data"}
Truth table66%2/3
A and not(B)
A= defined $event->{"data"}B= not ref $event->{"data"}
ABresult
0X0
100
111
359  
360 104 for my $f (qw(event id)) {
361 205 ▶ next unless exists $event->{$f} && defined $event->{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $event->{$f} and defined $event->{$f} 166 39 205
Condition66%2/3
exists $event->{$f} and defined $event->{$f}
!ll&&!rl&&r
166039
MC/DC50%1/2
exists $event->{$f} and defined $event->{$f}
exists $event->{$f} defined $event->{$f}
Truth table66%2/3
A and B
A= exists $event->{$f}B= defined $event->{$f}
ABresult
0X0
100
111
362 39 ▶ croak "sse.send '$f' must be a string" if ref $event->{$f};
Branch: 1 branch
BranchTrueFalseTotal
if ref $event->{$f} 0 39 39
363 croak "sse.send '$f' must not contain newline characters"
364 39 ▶ if $event->{$f} =~ /[\r\n]/;
Branch: 1 branch
BranchTrueFalseTotal
if $event->{$f} =~ /[\r\n]/ 4 35 39
365 }
366 100 ▶ if (exists $event->{retry} && defined $event->{retry}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $event->{"retry"} and defined $event->{"retry"}) 4 96 100
Condition66%2/3
exists $event->{"retry"} and defined $event->{"retry"}
!ll&&!rl&&r
9604
MC/DC50%1/2
exists $event->{"retry"} and defined $event->{"retry"}
exists $event->{"retry"} defined $event->{"retry"}
Truth table66%2/3
A and B
A= exists $event->{"retry"}B= defined $event->{"retry"}
ABresult
0X0
100
111
367 croak "sse.send 'retry' must be a non-negative integer"
368 4 ▶ unless _is_nonneg_int($event->{retry});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_nonneg_int($event->{"retry"}) 1 3 4
369 }
370 }
371  
372 8 ▶sub _validate_sse_comment {
Subroutine _validate_sse_comment: called CC=4 SCAR=13.9
373 8 my ($event) = @_;
374  
375 # comment is required (String)
376 croak "sse.comment requires 'comment' field"
377 8 ▶ unless exists $event->{comment};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $event->{"comment"} 1 7 8
378 croak "sse.comment 'comment' must be a string"
379 7 ▶ unless defined $event->{comment} && !ref $event->{comment};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $event->{"comment"} and not ref $event->{"comment"} 1 6 7
Condition66%2/3
defined $event->{"comment"} and not ref $event->{"comment"}
!ll&&!rl&&r
016
MC/DC50%1/2
defined $event->{"comment"} and not ref $event->{"comment"}
defined $event->{"comment"} not ref $event->{"comment"}
Truth table66%2/3
A and not(B)
A= defined $event->{"comment"}B= not ref $event->{"comment"}
ABresult
0X0
100
111
380 }
381  
382 24 ▶sub _validate_sse_keepalive {
Subroutine _validate_sse_keepalive: called CC=7 SCAR=19.5
383 24 my ($event) = @_;
384  
385 # interval is required (Number)
386 croak "sse.keepalive requires 'interval' field"
387 24 ▶ unless exists $event->{interval};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $event->{"interval"} 1 23 24
388 croak "sse.keepalive 'interval' must be a non-negative number"
389 23 ▶ unless _is_nonneg_number($event->{interval});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_nonneg_number($event->{"interval"}) 6 17 23
390  
391 # comment is optional (String); when present it must be encodable as
392 # UTF-8 -- validated here, at arm time, so an unencodable comment fails
393 # THIS send's Future instead of dying later inside the keepalive timer
394 # tick (which would escape uncaught and unwind the event loop).
395 17 ▶ if (exists $event->{comment} && defined $event->{comment}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $event->{"comment"} and defined $event->{"comment"}) 14 3 17
Condition66%2/3
exists $event->{"comment"} and defined $event->{"comment"}
!ll&&!rl&&r
3014
MC/DC50%1/2
exists $event->{"comment"} and defined $event->{"comment"}
exists $event->{"comment"} defined $event->{"comment"}
Truth table66%2/3
A and B
A= exists $event->{"comment"}B= defined $event->{"comment"}
ABresult
0X0
100
111
396 # Copy into a lexical before encoding: Encode::encode consumes its
397 # input string in place, and encoding the caller's own $event->{comment}
398 # directly would leave it mutated (or emptied) out from under the
399 # caller after this validation-only check.
400 14 my $comment = $event->{comment};
401 my $ok = !ref($comment)
402 14 ▶ && eval { Encode::encode('UTF-8', $comment, Encode::FB_CROAK); 1 };
Condition100%3/3
!ref($comment) && eval { do { &Encode::encode('UTF-8', $comment, 1); 1 } }
!ll&&!rl&&r
1211
MC/DC100%2/2
!ref($comment) && eval { do { &Encode::encode('UTF-8', $comment, 1); 1 } }
!ref($comment) eval { do { &Encode::encode('UTF-8', $comment, 1); 1 } }
Truth table100%3/3
not(A) && B
A= !ref($comment)B= eval { do { &Encode::encode('UTF-8', $comment, 1); 1 } }
ABresult
0X0
100
111
403 14 ▶ croak "sse.keepalive 'comment' must be a UTF-8-encodable string"
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 3 11 14
404 unless $ok;
405 }
406 }
407  
408 # =============================================================================
409 # Lifespan Event Validation
410 # =============================================================================
411  
412 387 ▶sub validate_lifespan_send {
Subroutine validate_lifespan_send: called CC=9 SCAR=22.3
Pod documented
413 387 my ($event) = @_;
414 387 ▶ my $type = $event->{type} // '';
Condition50%1/2
$event->{"type"} // ""
l!l
3870
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table50%1/2
A
A= $event->{"type"}
Aresult
11
00
415  
416 387 ▶ if ($type eq 'lifespan.startup.complete' or $type eq 'lifespan.shutdown.complete') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'lifespan.startup.complete' or $type eq 'lifespan.shutdown.complete') { } 341 46 387
elsif ($type eq 'lifespan.startup.failed' or $type eq 'lifespan.shutdown.failed') { } 7 39 46
Condition100%3/3
$type eq "lifespan.startup.complete" or $type eq "lifespan.shutdown.complete"
l!l&&r!l&&!r
17516646
MC/DC100%2/2
$type eq "lifespan.startup.complete" or $type eq "lifespan.shutdown.complete"
$type eq "lifespan.startup.complete" $type eq "lifespan.shutdown.complete"
Truth table100%3/3
A or B
A= $type eq "lifespan.startup.complete"B= $type eq "lifespan.shutdown.complete"
ABresult
1X1
011
000
417 # no fields beyond type
418 }
419 ▶ elsif ($type eq 'lifespan.startup.failed' or $type eq 'lifespan.shutdown.failed') {
Condition100%3/3
$type eq "lifespan.startup.failed" or $type eq "lifespan.shutdown.failed"
l!l&&r!l&&!r
6139
MC/DC100%2/2
$type eq "lifespan.startup.failed" or $type eq "lifespan.shutdown.failed"
$type eq "lifespan.startup.failed" $type eq "lifespan.shutdown.failed"
Truth table100%3/3
A or B
A= $type eq "lifespan.startup.failed"B= $type eq "lifespan.shutdown.failed"
ABresult
1X1
011
000
420 7 ▶ if (exists $event->{message} && defined $event->{message}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $event->{"message"} and defined $event->{"message"}) 7 0 7
Condition33%1/3
exists $event->{"message"} and defined $event->{"message"}
!ll&&!rl&&r
007
MC/DC0%0/2
exists $event->{"message"} and defined $event->{"message"}
exists $event->{"message"} defined $event->{"message"}
Truth table33%1/3
A and B
A= exists $event->{"message"}B= defined $event->{"message"}
ABresult
0X0
100
111
421 7 ▶ croak "$type 'message' must be a string" if ref $event->{message};
Branch: 1 branch
BranchTrueFalseTotal
if ref $event->{"message"} 2 5 7
422 }
423 }
424 else {
425 39 croak "Unrecognized event type '$type' for lifespan protocol";
426 }
427 346 return;
428 }
429  
430 # =============================================================================
431 # Sequence State Machines
432 #
433 # Each advance_* function is a pure transition function: given the current
434 # per-connection sequence state and a (shape-already-validated) event, it
435 # returns the next state or croaks describing the send-order violation.
436 # Dispatchers run shape validation first and call these separately.
437 # =============================================================================
438  
439 864 ▶sub _http_body_is_opaque {
Subroutine _http_body_is_opaque: called CC=2 SCAR=6.9
440 864 my ($event) = @_;
441 864 ▶ return defined $event->{file} || defined $event->{fh};
Condition100%3/3
defined $event->{'file'} || defined $event->{'fh'}
l!l&&r!l&&!r
5843763
MC/DC100%2/2
defined $event->{'file'} || defined $event->{'fh'}
defined $event->{'file'} defined $event->{'fh'}
Truth table100%3/3
A || B
A= defined $event->{'file'}B= defined $event->{'fh'}
ABresult
1X1
011
000
442 }
443  
444 429 ▶sub _http_body_is_terminal {
Subroutine _http_body_is_terminal: called CC=3 SCAR=11.0
445 429 my ($event) = @_;
446 429 ▶ return 1 if _http_body_is_opaque($event);
Branch: 1 branch
BranchTrueFalseTotal
if _http_body_is_opaque($event) 48 381 429
447 381 ▶ my $more = $event->{more} // 0;
Condition100%2/2
$event->{"more"} // 0
l!l
34932
MC/DC100%1/1
$event->{"more"} // 0
$event->{"more"}
Truth table100%2/2
A
A= $event->{"more"}
Aresult
11
00
448 381 return !$more;
449 }
450  
451 798 ▶sub advance_http {
Subroutine advance_http: called CC=17 SCAR=28.3
Pod documented
452 798 my ($state, $event) = @_;
453 798 ▶ my $type = $event->{type} // '';
Condition50%1/2
$event->{"type"} // ""
l!l
7980
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table50%1/2
A
A= $event->{"type"}
Aresult
11
00
454  
455 798 ▶ croak "cannot send '$type': response already complete"
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "complete" 5 793 798
456 if $state eq 'complete';
457  
458 793 ▶ if ($state eq 'initial') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "initial") 323 470 793
459 323 ▶ croak "cannot send '$type' before http.response.start"
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "http.response.start" 5 318 323
460 unless $type eq 'http.response.start';
461 318 ▶ return $event->{trailers} ? 'started_t' : 'started';
Branch: 1 branch
BranchTrueFalseTotal
$event->{'trailers'} ? : 24 294 318
462 }
463  
464 470 ▶ croak "cannot send duplicate http.response.start"
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "http.response.start" 4 466 470
465 if $type eq 'http.response.start';
466  
467 466 ▶ if ($type eq 'http.response.trailers') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "http.response.trailers") 24 442 466
468 24 ▶ croak "cannot send http.response.trailers: trailers were not declared or body is not complete"
Branch: 1 branch
BranchTrueFalseTotal
unless $state eq "awaiting_trailers" 4 20 24
469 unless $state eq 'awaiting_trailers';
470 20 return 'complete';
471 }
472  
473 442 ▶ return $state if $type eq 'http.fullflush';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "http.fullflush" 7 435 442
474  
475 435 ▶ if ($type eq 'http.response.body') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "http.response.body") 435 0 435
476 # A response's body is either a sequence of inline events or a single
477 # file/fh event, never both (PAGI::Spec::Www, "Payload kinds do not
478 # mix within a response"). The '_i' suffix records that inline bytes
479 # were delivered; once set, a delegated payload can no longer join
480 # them. An intermediary that transforms body bytes commits to an
481 # encoding before it could know a file event was coming, and cannot
482 # apply it to bytes the server streams on the application's behalf.
483 435 ▶ croak "cannot send a file/fh body after inline body bytes"
Branch: 1 branch
BranchTrueFalseTotal
if _http_body_is_opaque($event) and $state =~ /_i\z/ 5 430 435
Condition100%3/3
_http_body_is_opaque($event) and $state =~ /_i\z/
!ll&&!rl&&r
382485
MC/DC100%2/2
_http_body_is_opaque($event) and $state =~ /_i\z/
_http_body_is_opaque($event) $state =~ /_i\z/
Truth table100%3/3
A and B
A= _http_body_is_opaque($event)B= $state =~ /_i\z/
ABresult
0X0
100
111
484 if _http_body_is_opaque($event) && $state =~ /_i\z/;
485  
486 430 ▶ if ($state =~ /\Astarted(_t)?(?:_i)?\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state =~ /\Astarted(_t)?(?:_i)?\z/) 429 1 430
487 429 ▶ my $trailers_declared = $1 ? '_t' : '';
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 29 400 429
488 429 ▶ return _http_body_is_terminal($event)
Branch: 2 branches
BranchTrueFalseTotal
_http_body_is_terminal($event) ? : 319 110 429
$trailers_declared ? : 25 294 319
489 ? ($trailers_declared ? 'awaiting_trailers' : 'complete')
490 : "started${trailers_declared}_i";
491 }
492 }
493  
494 # awaiting_trailers state only accepts trailers/fullflush (handled above);
495 # anything else here means a body chunk arrived after completion was
496 # already declared but before the required trailers were sent.
497 1 croak "cannot send '$type' during http response phase '$state'";
498 }
499  
500 173 ▶sub advance_websocket {
Subroutine advance_websocket: called CC=16 SCAR=27.8
Pod documented
501 173 my ($state, $event) = @_;
502 173 ▶ my $type = $event->{type} // '';
Condition50%1/2
$event->{"type"} // ""
l!l
1730
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table50%1/2
A
A= $event->{"type"}
Aresult
11
00
503  
504 173 ▶ croak "cannot send '$type' after websocket.close"
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "closed" 4 169 173
505 if $state eq 'closed';
506 169 ▶ croak "cannot send '$type': denial response already complete"
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "denial_complete" 3 166 169
507 if $state eq 'denial_complete';
508  
509 166 ▶ if ($state eq 'connecting') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "connecting") 89 77 166
510 89 ▶ return 'accepted' if $type eq 'websocket.accept';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "websocket.accept" 71 18 89
511 18 ▶ return 'closed' if $type eq 'websocket.close';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "websocket.close" 5 13 18
512 13 ▶ return 'denial' if $type eq 'websocket.http.response.start';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "websocket.http.response.start" 10 3 13
513 3 croak "cannot send '$type' before websocket.accept";
514 }
515  
516 77 ▶ if ($state eq 'accepted') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "accepted") 62 15 77
517 62 ▶ return 'accepted' if $type eq 'websocket.send' || $type eq 'websocket.keepalive';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "websocket.send" or $type eq "websocket.keepalive" 49 13 62
Condition100%3/3
$type eq "websocket.send" or $type eq "websocket.keepalive"
l!l&&r!l&&!r
361313
MC/DC100%2/2
$type eq "websocket.send" or $type eq "websocket.keepalive"
$type eq "websocket.send" $type eq "websocket.keepalive"
Truth table100%3/3
A or B
A= $type eq "websocket.send"B= $type eq "websocket.keepalive"
ABresult
1X1
011
000
518 13 ▶ return 'closed' if $type eq 'websocket.close';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "websocket.close" 10 3 13
519 3 croak "cannot send '$type' after websocket.accept";
520 }
521  
522 15 ▶ if ($state eq 'denial') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "denial") 15 0 15
523 15 ▶ if ($type eq 'websocket.http.response.body') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "websocket.http.response.body") 12 3 15
524 12 ▶ my $more = $event->{more} // 0;
Condition100%2/2
$event->{"more"} // 0
l!l
57
MC/DC100%1/1
$event->{"more"} // 0
$event->{"more"}
Truth table100%2/2
A
A= $event->{"more"}
Aresult
11
00
525 12 ▶ return $more ? 'denial' : 'denial_complete';
Branch: 1 branch
BranchTrueFalseTotal
$more ? : 2 10 12
526 }
527 3 croak "cannot send '$type' after websocket.http.response.start";
528 }
529  
530 0 croak "cannot send '$type' in websocket state '$state'";
531 }
532  
533 273 ▶sub advance_sse {
Subroutine advance_sse: called CC=20 SCAR=30.0
Pod documented
534 273 my ($state, $event) = @_;
535 273 ▶ my $type = $event->{type} // '';
Condition50%1/2
$event->{"type"} // ""
l!l
2730
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table50%1/2
A
A= $event->{"type"}
Aresult
11
00
536  
537 273 ▶ if ($state eq 'closed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "closed") 7 266 273
538 7 ▶ return 'closed' if $type eq 'sse.close';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "sse.close" 1 6 7
539 6 croak "cannot send '$type' after sse.close";
540 }
541 266 ▶ croak "cannot send '$type': decline response already complete"
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "decline_complete" 3 263 266
542 if $state eq 'decline_complete';
543  
544 263 ▶ if ($state eq 'initial') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "initial") 102 161 263
545 102 ▶ return 'streaming' if $type eq 'sse.start';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "sse.start" 73 29 102
546 29 ▶ return 'declining' if $type eq 'sse.http.response.start';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "sse.http.response.start" 26 3 29
547 3 croak "cannot send '$type' before sse.start";
548 }
549  
550 161 ▶ if ($state eq 'streaming') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "streaming") 131 30 161
551 131 ▶ return 'streaming' if $type eq 'sse.send' || $type eq 'sse.comment' || $type eq 'sse.keepalive' || $type eq 'http.fullflush';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "sse.send" or $type eq "sse.comment" or $type eq "sse.keepalive" or $type eq "http.fullflush" 109 22 131
Condition100%9/9
$type eq "sse.send" or $type eq "sse.comment" or $type eq "sse.keepalive" or $type eq "http.fullflush"
l!l&&r!l&&!r
106322
$type eq "sse.send" or $type eq "sse.comment" or $type eq "sse.keepalive"
l!l&&r!l&&!r
951125
$type eq "sse.send" or $type eq "sse.comment"
l!l&&r!l&&!r
89636
MC/DC100%4/4
$type eq "sse.send" or $type eq "sse.comment" or $type eq "sse.keepalive" or $type eq "http.fullflush"
$type eq "sse.send" $type eq "sse.comment" $type eq "sse.keepalive" $type eq "http.fullflush"
Truth table100%5/5
A or B or C or D
A= $type eq "sse.send"B= $type eq "sse.comment"C= $type eq "sse.keepalive"D= $type eq "http.fullflush"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
552 22 ▶ return 'closed' if $type eq 'sse.close';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "sse.close" 17 5 22
553 5 ▶ croak "cannot decline with sse.http.response.start after sse.start"
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "sse.http.response.start" 3 2 5
554 if $type eq 'sse.http.response.start';
555 2 ▶ croak "cannot send duplicate sse.start"
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "sse.start" 1 1 2
556 if $type eq 'sse.start';
557 1 croak "cannot send '$type' after sse.start";
558 }
559  
560 30 ▶ if ($state eq 'declining') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "declining") 30 0 30
561 30 ▶ if ($type eq 'sse.http.response.body') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "sse.http.response.body") 27 3 30
562 27 ▶ my $more = $event->{more} // 0;
Condition100%2/2
$event->{"more"} // 0
l!l
261
MC/DC100%1/1
$event->{"more"} // 0
$event->{"more"}
Truth table100%2/2
A
A= $event->{"more"}
Aresult
11
00
563 27 ▶ return $more ? 'declining' : 'decline_complete';
Branch: 1 branch
BranchTrueFalseTotal
$more ? : 2 25 27
564 }
565 3 croak "cannot send '$type' after sse.http.response.start";
566 }
567  
568 0 croak "cannot send '$type' in sse state '$state'";
569 }
570  
571 351 ▶sub advance_lifespan {
Subroutine advance_lifespan: called CC=8 SCAR=21.0
Pod documented
572 351 my ($state, $event) = @_;
573 351 ▶ my $type = $event->{type} // '';
Condition50%1/2
$event->{"type"} // ""
l!l
3510
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table50%1/2
A
A= $event->{"type"}
Aresult
11
00
574  
575 351 ▶ if ($state eq 'startup_pending') {
Branch: 2 branches
BranchTrueFalseTotal
if ($state eq 'startup_pending') { } 181 170 351
elsif ($state eq 'shutdown_pending') { } 167 3 170
576 181 ▶ return 'running' if $type eq 'lifespan.startup.complete';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "lifespan.startup.complete" 174 7 181
577 7 ▶ return 'finished' if $type eq 'lifespan.startup.failed';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "lifespan.startup.failed" 5 2 7
578 }
579 elsif ($state eq 'shutdown_pending') {
580 167 ▶ return 'finished' if $type eq 'lifespan.shutdown.complete' || $type eq 'lifespan.shutdown.failed';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "lifespan.shutdown.complete" or $type eq "lifespan.shutdown.failed" 167 0 167
Condition66%2/3
$type eq "lifespan.shutdown.complete" or $type eq "lifespan.shutdown.failed"
l!l&&r!l&&!r
16610
MC/DC0%0/2
$type eq "lifespan.shutdown.complete" or $type eq "lifespan.shutdown.failed"
$type eq "lifespan.shutdown.complete" $type eq "lifespan.shutdown.failed"
Truth table66%2/3
A or B
A= $type eq "lifespan.shutdown.complete"B= $type eq "lifespan.shutdown.failed"
ABresult
1X1
011
000
581 }
582  
583 5 croak "cannot send '$type' during lifespan phase '$state'";
584 }
585  
586 156 1;
587  
588 __END__
« blib/lib/PAGI/Server/Protocol/HTTP2.pm ↑ Summary blib/lib/PAGI/Server/AppNormalizer.pm »