HTTP/Server.pm

filter: Statementstmt 22.8% 79 / 346 Branchbran 28.9% 48 / 166 Conditioncond 35.9% 37 / 103 MC/DCmcdc 21.0% 12 / 57 Subroutinesub 32.5% 13 / 40 Podpod 100% 6 / 6 totaltotal 27.1% 195 / 718 CC 129 SCAR 44.7
CC
129
Cov
27%
__ANON__
306.0
__ANON__
306.0
__ANON__
132.0
CRAP 87.8
« HTTP/Client.pm ↑ Summary HTTP/_Policy.pm »
1 package Unblock::HTTP::Server;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Scalar::Util qw(blessed refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Unblock::HTTP::_Policy;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Unblock::HTTP::_EngineFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 our $VERSION = '0.03';
11  
12 26 ▶sub new {
Subroutine new: called CC=23 SCAR=34.8
Pod documented
13 26 my ($class, @args) = @_;
14 26 ▶ croak 'new(): expected named arguments' if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 26 26
15 26 my %options = @args;
16 26 my (%protocols, %callbacks);
17 26 for my $name (qw(http1 http2 http3)) {
18 78 ▶ $protocols{$name} = delete $options{$name} if exists $options{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{$name} 27 51 78
19 }
20 26 for my $name (qw(on_headers on_request on_error on_websocket on_handoff)) {
21 127 ▶ next unless exists $options{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{$name} 101 26 127
22 26 my $callback = delete $options{$name};
23 26 ▶ croak "new(): $name must be a coderef" unless ref($callback) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 1 25 26
24 25 $callbacks{$name} = $callback;
25 }
26 25 my $defer = delete $options{defer};
27 25 ▶ croak 'new(): defer must be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
if defined $defer and ref $defer ne "CODE" 0 25 25
Condition33%1/3
defined $defer and ref $defer ne "CODE"
!ll&&!rl&&r
2500
MC/DC0%0/2
defined $defer and ref $defer ne "CODE"
defined $defer ref $defer ne "CODE"
Truth table33%1/3
A and B
A= defined $deferB= ref $defer ne "CODE"
ABresult
0X0
100
111
28 if defined($defer) && ref($defer) ne 'CODE';
29 25 ▶ croak 'new(): unknown option: ' . join(', ', sort keys %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 3 22 25
30 22 ▶ croak 'new(): on_request callback is required' unless $callbacks{on_request};
Branch: 1 branch
BranchTrueFalseTotal
unless $callbacks{"on_request"} 0 22 22
31 22 my $policy = Unblock::HTTP::_Policy->new(%protocols, _websocket_role => 'server');
32 11 ▶ my $websocket_enabled = $policy->websocket_enabled('http/1.1')
Condition100%6/6
$policy->websocket_enabled('http/1.1') || $policy->websocket_enabled('h2') || $policy->websocket_enabled('h3')
l!l&&r!l&&!r
218
$policy->websocket_enabled("http/1.1") or $policy->websocket_enabled("h2")
l!l&&r!l&&!r
119
MC/DC100%3/3
$policy->websocket_enabled('http/1.1') || $policy->websocket_enabled('h2') || $policy->websocket_enabled('h3')
$policy->websocket_enabled("http/1.1") $policy->websocket_enabled("h2") $policy->websocket_enabled('h3')
Truth table100%4/4
A or B || C
A= $policy->websocket_enabled("http/1.1")B= $policy->websocket_enabled("h2")C= $policy->websocket_enabled('h3')
ABCresult
1XX1
01X1
0011
0000
33 || $policy->websocket_enabled('h2')
34 || $policy->websocket_enabled('h3');
35 croak 'new(): on_websocket callback is required for enabled WebSocket support'
36 11 ▶ if $websocket_enabled && !$callbacks{on_websocket};
Branch: 1 branch
BranchTrueFalseTotal
if $websocket_enabled and not $callbacks{"on_websocket"} 3 8 11
Condition66%2/3
$websocket_enabled and not $callbacks{"on_websocket"}
!ll&&!rl&&r
803
MC/DC50%1/2
$websocket_enabled and not $callbacks{"on_websocket"}
$websocket_enabled not $callbacks{"on_websocket"}
Truth table66%2/3
A and not(B)
A= $websocket_enabledB= not $callbacks{"on_websocket"}
ABresult
0X0
100
111
37 croak 'new(): on_websocket requires an enabled websocket protocol'
38 8 ▶ if !$websocket_enabled && $callbacks{on_websocket};
Branch: 1 branch
BranchTrueFalseTotal
if not $websocket_enabled and $callbacks{"on_websocket"} 0 8 8
Condition33%1/3
not $websocket_enabled and $callbacks{"on_websocket"}
!ll&&!rl&&r
080
MC/DC0%0/2
not $websocket_enabled and $callbacks{"on_websocket"}
not $websocket_enabled $callbacks{"on_websocket"}
Truth table33%1/3
not(A) and B
A= not $websocket_enabledB= $callbacks{"on_websocket"}
ABresult
0X0
100
111
39 croak 'new(): multiplexed stream handoff requires a defer callback'
40 if ($policy->websocket_enabled('h2')
41 || $policy->websocket_enabled('h3')
42 8 ▶ || $callbacks{on_handoff}) && !$defer;
Branch: 1 branch
BranchTrueFalseTotal
if $policy->websocket_enabled("h2") || $policy->websocket_enabled("h3") || $callbacks{"on_handoff"} and not $defer 0 8 8
Condition33%3/9
$policy->websocket_enabled("h2") || $policy->websocket_enabled("h3") || $callbacks{"on_handoff"} and not $defer
!ll&&!rl&&r
800
$policy->websocket_enabled("h2") or $policy->websocket_enabled("h3") or $callbacks{"on_handoff"}
l!l&&r!l&&!r
008
$policy->websocket_enabled("h2") or $policy->websocket_enabled("h3")
l!l&&r!l&&!r
008
MC/DC0%0/4
$policy->websocket_enabled("h2") || $policy->websocket_enabled("h3") || $callbacks{"on_handoff"} and not $defer
$policy->websocket_enabled("h2") $policy->websocket_enabled("h3") $callbacks{"on_handoff"} not $defer
Truth table14%1/7
A or B or C and not(D)
A= $policy->websocket_enabled("h2")B= $policy->websocket_enabled("h3")C= $callbacks{"on_handoff"}D= not $defer
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
43 return bless {
44 policy => $policy,
45 on_websocket => $callbacks{on_websocket},
46 on_handoff => $callbacks{on_handoff},
47 defer => $defer,
48 # Map once at definition time; no transaction or body wrapper.
49 callbacks => {
50 on_request => $callbacks{on_headers} || \&_ignore_headers,
51 on_request_end => $callbacks{on_request},
52 8 ▶ ($callbacks{on_error} ? (on_error => $callbacks{on_error}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$callbacks{'on_error'} ? : 0 8 8
Condition50%1/2
$callbacks{'on_headers'} || \&Unblock::HTTP::Server::_ignore_headers
l!l
08
MC/DC0%0/1
$callbacks{'on_headers'} || \&Unblock::HTTP::Server::_ignore_headers
$callbacks{'on_headers'}
Truth table50%1/2
A
A= $callbacks{'on_headers'}
Aresult
11
00
53 },
54 }, $class;
55 }
56  
57 2 ▶sub _ignore_headers { return }
Subroutine _ignore_headers: called CC=1 SCAR=0.0
58  
59 # Recognize even incomplete WebSocket attempts so a malformed upgrade is not
60 # silently treated as an ordinary application request.
61 0 ▶sub _websocket_candidate {
Subroutine _websocket_candidate: not called CC=6 SCAR=37.4
62 0 my ($request) = @_;
63 0 for my $index (0 .. $request->header_count - 1) {
64 0 my $name = lc $request->header_name($index);
65 0 ▶ return 1 if $name =~ /\Asec-websocket-/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\Asec-websocket-/ 0 0 0
66 0 ▶ next unless $name eq 'upgrade';
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq "upgrade" 0 0 0
67 0 for my $token (split /,/, $request->header_value($index)) {
68 0 ▶ return 1 if $token =~ /\A[ \t]*websocket[ \t]*\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $token =~ /\A[ \t]*websocket[ \t]*\z/i 0 0 0
69 }
70 }
71 0 return 0;
72 }
73  
74 # HTTP1 sends a complete 101 Upgrade or successful CONNECT response before
75 # yielding the socket. The new consumer is framework-owned and implements
76 # the generic input/EOF/error/output interface, NOT an HTTP or WS class.
77 0 ▶sub _generic_switch {
Subroutine _generic_switch: not called CC=6 SCAR=37.4
78 0 my ($self, $host, $engine, $tx, $response) = @_;
79 0 ▶ my $handler = $self->{on_handoff} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $handler = $self->{"on_handoff"} or return 0 0 0
80 0 my $successor = $handler->($tx, $tx->request, $response, $host);
81 0 ▶ croak 'on_handoff must return a successor protocol consumer'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($successor) 0 0 0
82 unless blessed($successor);
83 0 for my $method (qw(input input_eof transport_error resume_output)) {
84 0 ▶ croak "on_handoff successor must implement $method()"
Branch: 1 branch
BranchTrueFalseTotal
unless $successor->can($method) 0 0 0
85 unless $successor->can($method);
86 }
87 0 $host->unblock_upgrade($successor);
88 0 my $tail = $engine->take_remainder;
89 0 ▶ $successor->input($tail) if length $tail;
Branch: 1 branch
BranchTrueFalseTotal
if length $tail 0 0 0
90 0 return;
91 }
92  
93 0 ▶sub _attach_handoff_stream {
Subroutine _attach_handoff_stream: not called CC=2 SCAR=17.9
94 0 my ($self, $transport, $engine_options) = @_;
95 0 ▶ croak 'attach_stream(): HTTP1 handoff requires unblock_upgrade()'
Branch: 1 branch
BranchTrueFalseTotal
unless $transport->can("unblock_upgrade") 0 0 0
96 unless $transport->can('unblock_upgrade');
97 0 my $host = $transport;
98 0 weaken($host);
99 0 my $engine;
100 0 my %callbacks = %{ $self->{callbacks} };
101 0 ▶ $callbacks{on_switch} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
102 0 my ($tx, $response) = @_;
103 0 ▶ croak 'handoff transport no longer exists' unless $host;
Branch: 1 branch
BranchTrueFalseTotal
unless $host 0 0 0
104 0 $self->_generic_switch($host, $engine, $tx, $response);
105 0 };
106 0 my $attached = Unblock::HTTP::_EngineFactory->create(
107 'Server', 'http/1.1', %$engine_options,
108 %callbacks, transport => $transport,
109 );
110 0 $engine = $attached;
111 0 weaken($engine);
112 0 return $attached;
113 }
114  
115 # HTTP1 switches the complete byte stream, not an individual transaction.
116 # Keep the lower engine and transport as the only byte owners. Only this
117 # opted-in attachment gets a pair of per-connection transition callbacks.
118 0 ▶sub _attach_websocket_stream {
Subroutine _attach_websocket_stream: not called CC=2 SCAR=17.9
119 0 my ($self, $transport, $engine_options) = @_;
120 0 ▶ croak 'attach_stream(): HTTP1 WebSocket transport must implement unblock_upgrade()'
Branch: 1 branch
BranchTrueFalseTotal
unless $transport->can("unblock_upgrade") 0 0 0
121 unless $transport->can('unblock_upgrade');
122  
123 0 my $host = $transport;
124 0 weaken($host);
125 0 my $engine;
126 0 my $handler = $self->{on_websocket};
127 0 my $on_http_request = $self->{callbacks}{on_request_end};
128 0 my $pending;
129  
130 0 ▶ my $on_request_end = sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
131 0 my ($tx, $request) = @_;
132 0 ▶ return $on_http_request->(@_) unless _websocket_candidate($request);
Branch: 1 branch
BranchTrueFalseTotal
unless _websocket_candidate($request) 0 0 0
133  
134 0 my $selected = $handler->($tx, $request);
135 0 ▶ unless (defined $selected) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $selected) 0 0 0
136 0 $tx->respond(status => 403, body => 'WebSocket denied');
137 0 return;
138 }
139 0 ▶ croak 'on_websocket must return a hash reference or undef'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $selected eq "HASH" 0 0 0
140 unless ref($selected) eq 'HASH';
141 0 my %option = %$selected;
142 0 my %handshake_option;
143 0 for my $name (qw(subprotocols permessage_deflate)) {
144 $handshake_option{$name} = delete $option{$name}
145 0 ▶ if exists $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{$name} 0 0 0
146 }
147 0 my $on_open = delete $option{on_open};
148 0 ▶ croak 'on_websocket on_open must be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_open and ref $on_open ne "CODE" 0 0 0
Condition0%0/3
defined $on_open and ref $on_open ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $on_open and ref $on_open ne "CODE"
defined $on_open ref $on_open ne "CODE"
Truth table0%0/3
A and B
A= defined $on_openB= ref $on_open ne "CODE"
ABresult
0X0
100
111
149 if defined($on_open) && ref($on_open) ne 'CODE';
150 croak 'on_websocket may not override the attached transport'
151 0 ▶ if exists $option{transport};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{"transport"} 0 0 0
152  
153 0 require Unblock::WebSocket::Handshake;
154 0 require Unblock::WebSocket::Server;
155 0 my $handshake;
156 0 my $valid = eval {
157 0 $handshake = Unblock::WebSocket::Handshake->server_accept(
158 $request, %handshake_option,
159 );
160 0 1;
161 };
162 0 ▶ unless ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid) 0 0 0
163 0 $tx->respond(status => 400, body => 'Invalid WebSocket handshake');
164 0 return;
165 }
166 my $ws = Unblock::WebSocket::Server->new(
167 0 %{ $handshake->connection_options },
168 %option,
169 transport => $host,
170 );
171 0 $pending = {
172 transaction => refaddr($tx),
173 websocket => $ws,
174 on_open => $on_open,
175 request => $request,
176 };
177 # Even a failed host write must release the pending WebSocket state.
178 0 ▶ $tx->on_finish(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
179 $pending = undef
180 0 ▶ if $pending && $pending->{transaction} == refaddr($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $pending and $pending->{"transaction"} == builtin::refaddr($_[0]) 0 0 0
Condition0%0/3
$pending and $pending->{"transaction"} == builtin::refaddr($_[0])
!ll&&!rl&&r
000
MC/DC0%0/2
$pending and $pending->{"transaction"} == builtin::refaddr($_[0])
$pending $pending->{"transaction"} == builtin::refaddr($_[0])
Truth table0%0/3
A and B
A= $pendingB= $pending->{"transaction"} == builtin::refaddr($_[0])
ABresult
0X0
100
111
181 0 });
182 0 my $ok = eval { $tx->respond($handshake->server_response); 1 };
183 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
184 0 $pending = undef;
185 0 die $@;
186 }
187 0 return;
188 0 };
189  
190 0 ▶ my $on_switch = sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
191 0 my ($tx) = @_;
192 # An ordinary CONNECT or unrelated protocol switch remains the
193 # lower HTTP1 engine's responsibility, not an implicit WebSocket.
194 0 ▶ if (!$pending) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pending) 0 0 0
195 0 ▶ croak 'handoff transport no longer exists' unless $host;
Branch: 1 branch
BranchTrueFalseTotal
unless $host 0 0 0
196 return $self->_generic_switch($host, $engine, $tx, $tx->response)
197 0 ▶ if $self->{on_handoff};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_handoff"} 0 0 0
198 0 return;
199 }
200 croak 'WebSocket switch transaction does not match the handshake'
201 0 ▶ unless $pending->{transaction} == refaddr($tx);
Branch: 1 branch
BranchTrueFalseTotal
unless $pending->{"transaction"} == builtin::refaddr($tx) 0 0 0
202 0 my $ready = $pending;
203 0 $pending = undef;
204 0 my $ws = $ready->{websocket};
205 0 ▶ croak 'WebSocket transport was destroyed during handshake' unless $host;
Branch: 1 branch
BranchTrueFalseTotal
unless $host 0 0 0
206 0 $host->unblock_upgrade($ws);
207 $ready->{on_open}->($ws, $tx, $ready->{request})
208 0 ▶ if $ready->{on_open};
Branch: 1 branch
BranchTrueFalseTotal
if $ready->{"on_open"} 0 0 0
209 0 my $tail = $engine->take_remainder;
210 0 ▶ $ws->input($tail) if length $tail;
Branch: 1 branch
BranchTrueFalseTotal
if length $tail 0 0 0
211 0 return;
212 0 };
213  
214 0 my %callbacks = %{ $self->{callbacks} };
215 0 $callbacks{on_request_end} = $on_request_end;
216 0 $callbacks{on_switch} = $on_switch;
217 0 my $attached = Unblock::HTTP::_EngineFactory->create(
218 'Server', 'http/1.1', %$engine_options,
219 %callbacks, transport => $transport,
220 );
221 0 $engine = $attached;
222 0 weaken($engine);
223 0 return $attached;
224 }
225  
226 # A generic CONNECT takes ownership of one multiplexed stream, not the
227 # TCP/TLS/QUIC transport. Reuse the existing bounded deferred body bridge.
228 # The caller supplies a byte consumer, never a new HTTP parser.
229 0 ▶sub _generic_multiplexed_handoff {
Subroutine _generic_multiplexed_handoff: not called CC=5 SCAR=34.0
230 0 my ($self, $tx, $request, $sessions, $attempts) = @_;
231 0 my $id = $tx->stream_id;
232 0 $attempts->{$id} = 1;
233 0 require Unblock::HTTP::_WebSocketH2Body;
234 my $bridge = Unblock::HTTP::_WebSocketH2Body->new(
235 transaction => $tx, defer => $self->{defer},
236 0 );
237 0 my $consumer = $self->{on_handoff}->($tx, $request, $bridge);
238 0 ▶ unless (defined $consumer) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $consumer) 0 0 0
239 0 $bridge->detach;
240 0 ▶ $tx->on_finish(sub { delete $attempts->{$id} });
Subroutine __ANON__: not called CC=1 SCAR=6.9
241 0 $tx->respond(status => 403, body => 'CONNECT denied');
242 0 return;
243 }
244 0 ▶ croak 'on_handoff must return a stream consumer'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($consumer) 0 0 0
245 unless blessed($consumer);
246 0 for my $method (qw(input input_eof transport_error resume_output)) {
247 0 ▶ croak "on_handoff stream consumer must implement $method()"
Branch: 1 branch
BranchTrueFalseTotal
unless $consumer->can($method) 0 0 0
248 unless $consumer->can($method);
249 }
250 0 $bridge->consumer($consumer);
251 0 $sessions->{$id} = {
252 bridge => $bridge, consumer => $consumer, generic => 1,
253 };
254 0 ▶ $tx->on_data(sub { $consumer->input($_[1]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
255 0 ▶ $tx->on_finish(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
256 0 my ($finished_tx) = @_;
257 0 ▶ $consumer->transport_error(
Branch: 1 branch
BranchTrueFalseTotal
unless $finished_tx->is_complete 0 0 0
Condition0%0/2
$finished_tx->error || 'CONNECT stream ended'
l!l
00
MC/DC0%0/1
$finished_tx->error || 'CONNECT stream ended'
$finished_tx->error
Truth table0%0/2
A
A= $finished_tx->error
Aresult
11
00
258 $finished_tx->error || 'CONNECT stream ended',
259 ) unless $finished_tx->is_complete;
260 0 $bridge->detach;
261 0 delete $sessions->{$id};
262 0 delete $attempts->{$id};
263 0 });
264 $tx->respond(
265 status => 200, stream_body => 1,
266 0 ▶ on_drain => sub { $bridge->resume },
Subroutine __ANON__: not called CC=1 SCAR=6.9
267 0 );
268 0 return;
269 }
270  
271 # Extended CONNECT belongs to one H2 transaction; the HTTP/2 connection
272 # remains able to serve independent ordinary HTTP and other WebSockets.
273 # Outgoing WebSocket frames must NEVER re-enter nghttp2 from input callbacks.
274 0 ▶sub _attach_h2_websocket_stream {
Subroutine _attach_h2_websocket_stream: not called CC=1 SCAR=6.9
275 0 my ($self, $transport, $engine_options) = @_;
276 0 require Unblock::HTTP::_WebSocketH2Body;
277 0 my $handler = $self->{on_websocket};
278 0 my $defer = $self->{defer};
279 0 my $on_headers = $self->{callbacks}{on_request};
280 0 my $on_request_end = $self->{callbacks}{on_request_end};
281 0 my (%sessions, %handled_generic);
282  
283 0 ▶ my $on_request = sub {
Subroutine __ANON__: not called CC=17 SCAR=57.2
284 0 my ($tx, $request) = @_;
285 0 my $protocol = $request->protocol;
286 0 ▶ my $candidate = $handler && $request->method eq 'CONNECT'
Condition0%0/12
$handler && $request->method eq 'CONNECT' && (defined $protocol && $protocol eq 'websocket' || _websocket_candidate($request))
!ll&&!rl&&r
000
$handler and $request->method eq "CONNECT"
!ll&&!rl&&r
000
defined $protocol and $protocol eq "websocket" or _websocket_candidate($request)
l!l&&r!l&&!r
000
defined $protocol and $protocol eq "websocket"
!ll&&!rl&&r
000
MC/DC0%0/5
$handler && $request->method eq 'CONNECT' && (defined $protocol && $protocol eq 'websocket' || _websocket_candidate($request))
$handler $request->method eq "CONNECT" defined $protocol $protocol eq "websocket" _websocket_candidate($request)
Truth table0%0/7
A and B && C and D or E
A= $handlerB= $request->method eq "CONNECT"C= defined $protocolD= $protocol eq "websocket"E= _websocket_candidate($request)
ABCDEresult
0XXXX0
10XXX0
110X00
111000
1111X1
110X11
111011
287 && ((defined($protocol) && $protocol eq 'websocket')
288 || _websocket_candidate($request));
289 0 ▶ unless ($candidate) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($candidate) 0 0 0
290 return $self->_generic_multiplexed_handoff(
291 $tx, $request, \%sessions, \%handled_generic,
292 0 ▶ ) if $self->{on_handoff} && $request->method eq 'CONNECT';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_handoff"} and $request->method eq "CONNECT" 0 0 0
Condition0%0/3
$self->{"on_handoff"} and $request->method eq "CONNECT"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"on_handoff"} and $request->method eq "CONNECT"
$self->{"on_handoff"} $request->method eq "CONNECT"
Truth table0%0/3
A and B
A= $self->{"on_handoff"}B= $request->method eq "CONNECT"
ABresult
0X0
100
111
293 0 return $on_headers->(@_);
294 }
295  
296 0 my $selected = $handler->($tx, $request);
297 0 ▶ unless (defined $selected) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $selected) 0 0 0
298 0 $tx->respond(status => 403, body => 'WebSocket denied');
299 0 return;
300 }
301 0 ▶ croak 'on_websocket must return a hash reference or undef'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $selected eq "HASH" 0 0 0
302 unless ref($selected) eq 'HASH';
303 0 my %option = %$selected;
304 0 my %handshake_option;
305 0 for my $name (qw(subprotocols permessage_deflate)) {
306 $handshake_option{$name} = delete $option{$name}
307 0 ▶ if exists $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{$name} 0 0 0
308 }
309 0 my $on_open = delete $option{on_open};
310 0 ▶ croak 'on_websocket on_open must be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_open and ref $on_open ne "CODE" 0 0 0
Condition0%0/3
defined $on_open and ref $on_open ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $on_open and ref $on_open ne "CODE"
defined $on_open ref $on_open ne "CODE"
Truth table0%0/3
A and B
A= defined $on_openB= ref $on_open ne "CODE"
ABresult
0X0
100
111
311 if defined($on_open) && ref($on_open) ne 'CODE';
312 croak 'on_websocket may not override the H2 stream transport'
313 0 ▶ if exists $option{transport};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{"transport"} 0 0 0
314  
315 0 require Unblock::WebSocket::Handshake;
316 0 require Unblock::WebSocket::Server;
317 0 my $handshake;
318 0 my $valid = eval {
319 0 $handshake = Unblock::WebSocket::Handshake->server_accept(
320 $request, %handshake_option,
321 );
322 0 1;
323 };
324 0 ▶ unless ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid) 0 0 0
325 0 $tx->respond(status => 400, body => 'Invalid WebSocket handshake');
326 0 return;
327 }
328 0 my $bridge = Unblock::HTTP::_WebSocketH2Body->new(
329 transaction => $tx, defer => $defer,
330 );
331 my $ws = Unblock::WebSocket::Server->new(
332 0 %{ $handshake->connection_options },
333 %option, transport => $bridge,
334 );
335 0 $bridge->websocket($ws);
336 0 my $id = $tx->stream_id;
337 0 $sessions{$id} = { bridge => $bridge, websocket => $ws };
338 0 ▶ $tx->on_data(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
339 0 $ws->input($_[1]);
340 0 });
341 0 ▶ $tx->on_finish(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
342 0 $bridge->detach;
343 0 delete $sessions{$id};
344 0 });
345 $tx->respond(
346 $handshake->server_response,
347 stream_body => 1,
348 0 ▶ on_drain => sub { $bridge->resume },
Subroutine __ANON__: not called CC=1 SCAR=6.9
349 0 );
350 0 ▶ $on_open->($ws, $tx, $request) if $on_open;
Branch: 1 branch
BranchTrueFalseTotal
if $on_open 0 0 0
351 0 return;
352 0 };
353  
354 0 ▶ my $on_request_complete = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
355 0 my ($tx) = @_;
356 0 my $id = $tx->stream_id;
357 0 ▶ if (my $session = $sessions{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $session = $sessions{$id}) 0 0 0
358 0 ▶ if ($session->{generic}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{'generic'}) { } 0 0 0
359 0 $session->{consumer}->input_eof;
360 } else {
361 0 $session->{websocket}->input_eof;
362 }
363 0 return;
364 }
365 0 ▶ return if $handled_generic{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $handled_generic{$id} 0 0 0
366 0 return $on_request_end->(@_);
367 0 };
368  
369 0 my %callbacks = %{ $self->{callbacks} };
370 0 $callbacks{on_request} = $on_request;
371 0 $callbacks{on_request_end} = $on_request_complete;
372 0 return Unblock::HTTP::_EngineFactory->create(
373 'Server', 'h2', %$engine_options,
374 %callbacks, transport => $transport,
375 );
376 }
377  
378 # A WebSocket CONNECT occupies an H3 QUIC request stream, not the QUIC
379 # connection. The real Transaction owns both directions; the private body
380 # bridge only defers outgoing WebSocket writes past the H3 receive callback.
381 0 ▶sub _attach_h3_websocket_connection {
Subroutine _attach_h3_websocket_connection: not called CC=1 SCAR=6.9
382 0 my ($self, $connection, $engine_options) = @_;
383 0 require Unblock::HTTP::_WebSocketH2Body;
384 0 my $handler = $self->{on_websocket};
385 0 my $defer = $self->{defer};
386 0 my $on_headers = $self->{callbacks}{on_request};
387 0 my $on_request_end = $self->{callbacks}{on_request_end};
388 0 my (%sessions, %websocket_attempts);
389  
390 0 ▶ my $on_request = sub {
Subroutine __ANON__: not called CC=17 SCAR=57.2
391 0 my ($tx, $request) = @_;
392 0 my $protocol = $request->protocol;
393 0 ▶ my $candidate = $handler && $request->method eq 'CONNECT'
Condition0%0/12
$handler && $request->method eq 'CONNECT' && (defined $protocol && $protocol eq 'websocket' || _websocket_candidate($request))
!ll&&!rl&&r
000
$handler and $request->method eq "CONNECT"
!ll&&!rl&&r
000
defined $protocol and $protocol eq "websocket" or _websocket_candidate($request)
l!l&&r!l&&!r
000
defined $protocol and $protocol eq "websocket"
!ll&&!rl&&r
000
MC/DC0%0/5
$handler && $request->method eq 'CONNECT' && (defined $protocol && $protocol eq 'websocket' || _websocket_candidate($request))
$handler $request->method eq "CONNECT" defined $protocol $protocol eq "websocket" _websocket_candidate($request)
Truth table0%0/7
A and B && C and D or E
A= $handlerB= $request->method eq "CONNECT"C= defined $protocolD= $protocol eq "websocket"E= _websocket_candidate($request)
ABCDEresult
0XXXX0
10XXX0
110X00
111000
1111X1
110X11
111011
394 && ((defined($protocol) && $protocol eq 'websocket')
395 || _websocket_candidate($request));
396 0 ▶ unless ($candidate) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($candidate) 0 0 0
397 return $self->_generic_multiplexed_handoff(
398 $tx, $request, \%sessions, \%websocket_attempts,
399 0 ▶ ) if $self->{on_handoff} && $request->method eq 'CONNECT';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_handoff"} and $request->method eq "CONNECT" 0 0 0
Condition0%0/3
$self->{"on_handoff"} and $request->method eq "CONNECT"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"on_handoff"} and $request->method eq "CONNECT"
$self->{"on_handoff"} $request->method eq "CONNECT"
Truth table0%0/3
A and B
A= $self->{"on_handoff"}B= $request->method eq "CONNECT"
ABresult
0X0
100
111
400 0 return $on_headers->(@_);
401 }
402  
403 0 my $id = $tx->stream_id;
404 0 $websocket_attempts{$id} = 1;
405 0 my $selected = $handler->($tx, $request);
406 0 ▶ unless (defined $selected) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $selected) 0 0 0
407 0 ▶ $tx->on_finish(sub { delete $websocket_attempts{$id} });
Subroutine __ANON__: not called CC=1 SCAR=6.9
408 0 $tx->respond(status => 403, body => 'WebSocket denied');
409 0 return;
410 }
411 0 ▶ croak 'on_websocket must return a hash reference or undef'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $selected eq "HASH" 0 0 0
412 unless ref($selected) eq 'HASH';
413 0 my %option = %$selected;
414 0 my %handshake_option;
415 0 for my $name (qw(subprotocols permessage_deflate)) {
416 $handshake_option{$name} = delete $option{$name}
417 0 ▶ if exists $option{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{$name} 0 0 0
418 }
419 0 my $on_open = delete $option{on_open};
420 0 ▶ croak 'on_websocket on_open must be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_open and ref $on_open ne "CODE" 0 0 0
Condition0%0/3
defined $on_open and ref $on_open ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $on_open and ref $on_open ne "CODE"
defined $on_open ref $on_open ne "CODE"
Truth table0%0/3
A and B
A= defined $on_openB= ref $on_open ne "CODE"
ABresult
0X0
100
111
421 if defined($on_open) && ref($on_open) ne 'CODE';
422 croak 'on_websocket may not override the H3 stream transport'
423 0 ▶ if exists $option{transport};
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{"transport"} 0 0 0
424  
425 0 require Unblock::WebSocket::Handshake;
426 0 require Unblock::WebSocket::Server;
427 0 my $handshake;
428 0 my $valid = eval {
429 0 $handshake = Unblock::WebSocket::Handshake->server_accept(
430 $request, %handshake_option,
431 );
432 0 1;
433 };
434 0 ▶ unless ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid) 0 0 0
435 0 ▶ $tx->on_finish(sub { delete $websocket_attempts{$id} });
Subroutine __ANON__: not called CC=1 SCAR=6.9
436 0 $tx->respond(status => 400, body => 'Invalid WebSocket handshake');
437 0 return;
438 }
439 0 my $bridge = Unblock::HTTP::_WebSocketH2Body->new(
440 transaction => $tx, defer => $defer,
441 );
442 my $ws = Unblock::WebSocket::Server->new(
443 0 %{ $handshake->connection_options },
444 %option, transport => $bridge,
445 );
446 0 $bridge->websocket($ws);
447 0 $sessions{$id} = { bridge => $bridge, websocket => $ws };
448 0 ▶ $tx->on_data(sub { $ws->input($_[1]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
449 0 ▶ $tx->on_finish(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
450 0 $bridge->detach;
451 0 delete $sessions{$id};
452 0 delete $websocket_attempts{$id};
453 0 });
454 $tx->respond(
455 $handshake->server_response,
456 stream_body => 1,
457 0 ▶ on_drain => sub { $bridge->resume },
Subroutine __ANON__: not called CC=1 SCAR=6.9
458 0 );
459 0 ▶ $on_open->($ws, $tx, $request) if $on_open;
Branch: 1 branch
BranchTrueFalseTotal
if $on_open 0 0 0
460 0 return;
461 0 };
462  
463 0 ▶ my $on_request_complete = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
464 0 my ($tx) = @_;
465 0 my $id = $tx->stream_id;
466 0 ▶ if ($websocket_attempts{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($websocket_attempts{$id}) 0 0 0
467 0 ▶ if (my $session = $sessions{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $session = $sessions{$id}) 0 0 0
468 0 ▶ if ($session->{generic}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{'generic'}) { } 0 0 0
469 0 $session->{consumer}->input_eof;
470 } else {
471 0 $session->{websocket}->input_eof;
472 }
473 }
474 # Even a 400/403 CONNECT belongs to on_websocket, not to
475 # the ordinary request-end responder. Do not respond twice.
476 0 return;
477 }
478 0 return $on_request_end->(@_);
479 0 };
480  
481 0 my %callbacks = %{ $self->{callbacks} };
482 0 $callbacks{on_request} = $on_request;
483 0 $callbacks{on_request_end} = $on_request_complete;
484 0 my $engine = Unblock::HTTP::_EngineFactory->create(
485 'Server', 'h3', %$engine_options,
486 %callbacks, quic => $connection,
487 );
488 0 $engine->start;
489 0 return $engine;
490 }
491  
492 1 ▶sub protocols { return $_[0]{policy}->protocols }
Subroutine protocols: called CC=1 SCAR=0.0
Pod documented
493 3 ▶sub alpn_protocols { return $_[0]{policy}->alpn_protocols }
Subroutine alpn_protocols: called CC=1 SCAR=0.0
Pod documented
494 9 ▶sub supports { return $_[0]{policy}->enabled($_[1]) }
Subroutine supports: called CC=1 SCAR=0.0
Pod documented
495  
496 18 ▶sub attach_stream {
Subroutine attach_stream: called CC=22 SCAR=31.4
Pod documented
497 18 my ($self, @args) = @_;
498 18 ▶ croak 'attach_stream(): expected named arguments' if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 1 17 18
499 17 my %options = @args;
500 croak 'attach_stream(): specify either protocol or alpn, not both'
501 17 ▶ if exists($options{protocol}) && exists($options{alpn});
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"protocol"} and exists $options{"alpn"} 1 16 17
Condition100%3/3
exists $options{"protocol"} and exists $options{"alpn"}
!ll&&!rl&&r
1421
MC/DC100%2/2
exists $options{"protocol"} and exists $options{"alpn"}
exists $options{"protocol"} exists $options{"alpn"}
Truth table100%3/3
A and B
A= exists $options{"protocol"}B= exists $options{"alpn"}
ABresult
0X0
100
111
502 16 my $transport = delete $options{transport};
503 16 my $protocol = delete $options{protocol};
504 16 my $alpn = delete $options{alpn};
505 16 ▶ croak 'attach_stream(): unknown option: ' . join(', ', sort keys %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 2 14 16
506 14 ▶ croak 'attach_stream(): protocol and alpn must be scalars'
Branch: 1 branch
BranchTrueFalseTotal
if ref $protocol or ref $alpn 1 13 14
Condition66%2/3
ref $protocol or ref $alpn
l!l&&r!l&&!r
0113
MC/DC50%1/2
ref $protocol or ref $alpn
ref $protocol ref $alpn
Truth table66%2/3
A or B
A= ref $protocolB= ref $alpn
ABresult
1X1
011
000
507 if ref($protocol) || ref($alpn);
508 13 ▶ $protocol = defined($alpn) && length($alpn) ? $alpn : 'http/1.1'
Branch: 2 branches
BranchTrueFalseTotal
unless defined $protocol 11 2 13
defined $alpn && length $alpn ? : 4 7 11
Condition100%3/3
defined $alpn and length $alpn
!ll&&!rl&&r
614
MC/DC100%2/2
defined $alpn and length $alpn
defined $alpn length $alpn
Truth table100%3/3
A and B
A= defined $alpnB= length $alpn
ABresult
0X0
100
111
509 unless defined $protocol;
510 13 ▶ croak "attach_stream(): unsupported byte-stream protocol '$protocol'"
Branch: 1 branch
BranchTrueFalseTotal
unless $protocol eq "http/1.1" or $protocol eq "h2" 2 11 13
Condition100%3/3
$protocol eq "http/1.1" or $protocol eq "h2"
l!l&&r!l&&!r
832
MC/DC100%2/2
$protocol eq "http/1.1" or $protocol eq "h2"
$protocol eq "http/1.1" $protocol eq "h2"
Truth table100%3/3
A or B
A= $protocol eq "http/1.1"B= $protocol eq "h2"
ABresult
1X1
011
000
511 unless $protocol eq 'http/1.1' || $protocol eq 'h2';
512 11 my $engine_options = $self->{policy}->options($protocol);
513 9 ▶ croak 'attach_stream(): transport must be an object' unless blessed($transport);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($transport) 1 8 9
514 8 for my $method (qw(unblock_send unblock_finish unblock_abort)) {
515 22 ▶ croak "attach_stream(): transport must implement $method()"
Branch: 1 branch
BranchTrueFalseTotal
unless $transport->can($method) 1 21 22
516 unless $transport->can($method);
517 }
518 return $self->_attach_websocket_stream($transport, $engine_options)
519 7 ▶ if $protocol eq 'http/1.1' && $self->{policy}->websocket_enabled($protocol);
Branch: 1 branch
BranchTrueFalseTotal
if $protocol eq "http/1.1" and $self->{"policy"}->websocket_enabled($protocol) 0 7 7
Condition66%2/3
$protocol eq "http/1.1" and $self->{"policy"}->websocket_enabled($protocol)
!ll&&!rl&&r
340
MC/DC0%0/2
$protocol eq "http/1.1" and $self->{"policy"}->websocket_enabled($protocol)
$protocol eq "http/1.1" $self->{"policy"}->websocket_enabled($protocol)
Truth table66%2/3
A and B
A= $protocol eq "http/1.1"B= $self->{"policy"}->websocket_enabled($protocol)
ABresult
0X0
100
111
520 return $self->_attach_handoff_stream($transport, $engine_options)
521 7 ▶ if $protocol eq 'http/1.1' && $self->{on_handoff};
Branch: 1 branch
BranchTrueFalseTotal
if $protocol eq "http/1.1" and $self->{"on_handoff"} 0 7 7
Condition66%2/3
$protocol eq "http/1.1" and $self->{"on_handoff"}
!ll&&!rl&&r
340
MC/DC0%0/2
$protocol eq "http/1.1" and $self->{"on_handoff"}
$protocol eq "http/1.1" $self->{"on_handoff"}
Truth table66%2/3
A and B
A= $protocol eq "http/1.1"B= $self->{"on_handoff"}
ABresult
0X0
100
111
522 return $self->_attach_h2_websocket_stream($transport, $engine_options)
523 if $protocol eq 'h2'
524 && ($self->{policy}->websocket_enabled($protocol)
525 7 ▶ || $self->{on_handoff});
Branch: 1 branch
BranchTrueFalseTotal
if $protocol eq "h2" and $self->{"policy"}->websocket_enabled($protocol) || $self->{"on_handoff"} 0 7 7
Condition50%3/6
$protocol eq "h2" and $self->{"policy"}->websocket_enabled($protocol) || $self->{"on_handoff"}
!ll&&!rl&&r
430
$self->{"policy"}->websocket_enabled($protocol) or $self->{"on_handoff"}
l!l&&r!l&&!r
003
MC/DC0%0/3
$protocol eq "h2" and $self->{"policy"}->websocket_enabled($protocol) || $self->{"on_handoff"}
$protocol eq "h2" $self->{"policy"}->websocket_enabled($protocol) $self->{"on_handoff"}
Truth table50%2/4
A and B or C
A= $protocol eq "h2"B= $self->{"policy"}->websocket_enabled($protocol)C= $self->{"on_handoff"}
ABCresult
0XX0
1000
11X1
1011
526 return Unblock::HTTP::_EngineFactory->create(
527 'Server', $protocol, %$engine_options,
528 7 %{ $self->{callbacks} }, transport => $transport,
529 );
530 }
531  
532 12 ▶sub attach_quic {
Subroutine attach_quic: called CC=10 SCAR=23.3
Pod documented
533 12 my ($self, @args) = @_;
534 12 ▶ croak 'attach_quic(): expected named arguments' if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 1 11 12
535 11 my %options = @args;
536 11 my $connection = delete $options{connection};
537 11 ▶ croak 'attach_quic(): unknown option: ' . join(', ', sort keys %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 1 10 11
538 10 my $engine_options = $self->{policy}->options('h3');
539 9 ▶ croak 'attach_quic(): connection must be a QUIC connection object'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($connection) and $connection->can("ready") and $connection->can("closed") 1 8 9
Condition66%4/6
builtin::blessed($connection) and $connection->can("ready") and $connection->can("closed")
!ll&&!rl&&r
108
builtin::blessed($connection) and $connection->can("ready")
!ll&&!rl&&r
108
MC/DC33%1/3
builtin::blessed($connection) and $connection->can("ready") and $connection->can("closed")
builtin::blessed($connection) $connection->can("ready") $connection->can("closed")
Truth table50%2/4
A and B and C
A= builtin::blessed($connection)B= $connection->can("ready")C= $connection->can("closed")
ABCresult
0XX0
10X0
1100
1111
540 unless blessed($connection) && $connection->can('ready') && $connection->can('closed');
541 8 ▶ croak 'attach_quic(): connection is closed' if $connection->closed;
Branch: 1 branch
BranchTrueFalseTotal
if $connection->closed 1 7 8
542 7 ▶ croak 'attach_quic(): connection is not ready' unless $connection->ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $connection->ready 1 6 7
543 return $self->_attach_h3_websocket_connection($connection, $engine_options)
544 6 ▶ if $self->{policy}->websocket_enabled('h3') || $self->{on_handoff};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"policy"}->websocket_enabled("h3") or $self->{"on_handoff"} 0 6 6
Condition33%1/3
$self->{"policy"}->websocket_enabled("h3") or $self->{"on_handoff"}
l!l&&r!l&&!r
006
MC/DC0%0/2
$self->{"policy"}->websocket_enabled("h3") or $self->{"on_handoff"}
$self->{"policy"}->websocket_enabled("h3") $self->{"on_handoff"}
Truth table33%1/3
A or B
A= $self->{"policy"}->websocket_enabled("h3")B= $self->{"on_handoff"}
ABresult
1X1
011
000
545 my $engine = Unblock::HTTP::_EngineFactory->create(
546 'Server', 'h3', %$engine_options,
547 6 %{ $self->{callbacks} }, quic => $connection,
548 );
549 6 $engine->start;
550 6 return $engine;
551 }
552  
553 6 1;
554  
555 __END__
« HTTP/Client.pm ↑ Summary HTTP/_Policy.pm »