HTTP/_Policy.pm

filter: Statementstmt 98.7% 77 / 78 Branchbran 90.0% 54 / 60 Conditioncond 63.2% 43 / 68 MC/DCmcdc 37.1% 13 / 35 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 6 totaltotal 76.7% 198 / 258 CC 56 SCAR 37.2
CC
56
Cov
84%
new
59.8
_copy
6.0
websocket_enabled
4.4
CRAP 41.1
« HTTP/Server.pm ↑ Summary HTTP/_AltSvc.pm »
1 package Unblock::HTTP::_Policy;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 my %NAME = ('http/1.1' => 'http1', h2 => 'http2', h3 => 'http3');
9 14 my @PROTOCOLS = ('http/1.1', 'h2', 'h3');
10  
11 85 ▶sub new {
Subroutine new: called CC=44 SCAR=40.9
Pod undocumented
12 85 my ($class, %options) = @_;
13 85 my $websocket_role = delete $options{_websocket_role};
14 85 ▶ croak 'invalid internal WebSocket policy role'
Branch: 1 branch
BranchTrueFalseTotal
if defined $websocket_role and $websocket_role ne "server" and $websocket_role ne "client" 0 85 85
Condition66%4/6
defined $websocket_role and $websocket_role ne "server" and $websocket_role ne "client"
!ll&&!rl&&r
32530
defined $websocket_role and $websocket_role ne "server"
!ll&&!rl&&r
03253
MC/DC0%0/3
defined $websocket_role and $websocket_role ne "server" and $websocket_role ne "client"
defined $websocket_role $websocket_role ne "server" $websocket_role ne "client"
Truth table50%2/4
A and B and C
A= defined $websocket_roleB= $websocket_role ne "server"C= $websocket_role ne "client"
ABCresult
0XX0
10X0
1100
1111
15 if defined($websocket_role)
16 && $websocket_role ne 'server' && $websocket_role ne 'client';
17 85 my (%engines, %websocket);
18 85 for my $protocol (@PROTOCOLS) {
19 239 my $name = $NAME{$protocol};
20 239 ▶ next unless exists $options{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{$name} 137 102 239
21 102 my $config = delete $options{$name};
22 102 ▶ croak "$name must be a hash reference; omit it to disable the protocol"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 3 99 102
23 unless ref($config) eq 'HASH';
24 99 $config = _copy($config);
25 my $websocket = exists($config->{websocket})
26 98 ▶ ? delete $config->{websocket} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'websocket'} ? : 5 93 98
27 98 ▶ croak "$name websocket must be 0 or 1"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $websocket and not ref $websocket and "$websocket" =~ /\A[01]\z/ 1 97 98
Condition50%3/6
defined $websocket and not ref $websocket and "$websocket" =~ /\A[01]\z/
!ll&&!rl&&r
0197
defined $websocket and not ref $websocket
!ll&&!rl&&r
0098
MC/DC33%1/3
defined $websocket and not ref $websocket and "$websocket" =~ /\A[01]\z/
defined $websocket not ref $websocket "$websocket" =~ /\A[01]\z/
Truth table50%2/4
A and not(B) and C
A= defined $websocketB= not ref $websocketC= "$websocket" =~ /\A[01]\z/
ABCresult
0XX0
10X0
1100
1111
28 unless defined($websocket) && !ref($websocket)
29 && "$websocket" =~ /\A[01]\z/;
30 97 ▶ if ($websocket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($websocket) 3 94 97
31 3 ▶ croak "$name WebSocket attachment is not implemented yet; see docs/IMPLEMENTATION.md"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $websocket_role and $protocol eq "http/1.1" || $protocol eq "h2" || $protocol eq "h3" 0 3 3
Condition66%6/9
defined $websocket_role and $protocol eq "http/1.1" || $protocol eq "h2" || $protocol eq "h3"
!ll&&!rl&&r
003
$protocol eq "http/1.1" or $protocol eq "h2" or $protocol eq "h3"
l!l&&r!l&&!r
210
$protocol eq "http/1.1" or $protocol eq "h2"
l!l&&r!l&&!r
111
MC/DC0%0/4
defined $websocket_role and $protocol eq "http/1.1" || $protocol eq "h2" || $protocol eq "h3"
defined $websocket_role $protocol eq "http/1.1" $protocol eq "h2" $protocol eq "h3"
Truth table60%3/5
A and B or C or D
A= defined $websocket_roleB= $protocol eq "http/1.1"C= $protocol eq "h2"D= $protocol eq "h3"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
32 unless defined($websocket_role)
33 && ($protocol eq 'http/1.1'
34 || $protocol eq 'h2'
35 || $protocol eq 'h3');
36 3 $websocket{$protocol} = 1;
37 }
38 97 for my $key (keys %$config) {
39 29 ▶ croak "$name option '$key' is managed by Unblock::HTTP"
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\Aon_/ or $key =~ /\A(?:transport|quic|protocol|alpn|receive_body)\z/ 3 26 29
Condition100%3/3
$key =~ /\Aon_/ or $key =~ /\A(?:transport|quic|protocol|alpn|receive_body)\z/
l!l&&r!l&&!r
1226
MC/DC100%2/2
$key =~ /\Aon_/ or $key =~ /\A(?:transport|quic|protocol|alpn|receive_body)\z/
$key =~ /\Aon_/ $key =~ /\A(?:transport|quic|protocol|alpn|receive_body)\z/
Truth table100%3/3
A or B
A= $key =~ /\Aon_/B= $key =~ /\A(?:transport|quic|protocol|alpn|receive_body)\z/
ABresult
1X1
011
000
40 if $key =~ /\Aon_/ || $key =~ /\A(?:transport|quic|protocol|alpn|receive_body)\z/;
41 }
42 94 $config->{receive_body} = 'buffered';
43 $config->{max_buffered_body_bytes} = 1_048_576
44 94 ▶ unless exists $config->{max_buffered_body_bytes};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $config->{"max_buffered_body_bytes"} 73 21 94
45 croak "$name max_buffered_body_bytes must be a positive integer"
46 unless defined($config->{max_buffered_body_bytes})
47 && !ref($config->{max_buffered_body_bytes})
48 && $config->{max_buffered_body_bytes} =~ /\A[0-9]+\z/
49 && $config->{max_buffered_body_bytes} > 0
50 94 ▶ && $config->{max_buffered_body_bytes} <= (~0);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"max_buffered_body_bytes"} and not ref $config->{"max_buffered_body_bytes"} and $config->{"max_buffered_body_bytes"} =~ /\A[0-9]+\z/ and $config->{"max_buffered_body_bytes"} > 0 and $config->{"max_buffered_body_bytes"} <= '18446744073709551615' 1 93 94
Condition50%6/12
defined $config->{"max_buffered_body_bytes"} and not ref $config->{"max_buffered_body_bytes"} and $config->{"max_buffered_body_bytes"} =~ /\A[0-9]+\z/ and $config->{"max_buffered_body_bytes"} > 0 and $config->{"max_buffered_body_bytes"} <= '18446744073709551615'
!ll&&!rl&&r
1093
defined $config->{"max_buffered_body_bytes"} and not ref $config->{"max_buffered_body_bytes"} and $config->{"max_buffered_body_bytes"} =~ /\A[0-9]+\z/ and $config->{"max_buffered_body_bytes"} > 0
!ll&&!rl&&r
0193
defined $config->{"max_buffered_body_bytes"} and not ref $config->{"max_buffered_body_bytes"} and $config->{"max_buffered_body_bytes"} =~ /\A[0-9]+\z/
!ll&&!rl&&r
0094
defined $config->{"max_buffered_body_bytes"} and not ref $config->{"max_buffered_body_bytes"}
!ll&&!rl&&r
0094
MC/DC20%1/5
defined $config->{"max_buffered_body_bytes"} and not ref $config->{"max_buffered_body_bytes"} and $config->{"max_buffered_body_bytes"} =~ /\A[0-9]+\z/ and $config->{"max_buffered_body_bytes"} > 0 and $config->{"max_buffered_body_bytes"} <= '18446744073709551615'
defined $config->{"max_buffered_body_bytes"} not ref $config->{"max_buffered_body_bytes"} $config->{"max_buffered_body_bytes"} =~ /\A[0-9]+\z/ $config->{"max_buffered_body_bytes"} > 0 $config->{"max_buffered_body_bytes"} <= '18446744073709551615'
Truth table33%2/6
A and not(B) and C and D and E
A= defined $config->{"max_buffered_body_bytes"}B= not ref $config->{"max_buffered_body_bytes"}C= $config->{"max_buffered_body_bytes"} =~ /\A[0-9]+\z/D= $config->{"max_buffered_body_bytes"} > 0E= $config->{"max_buffered_body_bytes"} <= '18446744073709551615'
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
51 93 ▶ if ($protocol eq 'h2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol eq "h2") 31 62 93
52 31 ▶ if (exists $config->{settings}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"settings"}) 3 28 31
53 croak 'http2 settings must be a hash reference'
54 3 ▶ unless ref($config->{settings}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config->{"settings"} eq "HASH" 1 2 3
55 }
56 30 ▶ if ($websocket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($websocket) { } 1 29 30
57 croak 'http2 WebSocket requires enable_connect_protocol => 1'
58 if exists($config->{enable_connect_protocol})
59 1 ▶ && !$config->{enable_connect_protocol};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{"enable_connect_protocol"} and not $config->{"enable_connect_protocol"} 0 1 1
Condition33%1/3
exists $config->{"enable_connect_protocol"} and not $config->{"enable_connect_protocol"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $config->{"enable_connect_protocol"} and not $config->{"enable_connect_protocol"}
exists $config->{"enable_connect_protocol"} not $config->{"enable_connect_protocol"}
Truth table33%1/3
A and not(B)
A= exists $config->{"enable_connect_protocol"}B= not $config->{"enable_connect_protocol"}
ABresult
0X0
100
111
60 croak 'http2 WebSocket conflicts with settings 8 => 0'
61 if exists($config->{settings}) && exists($config->{settings}{8})
62 1 ▶ && !$config->{settings}{8};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{"settings"} and exists $config->{"settings"}{8} and not $config->{"settings"}{8} 0 1 1
Condition33%2/6
exists $config->{"settings"} and exists $config->{"settings"}{8} and not $config->{"settings"}{8}
!ll&&!rl&&r
100
exists $config->{"settings"} and exists $config->{"settings"}{8}
!ll&&!rl&&r
100
MC/DC0%0/3
exists $config->{"settings"} and exists $config->{"settings"}{8} and not $config->{"settings"}{8}
exists $config->{"settings"} exists $config->{"settings"}{8} not $config->{"settings"}{8}
Truth table25%1/4
A and B and not(C)
A= exists $config->{"settings"}B= exists $config->{"settings"}{8}C= not $config->{"settings"}{8}
ABCresult
0XX0
10X0
1100
1111
63 1 $config->{enable_connect_protocol} = 1;
64 } else {
65 croak 'http2 enable_connect_protocol conflicts with disabled WebSocket support'
66 29 ▶ if $config->{enable_connect_protocol};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"enable_connect_protocol"} 1 28 29
67 croak 'http2 SETTINGS_ENABLE_CONNECT_PROTOCOL conflicts with disabled WebSocket support'
68 28 ▶ if exists($config->{settings}) && $config->{settings}{8};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{"settings"} and $config->{"settings"}{8} 1 27 28
Condition100%3/3
exists $config->{"settings"} and $config->{"settings"}{8}
!ll&&!rl&&r
2611
MC/DC100%2/2
exists $config->{"settings"} and $config->{"settings"}{8}
exists $config->{"settings"} $config->{"settings"}{8}
Truth table100%3/3
A and B
A= exists $config->{"settings"}B= $config->{"settings"}{8}
ABresult
0X0
100
111
69 27 $config->{enable_connect_protocol} = 0;
70 }
71 }
72 90 ▶ if ($protocol eq 'h3') {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol eq "h3") 17 73 90
73 17 ▶ if ($websocket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($websocket) { } 1 16 17
74 croak 'http3 WebSocket requires enable_extended_connect => 1'
75 if exists($config->{enable_extended_connect})
76 1 ▶ && !$config->{enable_extended_connect};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{"enable_extended_connect"} and not $config->{"enable_extended_connect"} 0 1 1
Condition33%1/3
exists $config->{"enable_extended_connect"} and not $config->{"enable_extended_connect"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $config->{"enable_extended_connect"} and not $config->{"enable_extended_connect"}
exists $config->{"enable_extended_connect"} not $config->{"enable_extended_connect"}
Truth table33%1/3
A and not(B)
A= exists $config->{"enable_extended_connect"}B= not $config->{"enable_extended_connect"}
ABresult
0X0
100
111
77 1 $config->{enable_extended_connect} = 1;
78 } else {
79 croak 'http3 enable_extended_connect conflicts with disabled WebSocket support'
80 16 ▶ if $config->{enable_extended_connect};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"enable_extended_connect"} 1 15 16
81 15 $config->{enable_extended_connect} = 0;
82 }
83 }
84 89 $engines{$protocol} = $config;
85 }
86 72 ▶ croak 'unknown protocol option: ' . join(', ', sort keys %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 72 72
87 72 ▶ croak 'enable at least one of http1, http2, or http3' unless keys %engines;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %engines 1 71 72
88 71 return bless { engines => \%engines, websocket => \%websocket }, $class;
89 }
90  
91 2 ▶sub protocols {
Subroutine protocols: called CC=1 SCAR=0.0
Pod undocumented
92 2 my ($self) = @_;
93 2 return grep { exists $self->{engines}{$_} } @PROTOCOLS;
94 }
95  
96 50 ▶sub alpn_protocols {
Subroutine alpn_protocols: called CC=1 SCAR=0.0
Pod undocumented
97 50 my ($self) = @_;
98 50 return grep { exists $self->{engines}{$_} } ('h2', 'http/1.1');
99 }
100  
101 2191 ▶sub enabled {
Subroutine enabled: called CC=4 SCAR=13.9
Pod undocumented
102 2191 my ($self, $protocol) = @_;
103 return defined($protocol) && !ref($protocol)
104 2191 ▶ && exists($self->{engines}{$protocol}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $protocol && !ref($protocol) && exists $self->{'engines'}{$protocol} ? : 792 1399 2191
Condition100%6/6
defined $protocol and not ref $protocol and exists $self->{"engines"}{$protocol}
!ll&&!rl&&r
21397792
defined $protocol and not ref $protocol
!ll&&!rl&&r
112189
MC/DC100%3/3
defined $protocol and not ref $protocol and exists $self->{"engines"}{$protocol}
defined $protocol not ref $protocol exists $self->{"engines"}{$protocol}
Truth table100%4/4
A and not(B) and C
A= defined $protocolB= not ref $protocolC= exists $self->{"engines"}{$protocol}
ABCresult
0XX0
10X0
1100
1111
105 }
106  
107 114 ▶sub websocket_enabled {
Subroutine websocket_enabled: called CC=4 SCAR=14.9
Pod undocumented
108 114 my ($self, $protocol) = @_;
109 return defined($protocol) && !ref($protocol)
110 114 ▶ && $self->{websocket}{$protocol} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $protocol && !ref($protocol) && $self->{'websocket'}{$protocol} ? : 3 111 114
Condition50%3/6
defined $protocol and not ref $protocol and $self->{"websocket"}{$protocol}
!ll&&!rl&&r
01113
defined $protocol and not ref $protocol
!ll&&!rl&&r
00114
MC/DC33%1/3
defined $protocol and not ref $protocol and $self->{"websocket"}{$protocol}
defined $protocol not ref $protocol $self->{"websocket"}{$protocol}
Truth table50%2/4
A and not(B) and C
A= defined $protocolB= not ref $protocolC= $self->{"websocket"}{$protocol}
ABCresult
0XX0
10X0
1100
1111
111 }
112  
113 98 ▶sub options {
Subroutine options: called CC=2 SCAR=6.9
Pod undocumented
114 98 my ($self, $protocol) = @_;
115 98 ▶ croak "HTTP protocol '$protocol' is disabled" unless $self->enabled($protocol);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled($protocol) 3 95 98
116 95 return _copy($self->{engines}{$protocol});
117 }
118  
119 # Copy configuration containers at construction and attachment. Host objects
120 # and callbacks retain their identity; no copying happens on the byte path.
121 474 ▶sub _copy {
Subroutine _copy: called CC=6 SCAR=17.9
122 474 my ($value, $active) = @_;
123 474 ▶ return $value unless ref($value) eq 'HASH' || ref($value) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" or ref $value eq "ARRAY" 274 200 474
Condition100%3/3
ref $value eq "HASH" or ref $value eq "ARRAY"
l!l&&r!l&&!r
1991274
MC/DC100%2/2
ref $value eq "HASH" or ref $value eq "ARRAY"
ref $value eq "HASH" ref $value eq "ARRAY"
Truth table100%3/3
A or B
A= ref $value eq "HASH"B= ref $value eq "ARRAY"
ABresult
1X1
011
000
124 200 ▶ $active ||= {};
Condition100%2/2
$active ||= {}
l!l
6194
MC/DC100%1/1
$active ||= {}
$active
Truth table100%2/2
A
A= $active
Aresult
11
00
125 200 my $id = refaddr($value);
126 croak 'protocol configuration must not contain circular containers'
127 200 ▶ if $active->{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $active->{$id} 1 199 200
128 199 local $active->{$id} = 1;
129 199 ▶ return { map { $_ => _copy($value->{$_}, $active) } keys %$value }
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 198 1 199
130 if ref($value) eq 'HASH';
131 1 return [ map { _copy($_, $active) } @$value ];
132 }
133  
134 14 1;
« HTTP/Server.pm ↑ Summary HTTP/_AltSvc.pm »