blib/lib/Unblock/HTTP3/Body/Reader.pm

filter: Statementstmt 91.2% 146 / 160 Branchbran 62.2% 61 / 98 Conditioncond 42.4% 14 / 33 MC/DCmcdc 13.6% 3 / 22 Subroutinesub 94.7% 18 / 19 Podpod 100% 4 / 4 totaltotal 73.2% 246 / 336 CC 63 SCAR 25.9
CC
63
Cov
76%
_configure
31.5
_maybe_finish
21.1
_new
10.5
CRAP 13.4
« blib/lib/Unblock/HTTP3/Extension/Stream.pm ↑ Summary blib/lib/Unblock/HTTP3/Capsule/Parser.pm »
1 package Unblock::HTTP3::Body::Reader;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use Unblock::HTTP3 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Unblock::HTTP3::_Bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 our $VERSION = '0.13';
13  
14 26 ▶sub _new {
Subroutine _new: called CC=9 SCAR=23.5
15 26 my ($class, $transaction, $kind, %option) = @_;
16  
17 26 ▶ croak 'body reader kind must be request or response'
Branch: 1 branch
BranchTrueFalseTotal
if $kind ne "request" and $kind ne "response" 0 26 26
Condition66%2/3
$kind ne "request" and $kind ne "response"
!ll&&!rl&&r
15110
MC/DC0%0/2
$kind ne "request" and $kind ne "response"
$kind ne "request" $kind ne "response"
Truth table66%2/3
A and B
A= $kind ne "request"B= $kind ne "response"
ABresult
0X0
100
111
18 if $kind ne 'request' && $kind ne 'response';
19 26 ▶ croak 'body reader requires a Unblock::HTTP3::Transaction'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($transaction) and $transaction->isa("Unblock::HTTP3::Transaction") 0 26 26
Condition33%1/3
builtin::blessed($transaction) and $transaction->isa("Unblock::HTTP3::Transaction")
!ll&&!rl&&r
0026
MC/DC0%0/2
builtin::blessed($transaction) and $transaction->isa("Unblock::HTTP3::Transaction")
builtin::blessed($transaction) $transaction->isa("Unblock::HTTP3::Transaction")
Truth table33%1/3
A and B
A= builtin::blessed($transaction)B= $transaction->isa("Unblock::HTTP3::Transaction")
ABresult
0X0
100
111
20 unless blessed($transaction)
21 && $transaction->isa('Unblock::HTTP3::Transaction');
22  
23 26 my $on_data = delete $option{on_data};
24 26 my $on_end = delete $option{on_end};
25 26 my $on_cancel = delete $option{on_cancel};
26 26 my $operation = $kind . '_body';
27  
28 26 for my $callback (
29 [ on_data => $on_data ],
30 [ on_end => $on_end ],
31 [ on_cancel => $on_cancel ],
32 ) {
33 78 ▶ croak "$operation(): $callback->[0] must be a coderef"
Branch: 1 branch
BranchTrueFalseTotal
if defined $callback->[1] and ref $callback->[1] ne "CODE" 0 78 78
Condition66%2/3
defined $callback->[1] and ref $callback->[1] ne "CODE"
!ll&&!rl&&r
68100
MC/DC0%0/2
defined $callback->[1] and ref $callback->[1] ne "CODE"
defined $callback->[1] ref $callback->[1] ne "CODE"
Truth table66%2/3
A and B
A= defined $callback->[1]B= ref $callback->[1] ne "CODE"
ABresult
0X0
100
111
34 if defined($callback->[1]) && ref($callback->[1]) ne 'CODE';
35 }
36  
37 26 ▶ croak "$operation(): unknown option: " . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 26 26
38 if %option;
39  
40 26 my $self = bless {
41 transaction => $transaction,
42 kind => $kind,
43 queue => [],
44 pending => 0,
45 ended => 0,
46 complete => 0,
47 cancelled => 0,
48 on_data => $on_data,
49 on_end => $on_end,
50 on_cancel => $on_cancel,
51 delivering => 0,
52 }, $class;
53  
54 26 weaken($self->{transaction});
55  
56 26 return $self;
57 }
58  
59 4 ▶sub _configure {
Subroutine _configure: called CC=15 SCAR=34.5
60 4 my ($self, %option) = @_;
61  
62 4 my $operation = $self->{kind} . '_body';
63 4 my $was_complete = $self->{complete};
64 4 my $was_cancelled = $self->{cancelled};
65 4 my $added_on_end = 0;
66 4 my $added_on_cancel = 0;
67  
68 4 for my $name (qw(on_data on_end on_cancel)) {
69 12 ▶ next unless exists $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option{$name} 3 9 12
70  
71 9 my $callback = delete $option{$name};
72  
73 9 ▶ croak "$operation(): $name must be a coderef"
Branch: 1 branch
BranchTrueFalseTotal
if defined $callback and ref $callback ne "CODE" 0 9 9
Condition33%1/3
defined $callback and ref $callback ne "CODE"
!ll&&!rl&&r
090
MC/DC0%0/2
defined $callback and ref $callback ne "CODE"
defined $callback ref $callback ne "CODE"
Truth table33%1/3
A and B
A= defined $callbackB= ref $callback ne "CODE"
ABresult
0X0
100
111
74 if defined($callback) && ref($callback) ne 'CODE';
75 croak "$operation(): $name is already configured"
76 9 ▶ if defined($self->{$name});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$name} 0 9 9
77  
78 9 $self->{$name} = $callback;
79 9 ▶ $added_on_end = 1 if $name eq 'on_end';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "on_end" 4 5 9
80 9 ▶ $added_on_cancel = 1 if $name eq 'on_cancel';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "on_cancel" 3 6 9
81 }
82  
83 4 ▶ croak "$operation(): unknown option: " . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 4 4
84 if %option;
85  
86 4 ▶ if ($was_cancelled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($was_cancelled) 0 4 4
87 0 my $callback = $self->{on_cancel};
88 0 ▶ $callback->($self) if $added_on_cancel && $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $added_on_cancel and $callback 0 0 0
Condition0%0/3
$added_on_cancel and $callback
!ll&&!rl&&r
000
MC/DC0%0/2
$added_on_cancel and $callback
$added_on_cancel $callback
Truth table0%0/3
A and B
A= $added_on_cancelB= $callback
ABresult
0X0
100
111
89 0 return $self;
90 }
91  
92 4 ▶ if ($was_complete) {
Branch: 1 branch
BranchTrueFalseTotal
if ($was_complete) 0 4 4
93 0 my $callback = $self->{on_end};
94 0 ▶ $callback->($self) if $added_on_end && $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $added_on_end and $callback 0 0 0
Condition0%0/3
$added_on_end and $callback
!ll&&!rl&&r
000
MC/DC0%0/2
$added_on_end and $callback
$added_on_end $callback
Truth table0%0/3
A and B
A= $added_on_endB= $callback
ABresult
0X0
100
111
95 0 return $self;
96 }
97  
98 4 $self->_deliver_callbacks;
99 4 $self->_maybe_finish;
100  
101 4 return $self;
102 }
103  
104 46 ▶sub pending_bytes {
Subroutine pending_bytes: called CC=2 SCAR=7.1
Pod documented
105 46 my ($self, @args) = @_;
106 46 ▶ croak 'pending_bytes() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 46 46
107 46 return $self->{pending};
108 }
109  
110 65 ▶sub is_complete {
Subroutine is_complete: called CC=3 SCAR=11.1
Pod documented
111 65 my ($self, @args) = @_;
112 65 ▶ croak 'is_complete() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 65 65
113 65 ▶ return $self->{complete} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'complete'} ? : 48 17 65
114 }
115  
116 1 ▶sub is_cancelled {
Subroutine is_cancelled: called CC=3 SCAR=11.7
Pod documented
117 1 my ($self, @args) = @_;
118 1 ▶ croak 'is_cancelled() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 1 1
119 1 ▶ return $self->{cancelled} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cancelled'} ? : 1 0 1
120 }
121  
122 18 ▶sub next_chunk {
Subroutine next_chunk: called CC=6 SCAR=18.3
Pod documented
123 18 my ($self, @args) = @_;
124  
125 18 ▶ croak 'next_chunk() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 18 18
126 croak 'next_chunk() cannot be called from on_data'
127 18 ▶ if $self->{delivering};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"delivering"} 0 18 18
128 18 ▶ return if $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cancelled"} 0 18 18
129  
130 18 my $bytes = shift @{ $self->{queue} };
131 18 ▶ if (defined $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bytes) 10 8 18
132 10 $self->{pending} -= length($bytes);
133  
134 my $transaction = $self->{transaction}
135 10 ▶ or croak 'next_chunk(): HTTP/3 Transaction is no longer available';
Branch: 1 branch
BranchTrueFalseTotal
my $transaction = $self->{"transaction"} or croak("next_chunk(): HTTP/3 Transaction is no longer available") 10 0 10
136  
137 $transaction->_consume_received_body(
138 $self->{kind},
139 10 length($bytes),
140 );
141 }
142  
143 18 $self->_maybe_finish;
144 18 return $bytes;
145 }
146  
147 0 ▶sub _push {
Subroutine _push: not called CC=2 SCAR=17.9
148 0 my ($self, $bytes) = @_;
149  
150 0 ▶ return if $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cancelled"} 0 0 0
151  
152 0 $bytes = Unblock::HTTP3::_Bytes::byte_string(
153 'received body',
154 $bytes,
155 );
156  
157 0 return $self->_push_owned($bytes);
158 }
159  
160 35 ▶sub _push_owned {
Subroutine _push_owned: called CC=3 SCAR=11.1
161 35 my ($self, $bytes) = @_;
162  
163 35 ▶ return if $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cancelled"} 0 35 35
164  
165 35 ▶ if (length $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bytes) 35 0 35
166 35 push @{ $self->{queue} }, $bytes;
167 35 $self->{pending} += length($bytes);
168 }
169  
170 35 $self->_deliver_callbacks;
171 34 return;
172 }
173  
174 18 ▶sub _mark_end {
Subroutine _mark_end: called CC=2 SCAR=7.0
175 18 my ($self) = @_;
176  
177 18 ▶ return if $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cancelled"} 0 18 18
178  
179 18 $self->{ended} = 1;
180 18 $self->_deliver_callbacks;
181 18 $self->_maybe_finish;
182  
183 18 return;
184 }
185  
186 57 ▶sub _deliver_callbacks {
Subroutine _deliver_callbacks: called CC=7 SCAR=19.5
187 57 my ($self) = @_;
188  
189 57 ▶ my $callback = $self->{on_data} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $callback = $self->{"on_data"} or return 31 26 57
190  
191 31 ▶ while (!$self->{cancelled} && @{ $self->{queue} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"cancelled"} and @{$self->{"queue"};}) 21 29 50
Condition66%2/3
not $self->{"cancelled"} and @{$self->{"queue"};}
!ll&&!rl&&r
02921
MC/DC50%1/2
not $self->{"cancelled"} and @{$self->{"queue"};}
not $self->{"cancelled"} @{$self->{"queue"};}
Truth table66%2/3
not(A) and B
A= not $self->{"cancelled"}B= @{$self->{"queue"};}
ABresult
0X0
100
111
192 21 my $bytes = $self->{queue}[0];
193  
194 21 $self->{delivering} = 1;
195  
196 21 my ($ok, $error);
197 {
198 21 local $@;
199 21 $ok = eval {
200 21 $callback->($self, $bytes);
201 20 1;
202 };
203 21 $error = $@;
204 }
205  
206 21 $self->{delivering} = 0;
207  
208 21 ▶ die $error unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 20 21
209 20 ▶ return if $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cancelled"} 1 19 20
210  
211 19 shift @{ $self->{queue} };
212 19 $self->{pending} -= length($bytes);
213  
214 my $transaction = $self->{transaction}
215 19 ▶ or croak 'body callback lost its HTTP/3 Transaction';
Branch: 1 branch
BranchTrueFalseTotal
my $transaction = $self->{"transaction"} or croak("body callback lost its HTTP/3 Transaction") 19 0 19
216  
217 $transaction->_consume_received_body(
218 $self->{kind},
219 19 length($bytes),
220 );
221 }
222  
223 29 $self->_maybe_finish;
224 29 return;
225 }
226  
227 69 ▶sub _maybe_finish {
Subroutine _maybe_finish: called CC=16 SCAR=30.5
228 69 my ($self) = @_;
229  
230 69 ▶ return if $self->{complete} || $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"complete"} or $self->{"cancelled"} 8 61 69
Condition66%2/3
$self->{"complete"} or $self->{"cancelled"}
l!l&&r!l&&!r
8061
MC/DC50%1/2
$self->{"complete"} or $self->{"cancelled"}
$self->{"complete"} $self->{"cancelled"}
Truth table66%2/3
A or B
A= $self->{"complete"}B= $self->{"cancelled"}
ABresult
1X1
011
000
231 61 ▶ return unless $self->{ended};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ended"} 39 22 61
232 22 ▶ return if @{ $self->{queue} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"queue"};} 4 18 22
233  
234 18 $self->{complete} = 1;
235  
236 18 my $transaction = $self->{transaction};
237 18 ▶ $transaction->{callback_depth}++ if $transaction;
Branch: 1 branch
BranchTrueFalseTotal
if $transaction 18 0 18
238 18 my $ok = eval {
239 $transaction->_received_body_complete($self->{kind})
240 18 ▶ if $transaction;
Branch: 1 branch
BranchTrueFalseTotal
if $transaction 18 0 18
241 18 my $callback = $self->{on_end};
242 18 ▶ $callback->($self) if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 5 13 18
243 18 1;
244 };
245 18 my $error = "$@";
246 18 ▶ if ($transaction) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transaction) 18 0 18
247 18 $transaction->{callback_depth}--;
248 18 ▶ if (!$ok && !$transaction->is_terminal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok or $transaction->is_terminal) 0 18 18
Condition33%1/3
$ok or $transaction->is_terminal
l!l&&r!l&&!r
1800
MC/DC0%0/2
$ok or $transaction->is_terminal
$ok $transaction->is_terminal
Truth table33%1/3
A or B
A= $okB= $transaction->is_terminal
ABresult
1X1
011
000
249 0 my $connection = $transaction->{connection};
250 0 ▶ $connection->_reject_message_stream($transaction->stream_id, $error)
Branch: 1 branch
BranchTrueFalseTotal
if $connection 0 0 0
251 if $connection;
252 0 eval { $transaction->_mark_error($error) };
253 }
254 18 ▶ $transaction->_notify_finish if $transaction->{finish_pending};
Branch: 1 branch
BranchTrueFalseTotal
if $transaction->{"finish_pending"} 2 16 18
255 18 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 18 0 18
256 18 my $connection = $transaction->{connection};
257 18 ▶ $connection->_maybe_complete_transaction($transaction->stream_id)
Branch: 1 branch
BranchTrueFalseTotal
if $connection 18 0 18
258 if $connection;
259 }
260 }
261 18 ▶ die $error unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 18 18
262  
263 18 return;
264 }
265  
266 8 ▶sub _cancel {
Subroutine _cancel: called CC=6 SCAR=18.2
267 8 my ($self) = @_;
268  
269 8 ▶ return if $self->{complete} || $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"complete"} or $self->{"cancelled"} 0 8 8
Condition33%1/3
$self->{"complete"} or $self->{"cancelled"}
l!l&&r!l&&!r
008
MC/DC0%0/2
$self->{"complete"} or $self->{"cancelled"}
$self->{"complete"} $self->{"cancelled"}
Truth table33%1/3
A or B
A= $self->{"complete"}B= $self->{"cancelled"}
ABresult
1X1
011
000
270  
271 8 $self->{cancelled} = 1;
272 8 my $pending = $self->{pending};
273 8 $self->{queue} = [];
274 8 $self->{pending} = 0;
275 8 my $transaction = $self->{transaction};
276 8 ▶ $transaction->_consume_received_body($self->{kind}, $pending)
Branch: 1 branch
BranchTrueFalseTotal
if $transaction and $pending 5 3 8
Condition66%2/3
$transaction and $pending
!ll&&!rl&&r
035
MC/DC50%1/2
$transaction and $pending
$transaction $pending
Truth table66%2/3
A and B
A= $transactionB= $pending
ABresult
0X0
100
111
277 if $transaction && $pending;
278  
279 8 my $callback = $self->{on_cancel};
280 8 ▶ $callback->($self) if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 1 7 8
281  
282 8 return;
283 }
284  
285 89 ▶sub _drain {
Subroutine _drain: called CC=1 SCAR=0.0
286 89 return;
287 }
288  
289 6 1;
290  
291 __END__
« blib/lib/Unblock/HTTP3/Extension/Stream.pm ↑ Summary blib/lib/Unblock/HTTP3/Capsule/Parser.pm »