HTTP2/Upgrade.pm

filter: Statementstmt 98.5% 66 / 67 Branchbran 84.3% 27 / 32 Conditioncond 71.4% 15 / 21 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 4 totaltotal 84.4% 125 / 148 CC 24 SCAR 22.0
CC
24
Cov
90%
decode_upgrade_request
13.9
decode_upgrade_response
10.0
upgrade_request
3.0
CRAP 9.0
« HTTP2/Frame/Settings.pm ↑ Summary HTTP2/Server.pm »
1 package Protocol::HTTP2::Upgrade;
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;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Protocol::HTTP2::Constants qw(:frame_types :errors :states);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Protocol::HTTP2::Trace qw(tracer);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use MIME::Base64 qw(encode_base64url decode_base64url);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #use re 'debug';
10 13 my $end_headers_re = qr/\G.+?\x0d?\x0a\x0d?\x0a/s;
11 13 my $header_re = qr/\G[ \t]*(.+?)[ \t]*\:[ \t]*(.+?)[ \t]*\x0d?\x0a/;
12  
13 1 ▶sub upgrade_request {
Subroutine upgrade_request: called CC=3 SCAR=11.0
Pod undocumented
14 1 my ( $con, %h ) = @_;
15 my $request = sprintf "%s %s HTTP/1.1\x0d\x0aHost: %s\x0d\x0a",
16 $h{':method'}, $h{':path'},
17 1 $h{':authority'};
18 1 ▶ while ( my ( $h, $v ) = splice( @{ $h{headers} }, 0, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@{$h{"headers"};}, 0, 2)) 2 1 3
19 2 ▶ next if grep { lc($h) eq $_ } (qw(connection upgrade http2-settings));
Branch: 1 branch
BranchTrueFalseTotal
if grep {lc $h eq $_;} "connection", "upgrade", "http2-settings" 0 2 2
20 2 $request .= $h . ': ' . $v . "\x0d\x0a";
21 }
22 1 $request .= join "\x0d\x0a",
23 'Connection: Upgrade, HTTP2-Settings',
24 'Upgrade: ' . Protocol::HTTP2::ident_plain,
25 'HTTP2-Settings: '
26 . encode_base64url( $con->frame_encode( SETTINGS, 0, 0, {} ) ),
27 '', '';
28 }
29  
30 0 ▶sub upgrade_response {
Subroutine upgrade_response: not called CC=1 SCAR=6.9
Pod undocumented
31  
32 0 join "\x0d\x0a",
33 "HTTP/1.1 101 Switching Protocols",
34 "Connection: Upgrade",
35 "Upgrade: " . Protocol::HTTP2::ident_plain,
36 "", "";
37  
38 }
39  
40 1 ▶sub decode_upgrade_request {
Subroutine decode_upgrade_request: called CC=13 SCAR=26.3
Pod undocumented
41 1 my ( $con, $buf_ref, $buf_offset, $headers_ref ) = @_;
42  
43 1 pos($$buf_ref) = $buf_offset;
44  
45 # Search end of headers
46 1 ▶ return 0 if $$buf_ref !~ /$end_headers_re/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $$buf_ref =~ /$end_headers_re/g 0 1 1
47 1 my $end_headers_pos = pos($$buf_ref) - $buf_offset;
48  
49 1 pos($$buf_ref) = $buf_offset;
50  
51 # Request
52 1 ▶ return undef if $$buf_ref !~ m#\G(\w+) ([^ ]+) HTTP/1\.1\x0d?\x0a#g;
Branch: 1 branch
BranchTrueFalseTotal
unless $$buf_ref =~ m[\G(\w+) ([^ ]+) HTTP/1\.1\x0d?\x0a]g 0 1 1
53 1 my ( $method, $uri ) = ( $1, $2 );
54  
55 # TODO: remove after http2 -> http/1.1 headers conversion implemented
56 1 push @$headers_ref, ":method", $method;
57 1 push @$headers_ref, ":path", $uri;
58 1 push @$headers_ref, ":scheme", 'http';
59  
60 1 my $success = 0;
61  
62 # Parse headers
63 1 ▶ while ( $success != 0b111 && $$buf_ref =~ /$header_re/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success != 7 and $$buf_ref =~ /$header_re/cg) 4 1 5
Condition66%2/3
$success != 7 and $$buf_ref =~ /$header_re/cg
!ll&&!rl&&r
104
MC/DC50%1/2
$success != 7 and $$buf_ref =~ /$header_re/cg
$success != 7 $$buf_ref =~ /$header_re/cg
Truth table66%2/3
A and B
A= $success != 7B= $$buf_ref =~ /$header_re/cg
ABresult
0X0
100
111
64 4 my ( $header, $value ) = ( lc($1), $2 );
65  
66 4 ▶ if ( $header eq "connection" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($header eq 'connection') { } 1 3 4
elsif ($header eq 'upgrade' and grep {$_ eq Protocol::HTTP2::ident_plain();} split(/\s*,\s*/, $value, 0)) { } 1 2 3
elsif ($header eq 'http2-settings' and defined $con->frame_decode(\decode_base64url($value), 0)) { } 1 1 2
67 1 my %h = map { $_ => 1 } split /\s*,\s*/, lc($value);
68 $success |= 0b001
69 1 ▶ if exists $h{'upgrade'} && exists $h{'http2-settings'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $h{"upgrade"} and exists $h{"http2-settings"} 1 0 1
Condition33%1/3
exists $h{"upgrade"} and exists $h{"http2-settings"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $h{"upgrade"} and exists $h{"http2-settings"}
exists $h{"upgrade"} exists $h{"http2-settings"}
Truth table33%1/3
A and B
A= exists $h{"upgrade"}B= exists $h{"http2-settings"}
ABresult
0X0
100
111
70 }
71 ▶ elsif (
Condition66%2/3
$header eq "upgrade" and grep {$_ eq Protocol::HTTP2::ident_plain();} split(/\s*,\s*/, $value, 0)
!ll&&!rl&&r
201
MC/DC50%1/2
$header eq "upgrade" and grep {$_ eq Protocol::HTTP2::ident_plain();} split(/\s*,\s*/, $value, 0)
$header eq "upgrade" grep {$_ eq Protocol::HTTP2::ident_plain();} split(/\s*,\s*/, $value, 0)
Truth table66%2/3
A and B
A= $header eq "upgrade"B= grep {$_ eq Protocol::HTTP2::ident_plain();} split(/\s*,\s*/, $value, 0)
ABresult
0X0
100
111
72 1 $header eq "upgrade" && grep { $_ eq Protocol::HTTP2::ident_plain }
73 split /\s*,\s*/,
74 $value
75 )
76 {
77 1 $success |= 0b010;
78 }
79 ▶ elsif ( $header eq "http2-settings"
Condition66%2/3
$header eq "http2-settings" and defined $con->frame_decode(\decode_base64url($value), 0)
!ll&&!rl&&r
101
MC/DC50%1/2
$header eq "http2-settings" and defined $con->frame_decode(\decode_base64url($value), 0)
$header eq "http2-settings" defined $con->frame_decode(\decode_base64url($value), 0)
Truth table66%2/3
A and B
A= $header eq "http2-settings"B= defined $con->frame_decode(\decode_base64url($value), 0)
ABresult
0X0
100
111
80 && defined $con->frame_decode( \decode_base64url($value), 0 ) )
81 {
82 1 $success |= 0b100;
83 }
84 else {
85 1 push @$headers_ref, $header, $value;
86 }
87 }
88  
89 1 ▶ return undef unless $success == 0b111;
Branch: 1 branch
BranchTrueFalseTotal
unless $success == 7 0 1 1
90  
91 # TODO: method POST also can contain data...
92  
93 1 return $end_headers_pos;
94  
95 }
96  
97 5 ▶sub decode_upgrade_response {
Subroutine decode_upgrade_response: called CC=10 SCAR=23.0
Pod undocumented
98 5 my ( $con, $buf_ref, $buf_offset ) = @_;
99  
100 5 pos($$buf_ref) = $buf_offset;
101  
102 # Search end of headers
103 5 ▶ return 0 if $$buf_ref !~ /$end_headers_re/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $$buf_ref =~ /$end_headers_re/g 1 4 5
104 4 my $end_headers_pos = pos($$buf_ref) - $buf_offset;
105  
106 4 pos($$buf_ref) = $buf_offset;
107  
108 # Switch Protocols failed
109 4 ▶ return undef if $$buf_ref !~ m#\GHTTP/1\.1 101 .+?\x0d?\x0a#g;
Branch: 1 branch
BranchTrueFalseTotal
unless $$buf_ref =~ m[\GHTTP/1\.1 101 .+?\x0d?\x0a]g 2 2 4
110  
111 2 my $success = 0;
112  
113 # Parse headers
114 2 ▶ while ( $success != 0b11 && $$buf_ref =~ /$header_re/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success != 3 and $$buf_ref =~ /$header_re/cg) 6 2 8
Condition100%3/3
$success != 3 and $$buf_ref =~ /$header_re/cg
!ll&&!rl&&r
116
MC/DC100%2/2
$success != 3 and $$buf_ref =~ /$header_re/cg
$success != 3 $$buf_ref =~ /$header_re/cg
Truth table100%3/3
A and B
A= $success != 3B= $$buf_ref =~ /$header_re/cg
ABresult
0X0
100
111
115 6 my ( $header, $value ) = ( lc($1), $2 );
116  
117 6 ▶ if ( $header eq "connection" && lc($value) eq "upgrade" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($header eq 'connection' and lc $value eq 'upgrade') { } 2 4 6
elsif ($header eq 'upgrade' and $value eq Protocol::HTTP2::ident_plain()) { } 1 5 6
Condition66%2/3
$header eq "connection" and lc $value eq "upgrade"
!ll&&!rl&&r
402
MC/DC50%1/2
$header eq "connection" and lc $value eq "upgrade"
$header eq "connection" lc $value eq "upgrade"
Truth table66%2/3
A and B
A= $header eq "connection"B= lc $value eq "upgrade"
ABresult
0X0
100
111
118 2 $success |= 0b01;
119 }
120 ▶ elsif ( $header eq "upgrade" && $value eq Protocol::HTTP2::ident_plain )
Condition100%3/3
$header eq "upgrade" and $value eq Protocol::HTTP2::ident_plain()
!ll&&!rl&&r
211
MC/DC100%2/2
$header eq "upgrade" and $value eq Protocol::HTTP2::ident_plain()
$header eq "upgrade" $value eq Protocol::HTTP2::ident_plain()
Truth table100%3/3
A and B
A= $header eq "upgrade"B= $value eq Protocol::HTTP2::ident_plain()
ABresult
0X0
100
111
121 {
122 1 $success |= 0b10;
123 }
124 }
125  
126 2 ▶ return undef unless $success == 0b11;
Branch: 1 branch
BranchTrueFalseTotal
unless $success == 3 1 1 2
127  
128 1 return $end_headers_pos;
129 }
130  
131 13 1;
« HTTP2/Frame/Settings.pm ↑ Summary HTTP2/Server.pm »