| 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::HTTP::Server::Connection'; | ||||||||||||||||||||||||||
|
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 | |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Linux::Event::Kernel::Timer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use Linux::Event::WebSocket::_Handshake; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use Linux::Event::WebSocket::_State; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 7 | ▶ | sub new ($class, %option) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 7 | my $self = $class->SUPER::new(%option); | |||||||||||||||||||||||||||
| 17 | 7 | my $config = $self->SUPER::data; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 7 | ▶ | croak 'WebSocket server connection is missing server configuration' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($config) and $config->isa("Linux::Event::WebSocket::Server::_Config")
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")
| |||||||||||||||||||||||||||||
| 20 | if !blessed($config) | ||||||||||||||||||||||||||||
| 21 | || !$config->isa('Linux::Event::WebSocket::Server::_Config'); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | my $state = Linux::Event::WebSocket::_State->new( | ||||||||||||||||||||||||||||
| 24 | endpoint_type => 'server', | ||||||||||||||||||||||||||||
| 25 | callbacks => $config->{callbacks}, | ||||||||||||||||||||||||||||
| 26 | subprotocols => $config->{subprotocols}, | ||||||||||||||||||||||||||||
| 27 | data => $config->{data}, | ||||||||||||||||||||||||||||
| 28 | secure => $config->{secure}, | ||||||||||||||||||||||||||||
| 29 | close_timeout => $config->{close_timeout}, | ||||||||||||||||||||||||||||
| 30 | max_message_size => $config->{max_message_size}, | ||||||||||||||||||||||||||||
| 31 | 7 | ); | |||||||||||||||||||||||||||
| 32 | 7 | $state->{connection_class} = $config->{connection_class}; | |||||||||||||||||||||||||||
| 33 | 7 | $state->{on_handshake} = $config->{on_handshake}; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 7 | $self->SUPER::data($state); | |||||||||||||||||||||||||||
| 36 | 7 | return $self; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 7 | ▶ | sub _websocket_state ($self) { | ||||||||||||||||||||||||||
|
Subroutine
_websocket_state: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 40 | 7 | my $state = $self->SUPER::data; | |||||||||||||||||||||||||||
| 41 | 7 | ▶ | croak 'WebSocket server connection state is unavailable' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($state) and $state->isa("Linux::Event::WebSocket::_State")
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")
| |||||||||||||||||||||||||||||
| 42 | if !blessed($state) | ||||||||||||||||||||||||||||
| 43 | || !$state->isa('Linux::Event::WebSocket::_State'); | ||||||||||||||||||||||||||||
| 44 | 7 | return $state; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub data ($self, @value) { | ||||||||||||||||||||||||||
|
Subroutine
data: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 0 | my $state = $self->_websocket_state; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | croak 'data() accepts at most one value' if @value > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $state->{data} = $value[0] if @value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | return $state->{data}; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub _report_handshake_error ($self, $error) { | ||||||||||||||||||||||||||
|
Subroutine
_report_handshake_error: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 55 | 0 | my $state = $self->_websocket_state; | |||||||||||||||||||||||||||
| 56 | 0 | my $message = "$error"; | |||||||||||||||||||||||||||
| 57 | 0 | $message =~ s/\s+\z//; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | if (my $callback = $state->{callbacks}{error}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | $callback->($self, $message); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | return; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 7 | ▶ | sub _finish_server_open ($timer) { | ||||||||||||||||||||||||||
|
Subroutine
_finish_server_open: called CC=7 SCAR=30.3
| |||||||||||||||||||||||||||||
| 65 | 7 | my $timer_state = $timer->data; | |||||||||||||||||||||||||||
| 66 | 7 | my $connection = $timer_state->{connection}; | |||||||||||||||||||||||||||
| 67 | 7 | ▶ | return if !$connection || $connection->is_closed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $connection or $connection->is_closed
MC/DC0%0/2
not $connection or $connection->is_closed
not $connection
$connection->is_closed
Truth table33%1/3
not(A) or B
A= not $connectionB= $connection->is_closed
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 7 | ▶ | if ($connection->isa('Linux::Event::WebSocket::Connection')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 7 | $connection->_ensure_websocket_open; | |||||||||||||||||||||||||||
| 71 | 7 | return; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | my $attempt = ($timer_state->{attempt} // 0) + 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
$timer_state->{"attempt"} // 0
MC/DC0%0/1
$timer_state->{"attempt"} // 0
$timer_state->{"attempt"}
Truth table0%0/2
A
A= $timer_state->{"attempt"}
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ($attempt > 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | $connection->_report_handshake_error( | |||||||||||||||||||||||||||
| 77 | 'WebSocket HTTP Upgrade did not complete protocol handoff' | ||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $connection->SUPER::close if !$connection->is_closed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | return; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | Linux::Event::Kernel::Timer->new( | ||||||||||||||||||||||||||||
| 84 | 0 | loop => $connection->loop, | |||||||||||||||||||||||||||
| 85 | after => 0, | ||||||||||||||||||||||||||||
| 86 | data => { | ||||||||||||||||||||||||||||
| 87 | connection => $connection, | ||||||||||||||||||||||||||||
| 88 | attempt => $attempt, | ||||||||||||||||||||||||||||
| 89 | }, | ||||||||||||||||||||||||||||
| 90 | on_timer => \&_finish_server_open, | ||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||
| 92 | 0 | return; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 7 | ▶ | sub _schedule_server_open ($self) { | ||||||||||||||||||||||||||
|
Subroutine
_schedule_server_open: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 7 | Linux::Event::Kernel::Timer->new( | |||||||||||||||||||||||||||
| 97 | loop => $self->loop, | ||||||||||||||||||||||||||||
| 98 | after => 0, | ||||||||||||||||||||||||||||
| 99 | data => { | ||||||||||||||||||||||||||||
| 100 | connection => $self, | ||||||||||||||||||||||||||||
| 101 | attempt => 0, | ||||||||||||||||||||||||||||
| 102 | }, | ||||||||||||||||||||||||||||
| 103 | on_timer => \&_finish_server_open, | ||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||
| 105 | 7 | return; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 7 | ▶ | sub on_request ($self, $request, $response) { | ||||||||||||||||||||||||||
|
Subroutine
on_request: called CC=5 SCAR=17.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 7 | my $state = $self->_websocket_state; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 7 | ▶ | if (my $callback = $state->{on_handshake}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 1 | my ($accepted, $ok, $error); | |||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||
| 114 | 1 | local $@; | |||||||||||||||||||||||||||
| 115 | 1 | $ok = eval { | |||||||||||||||||||||||||||
| 116 | 1 | $accepted = $callback->($request); | |||||||||||||||||||||||||||
| 117 | 1 | 1; | |||||||||||||||||||||||||||
| 118 | }; | ||||||||||||||||||||||||||||
| 119 | 1 | $error = $@; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | ▶ | if (!$ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | $self->_report_handshake_error($error); | |||||||||||||||||||||||||||
| 124 | 0 | $response->status(500); | |||||||||||||||||||||||||||
| 125 | 0 | $response->body("WebSocket handshake callback failed\n"); | |||||||||||||||||||||||||||
| 126 | 0 | return; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | ▶ | if (!$accepted) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | $response->status(403); | |||||||||||||||||||||||||||
| 131 | 0 | $response->body("WebSocket upgrade rejected\n"); | |||||||||||||||||||||||||||
| 132 | 0 | return; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 7 | my ($handshake, $ok, $error); | |||||||||||||||||||||||||||
| 137 | { | ||||||||||||||||||||||||||||
| 138 | 7 | local $@; | |||||||||||||||||||||||||||
| 139 | 7 | $ok = eval { | |||||||||||||||||||||||||||
| 140 | $handshake = Linux::Event::WebSocket::_Handshake->server_from_request( | ||||||||||||||||||||||||||||
| 141 | $request, | ||||||||||||||||||||||||||||
| 142 | subprotocols => $state->{subprotocols}, | ||||||||||||||||||||||||||||
| 143 | 7 | ); | |||||||||||||||||||||||||||
| 144 | 7 | Linux::Event::WebSocket::_Handshake->apply_server_response( | |||||||||||||||||||||||||||
| 145 | $handshake, | ||||||||||||||||||||||||||||
| 146 | $response, | ||||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||
| 148 | 7 | 1; | |||||||||||||||||||||||||||
| 149 | }; | ||||||||||||||||||||||||||||
| 150 | 7 | $error = $@; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 7 | ▶ | if (!$ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | $self->_report_handshake_error($error); | |||||||||||||||||||||||||||
| 155 | 0 | $response->status(400); | |||||||||||||||||||||||||||
| 156 | 0 | $response->body("Bad WebSocket handshake\n"); | |||||||||||||||||||||||||||
| 157 | 0 | return; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 7 | $state->{handshake} = $handshake; | |||||||||||||||||||||||||||
| 161 | 7 | $state->{request} = $request; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 7 | my $target = $state->{connection_class}; | |||||||||||||||||||||||||||
| 164 | 7 | $self->transaction->upgrade($target); | |||||||||||||||||||||||||||
| 165 | 7 | $self->_schedule_server_open; | |||||||||||||||||||||||||||
| 166 | 7 | return; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | sub on_error ($self, $error) { | ||||||||||||||||||||||||||
|
Subroutine
on_error: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 170 | 0 | my $state = $self->_websocket_state; | |||||||||||||||||||||||||||
| 171 | 0 | ▶ | if (my $callback = $state->{callbacks}{error}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | $callback->($self, $error); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 0 | return; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub on_eof ($self) { | ||||||||||||||||||||||||||
|
Subroutine
on_eof: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 178 | 0 | $self->_report_handshake_error('transport EOF during WebSocket handshake'); | |||||||||||||||||||||||||||
| 179 | 0 | ▶ | $self->SUPER::close if !$self->is_closed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | return; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # Linux::Event::HTTP intentionally preserves its constructor-installed drain | ||||||||||||||||||||||||||||
| 184 | # and close wrappers across transition_to(). Those wrappers retain these two | ||||||||||||||||||||||||||||
| 185 | # method coderefs, so forward them to the WebSocket lifecycle once the same | ||||||||||||||||||||||||||||
| 186 | # object has transitioned. | ||||||||||||||||||||||||||||
| 187 | 0 | ▶ | sub on_drain ($self) { | ||||||||||||||||||||||||||
|
Subroutine
on_drain: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | if ($self->isa('Linux::Event::WebSocket::Connection')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | Linux::Event::WebSocket::Connection::on_drain($self); | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 0 | return; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 3 | ▶ | sub on_close ($self) { | ||||||||||||||||||||||||||
|
Subroutine
on_close: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 195 | 3 | ▶ | if ($self->isa('Linux::Event::WebSocket::Connection')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 3 | $self->_websocket_transport_closed; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | 3 | return; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 6 | 1; | |||||||||||||||||||||||||||