blib/lib/Unblock/WebSocket/Handshake.pm

filter: Statementstmt 95.6% 375 / 392 Branchbran 74.1% 221 / 298 Conditioncond 54.8% 91 / 166 MC/DCmcdc 22.3% 21 / 94 Subroutinesub 97.0% 33 / 34 Podpod 100% 9 / 9 totaltotal 75.5% 750 / 993 CC 224 SCAR 35.2
CC
224
Cov
80%
client_request
104.2
_quoted_extension_value
30.0
_checked_pmd_option
29.6
CRAP 33.9
↑ Summary blib/lib/Unblock/WebSocket/_Frame.pm »
1 package Unblock::WebSocket::Handshake;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = '0.02';
7 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Digest::SHA qw(sha1);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use MIME::Base64 qw(decode_base64 encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Uniform::HTTP::FastPath 0.05 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use Unblock::WebSocket::_Random ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 my $GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
16 6 my $TOKEN_BODY = qr/[!#\$%&'*+\-.^_`|~0-9A-Za-z]+/;
17 6 my $TOKEN_RE = qr/\A$TOKEN_BODY\z/;
18  
19 242 ▶sub _trim {
Subroutine _trim: called CC=1 SCAR=0.0
20 242 my ($value) = @_;
21 242 $value =~ s/\A[ \t]+//;
22 242 $value =~ s/[ \t]+\z//;
23 242 return $value;
24 }
25  
26 47 ▶sub _fast_view {
Subroutine _fast_view: called CC=2 SCAR=6.9
27 47 my ($message) = @_;
28 47 ▶ return unless Uniform::HTTP::FastPath::can_view($message);
Branch: 1 branch
BranchTrueFalseTotal
unless Uniform::HTTP::FastPath::can_view($message) 2 45 47
29 45 return Uniform::HTTP::FastPath::view($message);
30 }
31  
32 76 ▶sub _message_value {
Subroutine _message_value: called CC=2 SCAR=6.9
33 76 my ($message, $view, $slot, $method) = @_;
34 76 ▶ return $view ? $view->[$slot] : $message->$method();
Branch: 1 branch
BranchTrueFalseTotal
$view ? : 73 3 76
35 }
36  
37 186 ▶sub _header_values {
Subroutine _header_values: called CC=6 SCAR=17.9
38 186 my ($message, $wanted, $view) = @_;
39 186 my @value;
40  
41 186 ▶ if ($view) {
Branch: 1 branch
BranchTrueFalseTotal
if ($view) 176 10 186
42 176 my $headers =
43 $view->[Uniform::HTTP::FastPath::SLOT_HEADERS()];
44 176 for my $field (@$headers) {
45 762 ▶ push @value, $field->[1]
Branch: 1 branch
BranchTrueFalseTotal
if lc $field->[0] eq lc $wanted 139 623 762
46 if lc($field->[0]) eq lc($wanted);
47 }
48 176 return @value;
49 }
50  
51 10 for my $index (0 .. $message->header_count - 1) {
52 50 my $name = $message->header_name($index);
53 50 ▶ push @value, $message->header_value($index)
Branch: 1 branch
BranchTrueFalseTotal
if lc $name eq lc $wanted 9 41 50
54 if lc($name) eq lc($wanted);
55 }
56 10 return @value;
57 }
58  
59 84 ▶sub _tokens {
Subroutine _tokens: called CC=5 SCAR=16.3
60 84 my ($where, @value) = @_;
61 84 my @token;
62 84 for my $value (@value) {
63 71 for my $token (split /,/, $value, -1) {
64 74 $token = _trim($token);
65 74 ▶ croak "$where contains an empty token" if $token eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $token eq "" 0 74 74
66 74 ▶ croak "$where contains an invalid token '$token'"
Branch: 1 branch
BranchTrueFalseTotal
unless $token =~ /$TOKEN_RE/ 0 74 74
67 if $token !~ $TOKEN_RE;
68 74 push @token, $token;
69 }
70 }
71 84 return @token;
72 }
73  
74 49 ▶sub _singleton {
Subroutine _singleton: called CC=3 SCAR=11.3
75 49 my ($message, $name, $view) = @_;
76 49 my @value = _header_values($message, $name, $view);
77 49 ▶ croak "WebSocket handshake requires $name" unless @value;
Branch: 1 branch
BranchTrueFalseTotal
unless @value 0 49 49
78 49 ▶ croak "WebSocket handshake contains duplicate $name" if @value > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @value > 1 0 49 49
79 49 return _trim($value[0]);
80 }
81  
82 61 ▶sub _has_token {
Subroutine _has_token: called CC=3 SCAR=11.1
83 61 my ($message, $name, $wanted, $view) = @_;
84 61 for my $token (_tokens(
85 $name,
86 _header_values($message, $name, $view),
87 )) {
88 60 ▶ return 1 if lc($token) eq lc($wanted);
Branch: 1 branch
BranchTrueFalseTotal
if lc $token eq lc $wanted 60 0 60
89 }
90 1 return 0;
91 }
92  
93 54 ▶sub _split_extension_list {
Subroutine _split_extension_list: called CC=10 SCAR=24.4
94 54 my ($where, $value, $delimiter) = @_;
95 54 my @part;
96 54 my $start = 0;
97 54 my $quoted = 0;
98 54 my $escaped = 0;
99  
100 54 for my $pos (0 .. length($value) - 1) {
101 2334 my $char = substr($value, $pos, 1);
102  
103 2334 ▶ if ($escaped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escaped) 0 2334 2334
104 0 $escaped = 0;
105 0 next;
106 }
107 2334 ▶ if ($quoted && $char eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($quoted and $char eq "\\") 0 2334 2334
Condition66%2/3
$quoted and $char eq "\\"
!ll&&!rl&&r
2302320
MC/DC0%0/2
$quoted and $char eq "\\"
$quoted $char eq "\\"
Truth table66%2/3
A and B
A= $quotedB= $char eq "\\"
ABresult
0X0
100
111
108 0 $escaped = 1;
109 0 next;
110 }
111 2334 ▶ if ($char eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "\"") 20 2314 2334
112 20 $quoted = !$quoted;
113 20 next;
114 }
115 2314 ▶ next unless !$quoted && $char eq $delimiter;
Branch: 1 branch
BranchTrueFalseTotal
unless not $quoted and $char eq $delimiter 2288 26 2314
Condition100%3/3
not $quoted and $char eq $delimiter
!ll&&!rl&&r
22226626
MC/DC100%2/2
not $quoted and $char eq $delimiter
not $quoted $char eq $delimiter
Truth table100%3/3
not(A) and B
A= not $quotedB= $char eq $delimiter
ABresult
0X0
100
111
116  
117 26 push @part, substr($value, $start, $pos - $start);
118 26 $start = $pos + 1;
119 }
120  
121 54 ▶ croak "$where contains an unterminated quoted string"
Branch: 1 branch
BranchTrueFalseTotal
if $quoted or $escaped 0 54 54
Condition33%1/3
$quoted or $escaped
l!l&&r!l&&!r
0054
MC/DC0%0/2
$quoted or $escaped
$quoted $escaped
Truth table33%1/3
A or B
A= $quotedB= $escaped
ABresult
1X1
011
000
122 if $quoted || $escaped;
123 54 push @part, substr($value, $start);
124 54 return @part;
125 }
126  
127 5 ▶sub _quoted_extension_value {
Subroutine _quoted_extension_value: called CC=12 SCAR=34.0
128 5 my ($where, $value) = @_;
129 5 ▶ croak "$where contains an invalid quoted parameter"
Branch: 1 branch
BranchTrueFalseTotal
unless length $value >= 2 and substr($value, 0, 1) eq "\"" and substr($value, -1, 1) eq "\"" 0 5 5
Condition33%2/6
length $value >= 2 and substr($value, 0, 1) eq "\"" and substr($value, -1, 1) eq "\""
!ll&&!rl&&r
005
length $value >= 2 and substr($value, 0, 1) eq "\""
!ll&&!rl&&r
005
MC/DC0%0/3
length $value >= 2 and substr($value, 0, 1) eq "\"" and substr($value, -1, 1) eq "\""
length $value >= 2 substr($value, 0, 1) eq "\"" substr($value, -1, 1) eq "\""
Truth table25%1/4
A and B and C
A= length $value >= 2B= substr($value, 0, 1) eq "\""C= substr($value, -1, 1) eq "\""
ABCresult
0XX0
10X0
1100
1111
130 unless length($value) >= 2
131 && substr($value, 0, 1) eq '"'
132 && substr($value, -1, 1) eq '"';
133  
134 5 my $inside = substr($value, 1, length($value) - 2);
135 5 my $out = '';
136 5 my $escaped = 0;
137  
138 5 for my $char (split //, $inside) {
139 11 ▶ if ($escaped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escaped) 0 11 11
140 0 my $ord = ord($char);
141 0 ▶ croak "$where contains an invalid quoted escape"
Branch: 1 branch
BranchTrueFalseTotal
if $ord > 127 0 0 0
142 if $ord > 0x7f;
143 0 $out .= $char;
144 0 $escaped = 0;
145 0 next;
146 }
147  
148 11 ▶ if ($char eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "\\") 0 11 11
149 0 $escaped = 1;
150 0 next;
151 }
152  
153 11 my $ord = ord($char);
154 11 ▶ croak "$where contains an invalid quoted parameter byte"
Branch: 1 branch
BranchTrueFalseTotal
if $ord < 32 and $char ne "\t" or $ord == 127 0 11 11
Condition33%2/6
$ord < 32 and $char ne "\t" or $ord == 127
l!l&&r!l&&!r
0011
$ord < 32 and $char ne "\t"
!ll&&!rl&&r
1100
MC/DC0%0/3
$ord < 32 and $char ne "\t" or $ord == 127
$ord < 32 $char ne "\t" $ord == 127
Truth table20%1/5
A and B or C
A= $ord < 32B= $char ne "\t"C= $ord == 127
ABCresult
11X1
0X11
1011
0X00
1000
155 if ($ord < 0x20 && $char ne "\t") || $ord == 0x7f;
156 11 $out .= $char;
157 }
158  
159 5 ▶ croak "$where contains an incomplete quoted escape" if $escaped;
Branch: 1 branch
BranchTrueFalseTotal
if $escaped 0 5 5
160 5 return $out;
161 }
162  
163 32 ▶sub _extension_elements {
Subroutine _extension_elements: called CC=14 SCAR=26.8
164 32 my ($message, $view) = @_;
165 32 my @element;
166  
167 32 for my $header (
168 _header_values($message, 'Sec-WebSocket-Extensions', $view)
169 ) {
170 26 for my $raw (
171 _split_extension_list(
172 'Sec-WebSocket-Extensions',
173 $header,
174 ',',
175 )
176 ) {
177 28 $raw = _trim($raw);
178 28 ▶ croak 'Sec-WebSocket-Extensions contains an empty extension'
Branch: 1 branch
BranchTrueFalseTotal
if $raw eq "" 0 28 28
179 if $raw eq '';
180  
181 28 my @part = _split_extension_list(
182 'Sec-WebSocket-Extensions',
183 $raw,
184 ';',
185 );
186 28 my $name = _trim(shift @part);
187 28 ▶ croak "Sec-WebSocket-Extensions contains invalid extension '$name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$TOKEN_RE/ 0 28 28
188 unless $name =~ $TOKEN_RE;
189  
190 28 my @parameter;
191 28 for my $raw_parameter (@part) {
192 24 $raw_parameter = _trim($raw_parameter);
193 24 ▶ croak 'Sec-WebSocket-Extensions contains an empty parameter'
Branch: 1 branch
BranchTrueFalseTotal
if $raw_parameter eq "" 0 24 24
194 if $raw_parameter eq '';
195  
196 24 my ($parameter_name, $tail) =
197 $raw_parameter =~ /\A($TOKEN_BODY)(.*)\z/;
198 24 ▶ croak "Sec-WebSocket-Extensions contains invalid parameter '$raw_parameter'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parameter_name 0 24 24
199 unless defined $parameter_name;
200  
201 24 $tail = _trim($tail);
202 24 my ($parameter_value, $quoted);
203 24 ▶ if ($tail ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail ne "") 15 9 24
204 15 ▶ croak "Sec-WebSocket-Extensions parameter '$parameter_name' is malformed"
Branch: 1 branch
BranchTrueFalseTotal
unless substr($tail, 0, 1) eq "=" 0 15 15
205 unless substr($tail, 0, 1) eq '=';
206 15 $tail = _trim(substr($tail, 1));
207 15 ▶ croak "Sec-WebSocket-Extensions parameter '$parameter_name' has no value"
Branch: 1 branch
BranchTrueFalseTotal
if $tail eq "" 0 15 15
208 if $tail eq '';
209  
210 15 ▶ if (substr($tail, 0, 1) eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($tail, 0, 1) eq '"') { } 5 10 15
211 5 $parameter_value = _quoted_extension_value(
212 "Sec-WebSocket-Extensions parameter '$parameter_name'",
213 $tail,
214 );
215 5 $quoted = 1;
216 }
217 else {
218 10 ▶ croak "Sec-WebSocket-Extensions parameter '$parameter_name' has invalid value"
Branch: 1 branch
BranchTrueFalseTotal
unless $tail =~ /$TOKEN_RE/ 0 10 10
219 unless $tail =~ $TOKEN_RE;
220 10 $parameter_value = $tail;
221 10 $quoted = 0;
222 }
223 }
224  
225 24 ▶ push @parameter, {
Branch: 1 branch
BranchTrueFalseTotal
$quoted ? : 5 19 24
226 name => lc($parameter_name),
227 value => $parameter_value,
228 quoted => $quoted ? 1 : 0,
229 };
230 }
231  
232 28 push @element, {
233 name => lc($name),
234 parameters => \@parameter,
235 };
236 }
237 }
238  
239 32 return @element;
240 }
241  
242 25 ▶sub _pmd_parameters {
Subroutine _pmd_parameters: called CC=14 SCAR=26.7
243 25 my ($where, $element, $response) = @_;
244 25 my %parameter;
245  
246 25 for my $item (@{ $element->{parameters} }) {
247 24 my $name = $item->{name};
248 croak "$where contains duplicate permessage-deflate parameter '$name'"
249 24 ▶ if exists $parameter{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parameter{$name} 1 23 24
250  
251 23 ▶ croak "$where contains unknown permessage-deflate parameter '$name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq "server_no_context_takeover" or $name eq "client_no_context_takeover" or $name eq "server_max_window_bits" or $name eq "client_max_window_bits" 0 23 23
Condition88%8/9
$name eq "server_no_context_takeover" or $name eq "client_no_context_takeover" or $name eq "server_max_window_bits" or $name eq "client_max_window_bits"
l!l&&r!l&&!r
1670
$name eq "server_no_context_takeover" or $name eq "client_no_context_takeover" or $name eq "server_max_window_bits"
l!l&&r!l&&!r
797
$name eq "server_no_context_takeover" or $name eq "client_no_context_takeover"
l!l&&r!l&&!r
4316
MC/DC0%0/4
$name eq "server_no_context_takeover" or $name eq "client_no_context_takeover" or $name eq "server_max_window_bits" or $name eq "client_max_window_bits"
$name eq "server_no_context_takeover" $name eq "client_no_context_takeover" $name eq "server_max_window_bits" $name eq "client_max_window_bits"
Truth table80%4/5
A or B or C or D
A= $name eq "server_no_context_takeover"B= $name eq "client_no_context_takeover"C= $name eq "server_max_window_bits"D= $name eq "client_max_window_bits"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
252 unless $name eq 'server_no_context_takeover'
253 || $name eq 'client_no_context_takeover'
254 || $name eq 'server_max_window_bits'
255 || $name eq 'client_max_window_bits';
256  
257 23 ▶ if ($name eq 'server_no_context_takeover'
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "server_no_context_takeover" or $name eq "client_no_context_takeover") 7 16 23
Condition100%3/3
$name eq "server_no_context_takeover" or $name eq "client_no_context_takeover"
l!l&&r!l&&!r
4316
MC/DC100%2/2
$name eq "server_no_context_takeover" or $name eq "client_no_context_takeover"
$name eq "server_no_context_takeover" $name eq "client_no_context_takeover"
Truth table100%3/3
A or B
A= $name eq "server_no_context_takeover"B= $name eq "client_no_context_takeover"
ABresult
1X1
011
000
258 || $name eq 'client_no_context_takeover') {
259 croak "$where parameter '$name' must not have a value"
260 7 ▶ if defined $item->{value};
Branch: 1 branch
BranchTrueFalseTotal
if defined $item->{"value"} 0 7 7
261 7 $parameter{$name} = 1;
262 7 next;
263 }
264  
265 16 ▶ if (!defined $item->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item->{"value"}) 1 15 16
266 1 ▶ croak "$where parameter '$name' requires a value"
Branch: 1 branch
BranchTrueFalseTotal
if $response or $name eq "server_max_window_bits" 0 1 1
Condition33%1/3
$response or $name eq "server_max_window_bits"
l!l&&r!l&&!r
001
MC/DC0%0/2
$response or $name eq "server_max_window_bits"
$response $name eq "server_max_window_bits"
Truth table33%1/3
A or B
A= $responseB= $name eq "server_max_window_bits"
ABresult
1X1
011
000
267 if $response || $name eq 'server_max_window_bits';
268 1 $parameter{$name} = undef;
269 1 next;
270 }
271  
272 15 my $bits = $item->{value};
273 15 ▶ croak "$where parameter '$name' has invalid window bits"
Branch: 1 branch
BranchTrueFalseTotal
unless $bits =~ /\A(?:8|9|1[0-5])\z/ 3 12 15
274 unless $bits =~ /\A(?:8|9|1[0-5])\z/;
275 12 $parameter{$name} = 0 + $bits;
276 }
277  
278 21 return \%parameter;
279 }
280  
281 48 ▶sub _checked_pmd_option {
Subroutine _checked_pmd_option: called CC=22 SCAR=33.9
282 48 my ($where, $value, $side) = @_;
283 48 ▶ return unless defined($value) && $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value 24 24 48
Condition66%2/3
defined $value and $value
!ll&&!rl&&r
24024
MC/DC50%1/2
defined $value and $value
defined $value $value
Truth table66%2/3
A and B
A= defined $valueB= $value
ABresult
0X0
100
111
284  
285 24 ▶ return {} if !ref($value) && "$value" eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $value and "$value" eq 1 13 11 24
Condition66%2/3
not ref $value and "$value" eq 1
!ll&&!rl&&r
11013
MC/DC50%1/2
not ref $value and "$value" eq 1
not ref $value "$value" eq 1
Truth table66%2/3
not(A) and B
A= not ref $valueB= "$value" eq 1
ABresult
0X0
100
111
286 11 ▶ croak "$where: permessage_deflate must be true or a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 0 11 11
287 unless ref($value) eq 'HASH';
288  
289 11 my %option = %$value;
290 11 my %out;
291  
292 11 for my $name (qw(
293 client_no_context_takeover
294 server_no_context_takeover
295 )) {
296 22 ▶ next unless exists $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option{$name} 17 5 22
297 5 my $flag = delete $option{$name};
298 5 ▶ croak "$where: permessage_deflate $name must be zero or one"
Branch: 1 branch
BranchTrueFalseTotal
if ref $flag or not "$flag" =~ /\A[01]\z/ 0 5 5
Condition33%1/3
ref $flag or not "$flag" =~ /\A[01]\z/
l!l&&r!l&&!r
005
MC/DC0%0/2
ref $flag or not "$flag" =~ /\A[01]\z/
ref $flag not "$flag" =~ /\A[01]\z/
Truth table33%1/3
A or not(B)
A= ref $flagB= not "$flag" =~ /\A[01]\z/
ABresult
1X1
011
000
299 if ref($flag) || "$flag" !~ /\A[01]\z/;
300 5 ▶ $out{$name} = $flag ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 5 0 5
301 }
302  
303 11 for my $name (qw(server_max_window_bits client_max_window_bits)) {
304 22 ▶ next unless exists $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option{$name} 13 9 22
305 9 my $bits = delete $option{$name};
306  
307 9 ▶ if ($side eq 'client'
Branch: 1 branch
BranchTrueFalseTotal
if ($side eq "client" and $name eq "client_max_window_bits" and not defined $bits) 1 8 9
Condition100%6/6
$side eq "client" and $name eq "client_max_window_bits" and not defined $bits
!ll&&!rl&&r
621
$side eq "client" and $name eq "client_max_window_bits"
!ll&&!rl&&r
423
MC/DC100%3/3
$side eq "client" and $name eq "client_max_window_bits" and not defined $bits
$side eq "client" $name eq "client_max_window_bits" not defined $bits
Truth table100%4/4
A and B and not(C)
A= $side eq "client"B= $name eq "client_max_window_bits"C= not defined $bits
ABCresult
0XX0
10X0
1100
1111
308 && $name eq 'client_max_window_bits'
309 && !defined $bits) {
310 1 $out{$name} = undef;
311 1 next;
312 }
313  
314 8 ▶ croak "$where: permessage_deflate $name must be an integer "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bits and not ref $bits and "$bits" =~ /\A[0-9]+\z/ and $bits >= 8 and $bits <= 15 0 8 8
Condition33%4/12
defined $bits and not ref $bits and "$bits" =~ /\A[0-9]+\z/ and $bits >= 8 and $bits <= 15
!ll&&!rl&&r
008
defined $bits and not ref $bits and "$bits" =~ /\A[0-9]+\z/ and $bits >= 8
!ll&&!rl&&r
008
defined $bits and not ref $bits and "$bits" =~ /\A[0-9]+\z/
!ll&&!rl&&r
008
defined $bits and not ref $bits
!ll&&!rl&&r
008
MC/DC0%0/5
defined $bits and not ref $bits and "$bits" =~ /\A[0-9]+\z/ and $bits >= 8 and $bits <= 15
defined $bits not ref $bits "$bits" =~ /\A[0-9]+\z/ $bits >= 8 $bits <= 15
Truth table16%1/6
A and not(B) and C and D and E
A= defined $bitsB= not ref $bitsC= "$bits" =~ /\A[0-9]+\z/D= $bits >= 8E= $bits <= 15
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
315 . 'from 8 through 15'
316 unless defined($bits) && !ref($bits)
317 && "$bits" =~ /\A[0-9]+\z/
318 && $bits >= 8 && $bits <= 15;
319 8 $out{$name} = 0 + $bits;
320 }
321  
322 11 ▶ croak "$where: unknown permessage_deflate option(s): "
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 11 11
323 . join(', ', sort keys %option)
324 if %option;
325  
326 11 return \%out;
327 }
328  
329 18 ▶sub _format_pmd {
Subroutine _format_pmd: called CC=6 SCAR=17.9
330 18 my ($parameter) = @_;
331 18 my @part = ('permessage-deflate');
332  
333 push @part, 'server_no_context_takeover'
334 18 ▶ if $parameter->{server_no_context_takeover};
Branch: 1 branch
BranchTrueFalseTotal
if $parameter->{"server_no_context_takeover"} 3 15 18
335 push @part, 'client_no_context_takeover'
336 18 ▶ if $parameter->{client_no_context_takeover};
Branch: 1 branch
BranchTrueFalseTotal
if $parameter->{"client_no_context_takeover"} 3 15 18
337 push @part, 'server_max_window_bits='
338 . $parameter->{server_max_window_bits}
339 18 ▶ if exists $parameter->{server_max_window_bits};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parameter->{"server_max_window_bits"} 3 15 18
340  
341 18 ▶ if (exists $parameter->{client_max_window_bits}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parameter->{"client_max_window_bits"}) 5 13 18
342 push @part, defined($parameter->{client_max_window_bits})
343 ? 'client_max_window_bits='
344 . $parameter->{client_max_window_bits}
345 5 ▶ : 'client_max_window_bits';
Branch: 1 branch
BranchTrueFalseTotal
defined $parameter->{'client_max_window_bits'} ? : 4 1 5
346 }
347  
348 18 return join('; ', @part);
349 }
350  
351 17 ▶sub _server_pmd_negotiation {
Subroutine _server_pmd_negotiation: called CC=17 SCAR=29.1
352 17 my ($request, $view, $policy) = @_;
353 17 ▶ return unless $policy;
Branch: 1 branch
BranchTrueFalseTotal
unless $policy 5 12 17
354  
355 12 for my $element (_extension_elements($request, $view)) {
356 13 ▶ next unless $element->{name} eq 'permessage-deflate';
Branch: 1 branch
BranchTrueFalseTotal
unless $element->{"name"} eq "permessage-deflate" 0 13 13
357  
358 13 my $offer = eval {
359 13 _pmd_parameters(
360 'WebSocket permessage-deflate offer',
361 $element,
362 0,
363 );
364 };
365 13 ▶ next unless $offer;
Branch: 1 branch
BranchTrueFalseTotal
unless $offer 4 9 13
366  
367 9 my %agreed;
368  
369 9 ▶ if ($offer->{server_no_context_takeover}
Branch: 1 branch
BranchTrueFalseTotal
if ($offer->{"server_no_context_takeover"} or $policy->{"server_no_context_takeover"}) 1 8 9
Condition66%2/3
$offer->{"server_no_context_takeover"} or $policy->{"server_no_context_takeover"}
l!l&&r!l&&!r
108
MC/DC50%1/2
$offer->{"server_no_context_takeover"} or $policy->{"server_no_context_takeover"}
$offer->{"server_no_context_takeover"} $policy->{"server_no_context_takeover"}
Truth table66%2/3
A or B
A= $offer->{"server_no_context_takeover"}B= $policy->{"server_no_context_takeover"}
ABresult
1X1
011
000
370 || $policy->{server_no_context_takeover}) {
371 1 $agreed{server_no_context_takeover} = 1;
372 }
373  
374 9 ▶ if (exists $offer->{server_max_window_bits}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $offer->{'server_max_window_bits'}) { } 3 6 9
elsif (exists $policy->{'server_max_window_bits'}) { } 0 6 6
375 3 my $offered = $offer->{server_max_window_bits};
376 3 ▶ next unless defined $offered;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offered 0 3 3
377  
378 my $bits = exists($policy->{server_max_window_bits})
379 ? $policy->{server_max_window_bits}
380 3 ▶ : $offered;
Branch: 1 branch
BranchTrueFalseTotal
exists $policy->{'server_max_window_bits'} ? : 1 2 3
381 3 ▶ $bits = $offered if $bits > $offered;
Branch: 1 branch
BranchTrueFalseTotal
if $bits > $offered 0 3 3
382 3 $agreed{server_max_window_bits} = $bits;
383 }
384 elsif (exists $policy->{server_max_window_bits}) {
385 $agreed{server_max_window_bits} =
386 0 $policy->{server_max_window_bits};
387 }
388  
389 9 ▶ if ($policy->{client_no_context_takeover}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($policy->{"client_no_context_takeover"}) 1 8 9
390 1 $agreed{client_no_context_takeover} = 1;
391 }
392  
393 9 ▶ if (exists($policy->{client_max_window_bits})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $policy->{"client_max_window_bits"} and exists $offer->{"client_max_window_bits"}) 3 6 9
Condition66%2/3
exists $policy->{"client_max_window_bits"} and exists $offer->{"client_max_window_bits"}
!ll&&!rl&&r
603
MC/DC50%1/2
exists $policy->{"client_max_window_bits"} and exists $offer->{"client_max_window_bits"}
exists $policy->{"client_max_window_bits"} exists $offer->{"client_max_window_bits"}
Truth table66%2/3
A and B
A= exists $policy->{"client_max_window_bits"}B= exists $offer->{"client_max_window_bits"}
ABresult
0X0
100
111
394 && exists($offer->{client_max_window_bits})) {
395 3 my $bits = $policy->{client_max_window_bits};
396 3 ▶ if (defined($offer->{client_max_window_bits})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offer->{"client_max_window_bits"} and $bits > $offer->{"client_max_window_bits"}) 0 3 3
Condition66%2/3
defined $offer->{"client_max_window_bits"} and $bits > $offer->{"client_max_window_bits"}
!ll&&!rl&&r
120
MC/DC0%0/2
defined $offer->{"client_max_window_bits"} and $bits > $offer->{"client_max_window_bits"}
defined $offer->{"client_max_window_bits"} $bits > $offer->{"client_max_window_bits"}
Truth table66%2/3
A and B
A= defined $offer->{"client_max_window_bits"}B= $bits > $offer->{"client_max_window_bits"}
ABresult
0X0
100
111
397 && $bits > $offer->{client_max_window_bits}) {
398 0 $bits = $offer->{client_max_window_bits};
399 }
400 3 $agreed{client_max_window_bits} = $bits;
401 }
402  
403 9 return \%agreed;
404 }
405  
406 3 return;
407 }
408  
409 20 ▶sub _client_pmd_response {
Subroutine _client_pmd_response: called CC=13 SCAR=25.8
410 20 my ($response, $view, $offer) = @_;
411 20 my @extension = _extension_elements($response, $view);
412  
413 20 ▶ if (!$offer) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($offer) 8 12 20
414 8 ▶ croak 'WebSocket handshake response selected an unsupported extension'
Branch: 1 branch
BranchTrueFalseTotal
if @extension 2 6 8
415 if @extension;
416 6 return;
417 }
418  
419 12 ▶ return unless @extension;
Branch: 1 branch
BranchTrueFalseTotal
unless @extension 0 12 12
420  
421 12 ▶ croak 'WebSocket handshake response selected multiple extensions'
Branch: 1 branch
BranchTrueFalseTotal
if @extension != 1 0 12 12
422 if @extension != 1;
423 croak 'WebSocket handshake response selected an unsupported extension'
424 12 ▶ unless $extension[0]{name} eq 'permessage-deflate';
Branch: 1 branch
BranchTrueFalseTotal
unless $extension[0]{"name"} eq "permessage-deflate" 0 12 12
425  
426 12 my $agreed = _pmd_parameters(
427 'WebSocket permessage-deflate response',
428 $extension[0],
429 1,
430 );
431  
432 croak 'WebSocket permessage-deflate response omitted required '
433 . 'server_no_context_takeover'
434 if $offer->{server_no_context_takeover}
435 12 ▶ && !$agreed->{server_no_context_takeover};
Branch: 1 branch
BranchTrueFalseTotal
if $offer->{"server_no_context_takeover"} and not $agreed->{"server_no_context_takeover"} 1 11 12
Condition100%3/3
$offer->{"server_no_context_takeover"} and not $agreed->{"server_no_context_takeover"}
!ll&&!rl&&r
1011
MC/DC100%2/2
$offer->{"server_no_context_takeover"} and not $agreed->{"server_no_context_takeover"}
$offer->{"server_no_context_takeover"} not $agreed->{"server_no_context_takeover"}
Truth table100%3/3
A and not(B)
A= $offer->{"server_no_context_takeover"}B= not $agreed->{"server_no_context_takeover"}
ABresult
0X0
100
111
436  
437 11 ▶ if (exists $offer->{server_max_window_bits}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $offer->{"server_max_window_bits"}) 2 9 11
438 croak 'WebSocket permessage-deflate response omitted required '
439 . 'server_max_window_bits'
440 2 ▶ unless exists $agreed->{server_max_window_bits};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $agreed->{"server_max_window_bits"} 0 2 2
441 croak 'WebSocket permessage-deflate response increased '
442 . 'server_max_window_bits'
443 if $agreed->{server_max_window_bits}
444 2 ▶ > $offer->{server_max_window_bits};
Branch: 1 branch
BranchTrueFalseTotal
if $agreed->{"server_max_window_bits"} > $offer->{"server_max_window_bits"} 1 1 2
445 }
446  
447 10 ▶ if (exists $agreed->{client_max_window_bits}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $agreed->{"client_max_window_bits"}) 4 6 10
448 croak 'WebSocket permessage-deflate response included '
449 . 'client_max_window_bits that was not offered'
450 4 ▶ unless exists $offer->{client_max_window_bits};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $offer->{"client_max_window_bits"} 1 3 4
451  
452 # A numeric value in the client's offer is only a hint. The response
453 # value is the agreed compressor limit and controls actual client
454 # behavior even when it is larger than the offered hint.
455 }
456  
457 # client_no_context_takeover in the offer is only a hint. Established
458 # connection state reflects the parameters actually selected by the
459 # server response.
460 9 return $agreed;
461 }
462  
463 49 ▶sub _checked_subprotocols {
Subroutine _checked_subprotocols: called CC=8 SCAR=22.8
464 49 my ($where, $value) = @_;
465 49 ▶ $value ||= [];
Condition50%1/2
$value ||= []
l!l
490
MC/DC0%0/1
$value ||= []
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
466 49 ▶ croak "$where: subprotocols must be an array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 0 49 49
467 unless ref($value) eq 'ARRAY';
468 49 my %seen;
469 my @copy;
470 49 for my $token (@$value) {
471 20 ▶ croak "$where: each subprotocol must be a WebSocket token"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $token or ref $token or not $token =~ /$TOKEN_RE/ 0 20 20
Condition33%2/6
not defined $token or ref $token or not $token =~ /$TOKEN_RE/
l!l&&r!l&&!r
0020
not defined $token or ref $token
l!l&&r!l&&!r
0020
MC/DC0%0/3
not defined $token or ref $token or not $token =~ /$TOKEN_RE/
not defined $token ref $token not $token =~ /$TOKEN_RE/
Truth table25%1/4
not(A) or B or not(C)
A= not defined $tokenB= ref $tokenC= not $token =~ /$TOKEN_RE/
ABCresult
1XX1
01X1
0011
0000
472 if !defined($token) || ref($token) || $token !~ $TOKEN_RE;
473 20 ▶ croak "$where: duplicate subprotocol '$token'" if $seen{$token}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$token}++ 1 19 20
474 19 push @copy, "$token";
475 }
476 48 return \@copy;
477 }
478  
479 60 ▶sub _valid_key {
Subroutine _valid_key: called CC=5 SCAR=17.5
480 60 my ($key) = @_;
481 60 ▶ return 0 unless defined($key) && !ref($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and not ref $key 0 60 60
Condition33%1/3
defined $key and not ref $key
!ll&&!rl&&r
0060
MC/DC0%0/2
defined $key and not ref $key
defined $key not ref $key
Truth table33%1/3
A and not(B)
A= defined $keyB= not ref $key
ABresult
0X0
100
111
482 60 ▶ return 0 unless $key =~ /\A[A-Za-z0-9+\/]{22}==\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ m[\A[A-Za-z0-9+/]{22}==\z] 1 59 60
483 59 ▶ return length(decode_base64($key)) == 16 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length &decode_base64($key) == 16 ? : 59 0 59
484 }
485  
486 30 ▶sub accept_key {
Subroutine accept_key: called CC=2 SCAR=7.1
Pod documented
487 30 my ($class, $key) = @_;
488 30 ▶ croak 'accept_key(): invalid Sec-WebSocket-Key' unless _valid_key($key);
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_key($key) 0 30 30
489 30 return encode_base64(sha1($key . $GUID), '');
490 }
491  
492 26 ▶sub client_request {
Subroutine client_request: called CC=54 SCAR=46.5
Pod documented
493 26 my ($class, $url, %option) = @_;
494 26 ▶ croak 'client_request(): URL must be a non-empty scalar'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $url or ref $url or $url eq "" 0 26 26
Condition33%2/6
not defined $url or ref $url or $url eq ""
l!l&&r!l&&!r
0026
not defined $url or ref $url
l!l&&r!l&&!r
0026
MC/DC0%0/3
not defined $url or ref $url or $url eq ""
not defined $url ref $url $url eq ""
Truth table25%1/4
not(A) or B or C
A= not defined $urlB= ref $urlC= $url eq ""
ABCresult
1XX1
01X1
0011
0000
495 if !defined($url) || ref($url) || $url eq '';
496  
497 my $http_version = exists($option{http_version})
498 26 ▶ ? delete($option{http_version}) : '1.1';
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'http_version'} ? : 22 4 26
499 26 ▶ croak 'client_request(): http_version must be 1.1, 2, or 3'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $http_version and not ref $http_version and $http_version eq "1.1" || $http_version eq 2 || $http_version eq 3 0 26 26
Condition58%7/12
defined $http_version and not ref $http_version and $http_version eq "1.1" || $http_version eq 2 || $http_version eq 3
!ll&&!rl&&r
0026
defined $http_version and not ref $http_version
!ll&&!rl&&r
0026
$http_version eq "1.1" or $http_version eq 2 or $http_version eq 3
l!l&&r!l&&!r
2420
$http_version eq "1.1" or $http_version eq 2
l!l&&r!l&&!r
2042
MC/DC0%0/5
defined $http_version and not ref $http_version and $http_version eq "1.1" || $http_version eq 2 || $http_version eq 3
defined $http_version not ref $http_version $http_version eq "1.1" $http_version eq 2 $http_version eq 3
Truth table50%3/6
A and not(B) and C or D or E
A= defined $http_versionB= not ref $http_versionC= $http_version eq "1.1"D= $http_version eq 2E= $http_version eq 3
ABCDEresult
0XXXX0
10XXX0
110000
111XX1
1101X1
110011
500 unless defined($http_version) && !ref($http_version)
501 && ($http_version eq '1.1' || $http_version eq '2' || $http_version eq '3');
502  
503 my $subprotocols = _checked_subprotocols(
504 26 ▶ 'client_request()', delete($option{subprotocols}) || [],
Condition100%2/2
delete $option{'subprotocols'} || []
l!l
818
MC/DC100%1/1
delete $option{'subprotocols'} || []
delete $option{'subprotocols'}
Truth table100%2/2
A
A= delete $option{'subprotocols'}
Aresult
11
00
505 );
506 my $pmd_offer = _checked_pmd_option(
507 'client_request()',
508 25 delete($option{permessage_deflate}),
509 'client',
510 );
511 25 my $origin = delete $option{origin};
512 25 ▶ croak 'client_request(): origin must be a scalar'
Branch: 1 branch
BranchTrueFalseTotal
if defined $origin and ref $origin 0 25 25
Condition33%1/3
defined $origin and ref $origin
!ll&&!rl&&r
2500
MC/DC0%0/2
defined $origin and ref $origin
defined $origin ref $origin
Truth table33%1/3
A and B
A= defined $originB= ref $origin
ABresult
0X0
100
111
513 if defined($origin) && ref($origin);
514  
515 25 my $key_option = delete $option{key};
516 25 ▶ croak 'client_request(): key must be a scalar'
Branch: 1 branch
BranchTrueFalseTotal
if defined $key_option and ref $key_option 0 25 25
Condition66%2/3
defined $key_option and ref $key_option
!ll&&!rl&&r
9160
MC/DC0%0/2
defined $key_option and ref $key_option
defined $key_option ref $key_option
Truth table66%2/3
A and B
A= defined $key_optionB= ref $key_option
ABresult
0X0
100
111
517 if defined($key_option) && ref($key_option);
518  
519 25 ▶ my $headers = delete($option{headers}) || [];
Condition100%2/2
delete $option{'headers'} || []
l!l
124
MC/DC100%1/1
delete $option{'headers'} || []
delete $option{'headers'}
Truth table100%2/2
A
A= delete $option{'headers'}
Aresult
11
00
520 25 ▶ croak 'client_request(): headers must be an array reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $headers eq "ARRAY" 0 25 25
521 unless ref($headers) eq 'ARRAY';
522  
523 25 ▶ croak 'client_request(): unknown option(s): ' . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 25 25
524 if %option;
525  
526 25 my ($scheme) = "$url" =~ /\A([A-Za-z][A-Za-z0-9+.-]*):/;
527 25 ▶ $scheme = lc($scheme || '');
Condition50%1/2
$scheme || ''
l!l
250
MC/DC0%0/1
$scheme || ''
$scheme
Truth table50%1/2
A
A= $scheme
Aresult
11
00
528 25 ▶ croak 'client_request(): URL scheme must be ws or wss'
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme eq "ws" or $scheme eq "wss" 1 24 25
Condition100%3/3
$scheme eq "ws" or $scheme eq "wss"
l!l&&r!l&&!r
3211
MC/DC100%2/2
$scheme eq "ws" or $scheme eq "wss"
$scheme eq "ws" $scheme eq "wss"
Truth table100%3/3
A or B
A= $scheme eq "ws"B= $scheme eq "wss"
ABresult
1X1
011
000
529 unless $scheme eq 'ws' || $scheme eq 'wss';
530  
531 24 my $http_url = "$url";
532 24 ▶ my $http_scheme = $scheme eq 'wss' ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'wss' ? : 21 3 24
533 24 $http_url =~ s/\A[A-Za-z][A-Za-z0-9+.-]*:/$http_scheme:/;
534 24 my $uri = URI->new($http_url);
535 24 ▶ croak 'client_request(): URL userinfo is not supported'
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->userinfo and length $uri->userinfo 1 23 24
Condition66%2/3
defined $uri->userinfo and length $uri->userinfo
!ll&&!rl&&r
2301
MC/DC50%1/2
defined $uri->userinfo and length $uri->userinfo
defined $uri->userinfo length $uri->userinfo
Truth table66%2/3
A and B
A= defined $uri->userinfoB= length $uri->userinfo
ABresult
0X0
100
111
536 if defined($uri->userinfo) && length($uri->userinfo);
537  
538 23 my $host = $uri->host;
539 23 ▶ croak 'client_request(): URL must contain a host'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and length $host 0 23 23
Condition33%1/3
defined $host and length $host
!ll&&!rl&&r
0023
MC/DC0%0/2
defined $host and length $host
defined $host length $host
Truth table33%1/3
A and B
A= defined $hostB= length $host
ABresult
0X0
100
111
540 unless defined($host) && length($host);
541 23 my $port = $uri->port;
542 23 ▶ my $default_port = $scheme eq 'wss' ? 443 : 80;
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'wss' ? : 21 2 23
543 23 ▶ my $authority = $host =~ /:/ ? "[$host]" : $host;
Branch: 1 branch
BranchTrueFalseTotal
$host =~ /:/ ? : 0 23 23
544 23 ▶ $authority .= ":$port" if defined($port) && $port != $default_port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port and $port != $default_port 0 23 23
Condition33%1/3
defined $port and $port != $default_port
!ll&&!rl&&r
0230
MC/DC0%0/2
defined $port and $port != $default_port
defined $port $port != $default_port
Truth table33%1/3
A and B
A= defined $portB= $port != $default_port
ABresult
0X0
100
111
545  
546 23 my $target = $uri->path;
547 23 ▶ $target = '/' unless defined($target) && length($target);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target and length $target 0 23 23
Condition33%1/3
defined $target and length $target
!ll&&!rl&&r
0023
MC/DC0%0/2
defined $target and length $target
defined $target length $target
Truth table33%1/3
A and B
A= defined $targetB= length $target
ABresult
0X0
100
111
548 23 my $query = $uri->query;
549 23 ▶ $target .= "?$query" if defined($query) && length($query);
Branch: 1 branch
BranchTrueFalseTotal
if defined $query and length $query 3 20 23
Condition66%2/3
defined $query and length $query
!ll&&!rl&&r
2003
MC/DC50%1/2
defined $query and length $query
defined $query length $query
Truth table66%2/3
A and B
A= defined $queryB= length $query
ABresult
0X0
100
111
550  
551 23 my %reserved = map { $_ => 1 } qw(
552 host connection upgrade origin sec-websocket-key
553 sec-websocket-version sec-websocket-protocol sec-websocket-extensions
554 );
555 23 my @extra;
556 23 for my $pair (@$headers) {
557 1 ▶ croak 'client_request(): each header must be a [name, value] pair'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pair eq "ARRAY" and @$pair == 2 0 1 1
Condition33%1/3
ref $pair eq "ARRAY" and @$pair == 2
!ll&&!rl&&r
001
MC/DC0%0/2
ref $pair eq "ARRAY" and @$pair == 2
ref $pair eq "ARRAY" @$pair == 2
Truth table33%1/3
A and B
A= ref $pair eq "ARRAY"B= @$pair == 2
ABresult
0X0
100
111
558 unless ref($pair) eq 'ARRAY' && @$pair == 2;
559 1 my ($name, $value) = @$pair;
560 1 ▶ croak 'client_request(): header name and value must be scalars'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or ref $name or not defined $value or ref $value 0 1 1
Condition33%3/9
not defined $name or ref $name or not defined $value or ref $value
l!l&&r!l&&!r
001
not defined $name or ref $name or not defined $value
l!l&&r!l&&!r
001
not defined $name or ref $name
l!l&&r!l&&!r
001
MC/DC0%0/4
not defined $name or ref $name or not defined $value or ref $value
not defined $name ref $name not defined $value ref $value
Truth table20%1/5
not(A) or B or not(C) or D
A= not defined $nameB= ref $nameC= not defined $valueD= ref $value
ABCDresult
1XXX1
01XX1
001X1
00011
00000
561 if !defined($name) || ref($name) || !defined($value) || ref($value);
562 croak "client_request(): WebSocket handshake owns header $name"
563 1 ▶ if $reserved{lc $name};
Branch: 1 branch
BranchTrueFalseTotal
if $reserved{lc $name} 1 0 1
564 0 push @extra, [ "$name", "$value" ];
565 }
566  
567 22 require Uniform::HTTP::Request;
568  
569 22 my ($request, $key);
570 22 ▶ if ($http_version eq '1.1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($http_version eq '1.1') { } 16 6 22
571 16 ▶ $key = defined($key_option)
Branch: 1 branch
BranchTrueFalseTotal
defined $key_option ? : 15 1 16
572 ? "$key_option"
573 : encode_base64(Unblock::WebSocket::_Random->bytes(16), '');
574 16 ▶ croak 'client_request(): key must be a valid base64-encoded 16-byte value'
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_key($key) 0 16 16
575 unless _valid_key($key);
576 16 my @ws_headers = (
577 [ Host => $authority ],
578 [ Upgrade => 'websocket' ],
579 [ Connection => 'Upgrade' ],
580 [ 'Sec-WebSocket-Key' => $key ],
581 [ 'Sec-WebSocket-Version' => '13' ],
582 );
583 16 ▶ push @ws_headers,
Branch: 1 branch
BranchTrueFalseTotal
if @$subprotocols 4 12 16
584 [ 'Sec-WebSocket-Protocol' => join(', ', @$subprotocols) ]
585 if @$subprotocols;
586 16 ▶ push @ws_headers,
Branch: 1 branch
BranchTrueFalseTotal
if $pmd_offer 10 6 16
587 [ 'Sec-WebSocket-Extensions' => _format_pmd($pmd_offer) ]
588 if $pmd_offer;
589 16 ▶ push @ws_headers, [ Origin => "$origin" ] if defined $origin;
Branch: 1 branch
BranchTrueFalseTotal
if defined $origin 0 16 16
590 16 push @ws_headers, @extra;
591  
592 16 ▶ $request = Uniform::HTTP::Request->new(
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'wss' ? : 15 1 16
593 method => 'GET',
594 target => $target,
595 scheme => $scheme eq 'wss' ? 'https' : 'http',
596 authority => $authority,
597 version => '1.1',
598 headers => \@ws_headers,
599 );
600 }
601 else {
602 6 ▶ croak 'client_request(): key is only valid for HTTP/1.1'
Branch: 1 branch
BranchTrueFalseTotal
if defined $key_option 1 5 6
603 if defined $key_option;
604 5 my @ws_headers = (
605 [ 'Sec-WebSocket-Version' => '13' ],
606 );
607 5 ▶ push @ws_headers,
Branch: 1 branch
BranchTrueFalseTotal
if @$subprotocols 3 2 5
608 [ 'Sec-WebSocket-Protocol' => join(', ', @$subprotocols) ]
609 if @$subprotocols;
610 5 ▶ push @ws_headers,
Branch: 1 branch
BranchTrueFalseTotal
if $pmd_offer 2 3 5
611 [ 'Sec-WebSocket-Extensions' => _format_pmd($pmd_offer) ]
612 if $pmd_offer;
613 5 ▶ push @ws_headers, [ Origin => "$origin" ] if defined $origin;
Branch: 1 branch
BranchTrueFalseTotal
if defined $origin 0 5 5
614 5 push @ws_headers, @extra;
615  
616 5 ▶ $request = Uniform::HTTP::Request->new(
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'wss' ? : 5 0 5
617 method => 'CONNECT',
618 protocol => 'websocket',
619 target => $target,
620 scheme => $scheme eq 'wss' ? 'https' : 'http',
621 authority => $authority,
622 version => "$http_version",
623 headers => \@ws_headers,
624 );
625 }
626  
627 21 my $self = bless {
628 side => 'client',
629 http_version => "$http_version",
630 key => $key,
631 offered_subprotocols => [ @$subprotocols ],
632 selected_subprotocol => undef,
633 permessage_deflate_offer => $pmd_offer,
634 permessage_deflate => undef,
635 }, $class;
636  
637 21 return ($self, $request);
638 }
639  
640 23 ▶sub server_accept {
Subroutine server_accept: called CC=21 SCAR=31.4
Pod documented
641 23 my ($class, $request, %option) = @_;
642 23 ▶ croak 'server_accept(): request object is required'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $request and ref $request 0 23 23
Condition33%1/3
defined $request and ref $request
!ll&&!rl&&r
0023
MC/DC0%0/2
defined $request and ref $request
defined $request ref $request
Truth table33%1/3
A and B
A= defined $requestB= ref $request
ABresult
0X0
100
111
643 unless defined($request) && ref($request);
644  
645 my $supported = _checked_subprotocols(
646 23 ▶ 'server_accept()', delete($option{subprotocols}) || [],
Condition100%2/2
delete $option{'subprotocols'} || []
l!l
518
MC/DC100%1/1
delete $option{'subprotocols'} || []
delete $option{'subprotocols'}
Truth table100%2/2
A
A= delete $option{'subprotocols'}
Aresult
11
00
647 );
648 my $pmd_policy = _checked_pmd_option(
649 'server_accept()',
650 23 delete($option{permessage_deflate}),
651 'server',
652 );
653 23 ▶ croak 'server_accept(): unknown option(s): ' . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 23 23
654 if %option;
655  
656 23 my $view = _fast_view($request);
657 23 my $version = _message_value(
658 $request,
659 $view,
660 Uniform::HTTP::FastPath::SLOT_VERSION(),
661 'version',
662 );
663 23 ▶ croak 'server_accept(): request version must be known'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 0 23 23
664 unless defined $version;
665  
666 23 my $key;
667 23 ▶ if ($version eq '1.1') {
Branch: 2 branches
BranchTrueFalseTotal
if ($version eq '1.1') { } 17 6 23
elsif ($version eq '2' or $version eq '3') { } 6 0 6
668 17 my $method = _message_value(
669 $request,
670 $view,
671 Uniform::HTTP::FastPath::SLOT_METHOD(),
672 'method',
673 );
674 17 ▶ croak 'WebSocket HTTP/1.1 handshake method must be GET'
Branch: 1 branch
BranchTrueFalseTotal
unless $method eq "GET" 1 16 17
675 unless $method eq 'GET';
676 16 ▶ croak 'WebSocket handshake Upgrade header must contain websocket'
Branch: 1 branch
BranchTrueFalseTotal
unless _has_token($request, "Upgrade", "websocket", $view) 1 15 16
677 unless _has_token($request, 'Upgrade', 'websocket', $view);
678 15 ▶ croak 'WebSocket handshake Connection header must contain Upgrade'
Branch: 1 branch
BranchTrueFalseTotal
unless _has_token($request, "Connection", "Upgrade", $view) 0 15 15
679 unless _has_token($request, 'Connection', 'Upgrade', $view);
680 15 my $ws_version =
681 _singleton($request, 'Sec-WebSocket-Version', $view);
682 15 ▶ croak 'WebSocket handshake Sec-WebSocket-Version must be 13'
Branch: 1 branch
BranchTrueFalseTotal
unless $ws_version eq 13 1 14 15
683 unless $ws_version eq '13';
684 14 $key = _singleton($request, 'Sec-WebSocket-Key', $view);
685 14 ▶ croak 'WebSocket handshake contains invalid Sec-WebSocket-Key'
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_key($key) 1 13 14
686 unless _valid_key($key);
687 }
688 ▶ elsif ($version eq '2' || $version eq '3') {
Condition66%2/3
$version eq 2 or $version eq 3
l!l&&r!l&&!r
330
MC/DC0%0/2
$version eq 2 or $version eq 3
$version eq 2 $version eq 3
Truth table66%2/3
A or B
A= $version eq 2B= $version eq 3
ABresult
1X1
011
000
689 6 my $method = _message_value(
690 $request,
691 $view,
692 Uniform::HTTP::FastPath::SLOT_METHOD(),
693 'method',
694 );
695 6 my $protocol = _message_value(
696 $request,
697 $view,
698 Uniform::HTTP::FastPath::SLOT_PROTOCOL(),
699 'protocol',
700 );
701 6 ▶ croak 'WebSocket Extended CONNECT method must be CONNECT'
Branch: 1 branch
BranchTrueFalseTotal
unless $method eq "CONNECT" 0 6 6
702 unless $method eq 'CONNECT';
703 6 ▶ croak 'WebSocket Extended CONNECT :protocol must be websocket'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $protocol and $protocol eq "websocket" 1 5 6
Condition66%2/3
defined $protocol and $protocol eq "websocket"
!ll&&!rl&&r
015
MC/DC50%1/2
defined $protocol and $protocol eq "websocket"
defined $protocol $protocol eq "websocket"
Truth table66%2/3
A and B
A= defined $protocolB= $protocol eq "websocket"
ABresult
0X0
100
111
704 unless defined($protocol) && $protocol eq 'websocket';
705 5 my $ws_version =
706 _singleton($request, 'Sec-WebSocket-Version', $view);
707 5 ▶ croak 'WebSocket handshake Sec-WebSocket-Version must be 13'
Branch: 1 branch
BranchTrueFalseTotal
unless $ws_version eq 13 0 5 5
708 unless $ws_version eq '13';
709 5 ▶ croak 'WebSocket HTTP/2 and HTTP/3 handshake must not contain Sec-WebSocket-Key'
Branch: 1 branch
BranchTrueFalseTotal
if _header_values($request, "Sec-WebSocket-Key", $view) 1 4 5
710 if _header_values($request, 'Sec-WebSocket-Key', $view);
711 }
712 else {
713 0 croak "WebSocket handshake does not support HTTP version $version";
714 }
715  
716 17 my @offered = _tokens(
717 'Sec-WebSocket-Protocol',
718 _header_values($request, 'Sec-WebSocket-Protocol', $view),
719 );
720 17 my %seen;
721 17 for my $token (@offered) {
722 croak "WebSocket handshake contains duplicate subprotocol '$token'"
723 8 ▶ if $seen{$token}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$token}++ 0 8 8
724 }
725  
726 17 my %supported = map { $_ => 1 } @$supported;
727 17 my ($selected) = grep { $supported{$_} } @offered;
728  
729 17 my $pmd = _server_pmd_negotiation(
730 $request,
731 $view,
732 $pmd_policy,
733 );
734  
735 17 return bless {
736 side => 'server',
737 http_version => "$version",
738 key => $key,
739 offered_subprotocols => \@offered,
740 selected_subprotocol => $selected,
741 permessage_deflate => $pmd,
742 }, $class;
743 }
744  
745 10 ▶sub server_response {
Subroutine server_response: called CC=7 SCAR=19.6
Pod documented
746 10 my ($self) = @_;
747 croak 'server_response(): requires a server handshake'
748 10 ▶ unless ref($self) && $self->{side} eq 'server';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self and $self->{"side"} eq "server" 0 10 10
Condition33%1/3
ref $self and $self->{"side"} eq "server"
!ll&&!rl&&r
0010
MC/DC0%0/2
ref $self and $self->{"side"} eq "server"
ref $self $self->{"side"} eq "server"
Truth table33%1/3
A and B
A= ref $selfB= $self->{"side"} eq "server"
ABresult
0X0
100
111
749  
750 10 require Uniform::HTTP::Response;
751  
752 10 my @headers;
753 my $status;
754 10 my $reason;
755 10 ▶ if ($self->{http_version} eq '1.1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'http_version'} eq '1.1') { } 6 4 10
756 6 $status = 101;
757 6 $reason = 'Switching Protocols';
758 @headers = (
759 [ Upgrade => 'websocket' ],
760 [ Connection => 'Upgrade' ],
761 6 [ 'Sec-WebSocket-Accept' => __PACKAGE__->accept_key($self->{key}) ],
762 );
763 }
764 else {
765 4 $status = 200;
766 }
767  
768 push @headers,
769 [ 'Sec-WebSocket-Protocol' => $self->{selected_subprotocol} ]
770 10 ▶ if defined $self->{selected_subprotocol};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"selected_subprotocol"} 4 6 10
771 push @headers,
772 [ 'Sec-WebSocket-Extensions' =>
773 _format_pmd($self->{permessage_deflate}) ]
774 10 ▶ if $self->{permessage_deflate};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"permessage_deflate"} 6 4 10
775  
776 return Uniform::HTTP::Response->new(
777 status => $status,
778 version => $self->{http_version},
779 10 ▶ (defined($reason) ? (reason => $reason) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $reason ? : 6 4 10
780 headers => \@headers,
781 );
782 }
783  
784 24 ▶sub validate_client_response {
Subroutine validate_client_response: called CC=16 SCAR=28.6
Pod documented
785 24 my ($self, $response) = @_;
786 croak 'validate_client_response(): requires a client handshake'
787 24 ▶ unless ref($self) && $self->{side} eq 'client';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self and $self->{"side"} eq "client" 0 24 24
Condition33%1/3
ref $self and $self->{"side"} eq "client"
!ll&&!rl&&r
0024
MC/DC0%0/2
ref $self and $self->{"side"} eq "client"
ref $self $self->{"side"} eq "client"
Truth table33%1/3
A and B
A= ref $selfB= $self->{"side"} eq "client"
ABresult
0X0
100
111
788 24 ▶ croak 'validate_client_response(): response object is required'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $response and ref $response 0 24 24
Condition33%1/3
defined $response and ref $response
!ll&&!rl&&r
0024
MC/DC0%0/2
defined $response and ref $response
defined $response ref $response
Truth table33%1/3
A and B
A= defined $responseB= ref $response
ABresult
0X0
100
111
789 unless defined($response) && ref($response);
790  
791 24 my $view = _fast_view($response);
792 24 my $status = _message_value(
793 $response,
794 $view,
795 Uniform::HTTP::FastPath::SLOT_STATUS(),
796 'status',
797 );
798  
799 24 ▶ if ($self->{http_version} eq '1.1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'http_version'} eq '1.1') { } 16 8 24
800 16 ▶ croak 'WebSocket handshake response status must be 101'
Branch: 1 branch
BranchTrueFalseTotal
unless $status == 101 1 15 16
801 unless $status == 101;
802 15 ▶ croak 'WebSocket handshake response Upgrade header must contain websocket'
Branch: 1 branch
BranchTrueFalseTotal
unless _has_token($response, "Upgrade", "websocket", $view) 0 15 15
803 unless _has_token($response, 'Upgrade', 'websocket', $view);
804 15 ▶ croak 'WebSocket handshake response Connection header must contain Upgrade'
Branch: 1 branch
BranchTrueFalseTotal
unless _has_token($response, "Connection", "Upgrade", $view) 0 15 15
805 unless _has_token($response, 'Connection', 'Upgrade', $view);
806 15 my $accept =
807 _singleton($response, 'Sec-WebSocket-Accept', $view);
808 croak 'WebSocket handshake response has invalid Sec-WebSocket-Accept'
809 15 ▶ unless $accept eq __PACKAGE__->accept_key($self->{key});
Branch: 1 branch
BranchTrueFalseTotal
unless $accept eq __PACKAGE__->accept_key($self->{"key"}) 1 14 15
810 }
811 else {
812 8 ▶ croak 'WebSocket Extended CONNECT response status must be 200'
Branch: 1 branch
BranchTrueFalseTotal
unless $status == 200 1 7 8
813 unless $status == 200;
814 7 ▶ croak 'WebSocket HTTP/2 and HTTP/3 response must not contain Sec-WebSocket-Accept'
Branch: 1 branch
BranchTrueFalseTotal
if _header_values($response, "Sec-WebSocket-Accept", $view) 1 6 7
815 if _header_values($response, 'Sec-WebSocket-Accept', $view);
816 }
817  
818 $self->{permessage_deflate} = _client_pmd_response(
819 $response,
820 $view,
821 $self->{permessage_deflate_offer},
822 20 );
823  
824 15 my @protocol =
825 _header_values($response, 'Sec-WebSocket-Protocol', $view);
826 15 ▶ croak 'WebSocket handshake response contains duplicate Sec-WebSocket-Protocol'
Branch: 1 branch
BranchTrueFalseTotal
if @protocol > 1 0 15 15
827 if @protocol > 1;
828 15 ▶ if (@protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if (@protocol) 6 9 15
829 6 my @token = _tokens('Sec-WebSocket-Protocol', $protocol[0]);
830 6 ▶ croak 'WebSocket handshake response must select one subprotocol'
Branch: 1 branch
BranchTrueFalseTotal
unless @token == 1 0 6 6
831 unless @token == 1;
832 6 my %offered = map { $_ => 1 } @{ $self->{offered_subprotocols} };
833 croak "WebSocket handshake response selected unknown subprotocol '$token[0]'"
834 6 ▶ unless $offered{$token[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless $offered{$token[0]} 1 5 6
835 5 $self->{selected_subprotocol} = $token[0];
836 }
837  
838 14 return $self;
839 }
840  
841 6 ▶sub subprotocol {
Subroutine subprotocol: called CC=1 SCAR=0.0
Pod documented
842 6 my ($self) = @_;
843 6 return $self->{selected_subprotocol};
844 }
845  
846 0 ▶sub http_version {
Subroutine http_version: not called CC=1 SCAR=6.9
Pod documented
847 0 my ($self) = @_;
848 0 return $self->{http_version};
849 }
850  
851 20 ▶sub permessage_deflate {
Subroutine permessage_deflate: called CC=2 SCAR=6.9
Pod documented
852 20 my ($self) = @_;
853 20 ▶ return unless $self->{permessage_deflate};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"permessage_deflate"} 3 17 20
854 17 return { %{ $self->{permessage_deflate} } };
855 }
856  
857 3 ▶sub connection_options {
Subroutine connection_options: called CC=2 SCAR=7.0
Pod documented
858 3 my ($self) = @_;
859 3 my %option;
860 $option{permessage_deflate} =
861 3 { %{ $self->{permessage_deflate} } }
862 3 ▶ if $self->{permessage_deflate};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"permessage_deflate"} 3 0 3
863 3 return \%option;
864 }
865  
866 6 1;
867  
868 __END__
↑ Summary blib/lib/Unblock/WebSocket/_Frame.pm »