HTTP/_WebSocketH2Body.pm

filter: Statementstmt 91.8% 90 / 98 Branchbran 70.4% 31 / 44 Conditioncond 48.8% 22 / 45 MC/DCmcdc 24.0% 6 / 25 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 8 totaltotal 68.9% 162 / 235 CC 38 SCAR 22.8
CC
38
Cov
76%
_flush
18.9
unblock_send
10.7
new
7.3
CRAP 9.8
« HTTP/QUIC.pm ↑ Summary HTTP/_Session.pm »
1 package Unblock::HTTP::_WebSocketH2Body;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Stream-specific adapter for HTTP2/HTTP3. WebSocket output is accepted
9 # into a bounded producer queue and flushed only after protocol callbacks
10 # unwind. The framework must schedule defer() on a later loop turn.
11 4 ▶sub new {
Subroutine new: called CC=6 SCAR=19.9
Pod undocumented
12 4 my ($class, %option) = @_;
13 4 my $tx = delete $option{transaction};
14 4 my $defer = delete $option{defer};
15 4 ▶ croak 'H2 WebSocket bridge requires a streaming Transaction'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($tx) and $tx->can("write") and $tx->can("end") 0 4 4
Condition33%2/6
builtin::blessed($tx) and $tx->can("write") and $tx->can("end")
!ll&&!rl&&r
004
builtin::blessed($tx) and $tx->can("write")
!ll&&!rl&&r
004
MC/DC0%0/3
builtin::blessed($tx) and $tx->can("write") and $tx->can("end")
builtin::blessed($tx) $tx->can("write") $tx->can("end")
Truth table25%1/4
A and B and C
A= builtin::blessed($tx)B= $tx->can("write")C= $tx->can("end")
ABCresult
0XX0
10X0
1100
1111
16 unless blessed($tx) && $tx->can('write') && $tx->can('end');
17 4 ▶ croak 'H2 WebSocket bridge requires a defer callback'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $defer eq "CODE" 0 4 4
18 unless ref($defer) eq 'CODE';
19 4 ▶ croak 'H2 WebSocket bridge unknown options'
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 4 4
20 if %option;
21 4 my $self = bless {
22 transaction => $tx,
23 defer => $defer,
24 queue => [],
25 queued => 0,
26 scheduled => 0,
27 waiting => 0,
28 ending => 0,
29 ended => 0,
30 aborted => undef,
31 detached => 0,
32 }, $class;
33 4 weaken($self->{transaction});
34 4 return $self;
35 }
36  
37 0 ▶sub websocket {
Subroutine websocket: not called CC=1 SCAR=6.9
Pod undocumented
38 0 my ($self, $ws) = @_;
39 0 return $self->consumer($ws);
40 }
41  
42 0 ▶sub consumer {
Subroutine consumer: not called CC=1 SCAR=6.9
Pod undocumented
43 0 my ($self, $consumer) = @_;
44 0 $self->{websocket} = $consumer;
45 0 weaken($self->{websocket});
46 0 return $self;
47 }
48  
49 # Whole-buffer acceptance. Return false after acceptance to backpressure
50 # the WebSocket producer. A single frame may exceed the high-water mark.
51 6 ▶sub unblock_send {
Subroutine unblock_send: called CC=8 SCAR=23.7
Pod undocumented
52 6 my ($self, $bytes) = @_;
53 croak 'H2 WebSocket body is no longer writable'
54 6 ▶ if $self->{detached} || $self->{ending} || defined($self->{aborted});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"detached"} or $self->{"ending"} or defined $self->{"aborted"} 0 6 6
Condition33%2/6
$self->{"detached"} or $self->{"ending"} or defined $self->{"aborted"}
l!l&&r!l&&!r
006
$self->{"detached"} or $self->{"ending"}
l!l&&r!l&&!r
006
MC/DC0%0/3
$self->{"detached"} or $self->{"ending"} or defined $self->{"aborted"}
$self->{"detached"} $self->{"ending"} defined $self->{"aborted"}
Truth table25%1/4
A or B or C
A= $self->{"detached"}B= $self->{"ending"}C= defined $self->{"aborted"}
ABCresult
1XX1
01X1
0011
0000
55 6 ▶ croak 'H2 WebSocket body requires byte strings'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bytes or ref $bytes 0 6 6
Condition33%1/3
not defined $bytes or ref $bytes
l!l&&r!l&&!r
006
MC/DC0%0/2
not defined $bytes or ref $bytes
not defined $bytes ref $bytes
Truth table33%1/3
not(A) or B
A= not defined $bytesB= ref $bytes
ABresult
1X1
011
000
56 if !defined($bytes) || ref($bytes);
57 6 ▶ if (length $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bytes) 6 0 6
58 6 push @{ $self->{queue} }, "$bytes";
59 6 $self->{queued} += length $bytes;
60 6 $self->_schedule;
61 }
62 6 ▶ return $self->{queued} < 65_536 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'queued'} < 65536 ? : 5 1 6
63 }
64  
65 1 ▶sub unblock_finish {
Subroutine unblock_finish: called CC=3 SCAR=11.8
Pod undocumented
66 1 my ($self) = @_;
67 1 ▶ return if $self->{detached} || $self->{ending};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"detached"} or $self->{"ending"} 0 1 1
Condition33%1/3
$self->{"detached"} or $self->{"ending"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"detached"} or $self->{"ending"}
$self->{"detached"} $self->{"ending"}
Truth table33%1/3
A or B
A= $self->{"detached"}B= $self->{"ending"}
ABresult
1X1
011
000
68 1 $self->{ending} = 1;
69 1 $self->_schedule;
70 1 return;
71 }
72  
73 2 ▶sub unblock_abort {
Subroutine unblock_abort: called CC=4 SCAR=14.0
Pod undocumented
74 2 my ($self, $reason) = @_;
75 2 ▶ return if $self->{detached} || defined($self->{aborted});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"detached"} or defined $self->{"aborted"} 1 1 2
Condition66%2/3
$self->{"detached"} or defined $self->{"aborted"}
l!l&&r!l&&!r
101
MC/DC50%1/2
$self->{"detached"} or defined $self->{"aborted"}
$self->{"detached"} defined $self->{"aborted"}
Truth table66%2/3
A or B
A= $self->{"detached"}B= defined $self->{"aborted"}
ABresult
1X1
011
000
76 1 ▶ $self->{aborted} = defined($reason) ? "$reason" : 'WebSocket failed';
Branch: 1 branch
BranchTrueFalseTotal
defined $reason ? : 1 0 1
77 1 $self->{queue} = [];
78 1 $self->{queued} = 0;
79 1 $self->_schedule;
80 1 return;
81 }
82  
83 2 ▶sub resume {
Subroutine resume: called CC=2 SCAR=7.0
Pod undocumented
84 2 my ($self) = @_;
85 2 ▶ return if $self->{detached};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"detached"} 0 2 2
86 2 $self->{waiting} = 0;
87 2 $self->_schedule;
88 2 return;
89 }
90  
91 2 ▶sub detach {
Subroutine detach: called CC=1 SCAR=0.0
Pod undocumented
92 2 my ($self) = @_;
93 2 $self->{detached} = 1;
94 2 $self->{queue} = [];
95 2 $self->{queued} = 0;
96 2 delete $self->{transaction};
97 2 delete $self->{websocket};
98 2 return;
99 }
100  
101 10 ▶sub _schedule {
Subroutine _schedule: called CC=4 SCAR=14.2
102 10 my ($self) = @_;
103 10 ▶ return if $self->{detached} || $self->{scheduled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"detached"} or $self->{"scheduled"} 3 7 10
Condition66%2/3
$self->{"detached"} or $self->{"scheduled"}
l!l&&r!l&&!r
037
MC/DC50%1/2
$self->{"detached"} or $self->{"scheduled"}
$self->{"detached"} $self->{"scheduled"}
Truth table66%2/3
A or B
A= $self->{"detached"}B= $self->{"scheduled"}
ABresult
1X1
011
000
104 7 $self->{scheduled} = 1;
105 7 my $ok = eval {
106 7 ▶ $self->{defer}->(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
107 7 $self->{scheduled} = 0;
108 7 $self->_flush;
109 7 });
110 7 1;
111 };
112 7 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 7 7
113 0 $self->{scheduled} = 0;
114 0 die $@;
115 }
116 7 return;
117 }
118  
119 7 ▶sub _flush {
Subroutine _flush: called CC=17 SCAR=29.4
120 7 my ($self) = @_;
121 7 ▶ return if $self->{detached};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"detached"} 0 7 7
122 7 my $tx = $self->{transaction};
123 7 ▶ return $self->detach unless $tx && !$tx->is_terminal;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx and not $tx->is_terminal 0 7 7
Condition33%1/3
$tx and not $tx->is_terminal
!ll&&!rl&&r
007
MC/DC0%0/2
$tx and not $tx->is_terminal
$tx not $tx->is_terminal
Truth table33%1/3
A and not(B)
A= $txB= not $tx->is_terminal
ABresult
0X0
100
111
124  
125 7 ▶ if (defined($self->{aborted})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"aborted"}) 1 6 7
126 1 $tx->cancel;
127 1 return;
128 }
129 6 ▶ return if $self->{waiting};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"waiting"} 1 5 6
130  
131 5 ▶ while (@{ $self->{queue} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"queue"};}) 5 4 9
132 5 my $bytes = shift @{ $self->{queue} };
133 5 $self->{queued} -= length $bytes;
134 # A false write return means the full buffer was accepted into H2
135 # but the producer must wait for on_drain before further writes.
136 5 my $ready = $tx->write($bytes);
137 5 ▶ unless ($ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ready) 1 4 5
138 1 $self->{waiting} = 1;
139 1 last;
140 }
141 }
142  
143 5 ▶ if (!$self->{waiting} && !@{ $self->{queue} } && $self->{ending}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"waiting"} || @{$self->{"queue"};} and $self->{"ending"} and not $self->{"ended"}) 1 4 5
Condition88%8/9
not $self->{"waiting"} || @{$self->{"queue"};} and $self->{"ending"} and not $self->{"ended"}
!ll&&!rl&&r
311
not $self->{"waiting"} || @{$self->{"queue"};} and $self->{"ending"}
!ll&&!rl&&r
122
$self->{'waiting'} || @{$self->{'queue'};}
l!l&&r!l&&!r
104
MC/DC75%3/4
not $self->{"waiting"} || @{$self->{"queue"};} and $self->{"ending"} and not $self->{"ended"}
$self->{'waiting'} @{$self->{'queue'};} $self->{"ending"} not $self->{"ended"}
Truth table80%4/5
not(A || B) and C and not(D)
A= $self->{'waiting'}B= @{$self->{'queue'};}C= $self->{"ending"}D= not $self->{"ended"}
ABCDresult
1XXX0
01XX0
000X0
00100
00111
144 && !$self->{ended}) {
145 1 $self->{ended} = 1;
146 1 $tx->end;
147 }
148  
149 5 ▶ if (!$self->{waiting} && $self->{queued} < 32_768) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"waiting"} and $self->{"queued"} < 32768) 4 1 5
Condition66%2/3
not $self->{"waiting"} and $self->{"queued"} < 32768
!ll&&!rl&&r
104
MC/DC50%1/2
not $self->{"waiting"} and $self->{"queued"} < 32768
not $self->{"waiting"} $self->{"queued"} < 32768
Truth table66%2/3
not(A) and B
A= not $self->{"waiting"}B= $self->{"queued"} < 32768
ABresult
0X0
100
111
150 4 my $ws = $self->{websocket};
151 4 ▶ $ws->resume_output if $ws
Branch: 1 branch
BranchTrueFalseTotal
if $ws and not $ws->can("is_closed") && $ws->is_closed 0 4 4
Condition16%1/6
$ws and not $ws->can("is_closed") && $ws->is_closed
!ll&&!rl&&r
400
$ws->can('is_closed') && $ws->is_closed
!ll&&!rl&&r
000
MC/DC0%0/3
$ws and not $ws->can("is_closed") && $ws->is_closed
$ws $ws->can('is_closed') $ws->is_closed
Truth table25%1/4
A and not(B && C)
A= $wsB= $ws->can('is_closed')C= $ws->is_closed
ABCresult
0XX0
1110
10X1
1101
152 && (!$ws->can('is_closed') || !$ws->is_closed);
153 }
154 5 return;
155 }
156  
157 1 1;
158  
159 __END__
« HTTP/QUIC.pm ↑ Summary HTTP/_Session.pm »