| 1 | package Unblock::HTTP::Server; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Carp qw(croak); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Scalar::Util qw(blessed refaddr weaken); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Unblock::HTTP::_Policy; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Unblock::HTTP::_EngineFactory; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | our $VERSION = '0.02'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 36 | ▶ | sub new { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=22 SCAR=32.8
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 36 | my ($class, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 36 | ▶ | croak 'new(): expected named arguments' if @args % 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 36 | my %options = @args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 36 | my (%protocols, %callbacks); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 36 | for my $name (qw(http1 http2 http3)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 108 | ▶ | $protocols{$name} = delete $options{$name} if exists $options{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 36 | for my $name (qw(on_headers on_request on_error on_websocket)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 142 | ▶ | next unless exists $options{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 56 | my $callback = delete $options{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 56 | ▶ | croak "new(): $name must be a coderef" unless ref($callback) eq 'CODE'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 55 | $callbacks{$name} = $callback; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 35 | my $defer = delete $options{defer}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 35 | ▶ | croak 'new(): defer must be a coderef' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $defer and ref $defer ne "CODE"
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"
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | if defined($defer) && ref($defer) ne 'CODE'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 35 | ▶ | croak 'new(): unknown option: ' . join(', ', sort keys %options) if %options; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 32 | ▶ | croak 'new(): on_request callback is required' unless $callbacks{on_request}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 32 | my $policy = Unblock::HTTP::_Policy->new(%protocols, _websocket_role => 'server'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 18 | ▶ | 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')
$policy->websocket_enabled("http/1.1") or $policy->websocket_enabled("h2")
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')
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | || $policy->websocket_enabled('h2') | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | || $policy->websocket_enabled('h3'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | croak 'new(): on_websocket callback is required for enabled WebSocket support' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 18 | ▶ | if $websocket_enabled && !$callbacks{on_websocket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$websocket_enabled and not $callbacks{"on_websocket"}
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"}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | croak 'new(): on_websocket requires an enabled websocket protocol' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 15 | ▶ | if !$websocket_enabled && $callbacks{on_websocket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $websocket_enabled and $callbacks{"on_websocket"}
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"}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 15 | ▶ | croak 'new(): HTTP2/HTTP3 WebSocket requires a defer callback' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$policy->websocket_enabled("h2") || $policy->websocket_enabled("h3") and not $defer
$policy->websocket_enabled("h2") or $policy->websocket_enabled("h3")
MC/DC0%0/3
$policy->websocket_enabled("h2") || $policy->websocket_enabled("h3") and not $defer
$policy->websocket_enabled("h2")
$policy->websocket_enabled("h3")
not $defer
Truth table20%1/5
A or B and not(C)
A= $policy->websocket_enabled("h2")B= $policy->websocket_enabled("h3")C= not $defer
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | if ($policy->websocket_enabled('h2') | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | || $policy->websocket_enabled('h3')) && !$defer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | return bless { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | policy => $policy, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | on_websocket => $callbacks{on_websocket}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | defer => $defer, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # Map once at definition time; no transaction or body wrapper. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | callbacks => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | on_request => $callbacks{on_headers} || \&_ignore_headers, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | on_request_end => $callbacks{on_request}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 15 | ▶ | ($callbacks{on_error} ? (on_error => $callbacks{on_error}) : ()), | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$callbacks{'on_headers'} || \&Unblock::HTTP::Server::_ignore_headers
MC/DC100%1/1
$callbacks{'on_headers'} || \&Unblock::HTTP::Server::_ignore_headers
$callbacks{'on_headers'}
Truth table100%2/2
A
A= $callbacks{'on_headers'}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | }, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | sub _ignore_headers { return } | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ignore_headers: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # Recognize even incomplete WebSocket attempts so a malformed upgrade is not | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # silently treated as an ordinary application request. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub _websocket_candidate { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_websocket_candidate: not called CC=6 SCAR=37.4
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my ($request) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | for my $index (0 .. $request->header_count - 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | my $name = lc $request->header_name($index); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | return 1 if $name =~ /\Asec-websocket-/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | next unless $name eq 'upgrade'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | for my $token (split /,/, $request->header_value($index)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | return 1 if $token =~ /\A[ \t]*websocket[ \t]*\z/i; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # HTTP1 switches the complete byte stream, not an individual transaction. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # Keep the lower engine and transport as the only byte owners. Only this | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # opted-in attachment gets a pair of per-connection transition callbacks. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub _attach_websocket_stream { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_attach_websocket_stream: not called CC=2 SCAR=17.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my ($self, $transport, $engine_options) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | croak 'attach_stream(): HTTP1 WebSocket transport must implement unblock_upgrade()' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | unless $transport->can('unblock_upgrade'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $host = $transport; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | weaken($host); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my $engine; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my $handler = $self->{on_websocket}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $on_http_request = $self->{callbacks}{on_request_end}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my $pending; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $on_request_end = sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=11 SCAR=48.8
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my ($tx, $request) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | return $on_http_request->(@_) unless _websocket_candidate($request); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | my $selected = $handler->($tx, $request); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | unless (defined $selected) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $tx->respond(status => 403, body => 'WebSocket denied'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | croak 'on_websocket must return a hash reference or undef' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | unless ref($selected) eq 'HASH'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my %option = %$selected; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my %handshake_option; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | for my $name (qw(subprotocols permessage_deflate)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | $handshake_option{$name} = delete $option{$name} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if exists $option{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my $on_open = delete $option{on_open}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | croak 'on_websocket on_open must be a coderef' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $on_open and ref $on_open ne "CODE"
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"
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | if defined($on_open) && ref($on_open) ne 'CODE'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | croak 'on_websocket may not override the attached transport' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if exists $option{transport}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | require Unblock::WebSocket::Handshake; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | require Unblock::WebSocket::Server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $handshake; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my $valid = eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $handshake = Unblock::WebSocket::Handshake->server_accept( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | $request, %handshake_option, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | unless ($valid) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | $tx->respond(status => 400, body => 'Invalid WebSocket handshake'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | my $ws = Unblock::WebSocket::Server->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | %{ $handshake->connection_options }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | %option, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | transport => $host, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $pending = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | transaction => refaddr($tx), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | websocket => $ws, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | on_open => $on_open, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | request => $request, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | # Even a failed host write must release the pending WebSocket state. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | $tx->on_finish(sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | $pending = undef | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if $pending && $pending->{transaction} == refaddr($_[0]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$pending and $pending->{"transaction"} == builtin::refaddr($_[0])
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])
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | my $ok = eval { $tx->respond($handshake->server_response); 1 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | unless ($ok) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | $pending = undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | die $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | my $on_switch = sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | my ($tx) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | # An ordinary CONNECT or unrelated protocol switch remains the | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | # lower HTTP1 engine's responsibility, not an implicit WebSocket. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | return unless $pending; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | croak 'WebSocket switch transaction does not match the handshake' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | unless $pending->{transaction} == refaddr($tx); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my $ready = $pending; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | $pending = undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | my $ws = $ready->{websocket}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | croak 'WebSocket transport was destroyed during handshake' unless $host; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | $host->unblock_upgrade($ws); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | $ready->{on_open}->($ws, $tx, $ready->{request}) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | if $ready->{on_open}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | my $tail = $engine->take_remainder; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | $ws->input($tail) if length $tail; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | my %callbacks = %{ $self->{callbacks} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | $callbacks{on_request_end} = $on_request_end; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | $callbacks{on_switch} = $on_switch; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | my $attached = Unblock::HTTP::_EngineFactory->create( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 'Server', 'http/1.1', %$engine_options, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | %callbacks, transport => $transport, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | $engine = $attached; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | weaken($engine); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | return $attached; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # Extended CONNECT belongs to one H2 transaction; the HTTP/2 connection | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # remains able to serve independent ordinary HTTP and other WebSockets. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # Outgoing WebSocket frames must NEVER re-enter nghttp2 from input callbacks. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | sub _attach_h2_websocket_stream { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_attach_h2_websocket_stream: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | my ($self, $transport, $engine_options) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | require Unblock::HTTP::_WebSocketH2Body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | my $handler = $self->{on_websocket}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | my $defer = $self->{defer}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | my $on_headers = $self->{callbacks}{on_request}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | my $on_request_end = $self->{callbacks}{on_request_end}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | my %sessions; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | my $on_request = sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=14 SCAR=53.5
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | my ($tx, $request) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | my $protocol = $request->protocol; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | my $candidate = $request->method eq 'CONNECT' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/9
$request->method eq 'CONNECT' && (defined $protocol && $protocol eq 'websocket' || _websocket_candidate($request))
defined $protocol and $protocol eq "websocket" or _websocket_candidate($request)
defined $protocol and $protocol eq "websocket"
MC/DC0%0/4
$request->method eq 'CONNECT' && (defined $protocol && $protocol eq 'websocket' || _websocket_candidate($request))
$request->method eq 'CONNECT'
defined $protocol
$protocol eq "websocket"
_websocket_candidate($request)
Truth table0%0/6
A && B and C or D
A= $request->method eq 'CONNECT'B= defined $protocolC= $protocol eq "websocket"D= _websocket_candidate($request)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | && ((defined($protocol) && $protocol eq 'websocket') | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | || _websocket_candidate($request)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | return $on_headers->(@_) unless $candidate; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | my $selected = $handler->($tx, $request); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | unless (defined $selected) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | $tx->respond(status => 403, body => 'WebSocket denied'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | croak 'on_websocket must return a hash reference or undef' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | unless ref($selected) eq 'HASH'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | my %option = %$selected; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | my %handshake_option; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | for my $name (qw(subprotocols permessage_deflate)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | $handshake_option{$name} = delete $option{$name} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | if exists $option{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | my $on_open = delete $option{on_open}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | croak 'on_websocket on_open must be a coderef' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $on_open and ref $on_open ne "CODE"
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"
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | if defined($on_open) && ref($on_open) ne 'CODE'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | croak 'on_websocket may not override the H2 stream transport' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | if exists $option{transport}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | require Unblock::WebSocket::Handshake; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | require Unblock::WebSocket::Server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | my $handshake; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | my $valid = eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | $handshake = Unblock::WebSocket::Handshake->server_accept( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | $request, %handshake_option, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | unless ($valid) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | $tx->respond(status => 400, body => 'Invalid WebSocket handshake'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | my $bridge = Unblock::HTTP::_WebSocketH2Body->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | transaction => $tx, defer => $defer, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | my $ws = Unblock::WebSocket::Server->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | %{ $handshake->connection_options }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | %option, transport => $bridge, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | $bridge->websocket($ws); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | my $id = $tx->stream_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | $sessions{$id} = { bridge => $bridge, websocket => $ws }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | $tx->on_data(sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | $ws->input($_[1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | $tx->on_finish(sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | $bridge->detach; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | delete $sessions{$id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | $tx->respond( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | $handshake->server_response, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | stream_body => 1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | on_drain => sub { $bridge->resume }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | ▶ | $on_open->($ws, $tx, $request) if $on_open; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | my $on_request_complete = sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | my ($tx) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | ▶ | if (my $session = $sessions{$tx->stream_id}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | $session->{websocket}->input_eof; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | return $on_request_end->(@_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | my %callbacks = %{ $self->{callbacks} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | $callbacks{on_request} = $on_request; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | $callbacks{on_request_end} = $on_request_complete; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | return Unblock::HTTP::_EngineFactory->create( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 'Server', 'h2', %$engine_options, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | %callbacks, transport => $transport, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | # A WebSocket CONNECT occupies an H3 QUIC request stream, not the QUIC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | # connection. The real Transaction owns both directions; the private body | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | # bridge only defers outgoing WebSocket writes past the H3 receive callback. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | sub _attach_h3_websocket_connection { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_attach_h3_websocket_connection: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | my ($self, $connection, $engine_options) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | require Unblock::HTTP::_WebSocketH2Body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | my $handler = $self->{on_websocket}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | my $defer = $self->{defer}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | my $on_headers = $self->{callbacks}{on_request}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | my $on_request_end = $self->{callbacks}{on_request_end}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | my (%sessions, %websocket_attempts); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | my $on_request = sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=14 SCAR=53.5
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | my ($tx, $request) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | my $protocol = $request->protocol; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | ▶ | my $candidate = $request->method eq 'CONNECT' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/9
$request->method eq 'CONNECT' && (defined $protocol && $protocol eq 'websocket' || _websocket_candidate($request))
defined $protocol and $protocol eq "websocket" or _websocket_candidate($request)
defined $protocol and $protocol eq "websocket"
MC/DC0%0/4
$request->method eq 'CONNECT' && (defined $protocol && $protocol eq 'websocket' || _websocket_candidate($request))
$request->method eq 'CONNECT'
defined $protocol
$protocol eq "websocket"
_websocket_candidate($request)
Truth table0%0/6
A && B and C or D
A= $request->method eq 'CONNECT'B= defined $protocolC= $protocol eq "websocket"D= _websocket_candidate($request)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | && ((defined($protocol) && $protocol eq 'websocket') | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | || _websocket_candidate($request)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | return $on_headers->(@_) unless $candidate; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | my $id = $tx->stream_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | $websocket_attempts{$id} = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | my $selected = $handler->($tx, $request); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | ▶ | unless (defined $selected) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | $tx->on_finish(sub { delete $websocket_attempts{$id} }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | $tx->respond(status => 403, body => 'WebSocket denied'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | ▶ | croak 'on_websocket must return a hash reference or undef' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | unless ref($selected) eq 'HASH'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | my %option = %$selected; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | my %handshake_option; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | for my $name (qw(subprotocols permessage_deflate)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | $handshake_option{$name} = delete $option{$name} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | ▶ | if exists $option{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | my $on_open = delete $option{on_open}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | ▶ | croak 'on_websocket on_open must be a coderef' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $on_open and ref $on_open ne "CODE"
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"
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | if defined($on_open) && ref($on_open) ne 'CODE'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | croak 'on_websocket may not override the H3 stream transport' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | ▶ | if exists $option{transport}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | require Unblock::WebSocket::Handshake; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | require Unblock::WebSocket::Server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | my $handshake; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | my $valid = eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | $handshake = Unblock::WebSocket::Handshake->server_accept( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | $request, %handshake_option, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | ▶ | unless ($valid) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | $tx->on_finish(sub { delete $websocket_attempts{$id} }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | $tx->respond(status => 400, body => 'Invalid WebSocket handshake'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | my $bridge = Unblock::HTTP::_WebSocketH2Body->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | transaction => $tx, defer => $defer, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | my $ws = Unblock::WebSocket::Server->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | %{ $handshake->connection_options }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | %option, transport => $bridge, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | $bridge->websocket($ws); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | $sessions{$id} = { bridge => $bridge, websocket => $ws }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | $tx->on_data(sub { $ws->input($_[1]) }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | $tx->on_finish(sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | $bridge->detach; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | delete $sessions{$id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | delete $websocket_attempts{$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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | ▶ | my $on_request_complete = sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | my ($tx) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | my $id = $tx->stream_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | ▶ | if ($websocket_attempts{$id}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | ▶ | if (my $session = $sessions{$id}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | $session->{websocket}->input_eof; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | # Even a 400/403 CONNECT belongs to on_websocket, not to | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | # the ordinary request-end responder. Do not respond twice. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | return $on_request_end->(@_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 0 | my %callbacks = %{ $self->{callbacks} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | $callbacks{on_request} = $on_request; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | $callbacks{on_request_end} = $on_request_complete; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | my $engine = Unblock::HTTP::_EngineFactory->create( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 'Server', 'h3', %$engine_options, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | %callbacks, quic => $connection, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | $engine->start; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | return $engine; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 1 | ▶ | sub protocols { return $_[0]{policy}->protocols } | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
protocols: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 3 | ▶ | sub alpn_protocols { return $_[0]{policy}->alpn_protocols } | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
alpn_protocols: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 7 | ▶ | sub supports { return $_[0]{policy}->enabled($_[1]) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supports: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 26 | ▶ | sub attach_stream { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
attach_stream: called CC=19 SCAR=29.6
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 26 | my ($self, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 26 | ▶ | croak 'attach_stream(): expected named arguments' if @args % 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 25 | my %options = @args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | croak 'attach_stream(): specify either protocol or alpn, not both' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 25 | ▶ | if exists($options{protocol}) && exists($options{alpn}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $options{"protocol"} and exists $options{"alpn"}
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"}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 24 | my $transport = delete $options{transport}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 24 | my $protocol = delete $options{protocol}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 24 | my $alpn = delete $options{alpn}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 24 | ▶ | croak 'attach_stream(): unknown option: ' . join(', ', sort keys %options) if %options; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 22 | ▶ | croak 'attach_stream(): protocol and alpn must be scalars' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $protocol or ref $alpn
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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | if ref($protocol) || ref($alpn); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 21 | ▶ | $protocol = defined($alpn) && length($alpn) ? $alpn : 'http/1.1' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $alpn and length $alpn
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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | unless defined $protocol; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 21 | ▶ | croak "attach_stream(): unsupported byte-stream protocol '$protocol'" | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$protocol eq "http/1.1" or $protocol eq "h2"
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"
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | unless $protocol eq 'http/1.1' || $protocol eq 'h2'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 19 | my $engine_options = $self->{policy}->options($protocol); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 17 | ▶ | croak 'attach_stream(): transport must be an object' unless blessed($transport); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 16 | for my $method (qw(unblock_send unblock_finish unblock_abort)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 46 | ▶ | croak "attach_stream(): transport must implement $method()" | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | unless $transport->can($method); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | return $self->_attach_websocket_stream($transport, $engine_options) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 15 | ▶ | if $protocol eq 'http/1.1' && $self->{policy}->websocket_enabled($protocol); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$protocol eq "http/1.1" and $self->{"policy"}->websocket_enabled($protocol)
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)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | return $self->_attach_h2_websocket_stream($transport, $engine_options) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 15 | ▶ | if $protocol eq 'h2' && $self->{policy}->websocket_enabled($protocol); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$protocol eq "h2" and $self->{"policy"}->websocket_enabled($protocol)
MC/DC0%0/2
$protocol eq "h2" and $self->{"policy"}->websocket_enabled($protocol)
$protocol eq "h2"
$self->{"policy"}->websocket_enabled($protocol)
Truth table66%2/3
A and B
A= $protocol eq "h2"B= $self->{"policy"}->websocket_enabled($protocol)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | return Unblock::HTTP::_EngineFactory->create( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 'Server', $protocol, %$engine_options, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 15 | %{ $self->{callbacks} }, transport => $transport, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 11 | ▶ | sub attach_quic { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
attach_quic: called CC=9 SCAR=22.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 11 | my ($self, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 11 | ▶ | croak 'attach_quic(): expected named arguments' if @args % 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 10 | my %options = @args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 10 | my $connection = delete $options{connection}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 10 | ▶ | croak 'attach_quic(): unknown option: ' . join(', ', sort keys %options) if %options; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 9 | my $engine_options = $self->{policy}->options('h3'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 8 | ▶ | croak 'attach_quic(): connection must be a QUIC connection object' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
builtin::blessed($connection) and $connection->can("ready") and $connection->can("closed")
builtin::blessed($connection) and $connection->can("ready")
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")
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | unless blessed($connection) && $connection->can('ready') && $connection->can('closed'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 7 | ▶ | croak 'attach_quic(): connection is closed' if $connection->closed; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 6 | ▶ | croak 'attach_quic(): connection is not ready' unless $connection->ready; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | return $self->_attach_h3_websocket_connection($connection, $engine_options) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 5 | ▶ | if $self->{policy}->websocket_enabled('h3'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | my $engine = Unblock::HTTP::_EngineFactory->create( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 'Server', 'h3', %$engine_options, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 5 | %{ $self->{callbacks} }, quic => $connection, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 5 | $engine->start; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 5 | return $engine; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 5 | 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | __END__ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||