blib/lib/Unblock/WebSocket/_Engine.pm

filter: Statementstmt 95.1% 429 / 451 Branchbran 72.1% 280 / 388 Conditioncond 48.1% 101 / 210 MC/DCmcdc 22.0% 26 / 118 Subroutinesub 94.2% 49 / 52 Podpod 0.0% 0 / 23 totaltotal 71.2% 885 / 1242 CC 274 SCAR 32.3
CC
274
Cov
77%
_new
88.1
_checked_deflate_config
48.0
_transport_sync
28.8
CRAP 25.2
« blib/lib/Unblock/WebSocket/_Frame.pm ↑ Summary blib/lib/Unblock/WebSocket/_Deflate.pm »
1 package Unblock::WebSocket::_Engine;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Unblock::WebSocket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Unblock::WebSocket::_Deflate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Unblock::WebSocket::_Frame ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Unblock::WebSocket::_Random ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Unblock::WebSocket::_UTF8 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 13 my %VALID_CLOSE = map { $_ => 1 } (
15 1000, 1001, 1002, 1003,
16 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014,
17 );
18  
19  
20 69 ▶sub _checked_deflate_config {
Subroutine _checked_deflate_config: called CC=16 SCAR=38.7
21 69 my ($value) = @_;
22 69 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 54 15 69
23 15 ▶ croak 'new(): permessage_deflate must be a hash reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 0 15 15
24 unless ref($value) eq 'HASH';
25  
26 15 my %copy = %$value;
27 15 my %out;
28  
29 15 for my $name (qw(
30 client_no_context_takeover
31 server_no_context_takeover
32 )) {
33 30 ▶ next unless exists $copy{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $copy{$name} 22 8 30
34 8 my $flag = delete $copy{$name};
35 8 ▶ croak "new(): permessage_deflate $name must be zero or one"
Branch: 1 branch
BranchTrueFalseTotal
if ref $flag or not "$flag" =~ /\A[01]\z/ 0 8 8
Condition33%1/3
ref $flag or not "$flag" =~ /\A[01]\z/
l!l&&r!l&&!r
008
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
36 if ref($flag) || "$flag" !~ /\A[01]\z/;
37 8 ▶ $out{$name} = $flag ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 8 0 8
38 }
39  
40 15 for my $name (qw(
41 client_max_window_bits
42 server_max_window_bits
43 )) {
44 30 ▶ next unless exists $copy{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $copy{$name} 30 0 30
45 0 my $bits = delete $copy{$name};
46 0 ▶ croak "new(): permessage_deflate $name must be an integer from 8 through 15"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bits and not ref $bits and "$bits" =~ /\A[0-9]+\z/ and $bits >= 8 and $bits <= 15 0 0 0
Condition0%0/12
defined $bits and not ref $bits and "$bits" =~ /\A[0-9]+\z/ and $bits >= 8 and $bits <= 15
!ll&&!rl&&r
000
defined $bits and not ref $bits and "$bits" =~ /\A[0-9]+\z/ and $bits >= 8
!ll&&!rl&&r
000
defined $bits and not ref $bits and "$bits" =~ /\A[0-9]+\z/
!ll&&!rl&&r
000
defined $bits and not ref $bits
!ll&&!rl&&r
000
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 table0%0/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
47 unless defined($bits) && !ref($bits)
48 && "$bits" =~ /\A[0-9]+\z/
49 && $bits >= 8 && $bits <= 15;
50 0 $out{$name} = 0 + $bits;
51 }
52  
53 15 ▶ croak 'new(): unknown permessage_deflate option(s): '
Branch: 1 branch
BranchTrueFalseTotal
if %copy 0 15 15
54 . join(', ', sort keys %copy)
55 if %copy;
56  
57 15 return \%out;
58 }
59  
60 15 ▶sub _compressed_wire_limit {
Subroutine _compressed_wire_limit: called CC=1 SCAR=0.0
61 15 my ($max_message_size) = @_;
62 15 return $max_message_size + int($max_message_size / 100) + 1024;
63 }
64  
65 70 ▶sub _new {
Subroutine _new: called CC=49 SCAR=44.8
66 70 my ($class, %option) = @_;
67 70 my $role = delete $option{role};
68 70 ▶ croak 'new(): role must be client or server'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $role and $role eq "client" || $role eq "server" 0 70 70
Condition50%3/6
defined $role and $role eq "client" || $role eq "server"
!ll&&!rl&&r
0070
$role eq "client" or $role eq "server"
l!l&&r!l&&!r
17530
MC/DC0%0/3
defined $role and $role eq "client" || $role eq "server"
defined $role $role eq "client" $role eq "server"
Truth table50%2/4
A and B or C
A= defined $roleB= $role eq "client"C= $role eq "server"
ABCresult
0XX0
1000
11X1
1011
69 unless defined($role) && ($role eq 'client' || $role eq 'server');
70  
71 70 my $transport = delete $option{transport};
72 70 ▶ if (defined $transport) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $transport) 13 57 70
73 13 ▶ croak 'new(): transport must be an object' unless blessed($transport);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($transport) 0 13 13
74 13 for my $method (qw(unblock_send unblock_finish unblock_abort)) {
75 37 ▶ croak "new(): transport must implement $method()"
Branch: 1 branch
BranchTrueFalseTotal
unless $transport->can($method) 1 36 37
76 unless $transport->can($method);
77 }
78 }
79  
80 my $max_message_size = exists($option{max_message_size})
81 69 ▶ ? delete($option{max_message_size}) : 16 * 1024 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'max_message_size'} ? : 4 65 69
82 69 ▶ croak 'new(): max_message_size must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_message_size and not ref $max_message_size and $max_message_size =~ /\A[0-9]+\z/ and $max_message_size > 0 0 69 69
Condition33%3/9
defined $max_message_size and not ref $max_message_size and $max_message_size =~ /\A[0-9]+\z/ and $max_message_size > 0
!ll&&!rl&&r
0069
defined $max_message_size and not ref $max_message_size and $max_message_size =~ /\A[0-9]+\z/
!ll&&!rl&&r
0069
defined $max_message_size and not ref $max_message_size
!ll&&!rl&&r
0069
MC/DC0%0/4
defined $max_message_size and not ref $max_message_size and $max_message_size =~ /\A[0-9]+\z/ and $max_message_size > 0
defined $max_message_size not ref $max_message_size $max_message_size =~ /\A[0-9]+\z/ $max_message_size > 0
Truth table20%1/5
A and not(B) and C and D
A= defined $max_message_sizeB= not ref $max_message_sizeC= $max_message_size =~ /\A[0-9]+\z/D= $max_message_size > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
83 unless defined($max_message_size) && !ref($max_message_size)
84 && $max_message_size =~ /\A[0-9]+\z/ && $max_message_size > 0;
85  
86 my $high_water = exists($option{high_water})
87 69 ▶ ? delete($option{high_water}) : 64 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'high_water'} ? : 0 69 69
88 my $low_water = exists($option{low_water})
89 69 ▶ ? delete($option{low_water}) : 32 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'low_water'} ? : 0 69 69
90 69 for my $pair ([high_water => $high_water], [low_water => $low_water]) {
91 138 ▶ croak "new(): $pair->[0] must be a non-negative integer"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pair->[1] and not ref $pair->[1] and $pair->[1] =~ /\A[0-9]+\z/ 0 138 138
Condition33%2/6
defined $pair->[1] and not ref $pair->[1] and $pair->[1] =~ /\A[0-9]+\z/
!ll&&!rl&&r
00138
defined $pair->[1] and not ref $pair->[1]
!ll&&!rl&&r
00138
MC/DC0%0/3
defined $pair->[1] and not ref $pair->[1] and $pair->[1] =~ /\A[0-9]+\z/
defined $pair->[1] not ref $pair->[1] $pair->[1] =~ /\A[0-9]+\z/
Truth table25%1/4
A and not(B) and C
A= defined $pair->[1]B= not ref $pair->[1]C= $pair->[1] =~ /\A[0-9]+\z/
ABCresult
0XX0
10X0
1100
1111
92 unless defined($pair->[1]) && !ref($pair->[1])
93 && $pair->[1] =~ /\A[0-9]+\z/;
94 }
95 69 ▶ croak 'new(): low_water must not exceed high_water'
Branch: 1 branch
BranchTrueFalseTotal
if $low_water > $high_water 0 69 69
96 if $low_water > $high_water;
97  
98 69 my $random_bytes = delete $option{random_bytes};
99 69 ▶ croak 'new(): random_bytes must be a code reference'
Branch: 1 branch
BranchTrueFalseTotal
if defined $random_bytes and ref $random_bytes ne "CODE" 0 69 69
Condition66%2/3
defined $random_bytes and ref $random_bytes ne "CODE"
!ll&&!rl&&r
6360
MC/DC0%0/2
defined $random_bytes and ref $random_bytes ne "CODE"
defined $random_bytes ref $random_bytes ne "CODE"
Truth table66%2/3
A and B
A= defined $random_bytesB= ref $random_bytes ne "CODE"
ABresult
0X0
100
111
100 if defined($random_bytes) && ref($random_bytes) ne 'CODE';
101  
102 my $deflate_config =
103 69 _checked_deflate_config(delete $option{permessage_deflate});
104  
105 69 my $backend = delete $option{backend};
106 69 ▶ if (!defined $backend) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $backend) 8 61 69
107 8 ▶ $backend = defined($random_bytes)
Branch: 2 branches
BranchTrueFalseTotal
defined $random_bytes ? : 2 6 8
'Unblock::WebSocket'->native_available ? : 6 0 6
108 ? 'perl'
109 : (Unblock::WebSocket->native_available ? 'native' : 'perl');
110 }
111 69 ▶ croak 'new(): backend must be native or perl'
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $backend and $backend eq "native" || $backend eq "perl" 0 69 69
Condition50%3/6
not ref $backend and $backend eq "native" || $backend eq "perl"
!ll&&!rl&&r
0069
$backend eq "native" or $backend eq "perl"
l!l&&r!l&&!r
38310
MC/DC0%0/3
not ref $backend and $backend eq "native" || $backend eq "perl"
not ref $backend $backend eq "native" $backend eq "perl"
Truth table50%2/4
not(A) and B or C
A= not ref $backendB= $backend eq "native"C= $backend eq "perl"
ABCresult
0XX0
1000
11X1
1011
112 unless !ref($backend) && ($backend eq 'native' || $backend eq 'perl');
113 69 ▶ croak 'new(): native backend is not available'
Branch: 1 branch
BranchTrueFalseTotal
if $backend eq "native" and not "Unblock::WebSocket"->native_available 0 69 69
Condition66%2/3
$backend eq "native" and not "Unblock::WebSocket"->native_available
!ll&&!rl&&r
31380
MC/DC0%0/2
$backend eq "native" and not "Unblock::WebSocket"->native_available
$backend eq "native" not "Unblock::WebSocket"->native_available
Truth table66%2/3
A and not(B)
A= $backend eq "native"B= not "Unblock::WebSocket"->native_available
ABresult
0X0
100
111
114 if $backend eq 'native' && !Unblock::WebSocket->native_available;
115 69 ▶ croak 'new(): random_bytes is only supported by the perl backend'
Branch: 1 branch
BranchTrueFalseTotal
if $backend eq "native" and defined $random_bytes 0 69 69
Condition66%2/3
$backend eq "native" and defined $random_bytes
!ll&&!rl&&r
31380
MC/DC0%0/2
$backend eq "native" and defined $random_bytes
$backend eq "native" defined $random_bytes
Truth table66%2/3
A and B
A= $backend eq "native"B= defined $random_bytes
ABresult
0X0
100
111
116 if $backend eq 'native' && defined $random_bytes;
117  
118 69 my $native;
119 69 ▶ if ($backend eq 'native') {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq "native") 38 31 69
120 38 require Unblock::WebSocket::_Native;
121 38 ▶ my $wire_limit = $deflate_config
Branch: 1 branch
BranchTrueFalseTotal
$deflate_config ? : 11 27 38
122 ? _compressed_wire_limit($max_message_size)
123 : $max_message_size;
124 38 ▶ $native = Unblock::WebSocket::_Native->new(
Branch: 1 branch
BranchTrueFalseTotal
$deflate_config ? : 11 27 38
125 $role,
126 $wire_limit,
127 $deflate_config ? 1 : 0,
128 );
129 }
130  
131 69 my %callback;
132 69 for my $name (qw(on_message on_ping on_pong on_close on_error on_drain)) {
133 414 ▶ next unless exists $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option{$name} 335 79 414
134 79 my $cb = delete $option{$name};
135 79 ▶ croak "new(): $name must be a code reference"
Branch: 1 branch
BranchTrueFalseTotal
if defined $cb and ref $cb ne "CODE" 0 79 79
Condition33%1/3
defined $cb and ref $cb ne "CODE"
!ll&&!rl&&r
0790
MC/DC0%0/2
defined $cb and ref $cb ne "CODE"
defined $cb ref $cb ne "CODE"
Truth table33%1/3
A and B
A= defined $cbB= ref $cb ne "CODE"
ABresult
0X0
100
111
136 if defined($cb) && ref($cb) ne 'CODE';
137 79 ▶ $callback{$name} = $cb if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 79 0 79
138 }
139  
140 69 ▶ croak 'new(): unknown option(s): ' . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 69 69
141 if %option;
142  
143 69 ▶ my $self = bless {
Branch: 7 branches
BranchTrueFalseTotal
$backend eq 'perl' ? : 31 38 69
$role eq 'server' ? : 22 9 31
$deflate_config ? : 4 27 31
$max_message_size < 125 ? : 1 26 27
$deflate_config ? : 4 27 31
$deflate_config ? : 15 54 69
defined $transport ? : 12 57 69
144 role => $role,
145 backend => $backend,
146 native => $native,
147 parser => $backend eq 'perl'
148 ? Unblock::WebSocket::_Frame->new_parser(
149 expect_masked => $role eq 'server' ? 1 : 0,
150 max_frame_size => $deflate_config
151 ? _compressed_wire_limit($max_message_size)
152 : ($max_message_size < 125 ? 125 : $max_message_size),
153 allow_rsv1 => $deflate_config ? 1 : 0,
154 )
155 : undef,
156 deflate => $deflate_config
157 ? Unblock::WebSocket::_Deflate->new(
158 role => $role,
159 config => $deflate_config,
160 max_message_size => $max_message_size,
161 )
162 : undef,
163 deflate_config => $deflate_config,
164 max_message_size => 0 + $max_message_size,
165 high_water => 0 + $high_water,
166 low_water => 0 + $low_water,
167 output => '',
168 output_blocked => 0,
169 callbacks => \%callback,
170 random_bytes => $random_bytes,
171 fragment_opcode => undef,
172 fragment_payload => '',
173 fragment_compressed => 0,
174 sent_close => 0,
175 received_close => 0,
176 closed => 0,
177 failed => 0,
178 driving => 0,
179 transport => $transport,
180 transport_attached => defined($transport) ? 1 : 0,
181 transport_blocked => 0,
182 transport_syncing => 0,
183 transport_in_send => 0,
184 transport_finished => 0,
185 transport_aborted => 0,
186 transport_producer_blocked => 0,
187 }, $class;
188 69 ▶ weaken($self->{transport}) if defined $transport;
Branch: 1 branch
BranchTrueFalseTotal
if defined $transport 12 57 69
189 69 return $self;
190 }
191  
192 0 ▶sub role { $_[0]{role} }
Subroutine role: not called CC=1 SCAR=6.9
Pod undocumented
193 3 ▶sub backend { $_[0]{backend} }
Subroutine backend: called CC=1 SCAR=0.0
Pod undocumented
194 1 ▶sub permessage_deflate {
Subroutine permessage_deflate: called CC=2 SCAR=7.0
Pod undocumented
195 1 my ($self) = @_;
196 1 ▶ return unless $self->{deflate_config};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"deflate_config"} 0 1 1
197 1 return { %{ $self->{deflate_config} } };
198 }
199 34 ▶sub is_open { !$_[0]{closed} && !$_[0]{sent_close} && !$_[0]{received_close} ? 1 : 0 }
Subroutine is_open: called CC=4 SCAR=19.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
!($_[0]{'closed'} || $_[0]{'sent_close'} || $_[0]{'received_close'}) ? : 34 0 34
Condition33%2/6
$_[0]{'closed'} || $_[0]{'sent_close'} || $_[0]{'received_close'}
l!l&&r!l&&!r
0034
$_[0]{"closed"} or $_[0]{"sent_close"}
l!l&&r!l&&!r
0034
MC/DC0%0/3
$_[0]{'closed'} || $_[0]{'sent_close'} || $_[0]{'received_close'}
$_[0]{"closed"} $_[0]{"sent_close"} $_[0]{'received_close'}
Truth table25%1/4
A or B || C
A= $_[0]{"closed"}B= $_[0]{"sent_close"}C= $_[0]{'received_close'}
ABCresult
1XX1
01X1
0011
0000
200 3 ▶sub is_closing { !$_[0]{closed} && ($_[0]{sent_close} || $_[0]{received_close}) ? 1 : 0 }
Subroutine is_closing: called CC=4 SCAR=19.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
!$_[0]{'closed'} && ($_[0]{'sent_close'} || $_[0]{'received_close'}) ? : 3 0 3
Condition33%2/6
not $_[0]{"closed"} and $_[0]{"sent_close"} || $_[0]{"received_close"}
!ll&&!rl&&r
003
$_[0]{"sent_close"} or $_[0]{"received_close"}
l!l&&r!l&&!r
300
MC/DC0%0/3
not $_[0]{"closed"} and $_[0]{"sent_close"} || $_[0]{"received_close"}
not $_[0]{"closed"} $_[0]{"sent_close"} $_[0]{"received_close"}
Truth table25%1/4
not(A) and B or C
A= not $_[0]{"closed"}B= $_[0]{"sent_close"}C= $_[0]{"received_close"}
ABCresult
0XX0
1000
11X1
1011
201 12 ▶sub is_closed { $_[0]{closed} ? 1 : 0 }
Subroutine is_closed: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'closed'} ? : 12 0 12
202 0 ▶sub want_read { !$_[0]{closed} ? 1 : 0 }
Subroutine want_read: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
!$_[0]{'closed'} ? : 0 0 0
203 1 ▶sub want_write { length($_[0]{output}) ? 1 : 0 }
Subroutine want_write: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
length $_[0]{'output'} ? : 1 0 1
204 0 ▶sub output_size { length($_[0]{output}) }
Subroutine output_size: not called CC=1 SCAR=6.9
Pod undocumented
205 10 ▶sub close_sent { $_[0]{sent_close} ? 1 : 0 }
Subroutine close_sent: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'sent_close'} ? : 8 2 10
206 12 ▶sub close_received { $_[0]{received_close} ? 1 : 0 }
Subroutine close_received: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'received_close'} ? : 6 6 12
207 7 ▶sub close_complete { $_[0]{sent_close} && $_[0]{received_close} ? 1 : 0 }
Subroutine close_complete: called CC=3 SCAR=14.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'sent_close'} && $_[0]{'received_close'} ? : 7 0 7
Condition33%1/3
$_[0]{"sent_close"} and $_[0]{"received_close"}
!ll&&!rl&&r
007
MC/DC0%0/2
$_[0]{"sent_close"} and $_[0]{"received_close"}
$_[0]{"sent_close"} $_[0]{"received_close"}
Truth table33%1/3
A and B
A= $_[0]{"sent_close"}B= $_[0]{"received_close"}
ABresult
0X0
100
111
208 7 ▶sub want_end {
Subroutine want_end: called CC=5 SCAR=16.7
Pod undocumented
209 7 my ($self) = @_;
210 7 ▶ return 0 if $self->{closed} || length($self->{output});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or length $self->{"output"} 1 6 7
Condition66%2/3
$self->{"closed"} or length $self->{"output"}
l!l&&r!l&&!r
016
MC/DC50%1/2
$self->{"closed"} or length $self->{"output"}
$self->{"closed"} length $self->{"output"}
Truth table66%2/3
A or B
A= $self->{"closed"}B= length $self->{"output"}
ABresult
1X1
011
000
211 6 ▶ return 1 if $self->{failed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failed"} 0 6 6
212 6 ▶ return $self->{received_close} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'received_close'} ? : 6 0 6
213 }
214  
215 105 ▶sub _invoke {
Subroutine _invoke: called CC=5 SCAR=20.3
216 105 my ($self, $name, @args) = @_;
217 105 ▶ my $cb = $self->{callbacks}{$name} or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $self->{"callbacks"}{$name} or return 1 87 18 105
218 87 my $ok = eval { $cb->($self, @args); 1 };
219 87 ▶ return 1 if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 87 0 87
220 0 ▶ my $error = $@ || "$name callback failed";
Condition0%0/2
$@ || "$name callback failed"
l!l
00
MC/DC0%0/1
$@ || "$name callback failed"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
221 0 ▶ $self->_fail($error, 1011) unless $name eq 'on_error';
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq "on_error" 0 0 0
222 0 return;
223 }
224  
225 71 ▶sub _notify_drain {
Subroutine _notify_drain: called CC=6 SCAR=18.8
226 71 my ($self) = @_;
227 71 ▶ return if $self->{closed} || $self->{transport_blocked};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $self->{"transport_blocked"} 0 71 71
Condition33%1/3
$self->{"closed"} or $self->{"transport_blocked"}
l!l&&r!l&&!r
0071
MC/DC0%0/2
$self->{"closed"} or $self->{"transport_blocked"}
$self->{"closed"} $self->{"transport_blocked"}
Truth table33%1/3
A or B
A= $self->{"closed"}B= $self->{"transport_blocked"}
ABresult
1X1
011
000
228 71 ▶ return if length($self->{output}) > $self->{low_water};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"output"} > $self->{"low_water"} 0 71 71
229 return unless $self->{output_blocked}
230 71 ▶ || $self->{transport_producer_blocked};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"output_blocked"} or $self->{"transport_producer_blocked"} 67 4 71
Condition66%2/3
$self->{"output_blocked"} or $self->{"transport_producer_blocked"}
l!l&&r!l&&!r
0467
MC/DC50%1/2
$self->{"output_blocked"} or $self->{"transport_producer_blocked"}
$self->{"output_blocked"} $self->{"transport_producer_blocked"}
Truth table66%2/3
A or B
A= $self->{"output_blocked"}B= $self->{"transport_producer_blocked"}
ABresult
1X1
011
000
231 4 $self->{output_blocked} = 0;
232 4 $self->{transport_producer_blocked} = 0;
233 4 $self->_invoke('on_drain');
234 4 return;
235 }
236  
237 # All bytes passed to unblock_send() are accepted or the host must throw.
238 # A false return signals congestion after acceptance, never a partial write.
239 28 ▶sub _transport_sync {
Subroutine _transport_sync: called CC=25 SCAR=33.6
240 28 my ($self) = @_;
241 28 ▶ return unless $self->{transport_attached};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"transport_attached"} 0 28 28
242 28 ▶ return if $self->{driving} || $self->{transport_syncing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"driving"} or $self->{"transport_syncing"} 2 26 28
Condition66%2/3
$self->{"driving"} or $self->{"transport_syncing"}
l!l&&r!l&&!r
2026
MC/DC50%1/2
$self->{"driving"} or $self->{"transport_syncing"}
$self->{"driving"} $self->{"transport_syncing"}
Truth table66%2/3
A or B
A= $self->{"driving"}B= $self->{"transport_syncing"}
ABresult
1X1
011
000
243 26 ▶ return if $self->{transport_aborted} || $self->{transport_finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport_aborted"} or $self->{"transport_finished"} 2 24 26
Condition66%2/3
$self->{"transport_aborted"} or $self->{"transport_finished"}
l!l&&r!l&&!r
0224
MC/DC50%1/2
$self->{"transport_aborted"} or $self->{"transport_finished"}
$self->{"transport_aborted"} $self->{"transport_finished"}
Truth table66%2/3
A or B
A= $self->{"transport_aborted"}B= $self->{"transport_finished"}
ABresult
1X1
011
000
244 24 ▶ return if $self->{transport_blocked};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport_blocked"} 4 20 24
245  
246 20 my $host = $self->{transport};
247 20 ▶ return $self->_transport_abort('WebSocket transport was destroyed')
Branch: 1 branch
BranchTrueFalseTotal
unless $host 0 20 20
248 unless $host;
249  
250 {
251 20 local $self->{transport_syncing} = 1;
252 20 ▶ while (!$self->{transport_blocked} && !$self->{transport_aborted}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"transport_blocked"} or $self->{"transport_aborted"}) 20 0 20
Condition33%1/3
$self->{"transport_blocked"} or $self->{"transport_aborted"}
l!l&&r!l&&!r
0020
MC/DC0%0/2
$self->{"transport_blocked"} or $self->{"transport_aborted"}
$self->{"transport_blocked"} $self->{"transport_aborted"}
Truth table33%1/3
A or B
A= $self->{"transport_blocked"}B= $self->{"transport_aborted"}
ABresult
1X1
011
000
253 20 ▶ if (length($self->{output})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"output"}) 16 4 20
254 16 my $bytes = $self->{output};
255 16 $self->{output} = '';
256 16 my $ready;
257 16 my $ok = eval {
258 16 local $self->{transport_in_send} = 1;
259 16 $ready = $host->unblock_send($bytes);
260 14 1;
261 };
262 16 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 14 16
263 2 ▶ $self->_transport_abort("$@" || 'unblock_send() failed');
Condition50%1/2
"$@" || 'unblock_send() failed'
l!l
20
MC/DC0%0/1
"$@" || 'unblock_send() failed'
"$@"
Truth table50%1/2
A
A= "$@"
Aresult
11
00
264 2 last;
265 }
266 14 ▶ $self->{transport_blocked} = 1
Branch: 1 branch
BranchTrueFalseTotal
if defined $ready and not $ready 4 10 14
Condition66%2/3
defined $ready and not $ready
!ll&&!rl&&r
1004
MC/DC50%1/2
defined $ready and not $ready
defined $ready not $ready
Truth table66%2/3
A and not(B)
A= defined $readyB= not $ready
ABresult
0X0
100
111
267 if defined($ready) && !$ready;
268 }
269 18 ▶ $self->_notify_drain unless $self->{transport_blocked};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"transport_blocked"} 14 4 18
270 18 ▶ last unless length($self->{output});
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"output"} 18 0 18
271 }
272 }
273  
274 # finish() must preserve the host's already accepted write queue.
275 # Sending our Close frame alone is not completion of the handshake.
276 20 ▶ if (!$self->{transport_aborted} && !$self->{transport_finished}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"transport_aborted"} || $self->{"transport_finished"} || $self->{"transport_blocked"} || length $self->{"output"} and $self->{"received_close"} || $self->{"failed"}) 4 16 20
Condition80%12/15
not $self->{"transport_aborted"} || $self->{"transport_finished"} || $self->{"transport_blocked"} || length $self->{"output"} and $self->{"received_close"} || $self->{"failed"}
!ll&&!rl&&r
6104
$self->{'transport_aborted'} || $self->{'transport_finished'} || $self->{'transport_blocked'} || length $self->{'output'}
l!l&&r!l&&!r
6014
$self->{"transport_aborted"} or $self->{"transport_finished"} or $self->{"transport_blocked"}
l!l&&r!l&&!r
2414
$self->{"transport_aborted"} or $self->{"transport_finished"}
l!l&&r!l&&!r
2018
$self->{"received_close"} or $self->{"failed"}
l!l&&r!l&&!r
4010
MC/DC50%3/6
not $self->{"transport_aborted"} || $self->{"transport_finished"} || $self->{"transport_blocked"} || length $self->{"output"} and $self->{"received_close"} || $self->{"failed"}
$self->{"transport_aborted"} $self->{"transport_finished"} $self->{"transport_blocked"} length $self->{'output'} $self->{"received_close"} $self->{"failed"}
Truth table57%4/7
not(A or B or C || D) and E or F
A= $self->{"transport_aborted"}B= $self->{"transport_finished"}C= $self->{"transport_blocked"}D= length $self->{'output'}E= $self->{"received_close"}F= $self->{"failed"}
ABCDEFresult
1XXXXX0
01XXXX0
001XXX0
0001XX0
0000000
00001X1
0000011
277 && !$self->{transport_blocked} && !length($self->{output})
278 && ($self->{received_close} || $self->{failed})) {
279 4 $self->{transport_finished} = 1;
280 4 my $ok = eval { $host->unblock_finish(); 1 };
281 4 ▶ $self->_transport_abort("$@" || 'unblock_finish() failed')
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 4 4
Condition0%0/2
"$@" || 'unblock_finish() failed'
l!l
00
MC/DC0%0/1
"$@" || 'unblock_finish() failed'
"$@"
Truth table0%0/2
A
A= "$@"
Aresult
11
00
282 unless $ok;
283 }
284 20 return;
285 }
286  
287 5 ▶sub resume_output {
Subroutine resume_output: called CC=6 SCAR=19.6
Pod undocumented
288 5 my ($self) = @_;
289 croak 'resume_output(): no attached transport'
290 5 ▶ unless $self->{transport_attached};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"transport_attached"} 1 4 5
291 croak 'resume_output(): cannot be called recursively from a callback'
292 4 ▶ if $self->{driving} || $self->{transport_syncing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"driving"} or $self->{"transport_syncing"} 0 4 4
Condition33%1/3
$self->{"driving"} or $self->{"transport_syncing"}
l!l&&r!l&&!r
004
MC/DC0%0/2
$self->{"driving"} or $self->{"transport_syncing"}
$self->{"driving"} $self->{"transport_syncing"}
Truth table33%1/3
A or B
A= $self->{"driving"}B= $self->{"transport_syncing"}
ABresult
1X1
011
000
293 4 ▶ return $self if $self->{transport_aborted} || $self->{transport_finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport_aborted"} or $self->{"transport_finished"} 0 4 4
Condition33%1/3
$self->{"transport_aborted"} or $self->{"transport_finished"}
l!l&&r!l&&!r
004
MC/DC0%0/2
$self->{"transport_aborted"} or $self->{"transport_finished"}
$self->{"transport_aborted"} $self->{"transport_finished"}
Truth table33%1/3
A or B
A= $self->{"transport_aborted"}B= $self->{"transport_finished"}
ABresult
1X1
011
000
294 4 $self->{transport_blocked} = 0;
295 4 $self->_transport_sync;
296 4 return $self;
297 }
298  
299 6 ▶sub _transport_abort {
Subroutine _transport_abort: called CC=6 SCAR=18.7
300 6 my ($self, $reason) = @_;
301 6 ▶ return if $self->{transport_aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport_aborted"} 0 6 6
302 6 ▶ $reason = 'transport error' unless defined($reason) && length($reason);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason and length $reason 0 6 6
Condition33%1/3
defined $reason and length $reason
!ll&&!rl&&r
006
MC/DC0%0/2
defined $reason and length $reason
defined $reason length $reason
Truth table33%1/3
A and B
A= defined $reasonB= length $reason
ABresult
0X0
100
111
303 6 $reason = "$reason";
304 6 $self->{transport_aborted} = 1;
305 6 $self->{closed} = 1;
306 6 $self->{failed} = 1;
307 6 $self->{output} = '';
308 6 $self->_invoke('on_error', $reason);
309 6 ▶ $self->_invoke('on_close', undef, $reason) unless $self->{received_close};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"received_close"} 6 0 6
310 6 ▶ if (my $host = $self->{transport}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = $self->{"transport"}) 6 0 6
311 6 eval { $host->unblock_abort($reason) };
312 }
313 6 return;
314 }
315  
316 4 ▶sub transport_error {
Subroutine transport_error: called CC=3 SCAR=11.0
Pod undocumented
317 4 my ($self, $reason) = @_;
318 4 ▶ return $self if $self->{transport_aborted} || $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport_aborted"} or $self->{"closed"} 2 2 4
Condition66%2/3
$self->{"transport_aborted"} or $self->{"closed"}
l!l&&r!l&&!r
202
MC/DC50%1/2
$self->{"transport_aborted"} or $self->{"closed"}
$self->{"transport_aborted"} $self->{"closed"}
Truth table66%2/3
A or B
A= $self->{"transport_aborted"}B= $self->{"closed"}
ABresult
1X1
011
000
319 2 $self->_transport_abort($reason);
320 2 return $self;
321 }
322  
323 103 ▶sub _queue_wire {
Subroutine _queue_wire: called CC=11 SCAR=24.2
324 103 my ($self, $bytes) = @_;
325 croak 'WebSocket output cannot be generated from unblock_send()'
326 103 ▶ if $self->{transport_in_send};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport_in_send"} 0 103 103
327 103 ▶ return 1 if !defined($bytes) || !length($bytes);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes and length $bytes 22 81 103
Condition66%2/3
defined $bytes and length $bytes
!ll&&!rl&&r
02281
MC/DC50%1/2
defined $bytes and length $bytes
defined $bytes length $bytes
Truth table66%2/3
A and B
A= defined $bytesB= length $bytes
ABresult
0X0
100
111
328 81 $self->{output} .= $bytes;
329 81 ▶ $self->_transport_sync if $self->{transport_attached};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport_attached"} 16 65 81
330 my $ready = length($self->{output}) < $self->{high_water}
331 && !$self->{transport_blocked}
332 81 ▶ && !$self->{transport_aborted};
Condition83%5/6
length $self->{'output'} < $self->{'high_water'} && !$self->{'transport_blocked'} && !$self->{'transport_aborted'}
!ll&&!rl&&r
6273
length $self->{"output"} < $self->{"high_water"} and not $self->{"transport_blocked"}
!ll&&!rl&&r
0675
MC/DC66%2/3
length $self->{'output'} < $self->{'high_water'} && !$self->{'transport_blocked'} && !$self->{'transport_aborted'}
length $self->{"output"} < $self->{"high_water"} not $self->{"transport_blocked"} !$self->{'transport_aborted'}
Truth table75%3/4
A and not(B) && not(C)
A= length $self->{"output"} < $self->{"high_water"}B= not $self->{"transport_blocked"}C= !$self->{'transport_aborted'}
ABCresult
0XX0
10X0
1100
1111
333 81 ▶ unless ($ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ready) 8 73 81
334 $self->{output_blocked} = 1
335 8 ▶ if length($self->{output}) >= $self->{high_water};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"output"} >= $self->{"high_water"} 0 8 8
336 $self->{transport_producer_blocked} = 1
337 8 ▶ if $self->{transport_blocked};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport_blocked"} 6 2 8
338 }
339 81 ▶ return $ready ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ready ? : 73 8 81
340 }
341  
342 64 ▶sub _sync_native_output {
Subroutine _sync_native_output: called CC=2 SCAR=7.0
343 64 my ($self) = @_;
344 64 ▶ return 1 unless $self->{backend} eq 'native';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"backend"} eq "native" 0 64 64
345 64 my $wire = $self->{native}->flush;
346 64 return $self->_queue_wire($wire);
347 }
348  
349 39 ▶sub _queue_portable_frame {
Subroutine _queue_portable_frame: called CC=7 SCAR=20.2
350 39 my ($self, $opcode, $payload, %option) = @_;
351 39 my %mask;
352 39 ▶ if ($self->{role} eq 'client') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"role"} eq "client") 13 26 39
353 my $key = $self->{random_bytes}
354 13 ▶ ? $self->{random_bytes}->(4)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'random_bytes'} ? : 10 3 13
355 : Unblock::WebSocket::_Random->bytes(4);
356 13 ▶ croak 'random byte provider must return exactly four bytes for a mask key'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or ref $key or length $key != 4 0 13 13
Condition33%2/6
not defined $key or ref $key or length $key != 4
l!l&&r!l&&!r
0013
not defined $key or ref $key
l!l&&r!l&&!r
0013
MC/DC0%0/3
not defined $key or ref $key or length $key != 4
not defined $key ref $key length $key != 4
Truth table25%1/4
not(A) or B or C
A= not defined $keyB= ref $keyC= length $key != 4
ABCresult
1XX1
01X1
0011
0000
357 if !defined($key) || ref($key) || length($key) != 4;
358 13 %mask = (mask_key => $key);
359 }
360 my $wire = Unblock::WebSocket::_Frame->encode(
361 opcode => $opcode,
362 payload => $payload,
363 39 ▶ masked => $self->{role} eq 'client' ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$self->{'role'} eq 'client' ? : 13 26 39
364 %mask,
365 %option,
366 );
367 39 return $self->_queue_wire($wire);
368 }
369  
370 52 ▶sub _queue_frame {
Subroutine _queue_frame: called CC=10 SCAR=23.0
371 52 my ($self, $opcode, $payload, %option) = @_;
372  
373 52 ▶ if ($self->{deflate} && ($opcode == 1 || $opcode == 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"deflate"} and $opcode == 1 || $opcode == 2) 6 46 52
Condition100%6/6
$self->{"deflate"} and $opcode == 1 || $opcode == 2
!ll&&!rl&&r
4516
$opcode == 1 or $opcode == 2
l!l&&r!l&&!r
511
MC/DC100%3/3
$self->{"deflate"} and $opcode == 1 || $opcode == 2
$self->{"deflate"} $opcode == 1 $opcode == 2
Truth table100%4/4
A and B or C
A= $self->{"deflate"}B= $opcode == 1C= $opcode == 2
ABCresult
0XX0
1000
11X1
1011
374 6 $payload = $self->{deflate}->compress($payload);
375 6 $option{rsv1} = 1;
376 }
377  
378 52 ▶ if ($self->{backend} eq 'native') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"backend"} eq "native") 15 37 52
379 15 ▶ my $compressed = delete($option{rsv1}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $option{'rsv1'} ? : 4 11 15
380 15 ▶ croak 'native frame options are internal-only' if %option;
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 15 15
381  
382 # A native input batch can contain a complete valid message followed by
383 # a malformed frame. bq has already entered error/closing state by the
384 # time Perl receives the earlier valid event, so its normal send API
385 # refuses the application response. Preserve wire ordering by encoding
386 # only responses generated while delivering those retained pre-error
387 # events through the portable framer. This is an exceptional error
388 # path; ordinary native traffic remains fully native.
389 15 ▶ if ($self->{native_pre_error_event}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"native_pre_error_event"} and $self->{"native"}->_error_code) 2 13 15
Condition66%2/3
$self->{"native_pre_error_event"} and $self->{"native"}->_error_code
!ll&&!rl&&r
1302
MC/DC50%1/2
$self->{"native_pre_error_event"} and $self->{"native"}->_error_code
$self->{"native_pre_error_event"} $self->{"native"}->_error_code
Truth table66%2/3
A and B
A= $self->{"native_pre_error_event"}B= $self->{"native"}->_error_code
ABresult
0X0
100
111
390 && $self->{native}->_error_code) {
391 2 ▶ return $self->_queue_portable_frame(
Branch: 1 branch
BranchTrueFalseTotal
$compressed ? : 1 1 2
392 $opcode,
393 $payload,
394 ($compressed ? (rsv1 => 1) : ()),
395 );
396 }
397  
398 $self->{native}->queue_message(
399 13 $opcode,
400 $payload,
401 $compressed,
402 );
403 13 return $self->_sync_native_output;
404 }
405  
406 37 return $self->_queue_portable_frame($opcode, $payload, %option);
407 }
408  
409 22 ▶sub send_text {
Subroutine send_text: called CC=6 SCAR=18.8
Pod undocumented
410 22 my ($self, $text) = @_;
411 22 ▶ croak 'send_text(): WebSocket is closing or closed' unless $self->is_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_open 0 22 22
412 22 ▶ croak 'send_text(): text must be a defined scalar'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $text or ref $text 0 22 22
Condition33%1/3
not defined $text or ref $text
l!l&&r!l&&!r
0022
MC/DC0%0/2
not defined $text or ref $text
not defined $text ref $text
Truth table33%1/3
not(A) or B
A= not defined $textB= ref $text
ABresult
1X1
011
000
413 if !defined($text) || ref($text);
414 22 my $bytes;
415 22 ▶ if (utf8::is_utf8($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($text)) { } 3 19 22
416 3 $bytes = Unblock::WebSocket::_UTF8->encode($text);
417 }
418 else {
419 19 $bytes = "$text";
420 19 ▶ croak 'send_text(): payload contains invalid UTF-8'
Branch: 1 branch
BranchTrueFalseTotal
unless "Unblock::WebSocket::_UTF8"->valid_bytes($bytes) 0 19 19
421 unless Unblock::WebSocket::_UTF8->valid_bytes($bytes);
422 }
423 22 return $self->_queue_frame(1, $bytes);
424 }
425  
426 4 ▶sub send_binary {
Subroutine send_binary: called CC=5 SCAR=17.8
Pod undocumented
427 4 my ($self, $bytes) = @_;
428 4 ▶ croak 'send_binary(): WebSocket is closing or closed' unless $self->is_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_open 0 4 4
429 4 ▶ croak 'send_binary(): payload must be a defined scalar'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bytes or ref $bytes 0 4 4
Condition33%1/3
not defined $bytes or ref $bytes
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $bytes or ref $bytes
not defined $bytes ref $bytes
Truth table33%1/3
not(A) or B
A= not defined $bytesB= ref $bytes
ABresult
1X1
011
000
430 if !defined($bytes) || ref($bytes);
431 4 my $copy = "$bytes";
432 4 ▶ croak 'send_binary(): payload must be a byte string'
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($copy, 1) 0 4 4
433 unless utf8::downgrade($copy, 1);
434 4 return $self->_queue_frame(2, $copy);
435 }
436  
437 6 ▶sub ping {
Subroutine ping: called CC=6 SCAR=18.6
Pod undocumented
438 6 my ($self, $bytes) = @_;
439 6 ▶ $bytes = '' unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 6 6
440 6 ▶ croak 'ping(): WebSocket is closing or closed' unless $self->is_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_open 0 6 6
441 6 ▶ croak 'ping(): payload must be a scalar' if ref($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if ref $bytes 0 6 6
442 6 my $copy = "$bytes";
443 6 ▶ croak 'ping(): payload must be a byte string'
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($copy, 1) 0 6 6
444 unless utf8::downgrade($copy, 1);
445 6 ▶ croak 'ping(): payload exceeds 125 bytes' if length($copy) > 125;
Branch: 1 branch
BranchTrueFalseTotal
if length $copy > 125 2 4 6
446 4 return $self->_queue_frame(9, $copy);
447 }
448  
449 42 ▶sub _valid_close_code {
Subroutine _valid_close_code: called CC=4 SCAR=14.6
450 42 my ($code) = @_;
451 42 ▶ return 1 if $VALID_CLOSE{$code};
Branch: 1 branch
BranchTrueFalseTotal
if $VALID_CLOSE{$code} 41 1 42
452 1 ▶ return 1 if $code >= 3000 && $code <= 4999;
Branch: 1 branch
BranchTrueFalseTotal
if $code >= 3000 and $code <= 4999 0 1 1
Condition33%1/3
$code >= 3000 and $code <= 4999
!ll&&!rl&&r
100
MC/DC0%0/2
$code >= 3000 and $code <= 4999
$code >= 3000 $code <= 4999
Truth table33%1/3
A and B
A= $code >= 3000B= $code <= 4999
ABresult
0X0
100
111
453 1 return 0;
454 }
455  
456 22 ▶sub _close_payload {
Subroutine _close_payload: called CC=10 SCAR=26.5
457 22 my ($code, $reason) = @_;
458 22 ▶ return '' unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 22 22
459 22 ▶ croak 'close(): code must be an integer from 1000 through 4999'
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $code and $code =~ /\A[0-9]+\z/ and _valid_close_code($code) 0 22 22
Condition33%2/6
not ref $code and $code =~ /\A[0-9]+\z/ and _valid_close_code($code)
!ll&&!rl&&r
0022
not ref $code and $code =~ /\A[0-9]+\z/
!ll&&!rl&&r
0022
MC/DC0%0/3
not ref $code and $code =~ /\A[0-9]+\z/ and _valid_close_code($code)
not ref $code $code =~ /\A[0-9]+\z/ _valid_close_code($code)
Truth table25%1/4
not(A) and B and C
A= not ref $codeB= $code =~ /\A[0-9]+\z/C= _valid_close_code($code)
ABCresult
0XX0
10X0
1100
1111
460 unless !ref($code) && $code =~ /\A[0-9]+\z/ && _valid_close_code($code);
461 22 ▶ $reason = '' unless defined $reason;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason 4 18 22
462 22 ▶ croak 'close(): reason must be a scalar' if ref($reason);
Branch: 1 branch
BranchTrueFalseTotal
if ref $reason 0 22 22
463 22 ▶ my $bytes = utf8::is_utf8($reason)
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($reason) ? : 0 22 22
464 ? Unblock::WebSocket::_UTF8->encode($reason)
465 : "$reason";
466 22 ▶ croak 'close(): reason contains invalid UTF-8'
Branch: 1 branch
BranchTrueFalseTotal
unless "Unblock::WebSocket::_UTF8"->valid_bytes($bytes) 0 22 22
467 unless Unblock::WebSocket::_UTF8->valid_bytes($bytes);
468 22 ▶ croak 'close(): reason exceeds 123 bytes' if length($bytes) > 123;
Branch: 1 branch
BranchTrueFalseTotal
if length $bytes > 123 0 22 22
469 22 return pack('n', $code) . $bytes;
470 }
471  
472 13 ▶sub close {
Subroutine close: called CC=8 SCAR=21.2
Pod undocumented
473 13 my ($self, %option) = @_;
474 croak 'close(): cannot be called from unblock_send()'
475 13 ▶ if $self->{transport_in_send};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport_in_send"} 0 13 13
476 13 ▶ return $self if $self->{closed} || $self->{sent_close};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $self->{"sent_close"} 0 13 13
Condition33%1/3
$self->{"closed"} or $self->{"sent_close"}
l!l&&r!l&&!r
0013
MC/DC0%0/2
$self->{"closed"} or $self->{"sent_close"}
$self->{"closed"} $self->{"sent_close"}
Truth table33%1/3
A or B
A= $self->{"closed"}B= $self->{"sent_close"}
ABresult
1X1
011
000
477 13 ▶ my $code = exists($option{code}) ? delete($option{code}) : 1000;
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'code'} ? : 9 4 13
478 13 my $reason = delete $option{reason};
479 13 ▶ croak 'close(): unknown option(s): ' . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 13 13
480 if %option;
481 13 my $payload = _close_payload($code, $reason);
482 13 ▶ if ($self->{backend} eq 'native') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'backend'} eq 'native') { } 6 7 13
483 6 ▶ $reason = '' unless defined $reason;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason 2 4 6
484 6 $self->{native}->queue_close($code, $reason);
485 6 $self->_sync_native_output;
486 }
487 else {
488 7 $self->_queue_frame(8, $payload);
489 }
490 13 $self->{sent_close} = 1;
491 13 return $self;
492 }
493  
494 2 ▶sub abort {
Subroutine abort: called CC=2 SCAR=7.2
Pod undocumented
495 2 my ($self) = @_;
496 2 ▶ if ($self->{transport_attached}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'transport_attached'}) { } 0 2 2
497 0 $self->_transport_abort('WebSocket aborted');
498 }
499 else {
500 2 $self->{closed} = 1;
501 2 $self->{output} = '';
502 }
503 2 return $self;
504 }
505  
506 10 ▶sub input_eof {
Subroutine input_eof: called CC=8 SCAR=20.9
Pod undocumented
507 10 my ($self) = @_;
508 10 ▶ return $self if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 2 8 10
509 croak 'input_eof(): cannot be called recursively from a callback'
510 8 ▶ if $self->{driving} || $self->{transport_syncing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"driving"} or $self->{"transport_syncing"} 0 8 8
Condition33%1/3
$self->{"driving"} or $self->{"transport_syncing"}
l!l&&r!l&&!r
008
MC/DC0%0/2
$self->{"driving"} or $self->{"transport_syncing"}
$self->{"driving"} $self->{"transport_syncing"}
Truth table33%1/3
A or B
A= $self->{"driving"}B= $self->{"transport_syncing"}
ABresult
1X1
011
000
511 8 ▶ if ($self->{transport_attached} && !$self->{received_close}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"transport_attached"} and not $self->{"received_close"}) 2 6 8
Condition100%3/3
$self->{"transport_attached"} and not $self->{"received_close"}
!ll&&!rl&&r
422
MC/DC100%2/2
$self->{"transport_attached"} and not $self->{"received_close"}
$self->{"transport_attached"} not $self->{"received_close"}
Truth table100%3/3
A and not(B)
A= $self->{"transport_attached"}B= not $self->{"received_close"}
ABresult
0X0
100
111
512 2 $self->_transport_abort('transport EOF without WebSocket Close');
513 2 return $self;
514 }
515 6 $self->{closed} = 1;
516 $self->_invoke('on_close', undef, 'transport EOF')
517 6 ▶ unless $self->{received_close};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"received_close"} 4 2 6
518 6 ▶ $self->_transport_sync if $self->{transport_attached};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport_attached"} 2 4 6
519 6 return $self;
520 }
521  
522 59 ▶sub output {
Subroutine output: called CC=9 SCAR=30.1
Pod undocumented
523 59 my ($self, $max) = @_;
524 croak 'output(): manual output is unavailable with an attached transport'
525 59 ▶ if $self->{transport_attached};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport_attached"} 2 57 59
526 croak 'output(): cannot be called recursively from a callback'
527 57 ▶ if $self->{driving};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"driving"} 0 57 57
528 57 ▶ return '' unless length $self->{output};
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"output"} 0 57 57
529  
530 57 my $take = length($self->{output});
531 57 ▶ if (defined $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max) 0 57 57
532 0 ▶ croak 'output(): maximum must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $max and $max =~ /\A[0-9]+\z/ and $max > 0 0 0 0
Condition0%0/6
not ref $max and $max =~ /\A[0-9]+\z/ and $max > 0
!ll&&!rl&&r
000
not ref $max and $max =~ /\A[0-9]+\z/
!ll&&!rl&&r
000
MC/DC0%0/3
not ref $max and $max =~ /\A[0-9]+\z/ and $max > 0
not ref $max $max =~ /\A[0-9]+\z/ $max > 0
Truth table0%0/4
not(A) and B and C
A= not ref $maxB= $max =~ /\A[0-9]+\z/C= $max > 0
ABCresult
0XX0
10X0
1100
1111
533 unless !ref($max) && $max =~ /\A[0-9]+\z/ && $max > 0;
534 0 ▶ $take = $max if $max < $take;
Branch: 1 branch
BranchTrueFalseTotal
if $max < $take 0 0 0
535 }
536  
537 57 my $bytes = substr($self->{output}, 0, $take, '');
538 57 $self->_notify_drain;
539 57 return $bytes;
540 }
541  
542 76 ▶sub input {
Subroutine input: called CC=19 SCAR=29.8
Pod undocumented
543 76 my ($self, $bytes) = @_;
544 76 ▶ croak 'input(): WebSocket is closed' if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 76 76
545 croak 'input(): cannot be called recursively from a callback'
546 76 ▶ if $self->{driving} || $self->{transport_syncing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"driving"} or $self->{"transport_syncing"} 0 76 76
Condition33%1/3
$self->{"driving"} or $self->{"transport_syncing"}
l!l&&r!l&&!r
0076
MC/DC0%0/2
$self->{"driving"} or $self->{"transport_syncing"}
$self->{"driving"} $self->{"transport_syncing"}
Truth table33%1/3
A or B
A= $self->{"driving"}B= $self->{"transport_syncing"}
ABresult
1X1
011
000
547  
548 76 ▶ if ($self->{backend} eq 'native') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"backend"} eq "native") 42 34 76
549 42 ▶ croak 'input(): bytes must be a scalar' if ref($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if ref $bytes 0 42 42
550 42 ▶ $bytes = '' unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 42 42
551 42 my $copy = "$bytes";
552 42 ▶ croak 'input(): bytes must be a byte string'
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($copy, 1) 0 42 42
553 unless utf8::downgrade($copy, 1);
554  
555 42 my $events = $self->{native}->feed($copy);
556 42 my $error_code = $self->{native}->_error_code;
557 {
558 42 local $self->{driving} = 1;
559 42 ▶ local $self->{native_pre_error_event} = $error_code ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$error_code ? : 10 32 42
560 42 $self->_handle_native_events($events);
561 }
562  
563 42 ▶ if ($error_code && !$self->{failed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_code and not $self->{"failed"}) 10 32 42
Condition66%2/3
$error_code and not $self->{"failed"}
!ll&&!rl&&r
32010
MC/DC50%1/2
$error_code and not $self->{"failed"}
$error_code not $self->{"failed"}
Truth table66%2/3
A and not(B)
A= $error_codeB= not $self->{"failed"}
ABresult
0X0
100
111
564 10 my $error = $self->{native}->_error_string;
565 10 my %message = (
566 LIMIT_MAX_RECV_MSG_SIZE => 'WebSocket message exceeds configured limit',
567 BAD_UTF8 => 'WebSocket text contains invalid UTF-8',
568 RESERVED_BIT => 'WebSocket frame uses reserved RSV bits',
569 BAD_CLOSE => 'invalid WebSocket Close payload',
570 );
571 10 ▶ $error = $message{$error} || "native WebSocket error: $error";
Condition100%2/2
$message{$error} || "native WebSocket error: $error"
l!l
82
MC/DC100%1/1
$message{$error} || "native WebSocket error: $error"
$message{$error}
Truth table100%2/2
A
A= $message{$error}
Aresult
11
00
572 10 $self->{failed} = 1;
573 10 $self->{sent_close} = 1;
574 10 $self->_invoke('on_error', $error);
575 }
576 42 $self->_sync_native_output;
577 42 ▶ $self->_transport_sync if $self->{transport_attached};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport_attached"} 3 39 42
578 42 return length($copy);
579 }
580  
581 34 my $count = $self->{parser}->input($bytes);
582 {
583 34 local $self->{driving} = 1;
584 34 ▶ while (!$self->{closed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"closed"}) 69 1 70
585 69 my $frame;
586 69 my $ok = eval { $frame = $self->{parser}->next_frame; 1 };
587 69 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 67 69
588 2 ▶ my $error = $@ || 'WebSocket frame parse failure';
Condition50%1/2
$@ || 'WebSocket frame parse failure'
l!l
20
MC/DC0%0/1
$@ || 'WebSocket frame parse failure'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
589 2 ▶ my $code = $error =~ /exceeds configured limit/ ? 1009 : 1002;
Branch: 1 branch
BranchTrueFalseTotal
$error =~ /exceeds configured limit/ ? : 0 2 2
590 2 $self->_fail($error, $code);
591 2 last;
592 }
593 67 ▶ last unless $frame;
Branch: 1 branch
BranchTrueFalseTotal
unless $frame 31 36 67
594 36 $self->_handle_frame($frame);
595 }
596 }
597 34 ▶ $self->_transport_sync if $self->{transport_attached};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transport_attached"} 3 31 34
598 34 return $count;
599 }
600  
601 42 ▶sub _handle_native_events {
Subroutine _handle_native_events: called CC=11 SCAR=24.1
602 42 my ($self, $events) = @_;
603 42 for my $event (@$events) {
604 40 ▶ last if $self->{closed} || $self->{failed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $self->{"failed"} 1 39 40
Condition66%2/3
$self->{"closed"} or $self->{"failed"}
l!l&&r!l&&!r
1039
MC/DC50%1/2
$self->{"closed"} or $self->{"failed"}
$self->{"closed"} $self->{"failed"}
Truth table66%2/3
A or B
A= $self->{"closed"}B= $self->{"failed"}
ABresult
1X1
011
000
605 39 my ($opcode, $payload, $flags) = @$event;
606 39 ▶ $flags ||= 0;
Condition100%2/2
$flags ||= 0
l!l
1029
MC/DC100%1/1
$flags ||= 0
$flags
Truth table100%2/2
A
A= $flags
Aresult
11
00
607  
608 39 ▶ if ($opcode == 1 || $opcode == 2) {
Branch: 4 branches
BranchTrueFalseTotal
if ($opcode == 1 or $opcode == 2) { } 22 17 39
elsif ($opcode == 8) { } 9 8 17
elsif ($opcode == 9) { } 7 1 8
elsif ($opcode == 10) { } 1 0 1
Condition100%3/3
$opcode == 1 or $opcode == 2
l!l&&r!l&&!r
16617
MC/DC100%2/2
$opcode == 1 or $opcode == 2
$opcode == 1 $opcode == 2
Truth table100%3/3
A or B
A= $opcode == 1B= $opcode == 2
ABresult
1X1
011
000
609 22 ▶ my $compressed = ($flags & 0x01) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 1 ? : 10 12 22
610 22 $self->_deliver_message($opcode, $payload, $compressed);
611 }
612 elsif ($opcode == 8) {
613 9 $self->_handle_native_close($payload);
614 }
615 elsif ($opcode == 9) {
616 7 $self->_invoke('on_ping', $payload);
617 }
618 elsif ($opcode == 10) {
619 1 $self->_invoke('on_pong', $payload);
620 }
621 else {
622 0 $self->_fail('native WebSocket engine returned unsupported event', 1002);
623 }
624 }
625 42 return;
626 }
627  
628 9 ▶sub _handle_native_close {
Subroutine _handle_native_close: called CC=5 SCAR=16.6
629 9 my ($self, $payload) = @_;
630 9 ▶ return if $self->{received_close};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"received_close"} 0 9 9
631  
632 9 my ($code, $reason);
633 9 my $ok = eval { ($code, $reason) = _parse_close($payload); 1 };
634 9 ▶ return $self->_fail($@ || 'invalid WebSocket Close payload', 1002)
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 9 9
Condition0%0/2
$@ || 'invalid WebSocket Close payload'
l!l
00
MC/DC0%0/1
$@ || 'invalid WebSocket Close payload'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
635 unless $ok;
636  
637 9 $self->{received_close} = 1;
638 9 ▶ $self->{sent_close} = 1 unless $self->{sent_close};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sent_close"} 4 5 9
639 9 $self->_invoke('on_close', $code, $reason);
640 9 return;
641 }
642  
643 36 ▶sub _handle_frame {
Subroutine _handle_frame: called CC=19 SCAR=33.1
644 36 my ($self, $frame) = @_;
645 36 my $opcode = $frame->{opcode};
646  
647 36 ▶ if ($opcode == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode == 0) 2 34 36
648 return $self->_fail(
649 'WebSocket continuation frame outside fragmented message', 1002
650 2 ▶ ) unless defined $self->{fragment_opcode};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"fragment_opcode"} 1 1 2
651  
652 1 $self->{fragment_payload} .= $frame->{payload};
653 my $limit = $self->{fragment_compressed}
654 ? _compressed_wire_limit($self->{max_message_size})
655 1 ▶ : $self->{max_message_size};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'fragment_compressed'} ? : 0 1 1
656 return $self->_fail('WebSocket message exceeds configured limit', 1009)
657 1 ▶ if length($self->{fragment_payload}) > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"fragment_payload"} > $limit 1 0 1
658  
659 0 ▶ if ($frame->{fin}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{"fin"}) 0 0 0
660 0 my $type_opcode = delete $self->{fragment_opcode};
661 0 my $payload = $self->{fragment_payload};
662 0 ▶ my $compressed = $self->{fragment_compressed} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'fragment_compressed'} ? : 0 0 0
663 0 $self->{fragment_payload} = '';
664 0 $self->{fragment_compressed} = 0;
665 0 $self->_deliver_message($type_opcode, $payload, $compressed);
666 }
667 0 return;
668 }
669  
670 34 ▶ if ($opcode == 1 || $opcode == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode == 1 or $opcode == 2) 17 17 34
Condition100%3/3
$opcode == 1 or $opcode == 2
l!l&&r!l&&!r
12517
MC/DC100%2/2
$opcode == 1 or $opcode == 2
$opcode == 1 $opcode == 2
Truth table100%3/3
A or B
A= $opcode == 1B= $opcode == 2
ABresult
1X1
011
000
671 return $self->_fail(
672 'WebSocket data frame while fragmented message is unfinished', 1002
673 17 ▶ ) if defined $self->{fragment_opcode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"fragment_opcode"} 1 16 17
674  
675 16 ▶ my $compressed = $frame->{rsv1} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$frame->{'rsv1'} ? : 3 13 16
676 16 ▶ if (!$frame->{fin}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame->{"fin"}) 2 14 16
677 2 $self->{fragment_opcode} = $opcode;
678 2 $self->{fragment_payload} = $frame->{payload};
679 2 $self->{fragment_compressed} = $compressed;
680  
681 my $limit = $compressed
682 ? _compressed_wire_limit($self->{max_message_size})
683 2 ▶ : $self->{max_message_size};
Branch: 1 branch
BranchTrueFalseTotal
$compressed ? : 0 2 2
684 return $self->_fail('WebSocket message exceeds configured limit', 1009)
685 2 ▶ if length($self->{fragment_payload}) > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"fragment_payload"} > $limit 0 2 2
686 2 return;
687 }
688 return $self->_deliver_message(
689 $opcode,
690 $frame->{payload},
691 14 $compressed,
692 );
693 }
694  
695 17 ▶ if ($opcode == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode == 8) 12 5 17
696 12 return $self->_handle_close($frame->{payload});
697 }
698  
699 5 ▶ if ($opcode == 9) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode == 9) 3 2 5
700 3 $self->_invoke('on_ping', $frame->{payload});
701 $self->_queue_frame(10, $frame->{payload})
702 3 ▶ unless $self->{closed} || $self->{failed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"closed"} or $self->{"failed"} 3 0 3
Condition33%1/3
$self->{"closed"} or $self->{"failed"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$self->{"closed"} or $self->{"failed"}
$self->{"closed"} $self->{"failed"}
Truth table33%1/3
A or B
A= $self->{"closed"}B= $self->{"failed"}
ABresult
1X1
011
000
703 3 return;
704 }
705  
706 2 ▶ if ($opcode == 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode == 10) 2 0 2
707 2 $self->_invoke('on_pong', $frame->{payload});
708 2 return;
709 }
710  
711 0 return $self->_fail('WebSocket frame has unsupported opcode', 1002);
712 }
713  
714 36 ▶sub _deliver_message {
Subroutine _deliver_message: called CC=8 SCAR=20.8
715 36 my ($self, $opcode, $payload, $compressed) = @_;
716  
717 36 ▶ if ($compressed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compressed) 13 23 36
718 13 my $ok = eval {
719 13 $payload = $self->{deflate}->decompress($payload);
720 11 1;
721 };
722 13 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 11 13
723 2 ▶ my $error = $@ || 'permessage-deflate decompression failed';
Condition50%1/2
$@ || 'permessage-deflate decompression failed'
l!l
20
MC/DC0%0/1
$@ || 'permessage-deflate decompression failed'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
724 2 ▶ my $code = $error =~ /exceeds configured limit/ ? 1009 : 1002;
Branch: 1 branch
BranchTrueFalseTotal
$error =~ /exceeds configured limit/ ? : 1 1 2
725 2 return $self->_fail($error, $code);
726 }
727 }
728  
729 return $self->_fail('WebSocket message exceeds configured limit', 1009)
730 34 ▶ if length($payload) > $self->{max_message_size};
Branch: 1 branch
BranchTrueFalseTotal
if length $payload > $self->{"max_message_size"} 0 34 34
731  
732 34 ▶ if ($opcode == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode == 1) { } 27 7 34
733 27 my $text = eval { Unblock::WebSocket::_UTF8->decode($payload) };
734 27 ▶ return $self->_fail('WebSocket text contains invalid UTF-8', 1007)
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 25 27
735 if $@;
736 25 $self->_invoke('on_message', $text, 'text');
737 }
738 else {
739 7 $self->_invoke('on_message', $payload, 'binary');
740 }
741 32 return;
742 }
743  
744 21 ▶sub _parse_close {
Subroutine _parse_close: called CC=5 SCAR=16.1
745 21 my ($payload) = @_;
746 21 ▶ return (undef, '') if length($payload) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $payload == 0 0 21 21
747 21 ▶ croak 'WebSocket Close payload has invalid one-byte length'
Branch: 1 branch
BranchTrueFalseTotal
if length $payload == 1 1 20 21
748 if length($payload) == 1;
749  
750 20 my $code = unpack('n', substr($payload, 0, 2));
751 20 ▶ croak "WebSocket Close uses invalid status code $code"
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_close_code($code) 1 19 20
752 unless _valid_close_code($code);
753 19 my $reason_bytes = substr($payload, 2);
754 19 my $reason = '';
755 19 ▶ if (length $reason_bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $reason_bytes) 11 8 19
756 11 $reason = Unblock::WebSocket::_UTF8->decode($reason_bytes);
757 }
758 18 return ($code, $reason);
759 }
760  
761 12 ▶sub _handle_close {
Subroutine _handle_close: called CC=6 SCAR=17.9
762 12 my ($self, $payload) = @_;
763 12 ▶ return if $self->{received_close};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"received_close"} 0 12 12
764  
765 12 my ($code, $reason);
766 12 my $ok = eval { ($code, $reason) = _parse_close($payload); 1 };
767 12 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 3 9 12
768 3 ▶ my $error = $@ || 'invalid WebSocket Close payload';
Condition50%1/2
$@ || 'invalid WebSocket Close payload'
l!l
30
MC/DC0%0/1
$@ || 'invalid WebSocket Close payload'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
769 3 ▶ my $close_code = $error =~ /UTF-8|Unicode scalar/i ? 1007 : 1002;
Branch: 1 branch
BranchTrueFalseTotal
$error =~ /UTF-8|Unicode scalar/i ? : 1 2 3
770 3 return $self->_fail($error, $close_code);
771 }
772  
773 9 $self->{received_close} = 1;
774 9 ▶ if (!$self->{sent_close}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sent_close"}) 3 6 9
775 3 $self->_queue_frame(8, $payload);
776 3 $self->{sent_close} = 1;
777 }
778 9 $self->_invoke('on_close', $code, $reason);
779 9 return;
780 }
781  
782 12 ▶sub _fail {
Subroutine _fail: called CC=7 SCAR=20.1
783 12 my ($self, $error, $close_code) = @_;
784 12 ▶ return if $self->{failed}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failed"}++ 0 12 12
785 12 $error = "$error";
786 12 $error =~ s/\s+\z//;
787 12 $self->_invoke('on_error', $error);
788 12 ▶ if (!$self->{sent_close} && !$self->{closed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sent_close"} or $self->{"closed"}) 12 0 12
Condition33%1/3
$self->{"sent_close"} or $self->{"closed"}
l!l&&r!l&&!r
0012
MC/DC0%0/2
$self->{"sent_close"} or $self->{"closed"}
$self->{"sent_close"} $self->{"closed"}
Truth table33%1/3
A or B
A= $self->{"sent_close"}B= $self->{"closed"}
ABresult
1X1
011
000
789 12 ▶ if ($self->{backend} eq 'native') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'backend'} eq 'native') { } 3 9 12
790 3 my $ok = eval { $self->{native}->queue_close($close_code, ''); 1 };
791 3 ▶ $self->_sync_native_output if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 3 0 3
792 }
793 else {
794 9 my $payload = eval { _close_payload($close_code, '') };
795 9 ▶ $self->_queue_frame(8, $payload) if defined $payload;
Branch: 1 branch
BranchTrueFalseTotal
if defined $payload 9 0 9
796 }
797 12 $self->{sent_close} = 1;
798 }
799 12 return;
800 }
801  
802 13 1;
« blib/lib/Unblock/WebSocket/_Frame.pm ↑ Summary blib/lib/Unblock/WebSocket/_Deflate.pm »