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

filter: Statementstmt 98.3% 119 / 121 Branchbran 79.4% 62 / 78 Conditioncond 66.6% 14 / 21 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 15 / 15 Podpod 100% 5 / 5 totaltotal 86.9% 220 / 253 CC 49 SCAR 22.8
CC
49
Cov
89%
new
18.6
feed
14.0
finish
5.2
CRAP 9.8
« blib/lib/Unblock/HTTP3/Body/Reader.pm ↑ Summary blib/lib/Unblock/HTTP3/Transaction.pm »
1 package Unblock::HTTP3::Capsule::Parser;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Unblock::HTTP3 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Unblock::HTTP3::Capsule ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Unblock::HTTP3::_Bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.13';
13  
14 16 ▶sub _decimal_mod {
Subroutine _decimal_mod: called CC=2 SCAR=6.9
15 16 my ($value, $divisor) = @_;
16  
17 16 my $remainder = 0;
18  
19 16 for my $digit (split //, "$value") {
20 35 $remainder = ($remainder * 10 + ord($digit) - 48) % $divisor;
21 }
22  
23 16 return $remainder;
24 }
25  
26 19 ▶sub _is_grease_type {
Subroutine _is_grease_type: called CC=5 SCAR=16.3
27 19 my ($type) = @_;
28  
29 19 ▶ return 0 if length($type) < 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $type < 2 3 16 19
30 16 ▶ return 0 if length($type) == 2 && $type lt '23';
Branch: 1 branch
BranchTrueFalseTotal
if length $type == 2 and $type lt 23 0 16 16
Condition66%2/3
length $type == 2 and $type lt 23
!ll&&!rl&&r
1150
MC/DC0%0/2
length $type == 2 and $type lt 23
length $type == 2 $type lt 23
Truth table66%2/3
A and B
A= length $type == 2B= $type lt 23
ABresult
0X0
100
111
31  
32 16 ▶ return _decimal_mod($type, 41) == 23 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_decimal_mod($type, 41) == 23 ? : 2 14 16
33 }
34  
35 12 ▶sub new {
Subroutine new: called CC=16 SCAR=29.2
Pod documented
36 12 my ($class, %option) = @_;
37  
38 12 my $handlers_supplied = exists $option{handlers};
39 12 my $handlers = delete $option{handlers};
40 12 my $on_capsule = delete $option{on_capsule};
41 my $max_capsule_size = exists($option{max_capsule_size})
42 ? delete($option{max_capsule_size})
43 12 ▶ : 4 * 1024 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'max_capsule_size'} ? : 6 6 12
44  
45 12 ▶ $handlers = {} unless defined $handlers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handlers 8 4 12
46  
47 12 ▶ croak 'handlers must be a hash reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $handlers eq "HASH" 0 12 12
48 unless ref($handlers) eq 'HASH';
49 12 ▶ croak 'on_capsule must be a code reference'
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_capsule and ref $on_capsule ne "CODE" 0 12 12
Condition66%2/3
defined $on_capsule and ref $on_capsule ne "CODE"
!ll&&!rl&&r
1110
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
50 if defined($on_capsule) && ref($on_capsule) ne 'CODE';
51 12 ▶ croak 'max_capsule_size must be a non-negative integer'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max_capsule_size or ref $max_capsule_size or not "$max_capsule_size" =~ /\A[0-9]+\z/ 0 12 12
Condition33%2/6
not defined $max_capsule_size or ref $max_capsule_size or not "$max_capsule_size" =~ /\A[0-9]+\z/
l!l&&r!l&&!r
0012
not defined $max_capsule_size or ref $max_capsule_size
l!l&&r!l&&!r
0012
MC/DC0%0/3
not defined $max_capsule_size or ref $max_capsule_size or not "$max_capsule_size" =~ /\A[0-9]+\z/
not defined $max_capsule_size ref $max_capsule_size not "$max_capsule_size" =~ /\A[0-9]+\z/
Truth table25%1/4
not(A) or B or not(C)
A= not defined $max_capsule_sizeB= ref $max_capsule_sizeC= not "$max_capsule_size" =~ /\A[0-9]+\z/
ABCresult
1XX1
01X1
0011
0000
52 if !defined($max_capsule_size)
53 || ref($max_capsule_size)
54 || "$max_capsule_size" !~ /\A[0-9]+\z/;
55 12 ▶ croak 'unknown Capsule parser option: ' . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 12 12
56 if %option;
57  
58 12 my %normalized_handlers;
59  
60 12 for my $raw_type (keys %$handlers) {
61 4 my $type = Unblock::HTTP3::Capsule::_normalize_varint(
62 $raw_type,
63 'Capsule handler type',
64 );
65 4 my $callback = $handlers->{$raw_type};
66  
67 4 ▶ croak "Capsule type $type is reserved for greasing"
Branch: 1 branch
BranchTrueFalseTotal
if _is_grease_type($type) 1 3 4
68 if _is_grease_type($type);
69 3 ▶ croak "Capsule handler for type $type must be a code reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 3 3
70 unless ref($callback) eq 'CODE';
71 croak "duplicate Capsule handler type $type"
72 3 ▶ if exists $normalized_handlers{$type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $normalized_handlers{$type} 0 3 3
73  
74 3 $normalized_handlers{$type} = $callback;
75 }
76  
77 11 ▶ return bless {
Branch: 1 branch
BranchTrueFalseTotal
!($handlers_supplied || defined $on_capsule) ? : 7 4 11
Condition100%3/3
$handlers_supplied || defined $on_capsule
l!l&&r!l&&!r
317
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
78 handlers => \%normalized_handlers,
79 on_capsule => $on_capsule,
80 polling => (!$handlers_supplied && !defined($on_capsule))
81 ? 1
82 : 0,
83 max_capsule_size => 0 + $max_capsule_size,
84 queue => [],
85 buffer => '',
86 stage => 'type',
87 type => undef,
88 remaining => 0,
89 value => '',
90 interested => 0,
91 finished => 0,
92 }, $class;
93 }
94  
95 15 ▶sub _is_interested {
Subroutine _is_interested: called CC=5 SCAR=16.1
96 15 my ($self, $type) = @_;
97  
98 15 ▶ return 0 if _is_grease_type($type);
Branch: 1 branch
BranchTrueFalseTotal
if _is_grease_type($type) 1 14 15
99 14 ▶ return 1 if exists $self->{handlers}{$type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"handlers"}{$type} 3 11 14
100 11 ▶ return 1 if defined $self->{on_capsule};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"on_capsule"} 1 10 11
101 10 ▶ return 1 if $self->{polling};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"polling"} 8 2 10
102  
103 2 return 0;
104 }
105  
106 12 ▶sub _emit {
Subroutine _emit: called CC=5 SCAR=16.2
107 12 my ($self) = @_;
108  
109 12 ▶ if ($self->{interested}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"interested"}) 9 3 12
110 my $capsule = Unblock::HTTP3::Capsule->new(
111 type => $self->{type},
112 value => $self->{value},
113 9 );
114  
115 9 my $handler = $self->{handlers}{ $self->{type} };
116  
117 9 ▶ if (defined $handler) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $handler) { } 3 6 9
elsif (defined $self->{'on_capsule'}) { } 0 6 6
elsif ($self->{'polling'}) { } 6 0 6
118 3 $handler->($self, $capsule);
119 } elsif (defined $self->{on_capsule}) {
120 0 $self->{on_capsule}->($self, $capsule);
121 } elsif ($self->{polling}) {
122 6 push @{ $self->{queue} }, $capsule;
123 }
124 }
125  
126 11 $self->{stage} = 'type';
127 11 $self->{type} = undef;
128 11 $self->{remaining} = 0;
129 11 $self->{value} = '';
130 11 $self->{interested} = 0;
131  
132 11 return;
133 }
134  
135 21 ▶sub feed {
Subroutine feed: called CC=14 SCAR=26.4
Pod documented
136 21 my ($self, $bytes) = @_;
137  
138 croak 'feed() cannot be called after finish()'
139 21 ▶ if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 0 21 21
140  
141 21 $bytes = Unblock::HTTP3::_Bytes::byte_string(
142 'Capsule Protocol bytes',
143 $bytes,
144 );
145  
146 21 $self->{buffer} .= $bytes;
147  
148 21 while (1) {
149 60 ▶ if ($self->{stage} eq 'type') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stage"} eq "type") 27 33 60
150 my ($type, $length) =
151 27 Unblock::HTTP3::Capsule::_decode_varint($self->{buffer}, 0);
152 27 ▶ return $self unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 12 15 27
153  
154 15 substr($self->{buffer}, 0, $length, '');
155 15 $self->{type} = $type;
156 15 $self->{stage} = 'length';
157 15 next;
158 }
159  
160 33 ▶ if ($self->{stage} eq 'length') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stage"} eq "length") 17 16 33
161 my ($length, $encoded_length) =
162 17 Unblock::HTTP3::Capsule::_decode_varint($self->{buffer}, 0);
163 17 ▶ return $self unless defined $length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length 2 15 17
164  
165 15 substr($self->{buffer}, 0, $encoded_length, '');
166  
167 15 $self->{remaining} = 0 + $length;
168 $self->{interested} =
169 15 $self->_is_interested($self->{type});
170  
171 15 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"interested"} and $length > $self->{"max_capsule_size"}) 1 14 15
Condition100%3/3
$self->{"interested"} and $length > $self->{"max_capsule_size"}
!ll&&!rl&&r
3111
MC/DC100%2/2
$self->{"interested"} and $length > $self->{"max_capsule_size"}
$self->{"interested"} $length > $self->{"max_capsule_size"}
Truth table100%3/3
A and B
A= $self->{"interested"}B= $length > $self->{"max_capsule_size"}
ABresult
0X0
100
111
172 $self->{interested}
173 && $length > $self->{max_capsule_size}
174 ) {
175 1 croak "Capsule type $self->{type} length $length exceeds "
176 . "configured maximum $self->{max_capsule_size}";
177 }
178  
179 14 $self->{value} = '';
180  
181 14 ▶ if ($self->{remaining} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"remaining"} == 0) 1 13 14
182 1 $self->_emit;
183 1 next;
184 }
185  
186 13 $self->{stage} = 'value';
187 13 next;
188 }
189  
190 16 ▶ if ($self->{stage} eq 'value') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stage"} eq "value") 16 0 16
191 16 ▶ return $self unless length $self->{buffer};
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"buffer"} 1 15 16
192  
193 15 my $take = length($self->{buffer});
194 $take = $self->{remaining}
195 15 ▶ if $take > $self->{remaining};
Branch: 1 branch
BranchTrueFalseTotal
if $take > $self->{"remaining"} 1 14 15
196  
197 15 my $chunk = substr($self->{buffer}, 0, $take, '');
198 $self->{value} .= $chunk
199 15 ▶ if $self->{interested};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"interested"} 12 3 15
200 15 $self->{remaining} -= $take;
201  
202 15 ▶ if ($self->{remaining} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"remaining"} == 0) 11 4 15
203 11 $self->_emit;
204 10 next;
205 }
206  
207 4 return $self;
208 }
209  
210 0 croak "unknown Capsule parser state '$self->{stage}'";
211 }
212 }
213  
214 16 ▶sub next_capsule {
Subroutine next_capsule: called CC=2 SCAR=7.0
Pod documented
215 16 my ($self, @args) = @_;
216 16 ▶ croak 'next_capsule() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 16 16
217 16 return shift @{ $self->{queue} };
218 }
219  
220 8 ▶sub finish {
Subroutine finish: called CC=5 SCAR=16.4
Pod documented
221 8 my ($self, @args) = @_;
222 8 ▶ croak 'finish() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 8 8
223  
224 8 ▶ return $self if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 0 8 8
225  
226 8 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stage"} ne "type" or length $self->{"buffer"}) 2 6 8
Condition66%2/3
$self->{"stage"} ne "type" or length $self->{"buffer"}
l!l&&r!l&&!r
206
MC/DC50%1/2
$self->{"stage"} ne "type" or length $self->{"buffer"}
$self->{"stage"} ne "type" length $self->{"buffer"}
Truth table66%2/3
A or B
A= $self->{"stage"} ne "type"B= length $self->{"buffer"}
ABresult
1X1
011
000
227 $self->{stage} ne 'type'
228 || length($self->{buffer})
229 ) {
230 2 croak 'Capsule Protocol stream ended with a truncated Capsule';
231 }
232  
233 6 $self->{finished} = 1;
234 6 return $self;
235 }
236  
237 1 ▶sub is_finished {
Subroutine is_finished: called CC=3 SCAR=11.7
Pod documented
238 1 my ($self, @args) = @_;
239 1 ▶ croak 'is_finished() does not accept arguments' if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 1 1
240 1 ▶ return $self->{finished} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'finished'} ? : 1 0 1
241 }
242  
243 3 1;
244  
245 __END__
« blib/lib/Unblock/HTTP3/Body/Reader.pm ↑ Summary blib/lib/Unblock/HTTP3/Transaction.pm »