blib/lib/Linux/Event/WebSocket/Connection.pm

filter: Statementstmt 84.7% 150 / 177 Branchbran 48.6% 36 / 74 Conditioncond 45.8% 11 / 24 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 81.6% 40 / 49 Podpod 33.3% 6 / 18 totaltotal 68.9% 247 / 358 CC 50 SCAR 15.1
CC
50
Cov
72%
data
12.0
_close_timeout_fired
12.0
_byte_payload
11.6
CRAP 4.5
« blib/lib/Linux/Event/WebSocket/Server.pm ↑ Summary blib/lib/Linux/Event/WebSocket/_UTF8.pm »
1 package Linux::Event::WebSocket::Connection;
2 7 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '0.003';
7  
8 7 ▶use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use utf8 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶use Linux::Event::Framer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Linux::Event::Kernel::Timer;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Linux::Event::WebSocket::_BQ ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Linux::Event::WebSocket::_Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use Linux::Event::WebSocket::_Handshake;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Linux::Event::WebSocket::_State;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use Linux::Event::WebSocket::_UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 7 Linux::Event::Framer->declare_native_consumer(
23 __PACKAGE__,
24 Linux::Event::WebSocket::_BQ->raw_consumer_definition,
25 );
26  
27 152 ▶sub _websocket_state ($self) {
Subroutine _websocket_state: called CC=3 SCAR=12.5
28 152 my $state = $self->SUPER::data;
29 152 ▶ croak ref($self) . ': missing Linux::Event::WebSocket connection state'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($state) and $state->isa("Linux::Event::WebSocket::_State") 0 152 152
Condition33%1/3
builtin::blessed($state) and $state->isa("Linux::Event::WebSocket::_State")
!ll&&!rl&&r
00152
MC/DC0%0/2
builtin::blessed($state) and $state->isa("Linux::Event::WebSocket::_State")
builtin::blessed($state) $state->isa("Linux::Event::WebSocket::_State")
Truth table33%1/3
A and B
A= builtin::blessed($state)B= $state->isa("Linux::Event::WebSocket::_State")
ABresult
0X0
100
111
30 if !blessed($state)
31 || !$state->isa('Linux::Event::WebSocket::_State');
32 152 return $state;
33 }
34  
35 0 ▶sub data ($self, @value) {
Subroutine data: not called CC=3 SCAR=24.8
Pod documented
36 0 my $state = $self->_websocket_state;
37 0 ▶ croak 'data() accepts at most one value' if @value > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @value > 1 0 0 0
38 0 ▶ $state->{data} = $value[0] if @value;
Branch: 1 branch
BranchTrueFalseTotal
if @value 0 0 0
39 0 return $state->{data};
40 }
41  
42 0 ▶sub endpoint_type ($self) { $self->_websocket_state->{endpoint_type} }
Subroutine endpoint_type: not called CC=1 SCAR=6.9
Pod undocumented
43 0 ▶sub is_open ($self) { !!$self->_websocket_state->{open} }
Subroutine is_open: not called CC=1 SCAR=6.9
Pod undocumented
44 0 ▶sub is_closing ($self) { !!$self->_websocket_state->{closing} }
Subroutine is_closing: not called CC=1 SCAR=6.9
Pod undocumented
45 2 ▶sub subprotocol ($self) { $self->_websocket_state->{subprotocol} }
Subroutine subprotocol: called CC=1 SCAR=0.0
Pod undocumented
46 4 ▶sub secure ($self) { !!$self->_websocket_state->{secure} }
Subroutine secure: called CC=1 SCAR=0.0
Pod undocumented
47 0 ▶sub url ($self) { $self->_websocket_state->{url} }
Subroutine url: not called CC=1 SCAR=6.9
Pod undocumented
48 1 ▶sub handshake_request ($self) { $self->_websocket_state->{request} }
Subroutine handshake_request: called CC=1 SCAR=0.0
Pod undocumented
49 2 ▶sub handshake_response ($self) { $self->_websocket_state->{response} }
Subroutine handshake_response: called CC=1 SCAR=0.0
Pod undocumented
50  
51 28 ▶sub _dispatch_websocket ($self, $name, @argument) {
Subroutine _dispatch_websocket: called CC=3 SCAR=11.2
52 28 my $state = $self->_websocket_state;
53 28 ▶ if (my $callback = $state->{callbacks}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $callback = $state->{"callbacks"}{$name}) 24 4 28
54 24 return $callback->($self, @argument);
55 }
56  
57 4 my $method = "websocket_$name";
58 4 ▶ if (my $handler = $self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handler = $self->can($method)) 0 4 4
59 0 return $handler->($self, @argument);
60 }
61 4 return;
62 }
63  
64 14 ▶sub _initialize_engine ($self, $state = undef, $native = undef) {
Subroutine _initialize_engine: called CC=6 SCAR=19.2
65 14 ▶ $state //= $self->_websocket_state;
Condition33%1/3
$state //= $self->_websocket_state
l!l&&r!l&&!r
1400
MC/DC0%0/2
$state //= $self->_websocket_state
$state $self->_websocket_state
Truth table33%1/3
A //= B
A= $stateB= $self->_websocket_state
ABresult
1X1
011
000
66 14 ▶ return $state->{engine} if $state->{engine};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"engine"} 0 14 14
67  
68 14 ▶ my %native = defined($native) ? (native => $native) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $native ? : 0 14 14
69 $state->{engine} = Linux::Event::WebSocket::_Engine->new(
70 connection => $self,
71 endpoint_type => $state->{endpoint_type},
72 max_message_size => $state->{max_message_size},
73 message_handler => $state->{message_handler},
74 14 %native,
75 );
76 14 return $state->{engine};
77 }
78  
79 14 ▶sub _prepare_message_handler ($self, $state) {
Subroutine _prepare_message_handler: called CC=2 SCAR=7.1
80 $state->{message_handler} = $state->{callbacks}{message}
81 14 ▶ // $self->can('websocket_message');
Condition66%2/3
$state->{"callbacks"}{"message"} // $self->can("websocket_message")
l!l&&r!l&&!r
806
MC/DC50%1/2
$state->{"callbacks"}{"message"} // $self->can("websocket_message")
$state->{"callbacks"}{"message"} $self->can("websocket_message")
Truth table66%2/3
A // B
A= $state->{"callbacks"}{"message"}B= $self->can("websocket_message")
ABresult
1X1
011
000
82 14 return;
83 }
84  
85 8 ▶sub _websocket_raw_config ($self) {
Subroutine _websocket_raw_config: called CC=1 SCAR=0.0
86 8 my $state = $self->_websocket_state;
87 8 return [ $state->{endpoint_type}, $state->{max_message_size} ];
88 }
89  
90 8 ▶sub _websocket_raw_native_ready ($self, $native) {
Subroutine _websocket_raw_native_ready: called CC=1 SCAR=0.0
91 8 my $state = $self->_ensure_websocket_open($native);
92 8 return $state->{engine};
93 }
94  
95 38 ▶sub _ensure_websocket_open ($self, $native = undef) {
Subroutine _ensure_websocket_open: called CC=6 SCAR=18.1
96 38 my $state = $self->SUPER::data;
97 return $state
98 if ref($state) eq 'Linux::Event::WebSocket::_State'
99 38 ▶ && $state->{open};
Branch: 1 branch
BranchTrueFalseTotal
if ref $state eq "Linux::Event::WebSocket::_State" and $state->{"open"} 24 14 38
Condition66%2/3
ref $state eq "Linux::Event::WebSocket::_State" and $state->{"open"}
!ll&&!rl&&r
01424
MC/DC50%1/2
ref $state eq "Linux::Event::WebSocket::_State" and $state->{"open"}
ref $state eq "Linux::Event::WebSocket::_State" $state->{"open"}
Truth table66%2/3
A and B
A= ref $state eq "Linux::Event::WebSocket::_State"B= $state->{"open"}
ABresult
0X0
100
111
100  
101 14 $state = $self->_websocket_state;
102 14 ▶ return $state if $state->{open};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"open"} 0 14 14
103  
104 14 $self->_prepare_message_handler($state);
105 14 $self->_initialize_engine($state, $native);
106 14 ▶ if (my $handshake = $state->{handshake}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handshake = $state->{"handshake"}) 14 0 14
107 $state->{subprotocol} =
108 14 Linux::Event::WebSocket::_Handshake->subprotocol($handshake);
109 }
110  
111 14 $state->{open} = 1;
112 14 $self->_dispatch_websocket('open');
113 14 return $state;
114 }
115  
116 3 ▶sub _byte_payload ($operation, $payload) {
Subroutine _byte_payload: called CC=5 SCAR=24.5
117 3 ▶ croak "$operation(): payload must be a defined scalar"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $payload or ref $payload 0 3 3
Condition33%1/3
not defined $payload or ref $payload
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $payload or ref $payload
not defined $payload ref $payload
Truth table33%1/3
not(A) or B
A= not defined $payloadB= ref $payload
ABresult
1X1
011
000
118 if !defined($payload) || ref($payload);
119 3 ▶ return $payload if !utf8::is_utf8($payload);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($payload) 3 0 3
120  
121 0 my $bytes = "$payload";
122 0 ▶ croak "$operation(): payload contains wide characters; encode it to bytes first"
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($bytes, 1) 0 0 0
123 if !utf8::downgrade($bytes, 1);
124 0 return $bytes;
125 }
126  
127 7 ▶sub send_text ($self, $payload) {
Subroutine send_text: called CC=4 SCAR=15.6
Pod undocumented
128 7 ▶ croak 'send_text(): payload must be a defined scalar'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $payload or ref $payload 0 7 7
Condition33%1/3
not defined $payload or ref $payload
l!l&&r!l&&!r
007
MC/DC0%0/2
not defined $payload or ref $payload
not defined $payload ref $payload
Truth table33%1/3
not(A) or B
A= not defined $payloadB= ref $payload
ABresult
1X1
011
000
129 if !defined($payload) || ref($payload);
130 7 my $state = $self->_ensure_websocket_open;
131 croak 'send_text(): WebSocket connection is closing'
132 7 ▶ if $state->{closing};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"closing"} 0 7 7
133  
134 7 return $state->{engine}->send_text($payload);
135 }
136  
137 2 ▶sub send_binary ($self, $payload) {
Subroutine send_binary: called CC=2 SCAR=7.0
Pod undocumented
138 2 my $state = $self->_ensure_websocket_open;
139 croak 'send_binary(): WebSocket connection is closing'
140 2 ▶ if $state->{closing};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"closing"} 0 2 2
141 2 my $bytes = _byte_payload('send_binary', $payload);
142 2 return $state->{engine}->send_binary($bytes);
143 }
144  
145 1 ▶sub ping ($self, $payload = '') {
Subroutine ping: called CC=3 SCAR=11.1
Pod undocumented
146 1 my $state = $self->_ensure_websocket_open;
147 1 ▶ croak 'ping(): WebSocket connection is closing' if $state->{closing};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"closing"} 0 1 1
148 1 my $bytes = _byte_payload('ping', $payload);
149 1 return $state->{engine}->ping($bytes);
150 }
151  
152 29 ▶sub _cancel_close_timer ($self) {
Subroutine _cancel_close_timer: called CC=2 SCAR=6.9
153 29 my $state = $self->_websocket_state;
154 29 ▶ if (my $timer = delete $state->{close_timer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $timer = delete $state->{"close_timer"}) 6 23 29
155 6 $timer->cancel;
156 }
157 29 return;
158 }
159  
160 0 ▶sub _close_timeout_fired ($timer) {
Subroutine _close_timeout_fired: not called CC=3 SCAR=24.8
161 0 my $self = $timer->data;
162 0 ▶ return if !$self || $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if not $self or $self->is_closed 0 0 0
Condition0%0/3
not $self or $self->is_closed
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self or $self->is_closed
not $self $self->is_closed
Truth table0%0/3
not(A) or B
A= not $selfB= $self->is_closed
ABresult
1X1
011
000
163 0 $self->abort;
164 0 return;
165 }
166  
167 6 ▶sub _arm_close_timeout ($self) {
Subroutine _arm_close_timeout: called CC=3 SCAR=12.3
168 6 my $state = $self->_websocket_state;
169 6 ▶ return if $state->{close_timer};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"close_timer"} 0 6 6
170  
171 6 ▶ if ($state->{close_timeout} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"close_timeout"} == 0) 0 6 6
172 0 $self->end;
173 0 return;
174 }
175  
176 $state->{close_timer} = Linux::Event::Kernel::Timer->new(
177 loop => $self->loop,
178 after => $state->{close_timeout},
179 6 data => $self,
180 on_timer => \&_close_timeout_fired,
181 );
182 6 return;
183 }
184  
185 6 ▶sub close ($self, %option) {
Subroutine close: called CC=8 SCAR=21.8
Pod documented
186 6 ▶ return $self if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 6 6
187 6 $self->_ensure_websocket_open;
188  
189 6 my $state = $self->_websocket_state;
190 6 ▶ return $self if $state->{closing};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"closing"} 0 6 6
191  
192 6 ▶ my $code = exists($option{code}) ? delete($option{code}) : 'SUCCESS';
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'code'} ? : 6 0 6
193 6 my $reason = delete $option{reason};
194 6 ▶ croak 'close(): unknown option(s): ' . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 6 6
195 if %option;
196  
197 6 my $reason_bytes = '';
198 6 ▶ if (defined $reason) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reason) 6 0 6
199 6 ▶ croak 'close(): reason must be a scalar' if ref $reason;
Branch: 1 branch
BranchTrueFalseTotal
if ref $reason 0 6 6
200 6 $reason_bytes = eval {
201 6 Linux::Event::WebSocket::_UTF8->encode($reason);
202 };
203 6 ▶ croak 'close(): reason contains invalid UTF-8' if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
204 }
205  
206 6 $state->{engine}->start_close($code, $reason_bytes);
207 6 $self->_arm_close_timeout;
208 6 return $self;
209 }
210  
211 5 ▶sub abort ($self) {
Subroutine abort: called CC=2 SCAR=7.0
Pod undocumented
212 5 ▶ return $self if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 5 5
213 5 $self->_cancel_close_timer;
214 5 $self->SUPER::close;
215 5 return $self;
216 }
217  
218 13 ▶sub _notify_websocket_close ($self, $code, $reason) {
Subroutine _notify_websocket_close: called CC=2 SCAR=7.0
219 13 my $state = $self->_websocket_state;
220 13 ▶ return if $state->{close_notified}++;
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"close_notified"}++ 0 13 13
221 13 $self->_cancel_close_timer;
222 13 $state->{closing} = 1;
223 13 $self->_dispatch_websocket('close', $code, $reason);
224 13 return;
225 }
226  
227 0 ▶sub _websocket_engine_message ($self, $payload, $type) {
Subroutine _websocket_engine_message: not called CC=2 SCAR=17.9
228 0 my $state = $self->SUPER::data;
229 0 ▶ if (my $handler = $state->{message_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handler = $state->{"message_handler"}) 0 0 0
230 0 $handler->($self, $payload, $type);
231 }
232 0 return;
233 }
234  
235 0 ▶sub _websocket_engine_error ($self, $error) {
Subroutine _websocket_engine_error: not called CC=1 SCAR=6.9
236 0 $self->_dispatch_websocket('error', $error);
237 0 return;
238 }
239  
240 14 ▶sub _websocket_engine_closing ($self) {
Subroutine _websocket_engine_closing: called CC=1 SCAR=0.0
241 14 $self->_websocket_state->{closing} = 1;
242 14 return;
243 }
244  
245 8 ▶sub _websocket_engine_close ($self, $code, $reason) {
Subroutine _websocket_engine_close: called CC=1 SCAR=0.0
246 8 $self->_notify_websocket_close($code, $reason);
247 8 return;
248 }
249  
250 4 ▶sub on_eof ($self) {
Subroutine on_eof: called CC=4 SCAR=14.1
Pod documented
251 4 ▶ return if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 4 4
252 4 my $state = $self->_websocket_state;
253 $self->_notify_websocket_close(undef, 'transport EOF')
254 4 ▶ if !$state->{close_notified};
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"close_notified"} 1 3 4
255 4 ▶ $self->SUPER::close if !$self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_closed 4 0 4
256 4 return;
257 }
258  
259 1 ▶sub on_error ($self, $error) {
Subroutine on_error: called CC=2 SCAR=7.1
Pod documented
260 1 ▶ return if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 1 1
261 1 $self->_dispatch_websocket('error', $error);
262 1 return;
263 }
264  
265 11 ▶sub on_close ($self) {
Subroutine on_close: called CC=1 SCAR=0.0
Pod documented
266 11 $self->_websocket_transport_closed;
267 11 return;
268 }
269  
270 0 ▶sub on_drain ($self) {
Subroutine on_drain: not called CC=2 SCAR=17.9
Pod documented
271 0 ▶ return if !$self->is_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_open 0 0 0
272 0 $self->_dispatch_websocket('drain');
273 0 return;
274 }
275  
276 11 ▶sub _websocket_transport_closed ($self) {
Subroutine _websocket_transport_closed: called CC=3 SCAR=11.0
277 11 my $state = $self->_websocket_state;
278 11 $self->_cancel_close_timer;
279 $self->_notify_websocket_close(undef, 'transport closed')
280 11 ▶ if $state->{open} && !$state->{close_notified};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"open"} and not $state->{"close_notified"} 4 7 11
Condition100%3/3
$state->{"open"} and not $state->{"close_notified"}
!ll&&!rl&&r
164
MC/DC100%2/2
$state->{"open"} and not $state->{"close_notified"}
$state->{"open"} not $state->{"close_notified"}
Truth table100%3/3
A and not(B)
A= $state->{"open"}B= not $state->{"close_notified"}
ABresult
0X0
100
111
281 11 return;
282 }
283  
284 7 1;
« blib/lib/Linux/Event/WebSocket/Server.pm ↑ Summary blib/lib/Linux/Event/WebSocket/_UTF8.pm »