HTTP/_Policy.pm

filter: Statementstmt 98.6% 74 / 75 Branchbran 89.6% 52 / 58 Conditioncond 61.5% 40 / 65 MC/DCmcdc 33.3% 11 / 33 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 6 totaltotal 75.8% 188 / 248 CC 54 SCAR 36.9
CC
54
Cov
84%
new
59.2
_copy
6.0
websocket_enabled
4.4
CRAP 40.1
« HTTP/Server.pm ↑ Summary HTTP/_AltSvc.pm »
1 package Unblock::HTTP::_Policy;
2  
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 16 my %NAME = ('http/1.1' => 'http1', h2 => 'http2', h3 => 'http3');
9 16 my @PROTOCOLS = ('http/1.1', 'h2', 'h3');
10  
11 82 ▶sub new {
Subroutine new: called CC=42 SCAR=40.8
Pod undocumented
12 82 my ($class, %options) = @_;
13 82 my $websocket_role = delete $options{_websocket_role};
14 82 ▶ 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 82 82
Condition66%4/6
defined $websocket_role and $websocket_role ne "server" and $websocket_role ne "client"
!ll&&!rl&&r
22600
defined $websocket_role and $websocket_role ne "server"
!ll&&!rl&&r
02260
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 82 my (%engines, %websocket);
18 82 for my $protocol (@PROTOCOLS) {
19 232 my $name = $NAME{$protocol};
20 232 ▶ next unless exists $options{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{$name} 143 89 232
21 89 my $config = delete $options{$name};
22 89 ▶ croak "$name must be a hash reference; omit it to disable the protocol"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 3 86 89
23 unless ref($config) eq 'HASH';
24 86 $config = _copy($config);
25 my $websocket = exists($config->{websocket})
26 85 ▶ ? delete $config->{websocket} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'websocket'} ? : 5 80 85
27 85 ▶ 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 84 85
Condition50%3/6
defined $websocket and not ref $websocket and "$websocket" =~ /\A[01]\z/
!ll&&!rl&&r
0184
defined $websocket and not ref $websocket
!ll&&!rl&&r
0085
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 84 ▶ if ($websocket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($websocket) 3 81 84
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 84 for my $key (keys %$config) {
39 8 ▶ 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 5 8
Condition100%3/3
$key =~ /\Aon_/ or $key =~ /\A(?:transport|quic|protocol|alpn|receive_body)\z/
l!l&&r!l&&!r
125
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 81 $config->{receive_body} = 'buffered';
43 $config->{max_buffered_body_bytes} = 1_048_576
44 81 ▶ unless exists $config->{max_buffered_body_bytes};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $config->{"max_buffered_body_bytes"} 80 1 81
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 81 ▶ && $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 80 81
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
1080
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
0180
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
0081
defined $config->{"max_buffered_body_bytes"} and not ref $config->{"max_buffered_body_bytes"}
!ll&&!rl&&r
0081
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 80 ▶ if ($protocol eq 'h2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol eq "h2") 20 60 80
52 20 ▶ if (exists $config->{settings}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"settings"}) 2 18 20
53 croak 'http2 settings must be a hash reference'
54 2 ▶ unless ref($config->{settings}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config->{"settings"} eq "HASH" 1 1 2
55 }
56 19 ▶ if ($websocket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($websocket) { } 1 18 19
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 # Generic Extended CONNECT is valid without WebSocket.
66 # Caller must opt into SETTINGS_ENABLE_CONNECT_PROTOCOL.
67 $config->{enable_connect_protocol} = 0
68 18 ▶ unless exists $config->{enable_connect_protocol};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $config->{"enable_connect_protocol"} 17 1 18
69 }
70 }
71 79 ▶ if ($protocol eq 'h3') {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol eq "h3") 19 60 79
72 19 ▶ if ($websocket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($websocket) { } 1 18 19
73 croak 'http3 WebSocket requires enable_extended_connect => 1'
74 if exists($config->{enable_extended_connect})
75 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
76 1 $config->{enable_extended_connect} = 1;
77 } else {
78 # RFC 9220 WebSocket is only one Extended CONNECT use.
79 $config->{enable_extended_connect} = 0
80 18 ▶ unless exists $config->{enable_extended_connect};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $config->{"enable_extended_connect"} 17 1 18
81 }
82 }
83 79 $engines{$protocol} = $config;
84 }
85 72 ▶ croak 'unknown protocol option: ' . join(', ', sort keys %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 72 72
86 72 ▶ croak 'enable at least one of http1, http2, or http3' unless keys %engines;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %engines 1 71 72
87 71 return bless { engines => \%engines, websocket => \%websocket }, $class;
88 }
89  
90 2 ▶sub protocols {
Subroutine protocols: called CC=1 SCAR=0.0
Pod undocumented
91 2 my ($self) = @_;
92 2 return grep { exists $self->{engines}{$_} } @PROTOCOLS;
93 }
94  
95 54 ▶sub alpn_protocols {
Subroutine alpn_protocols: called CC=1 SCAR=0.0
Pod undocumented
96 54 my ($self) = @_;
97 54 return grep { exists $self->{engines}{$_} } ('h2', 'http/1.1');
98 }
99  
100 2226 ▶sub enabled {
Subroutine enabled: called CC=4 SCAR=13.9
Pod undocumented
101 2226 my ($self, $protocol) = @_;
102 return defined($protocol) && !ref($protocol)
103 2226 ▶ && exists($self->{engines}{$protocol}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $protocol && !ref($protocol) && exists $self->{'engines'}{$protocol} ? : 810 1416 2226
Condition100%6/6
defined $protocol and not ref $protocol and exists $self->{"engines"}{$protocol}
!ll&&!rl&&r
21414810
defined $protocol and not ref $protocol
!ll&&!rl&&r
112224
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
104 }
105  
106 59 ▶sub websocket_enabled {
Subroutine websocket_enabled: called CC=4 SCAR=14.9
Pod undocumented
107 59 my ($self, $protocol) = @_;
108 return defined($protocol) && !ref($protocol)
109 59 ▶ && $self->{websocket}{$protocol} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $protocol && !ref($protocol) && $self->{'websocket'}{$protocol} ? : 3 56 59
Condition50%3/6
defined $protocol and not ref $protocol and $self->{"websocket"}{$protocol}
!ll&&!rl&&r
0563
defined $protocol and not ref $protocol
!ll&&!rl&&r
0059
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
110 }
111  
112 95 ▶sub options {
Subroutine options: called CC=2 SCAR=6.9
Pod undocumented
113 95 my ($self, $protocol) = @_;
114 95 ▶ croak "HTTP protocol '$protocol' is disabled" unless $self->enabled($protocol);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enabled($protocol) 3 92 95
115 92 return _copy($self->{engines}{$protocol});
116 }
117  
118 # Copy configuration containers at construction and attachment. Host objects
119 # and callbacks retain their identity; no copying happens on the byte path.
120 428 ▶sub _copy {
Subroutine _copy: called CC=6 SCAR=17.9
121 428 my ($value, $active) = @_;
122 428 ▶ 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" 245 183 428
Condition100%3/3
ref $value eq "HASH" or ref $value eq "ARRAY"
l!l&&r!l&&!r
1821245
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
123 183 ▶ $active ||= {};
Condition100%2/2
$active ||= {}
l!l
5178
MC/DC100%1/1
$active ||= {}
$active
Truth table100%2/2
A
A= $active
Aresult
11
00
124 183 my $id = refaddr($value);
125 croak 'protocol configuration must not contain circular containers'
126 183 ▶ if $active->{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $active->{$id} 1 182 183
127 182 local $active->{$id} = 1;
128 182 ▶ return { map { $_ => _copy($value->{$_}, $active) } keys %$value }
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 181 1 182
129 if ref($value) eq 'HASH';
130 0 return [ map { _copy($_, $active) } @$value ];
131 }
132  
133 16 1;
« HTTP/Server.pm ↑ Summary HTTP/_AltSvc.pm »