blib/lib/Linux/Event/WebSocket/Server/_HTTPConnection.pm

filter: Statementstmt 69.6% 87 / 125 Branchbran 34.7% 16 / 46 Conditioncond 16.6% 4 / 24 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 71.8% 23 / 32 Podpod 58.3% 7 / 12 totaltotal 54.5% 138 / 253 CC 32 SCAR 19.9
CC
32
Cov
55%
on_eof
30.0
on_close
20.0
data
12.0
CRAP 7.3
« blib/lib/Linux/Event/WebSocket/Client/_HTTPConnection.pm ↑ Summary blib/lib/Linux/Event/WebSocket/Server.pm »
1 package Linux::Event::WebSocket::Server::_HTTPConnection;
2 6 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶use parent 'Linux::Event::IO::Sock::Stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Unblock::HTTP1::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Linux::Event::Framer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Linux::Event::WebSocket::_BQ ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Begin with the existing HTTP-byte native consumer so Linux::Event can
15 # replace it with the native bq WebSocket consumer during transition_to().
16 # Linux::Event forbids adding a first provider during a live transition.
17 6 Linux::Event::Framer->declare_native_consumer(
18 __PACKAGE__,
19 Linux::Event::WebSocket::_BQ->http_bridge_consumer_definition,
20 );
21  
22 82 ▶sub can ($class, $name) {
Subroutine can: called CC=2 SCAR=6.9
Pod undocumented
23 82 ▶ return undef if $name eq 'on_data';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "on_data" 5 77 82
24 77 return UNIVERSAL::can($class, $name);
25 }
26  
27 7 ▶sub _websocket_http_input ($self, $bytes) {
Subroutine _websocket_http_input: called CC=1 SCAR=0.0
28 7 return $self->on_data($bytes);
29 }
30  
31 6 ▶use Uniform::HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 ▶use Linux::Event::WebSocket::_Handshake;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 6 ▶use Linux::Event::WebSocket::_State;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # This is an ordinary Linux::Event stream for just the opening handshake.
36 # The standalone HTTP/1 engine owns validation/framing. Once its 101 output
37 # is accepted, transition_to() installs the existing native WebSocket
38 # consumer on this SAME stream; the WebSocket API and bq frame path stay intact.
39 7 ▶sub new ($class, %option) {
Subroutine new: called CC=3 SCAR=11.4
Pod undocumented
40 7 my $config = $option{data};
41 7 ▶ croak 'WebSocket server connection is missing server configuration'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($config) and $config->isa("Linux::Event::WebSocket::Server::_Config") 0 7 7
Condition33%1/3
builtin::blessed($config) and $config->isa("Linux::Event::WebSocket::Server::_Config")
!ll&&!rl&&r
007
MC/DC0%0/2
builtin::blessed($config) and $config->isa("Linux::Event::WebSocket::Server::_Config")
builtin::blessed($config) $config->isa("Linux::Event::WebSocket::Server::_Config")
Truth table33%1/3
A and B
A= builtin::blessed($config)B= $config->isa("Linux::Event::WebSocket::Server::_Config")
ABresult
0X0
100
111
42 unless blessed($config)
43 && $config->isa('Linux::Event::WebSocket::Server::_Config');
44  
45 my $state = Linux::Event::WebSocket::_State->new(
46 endpoint_type => 'server',
47 callbacks => $config->{callbacks},
48 subprotocols => $config->{subprotocols},
49 data => $config->{data},
50 secure => $config->{secure},
51 close_timeout => $config->{close_timeout},
52 max_message_size => $config->{max_message_size},
53 7 );
54 7 $state->{connection_class} = $config->{connection_class};
55 7 $state->{on_handshake} = $config->{on_handshake};
56 7 $option{data} = $state;
57 7 return $class->SUPER::new(%option);
58 }
59  
60 14 ▶sub _websocket_state ($self) {
Subroutine _websocket_state: called CC=3 SCAR=12.5
61 14 my $state = $self->SUPER::data;
62 14 ▶ croak 'WebSocket server connection state is unavailable'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($state) and $state->isa("Linux::Event::WebSocket::_State") 0 14 14
Condition33%1/3
builtin::blessed($state) and $state->isa("Linux::Event::WebSocket::_State")
!ll&&!rl&&r
0014
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
63 unless blessed($state) && $state->isa('Linux::Event::WebSocket::_State');
64 14 return $state;
65 }
66  
67 0 ▶sub data ($self, @value) {
Subroutine data: not called CC=3 SCAR=24.8
Pod documented
68 0 my $state = $self->_websocket_state;
69 0 ▶ croak 'data() accepts at most one value' if @value > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @value > 1 0 0 0
70 0 ▶ $state->{data} = $value[0] if @value;
Branch: 1 branch
BranchTrueFalseTotal
if @value 0 0 0
71 0 return $state->{data};
72 }
73  
74 0 ▶sub _report_handshake_error ($self, $error) {
Subroutine _report_handshake_error: not called CC=2 SCAR=17.9
75 0 my $state = $self->_websocket_state;
76 0 my $message = "$error";
77 0 $message =~ s/\s+\z//;
78 0 ▶ if (my $callback = $state->{callbacks}{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $callback = $state->{"callbacks"}{"error"}) 0 0 0
79 0 $callback->($self, $message);
80 }
81 0 return;
82 }
83  
84 7 ▶sub on_ready ($self) {
Subroutine on_ready: called CC=2 SCAR=8.1
Pod documented
85 my $http = Unblock::HTTP1::Server->new(
86 transport => $self,
87 7 ▶ on_request => sub ($tx, $request) {
Subroutine __ANON__: called CC=1 SCAR=0.0
88 7 $self->_on_http_request($tx, $request);
89 },
90 0 ▶ on_error => sub ($tx, $error) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
91 0 ▶ $self->_report_handshake_error($error) unless $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_closed 0 0 0
92 },
93 7 ▶ on_switch => sub ($tx, $response) {
Subroutine __ANON__: called CC=1 SCAR=0.0
94 7 $self->_on_http_switch($tx, $response);
95 },
96 7 );
97 7 ▶ $self->{ws_opening_http} = $http unless $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_closed 7 0 7
98 7 return;
99 }
100  
101 7 ▶sub _on_http_request ($self, $tx, $request) {
Subroutine _on_http_request: called CC=5 SCAR=18.1
102 7 my $state = $self->_websocket_state;
103 7 ▶ if (my $callback = $state->{on_handshake}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $callback = $state->{"on_handshake"}) 1 6 7
104 1 my $accepted = eval { $callback->($request) };
105 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
106 0 $self->_report_handshake_error($@);
107 0 $tx->respond(status => 500, body => "WebSocket handshake callback failed\n");
108 0 return;
109 }
110 1 ▶ unless ($accepted) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($accepted) 0 1 1
111 0 $tx->respond(status => 403, body => "WebSocket upgrade rejected\n");
112 0 return;
113 }
114 }
115  
116 7 my $handshake = eval {
117 Linux::Event::WebSocket::_Handshake->server_from_request(
118 $request, subprotocols => $state->{subprotocols},
119 7 );
120 };
121 7 ▶ if (!$handshake) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handshake) 0 7 7
122 0 $self->_report_handshake_error($@);
123 0 $tx->respond(status => 400, body => "Bad WebSocket handshake\n");
124 0 return;
125 }
126  
127 7 my $response = Uniform::HTTP::Response->new(status => 101);
128 7 Linux::Event::WebSocket::_Handshake->apply_server_response(
129 $handshake, $response,
130 );
131 7 $state->{handshake} = $handshake;
132 7 $state->{request} = $request;
133 7 $tx->respond($response);
134 7 return;
135 }
136  
137 7 ▶sub _on_http_switch ($self, $tx, $response) {
Subroutine _on_http_switch: called CC=5 SCAR=16.3
138 7 ▶ return if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 7 7
139 7 my $state = $self->_websocket_state;
140 7 $state->{response} = $response;
141 7 my $http = delete $self->{ws_opening_http};
142 7 ▶ my $tail = $http ? $http->take_remainder : '';
Branch: 1 branch
BranchTrueFalseTotal
$http ? : 7 0 7
143 7 $self->transition_to($state->{connection_class});
144 7 $self->_ensure_websocket_open;
145 # An HTTP header and first masked WebSocket frame can arrive in a single
146 # read. Hand off exactly the unparsed bytes after on_open.
147 7 ▶ $state->{engine}->feed($tail) if length $tail && !$self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if length $tail and not $self->is_closed 1 6 7
Condition66%2/3
length $tail and not $self->is_closed
!ll&&!rl&&r
601
MC/DC50%1/2
length $tail and not $self->is_closed
length $tail not $self->is_closed
Truth table66%2/3
A and not(B)
A= length $tailB= not $self->is_closed
ABresult
0X0
100
111
148 7 return;
149 }
150  
151 7 ▶sub on_data ($self, $bytes) {
Subroutine on_data: called CC=4 SCAR=16.1
Pod documented
152 7 my $http = $self->{ws_opening_http};
153 7 ▶ return unless $http;
Branch: 1 branch
BranchTrueFalseTotal
unless $http 0 7 7
154 7 my $ok = eval { $http->input($bytes); 1 };
155 7 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 7 7
156 0 $self->_report_handshake_error($@);
157 0 ▶ $self->SUPER::close unless $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_closed 0 0 0
158 }
159 7 return;
160 }
161  
162 0 ▶sub on_eof ($self) {
Subroutine on_eof: not called CC=5 SCAR=34.0
Pod documented
163 0 my $http = $self->{ws_opening_http};
164 0 ▶ if ($http && !$http->is_closed && !$http->is_switched) {
Branch: 1 branch
BranchTrueFalseTotal
if ($http and not $http->is_closed and not $http->is_switched) 0 0 0
Condition0%0/6
$http and not $http->is_closed and not $http->is_switched
!ll&&!rl&&r
000
$http and not $http->is_closed
!ll&&!rl&&r
000
MC/DC0%0/3
$http and not $http->is_closed and not $http->is_switched
$http not $http->is_closed not $http->is_switched
Truth table0%0/4
A and not(B) and not(C)
A= $httpB= not $http->is_closedC= not $http->is_switched
ABCresult
0XX0
10X0
1100
1111
165 0 eval { $http->input_eof };
166 }
167 0 ▶ $self->SUPER::close unless $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_closed 0 0 0
168 0 return;
169 }
170  
171 0 ▶sub on_error ($self, $error) {
Subroutine on_error: not called CC=1 SCAR=6.9
Pod documented
172 0 $self->_report_handshake_error($error);
173 0 return;
174 }
175  
176 0 ▶sub on_drain ($self) {
Subroutine on_drain: not called CC=3 SCAR=24.8
Pod documented
177 0 my $http = $self->{ws_opening_http};
178 0 ▶ $http->resume_output if $http && !$http->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $http and not $http->is_closed 0 0 0
Condition0%0/3
$http and not $http->is_closed
!ll&&!rl&&r
000
MC/DC0%0/2
$http and not $http->is_closed
$http not $http->is_closed
Truth table0%0/3
A and not(B)
A= $httpB= not $http->is_closed
ABresult
0X0
100
111
179 0 return;
180 }
181  
182 0 ▶sub on_close ($self) {
Subroutine on_close: not called CC=4 SCAR=30.0
Pod documented
183 0 my $http = delete $self->{ws_opening_http};
184 0 ▶ $http->transport_error('WebSocket opening transport closed')
Branch: 1 branch
BranchTrueFalseTotal
if $http and not $http->is_closed and not $http->is_switched 0 0 0
Condition0%0/6
$http and not $http->is_closed and not $http->is_switched
!ll&&!rl&&r
000
$http and not $http->is_closed
!ll&&!rl&&r
000
MC/DC0%0/3
$http and not $http->is_closed and not $http->is_switched
$http not $http->is_closed not $http->is_switched
Truth table0%0/4
A and not(B) and not(C)
A= $httpB= not $http->is_closedC= not $http->is_switched
ABCresult
0XX0
10X0
1100
1111
185 if $http && !$http->is_closed && !$http->is_switched;
186 0 return;
187 }
188  
189 7 ▶sub unblock_send ($self, $bytes) { return $self->write($bytes) }
Subroutine unblock_send: called CC=1 SCAR=0.0
Pod undocumented
190 0 ▶sub unblock_finish ($self) { return $self->end }
Subroutine unblock_finish: not called CC=1 SCAR=6.9
Pod undocumented
191 0 ▶sub unblock_abort ($self, $error) {
Subroutine unblock_abort: not called CC=2 SCAR=17.9
Pod undocumented
192 0 ▶ $self->SUPER::close unless $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_closed 0 0 0
193 0 return;
194 }
195  
196 6 1;
« blib/lib/Linux/Event/WebSocket/Client/_HTTPConnection.pm ↑ Summary blib/lib/Linux/Event/WebSocket/Server.pm »