HTTP/_WebSocketH2Body.pm

filter: Statementstmt 93.7% 90 / 96 Branchbran 70.4% 31 / 44 Conditioncond 52.3% 22 / 42 MC/DCmcdc 25.0% 6 / 24 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 7 totaltotal 71.3% 162 / 227 CC 37 SCAR 22.0
CC
37
Cov
79%
_flush
16.8
unblock_send
10.7
new
7.3
CRAP 9.1
« 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 $self->{websocket} = $ws;
40 0 weaken($self->{websocket});
41 0 return $self;
42 }
43  
44 # Whole-buffer acceptance. Return false after acceptance to backpressure
45 # the WebSocket producer. A single frame may exceed the high-water mark.
46 6 ▶sub unblock_send {
Subroutine unblock_send: called CC=8 SCAR=23.7
Pod undocumented
47 6 my ($self, $bytes) = @_;
48 croak 'H2 WebSocket body is no longer writable'
49 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
50 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
51 if !defined($bytes) || ref($bytes);
52 6 ▶ if (length $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bytes) 6 0 6
53 6 push @{ $self->{queue} }, "$bytes";
54 6 $self->{queued} += length $bytes;
55 6 $self->_schedule;
56 }
57 6 ▶ return $self->{queued} < 65_536 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'queued'} < 65536 ? : 5 1 6
58 }
59  
60 1 ▶sub unblock_finish {
Subroutine unblock_finish: called CC=3 SCAR=11.8
Pod undocumented
61 1 my ($self) = @_;
62 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
63 1 $self->{ending} = 1;
64 1 $self->_schedule;
65 1 return;
66 }
67  
68 2 ▶sub unblock_abort {
Subroutine unblock_abort: called CC=4 SCAR=14.0
Pod undocumented
69 2 my ($self, $reason) = @_;
70 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
71 1 ▶ $self->{aborted} = defined($reason) ? "$reason" : 'WebSocket failed';
Branch: 1 branch
BranchTrueFalseTotal
defined $reason ? : 1 0 1
72 1 $self->{queue} = [];
73 1 $self->{queued} = 0;
74 1 $self->_schedule;
75 1 return;
76 }
77  
78 2 ▶sub resume {
Subroutine resume: called CC=2 SCAR=7.0
Pod undocumented
79 2 my ($self) = @_;
80 2 ▶ return if $self->{detached};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"detached"} 0 2 2
81 2 $self->{waiting} = 0;
82 2 $self->_schedule;
83 2 return;
84 }
85  
86 2 ▶sub detach {
Subroutine detach: called CC=1 SCAR=0.0
Pod undocumented
87 2 my ($self) = @_;
88 2 $self->{detached} = 1;
89 2 $self->{queue} = [];
90 2 $self->{queued} = 0;
91 2 delete $self->{transaction};
92 2 delete $self->{websocket};
93 2 return;
94 }
95  
96 10 ▶sub _schedule {
Subroutine _schedule: called CC=4 SCAR=14.2
97 10 my ($self) = @_;
98 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
99 7 $self->{scheduled} = 1;
100 7 my $ok = eval {
101 7 ▶ $self->{defer}->(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
102 7 $self->{scheduled} = 0;
103 7 $self->_flush;
104 7 });
105 7 1;
106 };
107 7 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 7 7
108 0 $self->{scheduled} = 0;
109 0 die $@;
110 }
111 7 return;
112 }
113  
114 7 ▶sub _flush {
Subroutine _flush: called CC=16 SCAR=28.2
115 7 my ($self) = @_;
116 7 ▶ return if $self->{detached};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"detached"} 0 7 7
117 7 my $tx = $self->{transaction};
118 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
119  
120 7 ▶ if (defined($self->{aborted})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"aborted"}) 1 6 7
121 1 $tx->cancel;
122 1 return;
123 }
124 6 ▶ return if $self->{waiting};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"waiting"} 1 5 6
125  
126 5 ▶ while (@{ $self->{queue} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"queue"};}) 5 4 9
127 5 my $bytes = shift @{ $self->{queue} };
128 5 $self->{queued} -= length $bytes;
129 # A false write return means the full buffer was accepted into H2
130 # but the producer must wait for on_drain before further writes.
131 5 my $ready = $tx->write($bytes);
132 5 ▶ unless ($ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ready) 1 4 5
133 1 $self->{waiting} = 1;
134 1 last;
135 }
136 }
137  
138 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
139 && !$self->{ended}) {
140 1 $self->{ended} = 1;
141 1 $tx->end;
142 }
143  
144 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
145 4 my $ws = $self->{websocket};
146 4 ▶ $ws->resume_output if $ws && !$ws->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $ws and not $ws->is_closed 0 4 4
Condition33%1/3
$ws and not $ws->is_closed
!ll&&!rl&&r
400
MC/DC0%0/2
$ws and not $ws->is_closed
$ws not $ws->is_closed
Truth table33%1/3
A and not(B)
A= $wsB= not $ws->is_closed
ABresult
0X0
100
111
147 }
148 5 return;
149 }
150  
151 1 1;
152  
153 __END__
« HTTP/QUIC.pm ↑ Summary HTTP/_Session.pm »