HTTP2/HeaderCompression.pm

filter: Statementstmt 80.4% 169 / 210 Branchbran 63.7% 51 / 80 Conditioncond 52.0% 13 / 25 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 8 totaltotal 71.3% 251 / 352 CC 53 SCAR 40.0
CC
53
Cov
74%
headers_decode
112.6
headers_encode
8.1
int_decode
7.3
CRAP 54.6
« HTTP2/Connection.pm ↑ Summary HTTP2/Frame/Push_promise.pm »
1 package Protocol::HTTP2::HeaderCompression;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Protocol::HTTP2::Huffman;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Protocol::HTTP2::StaticTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Protocol::HTTP2::Constants qw(:errors :settings :limits);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Protocol::HTTP2::Trace qw(tracer bin2hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 our @EXPORT_OK = qw(int_encode int_decode str_encode str_decode headers_decode
10 headers_encode);
11  
12 4369 ▶sub int_encode {
Subroutine int_encode: called CC=4 SCAR=13.9
Pod undocumented
13 4369 my ( $int, $N ) = @_;
14 4369 ▶ $N ||= 7;
Condition50%1/2
$N ||= 7
l!l
43690
MC/DC0%0/1
$N ||= 7
$N
Truth table50%1/2
A
A= $N
Aresult
11
00
15 4369 my $ff = ( 1 << $N ) - 1;
16  
17 4369 ▶ if ( $int < $ff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int < $ff) 4354 15 4369
18 4354 return pack 'C', $int;
19 }
20  
21 15 my $res = pack 'C', $ff;
22 15 $int -= $ff;
23  
24 15 ▶ while ( $int >= 0x80 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int >= 128) 13 15 28
25 13 $res .= pack( 'C', ( $int & 0x7f ) | 0x80 );
26 13 $int >>= 7;
27 }
28  
29 15 return $res . pack( 'C', $int );
30 }
31  
32 # int_decode()
33 #
34 # arguments:
35 # buf_ref - ref to buffer with encoded data
36 # buf_offset - offset in buffer
37 # int_ref - ref to scalar where result will be stored
38 # N - bits in first byte
39 #
40 # returns: count of read bytes of encoded integer
41 # or undef on error (malformed data)
42  
43 6317 ▶sub int_decode {
Subroutine int_decode: called CC=7 SCAR=19.8
Pod undocumented
44 6317 my ( $buf_ref, $buf_offset, $int_ref, $N ) = @_;
45 6317 ▶ return undef if length($$buf_ref) - $buf_offset <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if length($$buf_ref) - $buf_offset <= 0 0 6317 6317
46 6317 ▶ $N ||= 7;
Condition50%1/2
$N ||= 7
l!l
63170
MC/DC0%0/1
$N ||= 7
$N
Truth table50%1/2
A
A= $N
Aresult
11
00
47 6317 my $ff = ( 1 << $N ) - 1;
48  
49 6317 $$int_ref = $ff & vec( $$buf_ref, $buf_offset, 8 );
50 6317 ▶ return 1 if $$int_ref < $ff;
Branch: 1 branch
BranchTrueFalseTotal
if $$int_ref < $ff 6304 13 6317
51  
52 13 my $l = length($$buf_ref) - $buf_offset - 1;
53  
54 13 for my $i ( 1 .. $l ) {
55 26 ▶ return undef if $i > MAX_INT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 4 0 26 26
56 26 my $s = vec( $$buf_ref, $i + $buf_offset, 8 );
57 26 $$int_ref += ( $s & 0x7f ) << ( $i - 1 ) * 7;
58 26 ▶ return $i + 1 if $s < 0x80;
Branch: 1 branch
BranchTrueFalseTotal
if $s < 128 13 13 26
59 }
60  
61 0 return undef;
62 }
63  
64 102 ▶sub str_encode {
Subroutine str_encode: called CC=2 SCAR=6.9
Pod undocumented
65 102 my $str = shift;
66 102 my $huff_str = huffman_encode($str);
67 102 my $pack;
68 102 ▶ if ( length($huff_str) < length($str) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $huff_str < length $str) { } 77 25 102
69 77 $pack = int_encode( length($huff_str), 7 );
70 77 vec( $pack, 7, 1 ) = 1;
71 77 $pack .= $huff_str;
72 }
73 else {
74 25 $pack = int_encode( length($str), 7 );
75 25 $pack .= $str;
76 }
77 102 return $pack;
78 }
79  
80 # str_decode()
81 # arguments:
82 # buf_ref - ref to buffer with encoded data
83 # buf_offset - offset in buffer
84 # str_ref - ref to scalar where result will be stored
85 # returns: count of read bytes of encoded data
86  
87 103 ▶sub str_decode {
Subroutine str_decode: called CC=4 SCAR=14.3
Pod undocumented
88 103 my ( $buf_ref, $buf_offset, $str_ref ) = @_;
89 103 my $offset = int_decode( $buf_ref, $buf_offset, \my $l, 7 );
90 return undef
91 103 ▶ unless defined $offset
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset and length($$buf_ref) - $buf_offset - $offset >= $l 0 103 103
Condition33%1/3
defined $offset and length($$buf_ref) - $buf_offset - $offset >= $l
!ll&&!rl&&r
00103
MC/DC0%0/2
defined $offset and length($$buf_ref) - $buf_offset - $offset >= $l
defined $offset length($$buf_ref) - $buf_offset - $offset >= $l
Truth table33%1/3
A and B
A= defined $offsetB= length($$buf_ref) - $buf_offset - $offset >= $l
ABresult
0X0
100
111
92 && length($$buf_ref) - $buf_offset - $offset >= $l;
93  
94 103 $$str_ref = substr $$buf_ref, $offset + $buf_offset, $l;
95 103 ▶ $$str_ref = huffman_decode($$str_ref)
Branch: 1 branch
BranchTrueFalseTotal
if vec($$buf_ref, $buf_offset * 8 + 7, 1) == 1 77 26 103
96 if vec( $$buf_ref, $buf_offset * 8 + 7, 1 ) == 1;
97 103 return $offset + $l;
98 }
99  
100 160 ▶sub evict_ht {
Subroutine evict_ht: called CC=2 SCAR=6.9
Pod undocumented
101 160 my ( $context, $size ) = @_;
102 160 my @evicted;
103  
104 160 my $ht = $context->{header_table};
105  
106 160 ▶ while ( $context->{ht_size} + $size > $context->{max_ht_size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"ht_size"} + $size > $context->{"max_ht_size"}) 13 160 173
107 13 my $n = $#$ht;
108 13 my $kv_ref = pop @$ht;
109 $context->{ht_size} -=
110 13 32 + length( $kv_ref->[0] ) + length( $kv_ref->[1] );
111 13 tracer->debug( sprintf "Evicted header [%i] %s = %s\n",
112 $n + 1, @$kv_ref );
113 13 push @evicted, [ $n, @$kv_ref ];
114 }
115 160 return @evicted;
116 }
117  
118 172 ▶sub add_to_ht {
Subroutine add_to_ht: called CC=2 SCAR=6.9
Pod undocumented
119 172 my ( $context, $key, $value ) = @_;
120 172 my $size = length($key) + length($value) + 32;
121 172 ▶ return () if $size > $context->{max_ht_size};
Branch: 1 branch
BranchTrueFalseTotal
if $size > $context->{"max_ht_size"} 12 160 172
122  
123 160 my @evicted = evict_ht( $context, $size );
124  
125 160 my $ht = $context->{header_table};
126 160 my $kv_ref = [ $key, $value ];
127  
128 160 unshift @$ht, $kv_ref;
129 160 $context->{ht_size} += $size;
130 160 return @evicted;
131 }
132  
133 79 ▶sub headers_decode {
Subroutine headers_decode: called CC=31 SCAR=47.2
Pod undocumented
134 79 my ( $con, $buf_ref, $buf_offset, $length, $stream_id ) = @_;
135  
136 79 my $context = $con->decode_context;
137  
138 79 my $ht = $context->{header_table};
139 79 my $eh = $context->{emitted_headers};
140  
141 79 my $offset = 0;
142 79 my $hsize = 0;
143  
144 79 ▶ while ( $offset < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $length) 6229 77 6306
145  
146 6229 my $f = vec( $$buf_ref, $buf_offset + $offset, 8 );
147 6229 tracer->debug( sprintf "\toffset: %d, byte: %02x\n", $offset, $f );
148  
149 # Indexed Header
150 6229 ▶ if ( $f & 0x80 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($f & 128) { } 6143 86 6229
elsif ($f == 64 or $f == 0 or $f == 16) { } 16 70 86
elsif (($f & 192) == 64 or ($f & 240) == 0 or ($f & 240) == 16) { } 70 0 70
elsif (($f & 224) == 32) { } 0 0 0
151 6143 my $size =
152 int_decode( $buf_ref, $buf_offset + $offset, \my $index, 7 );
153 6143 ▶ last unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 0 6143 6143
154  
155 # DECODING ERROR
156 6143 ▶ if ( $index == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == 0) 0 6143 6143
157 0 tracer->error("Indexed header with zero index\n");
158 0 $con->error(COMPRESSION_ERROR);
159 0 return undef;
160 }
161  
162 6143 tracer->debug("\tINDEXED($index) HEADER\t");
163  
164 # Static table or Header Table entry
165 6143 ▶ if ( $index <= @stable ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($index <= @Protocol::HTTP2::HeaderCompression::stable) { } 156 5987 6143
elsif ($index > @Protocol::HTTP2::HeaderCompression::stable + @$ht) { } 0 5987 5987
166 156 my ( $key, $value ) = @{ $stable[ $index - 1 ] };
167 156 $hsize += length($key) + length($value) + 32;
168 156 push @$eh, $key, $value;
169 156 tracer->debug("$key = $value\n");
170 }
171 elsif ( $index > @stable + @$ht ) {
172 0 tracer->error(
173 "Indexed header with index out of header table: "
174 . $index
175 . "\n" );
176 0 $con->error(COMPRESSION_ERROR);
177 0 return undef;
178 }
179 else {
180 5987 my $kv_ref = $ht->[ $index - @stable - 1 ];
181  
182 5987 $hsize += length( $kv_ref->[0] ) + length( $kv_ref->[1] ) + 32;
183 5987 push @$eh, @$kv_ref;
184 5987 tracer->debug("$kv_ref->[0] = $kv_ref->[1]\n");
185 }
186  
187 6143 $offset += $size;
188 }
189  
190 # Literal Header Field - New Name
191 ▶ elsif ( $f == 0x40 || $f == 0x00 || $f == 0x10 ) {
Condition66%4/6
$f == 64 or $f == 0 or $f == 16
l!l&&r!l&&!r
16070
$f == 64 or $f == 0
l!l&&r!l&&!r
16070
MC/DC33%1/3
$f == 64 or $f == 0 or $f == 16
$f == 64 $f == 0 $f == 16
Truth table50%2/4
A or B or C
A= $f == 64B= $f == 0C= $f == 16
ABCresult
1XX1
01X1
0011
0000
192 16 my $key_size =
193 str_decode( $buf_ref, $buf_offset + $offset + 1, \my $key );
194 16 ▶ last unless $key_size;
Branch: 1 branch
BranchTrueFalseTotal
unless $key_size 0 16 16
195  
196 16 ▶ if ( $key_size == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_size == 1) 0 16 16
197 0 tracer->error("Empty literal header name");
198 0 $con->error(COMPRESSION_ERROR);
199 0 return undef;
200 }
201  
202 16 ▶ if ( $key =~ /[^a-z0-9\!\#\$\%\&\'\*\+\-\^\_\`]/ && $key !~ /^\:/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /[^a-z0-9\!\#\$\%\&\'\*\+\-\^\_\`]/ and not $key =~ /^\:/) 0 16 16
Condition33%1/3
$key =~ /[^a-z0-9\!\#\$\%\&\'\*\+\-\^\_\`]/ and not $key =~ /^\:/
!ll&&!rl&&r
1600
MC/DC0%0/2
$key =~ /[^a-z0-9\!\#\$\%\&\'\*\+\-\^\_\`]/ and not $key =~ /^\:/
$key =~ /[^a-z0-9\!\#\$\%\&\'\*\+\-\^\_\`]/ not $key =~ /^\:/
Truth table33%1/3
A and not(B)
A= $key =~ /[^a-z0-9\!\#\$\%\&\'\*\+\-\^\_\`]/B= not $key =~ /^\:/
ABresult
0X0
100
111
203 {
204 0 tracer->warning("Illegal characters in header name");
205 0 $con->stream_error( $stream_id, PROTOCOL_ERROR );
206 0 return undef;
207 }
208  
209 16 my $value_size =
210 str_decode( $buf_ref, $buf_offset + $offset + 1 + $key_size,
211 \my $value );
212 16 ▶ last unless $value_size;
Branch: 1 branch
BranchTrueFalseTotal
unless $value_size 0 16 16
213  
214 # Emitting header
215 16 $hsize += length($key) + length($value) + 32;
216 16 push @$eh, $key, $value;
217  
218 # Add to index
219 16 ▶ if ( $f == 0x40 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f == 64) 16 0 16
220 16 add_to_ht( $context, $key, $value );
221 }
222 16 tracer->debug( sprintf "\tLITERAL(new) HEADER\t%s: %s\n",
223 $key, substr( $value, 0, 30 ) );
224  
225 16 $offset += 1 + $key_size + $value_size;
226 }
227  
228 # Literal Header Field - Indexed Name
229 ▶ elsif (( $f & 0xC0 ) == 0x40
Condition33%2/6
($f & 192) == 64 or ($f & 240) == 0 or ($f & 240) == 16
l!l&&r!l&&!r
7000
($f & 192) == 64 or ($f & 240) == 0
l!l&&r!l&&!r
7000
MC/DC0%0/3
($f & 192) == 64 or ($f & 240) == 0 or ($f & 240) == 16
($f & 192) == 64 ($f & 240) == 0 ($f & 240) == 16
Truth table25%1/4
A or B or C
A= ($f & 192) == 64B= ($f & 240) == 0C= ($f & 240) == 16
ABCresult
1XX1
01X1
0011
0000
230 || ( $f & 0xF0 ) == 0x00
231 || ( $f & 0xF0 ) == 0x10 )
232 {
233 70 ▶ my $size = int_decode( $buf_ref, $buf_offset + $offset,
Branch: 1 branch
BranchTrueFalseTotal
($f & 192) == 64 ? : 70 0 70
234 \my $index, ( $f & 0xC0 ) == 0x40 ? 6 : 4 );
235 70 ▶ last unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 0 70 70
236  
237 70 my $value_size =
238 str_decode( $buf_ref, $buf_offset + $offset + $size, \my $value );
239 70 ▶ last unless $value_size;
Branch: 1 branch
BranchTrueFalseTotal
unless $value_size 0 70 70
240  
241 70 my $key;
242  
243 70 ▶ if ( $index <= @stable ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($index <= @Protocol::HTTP2::HeaderCompression::stable) { } 70 0 70
elsif ($index > @Protocol::HTTP2::HeaderCompression::stable + @$ht) { } 0 0 0
244 70 $key = $stable[ $index - 1 ]->[0];
245 }
246 elsif ( $index > @stable + @$ht ) {
247 0 tracer->error(
248 "Literal header with index out of header table: "
249 . $index
250 . "\n" );
251 0 $con->error(COMPRESSION_ERROR);
252 0 return undef;
253 }
254 else {
255 0 $key = $ht->[ $index - @stable - 1 ]->[0];
256 }
257  
258 # Emitting header
259 70 $hsize += length($key) + length($value) + 32;
260 70 push @$eh, $key, $value;
261  
262 # Add to index
263 70 ▶ if ( ( $f & 0xC0 ) == 0x40 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($f & 192) == 64) 70 0 70
264 70 add_to_ht( $context, $key, $value );
265 }
266 70 tracer->debug("\tLITERAL($index) HEADER\t$key: $value\n");
267  
268 70 $offset += $size + $value_size;
269 }
270  
271 # Encoding Context Update - Maximum Header Table Size change
272 elsif ( ( $f & 0xE0 ) == 0x20 ) {
273 0 my $size =
274 int_decode( $buf_ref, $buf_offset + $offset, \my $ht_size, 5 );
275 0 ▶ last unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 0 0 0
276  
277 # It's not possible to increase size of HEADER_TABLE
278 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($ht_size > $context->{"settings"}{&SETTINGS_HEADER_TABLE_SIZE}) 0 0 0
279 $ht_size > $context->{settings}->{&SETTINGS_HEADER_TABLE_SIZE} )
280 {
281 tracer->error( "Peer attempt to increase "
282 . "maximum header table size higher than current size: "
283 . "$ht_size > "
284 0 . $context->{settings}->{&SETTINGS_HEADER_TABLE_SIZE} );
285 0 $con->error(COMPRESSION_ERROR);
286 0 return undef;
287 }
288 0 ▶ if (@$eh) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$eh) 0 0 0
289 0 tracer->error(
290 "Attempt to change header table size after headers");
291 0 $con->error(COMPRESSION_ERROR);
292 0 return undef;
293 }
294 tracer->debug( "Update header table size from "
295 0 . $context->{max_ht_size} . " to "
296 . $ht_size );
297 0 $context->{max_ht_size} = $ht_size;
298 0 evict_ht( $context, 0 );
299 0 $offset += $size;
300 }
301  
302 # Encoding Error
303 else {
304 0 tracer->error( sprintf( "Unknown header type: %08b", $f ) );
305 0 $con->error(COMPRESSION_ERROR);
306 0 return undef;
307 }
308  
309 # Check header limit
310 6229 ▶ if ( $hsize > $context->{settings}->{&SETTINGS_MAX_HEADER_LIST_SIZE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hsize > $context->{"settings"}{&SETTINGS_MAX_HEADER_LIST_SIZE}) 2 6227 6229
311 2 tracer->error( "Headers size has exceeded the allowed limit: "
312 . $hsize
313 . "\n" );
314 2 $con->error(ENHANCE_YOUR_CALM);
315 2 return undef;
316 }
317  
318 }
319  
320 77 ▶ if ( $offset != $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset != $length) 0 77 77
321 0 tracer->error(
322 "Headers decoding stopped at offset $offset of $length\n");
323 0 $con->error(COMPRESSION_ERROR);
324 0 return undef;
325 }
326  
327 77 return $offset;
328 }
329  
330 81 ▶sub headers_encode {
Subroutine headers_encode: called CC=8 SCAR=20.9
Pod undocumented
331 81 my ( $context, $headers ) = @_;
332 81 my $res = '';
333 81 my $ht = $context->{header_table};
334 81 my $sht = $context->{settings}->{&SETTINGS_HEADER_TABLE_SIZE};
335  
336 # Encode dynamic table size update
337 81 ▶ if ( $context->{max_ht_size} != $sht ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"max_ht_size"} != $sht) 0 81 81
338 0 $res .= int_encode( $sht, 5 );
339 0 vec( $res, 3, 2 ) = 0;
340 0 vec( $res, 5, 1 ) = 1;
341 0 $context->{max_ht_size} = $sht;
342 }
343  
344 HLOOP:
345 81 for my $n ( 0 .. $#$headers / 2 ) {
346 4277 my $header = lc( $headers->[ 2 * $n ] );
347 4277 my $value = $headers->[ 2 * $n + 1 ];
348 4277 my $hdr;
349  
350 4277 tracer->debug("Encoding header: $header = $value\n");
351  
352 4277 for my $i ( 0 .. $#$ht ) {
353 next
354 4312 ▶ unless $ht->[$i]->[0] eq $header
Branch: 1 branch
BranchTrueFalseTotal
unless $ht->[$i][0] eq $header and $ht->[$i][1] eq $value 281 4031 4312
Condition100%3/3
$ht->[$i][0] eq $header and $ht->[$i][1] eq $value
!ll&&!rl&&r
27474031
MC/DC100%2/2
$ht->[$i][0] eq $header and $ht->[$i][1] eq $value
$ht->[$i][0] eq $header $ht->[$i][1] eq $value
Truth table100%3/3
A and B
A= $ht->[$i][0] eq $headerB= $ht->[$i][1] eq $value
ABresult
0X0
100
111
355 && $ht->[$i]->[1] eq $value;
356 4031 $hdr = int_encode( $i + @stable + 1, 7 );
357 4031 vec( $hdr, 7, 1 ) = 1;
358 4031 $res .= $hdr;
359 4031 tracer->debug(
360 "\talready in header table, index " . ( $i + 1 ) . "\n" );
361 4031 next HLOOP;
362 }
363  
364 # 7.1 Indexed header field representation
365 246 ▶ if ( exists $rstable{ $header . ' ' . $value } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $Protocol::HTTP2::HeaderCompression::rstable{$header . ' ' . $value}) { } 160 86 246
elsif (exists $Protocol::HTTP2::HeaderCompression::rstable{$header . ' '}) { } 71 15 86
366 160 $hdr = int_encode( $rstable{ $header . ' ' . $value }, 7 );
367 160 vec( $hdr, 7, 1 ) = 1;
368 tracer->debug( "\tIndexed header "
369 160 . $rstable{ $header . ' ' . $value }
370 . " from table\n" );
371 }
372  
373 # 7.2.1 Literal Header Field with Incremental Indexing
374 # (Indexed Name)
375 elsif ( exists $rstable{ $header . ' ' } ) {
376 71 $hdr = int_encode( $rstable{ $header . ' ' }, 6 );
377 71 vec( $hdr, 3, 2 ) = 1;
378 71 $hdr .= str_encode($value);
379 71 add_to_ht( $context, $header, $value );
380 tracer->debug( "\tLiteral header "
381 71 . $rstable{ $header . ' ' }
382 . " indexed name\n" );
383 }
384  
385 # 7.2.1 Literal Header Field with Incremental Indexing
386 # (New Name)
387 else {
388 15 $hdr = pack( 'C', 0x40 );
389 15 $hdr .= str_encode($header) . str_encode($value);
390 15 add_to_ht( $context, $header, $value );
391 15 tracer->debug("\tLiteral header new name\n");
392 }
393  
394 246 $res .= $hdr;
395 }
396  
397 81 return $res;
398 }
399  
400 13 1;
« HTTP2/Connection.pm ↑ Summary HTTP2/Frame/Push_promise.pm »