blib/lib/Unblock/HTTP3/Capsule/Stream.pm

filter: Statementstmt 81.4% 136 / 167 Branchbran 49.1% 55 / 112 Conditioncond 40.0% 14 / 35 MC/DCmcdc 9.5% 2 / 21 Subroutinesub 85.0% 17 / 20 Podpod 100% 5 / 5 totaltotal 63.6% 229 / 360 CC 72 SCAR 30.7
CC
72
Cov
65%
_new
54.5
__ANON__
20.0
__ANON__
20.0
CRAP 21.5
« blib/lib/Unblock/HTTP3/Connection.pm ↑ Summary blib/lib/Unblock/HTTP3/Extension/Stream.pm »
1 package Unblock::HTTP3::Capsule::Stream;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Unblock::HTTP3 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Unblock::HTTP3::Capsule ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Unblock::HTTP3::Capsule::Parser ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '0.14';
14  
15 4 ▶sub _new {
Subroutine _new: called CC=23 SCAR=40.0
16 4 my ($class, $transaction, %option) = @_;
17  
18 4 ▶ croak 'Capsule stream requires a Unblock::HTTP3::Transaction'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($transaction) and $transaction->isa("Unblock::HTTP3::Transaction") 0 4 4
Condition33%1/3
builtin::blessed($transaction) and $transaction->isa("Unblock::HTTP3::Transaction")
!ll&&!rl&&r
004
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
19 unless blessed($transaction)
20 && $transaction->isa('Unblock::HTTP3::Transaction');
21 4 ▶ croak 'Capsule Protocol requires Extended CONNECT'
Branch: 1 branch
BranchTrueFalseTotal
unless $transaction->is_extended_connect 0 4 4
22 unless $transaction->is_extended_connect;
23  
24 4 my $on_capsule = delete $option{on_capsule};
25 4 my $handlers_supplied = exists $option{handlers};
26 4 my $handlers = delete $option{handlers};
27 4 my $on_end = delete $option{on_end};
28 4 my $on_cancel = delete $option{on_cancel};
29 my $max_capsule_size = exists($option{max_capsule_size})
30 ? delete($option{max_capsule_size})
31 4 ▶ : 4 * 1024 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'max_capsule_size'} ? : 0 4 4
32  
33 4 ▶ croak 'on_capsule must be a code reference'
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_capsule and ref $on_capsule ne "CODE" 0 4 4
Condition66%2/3
defined $on_capsule and ref $on_capsule ne "CODE"
!ll&&!rl&&r
310
MC/DC0%0/2
defined $on_capsule and ref $on_capsule ne "CODE"
defined $on_capsule ref $on_capsule ne "CODE"
Truth table66%2/3
A and B
A= defined $on_capsuleB= ref $on_capsule ne "CODE"
ABresult
0X0
100
111
34 if defined($on_capsule) && ref($on_capsule) ne 'CODE';
35 4 ▶ croak 'handlers must be a hash reference'
Branch: 1 branch
BranchTrueFalseTotal
if defined $handlers and ref $handlers ne "HASH" 0 4 4
Condition66%2/3
defined $handlers and ref $handlers ne "HASH"
!ll&&!rl&&r
310
MC/DC0%0/2
defined $handlers and ref $handlers ne "HASH"
defined $handlers ref $handlers ne "HASH"
Truth table66%2/3
A and B
A= defined $handlersB= ref $handlers ne "HASH"
ABresult
0X0
100
111
36 if defined($handlers) && ref($handlers) ne 'HASH';
37 4 ▶ croak 'on_end must be a code reference'
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_end and ref $on_end ne "CODE" 0 4 4
Condition33%1/3
defined $on_end and ref $on_end ne "CODE"
!ll&&!rl&&r
400
MC/DC0%0/2
defined $on_end and ref $on_end ne "CODE"
defined $on_end ref $on_end ne "CODE"
Truth table33%1/3
A and B
A= defined $on_endB= ref $on_end ne "CODE"
ABresult
0X0
100
111
38 if defined($on_end) && ref($on_end) ne 'CODE';
39 4 ▶ croak 'on_cancel must be a code reference'
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_cancel and ref $on_cancel ne "CODE" 0 4 4
Condition33%1/3
defined $on_cancel and ref $on_cancel ne "CODE"
!ll&&!rl&&r
400
MC/DC0%0/2
defined $on_cancel and ref $on_cancel ne "CODE"
defined $on_cancel ref $on_cancel ne "CODE"
Truth table33%1/3
A and B
A= defined $on_cancelB= ref $on_cancel ne "CODE"
ABresult
0X0
100
111
40 if defined($on_cancel) && ref($on_cancel) ne 'CODE';
41 4 ▶ croak 'unknown Capsule stream option: ' . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 4 4
42 if %option;
43  
44 4 ▶ my $self = bless {
Branch: 1 branch
BranchTrueFalseTotal
!($handlers_supplied || defined $on_capsule) ? : 2 2 4
Condition100%3/3
$handlers_supplied || defined $on_capsule
l!l&&r!l&&!r
112
MC/DC100%2/2
$handlers_supplied || defined $on_capsule
$handlers_supplied defined $on_capsule
Truth table100%3/3
A || B
A= $handlers_suppliedB= defined $on_capsule
ABresult
1X1
011
000
45 transaction => $transaction,
46 parser => undef,
47 writer => undef,
48 reader => undef,
49 on_end => $on_end,
50 on_cancel => $on_cancel,
51 polling => (!$handlers_supplied && !defined($on_capsule)) ? 1 : 0,
52 callback_error => undef,
53 ended => 0,
54 cancelled => 0,
55 }, $class;
56  
57 4 weaken($self->{transaction});
58  
59 4 my $weak_self = $self;
60 4 weaken($weak_self);
61  
62 4 my %parser_option = (
63 max_capsule_size => $max_capsule_size,
64 );
65  
66 4 ▶ if (defined $handlers) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handlers) 1 3 4
67 1 my %wrapped;
68  
69 1 for my $type (keys %$handlers) {
70 1 my $callback = $handlers->{$type};
71  
72 1 ▶ croak "Capsule handler for type $type must be a code reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 1 1
73 unless ref($callback) eq 'CODE';
74  
75 1 ▶ $wrapped{$type} = sub {
Subroutine __ANON__: called CC=4 SCAR=16.8
76 1 my ($parser, $capsule) = @_;
77 1 ▶ my $stream = $weak_self or return;
Branch: 1 branch
BranchTrueFalseTotal
my $stream = $weak_self or return 1 0 1
78  
79 1 my $ok = eval {
80 1 $callback->($stream, $capsule);
81 1 1;
82 };
83  
84 1 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 1 1
85 0 ▶ my $error = $@ || 'Capsule handler failed';
Condition0%0/2
$@ || 'Capsule handler failed'
l!l
00
MC/DC0%0/1
$@ || 'Capsule handler failed'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
86 0 $stream->{callback_error} = $error;
87 0 die $error;
88 }
89  
90 1 return;
91 1 };
92 }
93  
94 1 $parser_option{handlers} = \%wrapped;
95 }
96  
97 4 ▶ if (defined $on_capsule) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $on_capsule) 1 3 4
98 0 ▶ $parser_option{on_capsule} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
99 0 my ($parser, $capsule) = @_;
100 0 ▶ my $stream = $weak_self or return;
Branch: 1 branch
BranchTrueFalseTotal
my $stream = $weak_self or return 0 0 0
101  
102 0 my $ok = eval {
103 0 $on_capsule->($stream, $capsule);
104 0 1;
105 };
106  
107 0 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
108 0 ▶ my $error = $@ || 'Capsule callback failed';
Condition0%0/2
$@ || 'Capsule callback failed'
l!l
00
MC/DC0%0/1
$@ || 'Capsule callback failed'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
109 0 $stream->{callback_error} = $error;
110 0 die $error;
111 }
112  
113 0 return;
114 1 };
115 }
116  
117 $self->{parser} =
118 4 Unblock::HTTP3::Capsule::Parser->new(%parser_option);
119  
120 my $connection = $transaction->{connection}
121 4 ▶ or croak 'Capsule stream lost its HTTP/3 connection';
Branch: 1 branch
BranchTrueFalseTotal
my $connection = $transaction->{"connection"} or croak("Capsule stream lost its HTTP/3 connection") 4 0 4
122  
123 4 $self->_assert_protocol_ready;
124  
125 3 ▶ if ($connection->role eq 'client') {
Branch: 1 branch
BranchTrueFalseTotal
if ($connection->role eq 'client') { } 1 2 3
126 1 $self->{writer} = $transaction->request_body;
127 1 $self->{reader} = $transaction->response_body;
128 } else {
129 2 $self->{writer} = $transaction->response_body;
130 2 $self->{reader} = $transaction->request_body;
131 }
132  
133 my %reader_option = (
134 3 ▶ on_end => sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
135 3 ▶ my $stream = $weak_self or return;
Branch: 1 branch
BranchTrueFalseTotal
my $stream = $weak_self or return 3 0 3
136 3 ▶ return if $stream->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $stream->{"cancelled"} 0 3 3
137  
138 3 my $ok = eval {
139 3 $stream->_assert_protocol_ready;
140 3 $stream->{parser}->finish;
141 2 1;
142 };
143  
144 3 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 2 3
145 1 ▶ my $error = $@ || 'Capsule Protocol truncated stream';
Condition50%1/2
$@ || 'Capsule Protocol truncated stream'
l!l
10
MC/DC0%0/1
$@ || 'Capsule Protocol truncated stream'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
146 1 my $tx = $stream->{transaction};
147 1 ▶ $tx->_capsule_protocol_error($error)
Branch: 1 branch
BranchTrueFalseTotal
if defined $tx 1 0 1
148 if defined $tx;
149 1 return;
150 }
151  
152 2 $stream->{ended} = 1;
153  
154 2 my $callback = $stream->{on_end};
155 2 ▶ $callback->($stream) if defined $callback;
Branch: 1 branch
BranchTrueFalseTotal
if defined $callback 0 2 2
156  
157 2 return;
158 },
159 0 ▶ on_cancel => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
160 0 ▶ my $stream = $weak_self or return;
Branch: 1 branch
BranchTrueFalseTotal
my $stream = $weak_self or return 0 0 0
161 0 ▶ return if $stream->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $stream->{"cancelled"} 0 0 0
162  
163 0 $stream->{cancelled} = 1;
164  
165 0 my $callback = $stream->{on_cancel};
166 0 ▶ $callback->($stream) if defined $callback;
Branch: 1 branch
BranchTrueFalseTotal
if defined $callback 0 0 0
167  
168 0 return;
169 },
170 3 );
171  
172 3 ▶ if (!$self->{polling}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"polling"}) 2 1 3
173 3 ▶ $reader_option{on_data} = sub {
Subroutine __ANON__: called CC=7 SCAR=27.3
174 3 my ($reader, $bytes) = @_;
175 3 ▶ my $stream = $weak_self or return;
Branch: 1 branch
BranchTrueFalseTotal
my $stream = $weak_self or return 3 0 3
176  
177 3 ▶ return if $stream->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $stream->{"cancelled"} 0 3 3
178  
179 3 my $ok = eval {
180 3 $stream->_assert_protocol_ready;
181 3 $stream->{parser}->feed($bytes);
182 3 1;
183 };
184  
185 3 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 3 3
186 0 ▶ my $error = $@ || 'Capsule Protocol parse error';
Condition0%0/2
$@ || 'Capsule Protocol parse error'
l!l
00
MC/DC0%0/1
$@ || 'Capsule Protocol parse error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
187  
188 0 ▶ if (defined $stream->{callback_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stream->{"callback_error"}) 0 0 0
189 0 my $callback_error = delete $stream->{callback_error};
190 0 die $callback_error;
191 }
192  
193 0 my $tx = $stream->{transaction};
194 0 ▶ $tx->_capsule_protocol_error($error)
Branch: 1 branch
BranchTrueFalseTotal
if defined $tx 0 0 0
195 if defined $tx;
196 }
197  
198 3 return;
199 2 };
200 }
201  
202 3 $self->{reader}->_configure(%reader_option);
203  
204 3 return $self;
205 }
206  
207 33 ▶sub _has_forbidden_field {
Subroutine _has_forbidden_field: called CC=3 SCAR=11.1
208 33 my ($message) = @_;
209  
210 33 for my $name (qw(content-length content-type transfer-encoding)) {
211 99 my $values = $message->header_values($name);
212 99 ▶ return $name if @$values;
Branch: 1 branch
BranchTrueFalseTotal
if @$values 0 99 99
213 }
214  
215 33 return;
216 }
217  
218 17 ▶sub _assert_protocol_ready {
Subroutine _assert_protocol_ready: called CC=11 SCAR=29.0
219 17 my ($self) = @_;
220  
221 my $transaction = $self->{transaction}
222 17 ▶ or croak 'Capsule Protocol Transaction is no longer available';
Branch: 1 branch
BranchTrueFalseTotal
my $transaction = $self->{"transaction"} or croak("Capsule Protocol Transaction is no longer available") 17 0 17
223  
224 17 my $request = $transaction->request;
225  
226 17 ▶ croak 'Capsule Protocol requires Extended CONNECT'
Branch: 1 branch
BranchTrueFalseTotal
unless $transaction->is_extended_connect 0 17 17
227 unless $transaction->is_extended_connect;
228  
229 17 ▶ if (my $field = _has_forbidden_field($request)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = _has_forbidden_field($request)) 0 17 17
230 0 croak "Capsule Protocol request must not contain $field";
231 }
232  
233 17 ▶ my $response = $transaction->response
Branch: 1 branch
BranchTrueFalseTotal
my $response = $transaction->response or croak("Capsule Protocol requires a final response") 16 1 17
234 or croak 'Capsule Protocol requires a final response';
235  
236 16 my $status = $response->status;
237  
238 16 ▶ croak 'Capsule Protocol requires a successful response'
Branch: 1 branch
BranchTrueFalseTotal
unless $status >= 200 and $status < 300 0 16 16
Condition33%1/3
$status >= 200 and $status < 300
!ll&&!rl&&r
0016
MC/DC0%0/2
$status >= 200 and $status < 300
$status >= 200 $status < 300
Truth table33%1/3
A and B
A= $status >= 200B= $status < 300
ABresult
0X0
100
111
239 unless $status >= 200 && $status < 300;
240 16 ▶ croak "Capsule Protocol cannot use HTTP status $status"
Branch: 1 branch
BranchTrueFalseTotal
if $status == 204 or $status == 205 or $status == 206 0 16 16
Condition33%2/6
$status == 204 or $status == 205 or $status == 206
l!l&&r!l&&!r
0016
$status == 204 or $status == 205
l!l&&r!l&&!r
0016
MC/DC0%0/3
$status == 204 or $status == 205 or $status == 206
$status == 204 $status == 205 $status == 206
Truth table25%1/4
A or B or C
A= $status == 204B= $status == 205C= $status == 206
ABCresult
1XX1
01X1
0011
0000
241 if $status == 204 || $status == 205 || $status == 206;
242  
243 16 ▶ if (my $field = _has_forbidden_field($response)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = _has_forbidden_field($response)) 0 16 16
244 0 croak "Capsule Protocol response must not contain $field";
245 }
246  
247 16 return 1;
248 }
249  
250 5 ▶sub send {
Subroutine send: called CC=3 SCAR=11.3
Pod documented
251 5 my ($self, $type, $value) = @_;
252  
253 croak 'send() cannot be used after Capsule stream cancellation'
254 5 ▶ if $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cancelled"} 0 5 5
255  
256 5 $self->_assert_protocol_ready;
257  
258 5 ▶ my $capsule = Unblock::HTTP3::Capsule->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 5 0 5
259 type => $type,
260 value => defined($value) ? $value : '',
261 );
262  
263 5 return $self->{writer}->write($capsule->encode);
264 }
265  
266 5 ▶sub next_capsule {
Subroutine next_capsule: called CC=8 SCAR=22.9
Pod documented
267 5 my ($self, @args) = @_;
268  
269 5 ▶ croak 'next_capsule() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 5 5
270 croak 'next_capsule() is only available in polling mode'
271 5 ▶ unless $self->{polling};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"polling"} 0 5 5
272 5 ▶ return if $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cancelled"} 0 5 5
273  
274 5 my $capsule = $self->{parser}->next_capsule;
275 5 ▶ return $capsule if defined $capsule;
Branch: 1 branch
BranchTrueFalseTotal
if defined $capsule 0 5 5
276  
277 5 ▶ while (defined(my $bytes = $self->{reader}->next_chunk)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $bytes = $self->{"reader"}->next_chunk)) 2 3 5
278 2 $self->{parser}->feed($bytes);
279  
280 2 $capsule = $self->{parser}->next_capsule;
281 2 ▶ return $capsule if defined $capsule;
Branch: 1 branch
BranchTrueFalseTotal
if defined $capsule 2 0 2
282  
283 0 ▶ return if $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cancelled"} 0 0 0
284 }
285  
286 3 return $self->{parser}->next_capsule;
287 }
288  
289 2 ▶sub complete {
Subroutine complete: called CC=4 SCAR=14.5
Pod documented
290 2 my ($self, @args) = @_;
291 2 ▶ croak 'complete() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 2 2
292 croak 'Capsule stream was cancelled'
293 2 ▶ if $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cancelled"} 0 2 2
294  
295 2 $self->_assert_protocol_ready;
296 $self->{writer}->complete
297 2 ▶ unless $self->{writer}->is_complete;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"writer"}->is_complete 2 0 2
298  
299 2 return $self;
300 }
301  
302 4 ▶sub is_receive_complete {
Subroutine is_receive_complete: called CC=3 SCAR=11.1
Pod documented
303 4 my ($self, @args) = @_;
304 4 ▶ croak 'is_receive_complete() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 4 4
305 4 ▶ return $self->{ended} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ended'} ? : 2 2 4
306 }
307  
308 0 ▶sub is_cancelled {
Subroutine is_cancelled: not called CC=3 SCAR=24.8
Pod documented
309 0 my ($self, @args) = @_;
310 0 ▶ croak 'is_cancelled() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 0 0
311 0 ▶ return $self->{cancelled} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cancelled'} ? : 0 0 0
312 }
313  
314 2 1;
315  
316 __END__
« blib/lib/Unblock/HTTP3/Connection.pm ↑ Summary blib/lib/Unblock/HTTP3/Extension/Stream.pm »