| 1 | package PAGI::WebSocket; | ||||||||||||||||||||||||||||
| 2 | 22 | $PAGI::WebSocket::VERSION = '0.002000'; | |||||||||||||||||||||||||||
| 3 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use Encode qw(decode FB_CROAK FB_DEFAULT LEAVE_SRC); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 22 | ▶ | use Hash::MultiValue; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 22 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 22 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 22 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 22 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub new { | ||||||||||||||||||||||||||||
| 15 | 156 | ▶ | my ($class, $scope, $receive, $send) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 156 | ▶ | croak "PAGI::WebSocket requires scope hashref" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$scope and ref $scope eq "HASH"
MC/DC100%2/2
$scope and ref $scope eq "HASH"
$scope
ref $scope eq "HASH"
Truth table100%3/3
A and B
A= $scopeB= ref $scope eq "HASH"
| |||||||||||||||||||||||||||||
| 18 | unless $scope && ref($scope) eq 'HASH'; | ||||||||||||||||||||||||||||
| 19 | 154 | ▶ | croak "PAGI::WebSocket requires receive coderef" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$receive and ref $receive eq "CODE"
MC/DC100%2/2
$receive and ref $receive eq "CODE"
$receive
ref $receive eq "CODE"
Truth table100%3/3
A and B
A= $receiveB= ref $receive eq "CODE"
| |||||||||||||||||||||||||||||
| 20 | unless $receive && ref($receive) eq 'CODE'; | ||||||||||||||||||||||||||||
| 21 | 152 | ▶ | croak "PAGI::WebSocket requires send coderef" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$send and ref $send eq "CODE"
MC/DC100%2/2
$send and ref $send eq "CODE"
$send
ref $send eq "CODE"
Truth table100%3/3
A and B
A= $sendB= ref $send eq "CODE"
| |||||||||||||||||||||||||||||
| 22 | unless $send && ref($send) eq 'CODE'; | ||||||||||||||||||||||||||||
| 23 | croak "PAGI::WebSocket requires scope type 'websocket', got '$scope->{type}'" | ||||||||||||||||||||||||||||
| 24 | 150 | ▶ | unless ($scope->{type} // '') eq 'websocket'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$scope->{"type"} // ""
MC/DC0%0/1
$scope->{"type"} // ""
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Return existing WebSocket object if one was already created for this scope | ||||||||||||||||||||||||||||
| 27 | # This ensures consistent state (is_connected, is_closed, callbacks) if | ||||||||||||||||||||||||||||
| 28 | # multiple code paths create WebSocket objects from the same scope. | ||||||||||||||||||||||||||||
| 29 | 149 | ▶ | return $scope->{'pagi.websocket'} if $scope->{'pagi.websocket'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 149 | my $self = bless { | |||||||||||||||||||||||||||
| 32 | scope => $scope, | ||||||||||||||||||||||||||||
| 33 | receive => $receive, | ||||||||||||||||||||||||||||
| 34 | send => $send, | ||||||||||||||||||||||||||||
| 35 | _state => 'connecting', # connecting -> connected -> closed | ||||||||||||||||||||||||||||
| 36 | _close_code => undef, | ||||||||||||||||||||||||||||
| 37 | _close_reason => undef, | ||||||||||||||||||||||||||||
| 38 | _on_close => [], | ||||||||||||||||||||||||||||
| 39 | _on_error => [], | ||||||||||||||||||||||||||||
| 40 | _on_message => [], | ||||||||||||||||||||||||||||
| 41 | }, $class; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Cache in scope for reuse (weakened to avoid circular reference leak) | ||||||||||||||||||||||||||||
| 44 | 149 | $scope->{'pagi.websocket'} = $self; | |||||||||||||||||||||||||||
| 45 | 149 | Scalar::Util::weaken($scope->{'pagi.websocket'}); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 149 | return $self; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Scope property accessors | ||||||||||||||||||||||||||||
| 51 | 4 | ▶ | sub scope { shift->{scope} } | ||||||||||||||||||||||||||
|
Subroutine
scope: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 2 | ▶ | sub path { shift->{scope}{path} } | ||||||||||||||||||||||||||
|
Subroutine
path: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 2 | ▶ | sub raw_path { my $s = shift; $s->{scope}{raw_path} // $s->{scope}{path} } | ||||||||||||||||||||||||||
|
Subroutine
raw_path: called CC=2 SCAR=7.1
Pod
documented
Condition66%2/3
$s->{"scope"}{"raw_path"} // $s->{"scope"}{"path"}
MC/DC0%0/2
$s->{"scope"}{"raw_path"} // $s->{"scope"}{"path"}
$s->{"scope"}{"raw_path"}
$s->{"scope"}{"path"}
Truth table66%2/3
A // B
A= $s->{"scope"}{"raw_path"}B= $s->{"scope"}{"path"}
| |||||||||||||||||||||||||||||
| 54 | 19 | ▶ | sub query_string { shift->{scope}{query_string} // '' } | ||||||||||||||||||||||||||
|
Subroutine
query_string: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
+(shift())->{"scope"}{"query_string"} // ""
MC/DC100%1/1
+(shift())->{"scope"}{"query_string"} // ""
+(shift())->{"scope"}{"query_string"}
Truth table100%2/2
A
A= +(shift())->{"scope"}{"query_string"}
| |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | sub scheme { shift->{scope}{scheme} // 'ws' } | ||||||||||||||||||||||||||
|
Subroutine
scheme: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
+(shift())->{"scope"}{"scheme"} // "ws"
MC/DC100%1/1
+(shift())->{"scope"}{"scheme"} // "ws"
+(shift())->{"scope"}{"scheme"}
Truth table100%2/2
A
A= +(shift())->{"scope"}{"scheme"}
| |||||||||||||||||||||||||||||
| 56 | 3 | ▶ | sub http_version { shift->{scope}{http_version} // '1.1' } | ||||||||||||||||||||||||||
|
Subroutine
http_version: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
+(shift())->{"scope"}{"http_version"} // "1.1"
MC/DC100%1/1
+(shift())->{"scope"}{"http_version"} // "1.1"
+(shift())->{"scope"}{"http_version"}
Truth table100%2/2
A
A= +(shift())->{"scope"}{"http_version"}
| |||||||||||||||||||||||||||||
| 57 | 3 | ▶ | sub subprotocols { shift->{scope}{subprotocols} // [] } | ||||||||||||||||||||||||||
|
Subroutine
subprotocols: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
+(shift())->{"scope"}{"subprotocols"} // []
MC/DC100%1/1
+(shift())->{"scope"}{"subprotocols"} // []
+(shift())->{"scope"}{"subprotocols"}
Truth table100%2/2
A
A= +(shift())->{"scope"}{"subprotocols"}
| |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | sub client { shift->{scope}{client} } | ||||||||||||||||||||||||||
|
Subroutine
client: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | sub server { shift->{scope}{server} } | ||||||||||||||||||||||||||
|
Subroutine
server: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Application state (injected by PAGI::Lifespan, read-only) | ||||||||||||||||||||||||||||
| 63 | sub state { | ||||||||||||||||||||||||||||
| 64 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
state: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 6 | ▶ | return $self->{scope}{state} // {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"scope"}{"state"} // {}
MC/DC100%1/1
$self->{"scope"}{"state"} // {}
$self->{"scope"}{"state"}
Truth table100%2/2
A
A= $self->{"scope"}{"state"}
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Path parameter accessors - captured from URL path by router | ||||||||||||||||||||||||||||
| 69 | # Stored in scope->{path_params} for router-agnostic access | ||||||||||||||||||||||||||||
| 70 | sub path_params { | ||||||||||||||||||||||||||||
| 71 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
path_params: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 2 | ▶ | return $self->{scope}{path_params} // {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"scope"}{"path_params"} // {}
MC/DC100%1/1
$self->{"scope"}{"path_params"} // {}
$self->{"scope"}{"path_params"}
Truth table100%2/2
A
A= $self->{"scope"}{"path_params"}
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub path_param { | ||||||||||||||||||||||||||||
| 76 | 6 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
path_param: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 6 | ▶ | my $params = $self->{scope}{path_params} // {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"scope"}{"path_params"} // {}
MC/DC100%1/1
$self->{"scope"}{"path_params"} // {}
$self->{"scope"}{"path_params"}
Truth table100%2/2
A
A= $self->{"scope"}{"path_params"}
| |||||||||||||||||||||||||||||
| 78 | 6 | return $params->{$name}; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Internal: URL decode a string (handles + as space) | ||||||||||||||||||||||||||||
| 82 | sub _url_decode { | ||||||||||||||||||||||||||||
| 83 | 58 | ▶ | my ($str) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_url_decode: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 84 | 58 | ▶ | return '' unless defined $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 58 | $str =~ s/\+/ /g; | |||||||||||||||||||||||||||
| 86 | 58 | $str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge; | |||||||||||||||||||||||||||
| 87 | 58 | return $str; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Internal: Decode UTF-8 with replacement or croak in strict mode | ||||||||||||||||||||||||||||
| 91 | sub _decode_utf8 { | ||||||||||||||||||||||||||||
| 92 | 50 | ▶ | my ($str, $strict) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_decode_utf8: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 93 | 50 | ▶ | return '' unless defined $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 50 | ▶ | my $flag = $strict ? FB_CROAK : FB_DEFAULT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 50 | $flag |= LEAVE_SRC; | |||||||||||||||||||||||||||
| 96 | 50 | return decode('UTF-8', $str, $flag); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # Query params as Hash::MultiValue (cached in scope) | ||||||||||||||||||||||||||||
| 100 | # Options: strict => 1 (croak on invalid UTF-8), raw => 1 (skip UTF-8 decoding) | ||||||||||||||||||||||||||||
| 101 | sub query_params { | ||||||||||||||||||||||||||||
| 102 | 32 | ▶ | my ($self, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
query_params: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 32 | ▶ | my $strict = delete $opts{strict} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $opts{"strict"} // 0
MC/DC100%1/1
delete $opts{"strict"} // 0
delete $opts{"strict"}
Truth table100%2/2
A
A= delete $opts{"strict"}
| |||||||||||||||||||||||||||||
| 104 | 32 | ▶ | my $raw = delete $opts{raw} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $opts{"raw"} // 0
MC/DC100%1/1
delete $opts{"raw"} // 0
delete $opts{"raw"}
Truth table100%2/2
A
A= delete $opts{"raw"}
| |||||||||||||||||||||||||||||
| 105 | 32 | ▶ | croak("Unknown options to query_params: " . join(', ', keys %opts)) if %opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 31 | ▶ | my $cache_key = $raw ? 'pagi.websocket.query.raw' : ($strict ? 'pagi.websocket.query.strict' : 'pagi.websocket.query'); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 108 | 31 | ▶ | return $self->{scope}{$cache_key} if $self->{scope}{$cache_key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 17 | my $qs = $self->query_string; | |||||||||||||||||||||||||||
| 111 | 17 | my @pairs; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 17 | for my $part (split /[&;]/, $qs) { | |||||||||||||||||||||||||||
| 114 | 29 | ▶ | next unless length $part; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 29 | my ($key, $val) = split /=/, $part, 2; | |||||||||||||||||||||||||||
| 116 | 29 | ▶ | $key //= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$key //= ""
MC/DC0%0/1
$key //= ""
$key
Truth table50%1/2
A
A= $key
| |||||||||||||||||||||||||||||
| 117 | 29 | ▶ | $val //= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$val //= ""
MC/DC100%1/1
$val //= ""
$val
Truth table100%2/2
A
A= $val
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # URL decode (handles + as space) | ||||||||||||||||||||||||||||
| 120 | 29 | my $key_decoded = _url_decode($key); | |||||||||||||||||||||||||||
| 121 | 29 | my $val_decoded = _url_decode($val); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # UTF-8 decode unless raw mode | ||||||||||||||||||||||||||||
| 124 | 29 | ▶ | my $key_final = $raw ? $key_decoded : _decode_utf8($key_decoded, $strict); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 29 | ▶ | my $val_final = $raw ? $val_decoded : _decode_utf8($val_decoded, $strict); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 28 | push @pairs, $key_final, $val_final; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 16 | $self->{scope}{$cache_key} = Hash::MultiValue->new(@pairs); | |||||||||||||||||||||||||||
| 131 | 16 | return $self->{scope}{$cache_key}; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # Raw query params (no UTF-8 decoding) | ||||||||||||||||||||||||||||
| 135 | sub raw_query_params { | ||||||||||||||||||||||||||||
| 136 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
raw_query_params: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 2 | return $self->query_params(raw => 1); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # Shortcut for single query param | ||||||||||||||||||||||||||||
| 141 | sub query { | ||||||||||||||||||||||||||||
| 142 | 23 | ▶ | my ($self, $name, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 23 | return $self->query_params(%opts)->get($name); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Raw single query param | ||||||||||||||||||||||||||||
| 147 | sub raw_query { | ||||||||||||||||||||||||||||
| 148 | 2 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
raw_query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 2 | return $self->query($name, raw => 1); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # Single header lookup (case-insensitive, returns last value) | ||||||||||||||||||||||||||||
| 153 | sub header { | ||||||||||||||||||||||||||||
| 154 | 4 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
header: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 4 | $name = lc($name); | |||||||||||||||||||||||||||
| 156 | 4 | my $value; | |||||||||||||||||||||||||||
| 157 | 4 | ▶ | for my $pair (@{$self->{scope}{headers} // []}) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"scope"}{"headers"} // []
MC/DC0%0/1
$self->{"scope"}{"headers"} // []
$self->{"scope"}{"headers"}
Truth table50%1/2
A
A= $self->{"scope"}{"headers"}
| |||||||||||||||||||||||||||||
| 158 | 20 | ▶ | if (lc($pair->[0]) eq $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 4 | $value = $pair->[1]; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 4 | return $value; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # All headers as Hash::MultiValue (cached in scope) | ||||||||||||||||||||||||||||
| 166 | sub headers { | ||||||||||||||||||||||||||||
| 167 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
headers: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | 3 | ▶ | return $self->{scope}{'pagi.request.headers'} if $self->{scope}{'pagi.request.headers'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 2 | my @pairs; | |||||||||||||||||||||||||||
| 171 | 2 | ▶ | for my $pair (@{$self->{scope}{headers} // []}) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"scope"}{"headers"} // []
MC/DC0%0/1
$self->{"scope"}{"headers"} // []
$self->{"scope"}{"headers"}
Truth table50%1/2
A
A= $self->{"scope"}{"headers"}
| |||||||||||||||||||||||||||||
| 172 | 8 | push @pairs, lc($pair->[0]), $pair->[1]; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 2 | $self->{scope}{'pagi.request.headers'} = Hash::MultiValue->new(@pairs); | |||||||||||||||||||||||||||
| 176 | 2 | return $self->{scope}{'pagi.request.headers'}; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # All values for a header | ||||||||||||||||||||||||||||
| 180 | sub header_all { | ||||||||||||||||||||||||||||
| 181 | 2 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
header_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 182 | 2 | return $self->headers->get_all(lc($name)); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # State accessors | ||||||||||||||||||||||||||||
| 186 | 4 | ▶ | sub connection_state { shift->{_state} } | ||||||||||||||||||||||||||
|
Subroutine
connection_state: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | sub is_connected { | ||||||||||||||||||||||||||||
| 189 | 17 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
is_connected: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 17 | return $self->{_state} eq 'connected'; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | sub is_closed { | ||||||||||||||||||||||||||||
| 194 | 135 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
is_closed: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 195 | 135 | return $self->{_state} eq 'closed'; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 50 | ▶ | sub close_code { shift->{_close_code} } | ||||||||||||||||||||||||||
|
Subroutine
close_code: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 199 | 50 | ▶ | sub close_reason { shift->{_close_reason} } | ||||||||||||||||||||||||||
|
Subroutine
close_reason: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | # Outbound flow-control introspection (delegates to the pagi.transport handle) | ||||||||||||||||||||||||||||
| 202 | sub buffered_amount { | ||||||||||||||||||||||||||||
| 203 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
buffered_amount: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 204 | 2 | my $t = $self->{scope}{'pagi.transport'}; | |||||||||||||||||||||||||||
| 205 | 2 | ▶ | return 0 unless $t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 1 | return $t->buffered_amount; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | sub high_water_mark { | ||||||||||||||||||||||||||||
| 210 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
high_water_mark: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 211 | 2 | my $t = $self->{scope}{'pagi.transport'}; | |||||||||||||||||||||||||||
| 212 | 2 | ▶ | return undef unless $t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 1 | return $t->high_water_mark; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | sub low_water_mark { | ||||||||||||||||||||||||||||
| 217 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
low_water_mark: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 218 | 2 | my $t = $self->{scope}{'pagi.transport'}; | |||||||||||||||||||||||||||
| 219 | 2 | ▶ | return undef unless $t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 1 | return $t->low_water_mark; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | sub on_high_water { | ||||||||||||||||||||||||||||
| 224 | 2 | ▶ | my ($self, $cb) = @_; | ||||||||||||||||||||||||||
|
Subroutine
on_high_water: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 225 | 2 | my $t = $self->{scope}{'pagi.transport'}; | |||||||||||||||||||||||||||
| 226 | 2 | ▶ | $t->on_high_water($cb) if $t && $t->can('on_high_water'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$t and $t->can("on_high_water")
MC/DC50%1/2
$t and $t->can("on_high_water")
$t
$t->can("on_high_water")
Truth table66%2/3
A and B
A= $tB= $t->can("on_high_water")
| |||||||||||||||||||||||||||||
| 227 | 2 | return $self; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | sub on_drain { | ||||||||||||||||||||||||||||
| 231 | 2 | ▶ | my ($self, $cb) = @_; | ||||||||||||||||||||||||||
|
Subroutine
on_drain: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 232 | 2 | my $t = $self->{scope}{'pagi.transport'}; | |||||||||||||||||||||||||||
| 233 | 2 | ▶ | $t->on_drain($cb) if $t && $t->can('on_drain'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$t and $t->can("on_drain")
MC/DC50%1/2
$t and $t->can("on_drain")
$t
$t->can("on_drain")
Truth table66%2/3
A and B
A= $tB= $t->can("on_drain")
| |||||||||||||||||||||||||||||
| 234 | 2 | return $self; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | sub is_writable { | ||||||||||||||||||||||||||||
| 238 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
is_writable: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 239 | 3 | my $t = $self->{scope}{'pagi.transport'}; | |||||||||||||||||||||||||||
| 240 | 3 | ▶ | return 1 unless $t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 2 | my $high = $t->high_water_mark; | |||||||||||||||||||||||||||
| 242 | 2 | ▶ | return 1 unless defined $high; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 2 | ▶ | return $t->buffered_amount < $high ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | # Internal state setters | ||||||||||||||||||||||||||||
| 247 | sub _set_state { | ||||||||||||||||||||||||||||
| 248 | 101 | ▶ | my ($self, $state) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_state: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 249 | 101 | $self->{_state} = $state; | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | sub _set_closed { | ||||||||||||||||||||||||||||
| 253 | 50 | ▶ | my ($self, $code, $reason) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_closed: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 254 | 50 | $self->{_state} = 'closed'; | |||||||||||||||||||||||||||
| 255 | 50 | ▶ | $self->{_close_code} = $code // 1005; | ||||||||||||||||||||||||||
|
Condition100%2/2
$code // 1005
MC/DC100%1/1
$code // 1005
$code
Truth table100%2/2
A
A= $code
| |||||||||||||||||||||||||||||
| 256 | 50 | ▶ | $self->{_close_reason} = $reason // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$reason // ""
MC/DC100%1/1
$reason // ""
$reason
Truth table100%2/2
A
A= $reason
| |||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | # Register callback to run on disconnect/close | ||||||||||||||||||||||||||||
| 260 | sub on_close { | ||||||||||||||||||||||||||||
| 261 | 17 | ▶ | my ($self, $callback) = @_; | ||||||||||||||||||||||||||
|
Subroutine
on_close: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 262 | 17 | push @{$self->{_on_close}}, $callback; | |||||||||||||||||||||||||||
| 263 | 17 | return $self; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # Internal: run all on_close callbacks exactly once | ||||||||||||||||||||||||||||
| 267 | 43 | ▶ | async sub _run_close_callbacks { | ||||||||||||||||||||||||||
|
Subroutine
_run_close_callbacks: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 268 | 43 | my ($self) = @_; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | # Only run once | ||||||||||||||||||||||||||||
| 271 | 43 | ▶ | return if $self->{_close_callbacks_ran}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 43 | $self->{_close_callbacks_ran} = 1; | |||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 43 | my $code = $self->close_code; | |||||||||||||||||||||||||||
| 275 | 43 | my $reason = $self->close_reason; | |||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 43 | for my $cb (@{$self->{_on_close}}) { | |||||||||||||||||||||||||||
| 278 | 15 | eval { | |||||||||||||||||||||||||||
| 279 | 15 | my $r = $cb->($code, $reason); | |||||||||||||||||||||||||||
| 280 | # Only await if callback returns a Future | ||||||||||||||||||||||||||||
| 281 | 15 | ▶ | if (blessed($r) && $r->isa('Future')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($r) and $r->isa("Future")
MC/DC100%2/2
builtin::blessed($r) and $r->isa("Future")
builtin::blessed($r)
$r->isa("Future")
Truth table100%3/3
A and B
A= builtin::blessed($r)B= $r->isa("Future")
| |||||||||||||||||||||||||||||
| 282 | 10 | await $r; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | }; | ||||||||||||||||||||||||||||
| 285 | 15 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 1 | warn "PAGI::WebSocket on_close callback error: $@"; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | # Clear all callback arrays to break any closure-based cycles | ||||||||||||||||||||||||||||
| 291 | 43 | $self->{_on_close} = []; | |||||||||||||||||||||||||||
| 292 | 43 | $self->{_on_error} = []; | |||||||||||||||||||||||||||
| 293 | 43 | $self->{_on_message} = []; | |||||||||||||||||||||||||||
| 294 | 22 | } | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # Register callback to run on errors | ||||||||||||||||||||||||||||
| 297 | sub on_error { | ||||||||||||||||||||||||||||
| 298 | 10 | ▶ | my ($self, $callback) = @_; | ||||||||||||||||||||||||||
|
Subroutine
on_error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 299 | 10 | push @{$self->{_on_error}}, $callback; | |||||||||||||||||||||||||||
| 300 | 10 | return $self; | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | # Register callback to run on message receive | ||||||||||||||||||||||||||||
| 304 | sub on_message { | ||||||||||||||||||||||||||||
| 305 | 12 | ▶ | my ($self, $callback) = @_; | ||||||||||||||||||||||||||
|
Subroutine
on_message: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 306 | 12 | push @{$self->{_on_message}}, $callback; | |||||||||||||||||||||||||||
| 307 | 12 | return $self; | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # Generic event registration (Socket.IO style) | ||||||||||||||||||||||||||||
| 311 | sub on { | ||||||||||||||||||||||||||||
| 312 | 8 | ▶ | my ($self, $event, $callback) = @_; | ||||||||||||||||||||||||||
|
Subroutine
on: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 8 | ▶ | if ($event eq 'message') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 315 | 3 | return $self->on_message($callback); | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | elsif ($event eq 'close') { | ||||||||||||||||||||||||||||
| 318 | 2 | return $self->on_close($callback); | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | elsif ($event eq 'error') { | ||||||||||||||||||||||||||||
| 321 | 2 | return $self->on_error($callback); | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | else { | ||||||||||||||||||||||||||||
| 324 | 1 | croak "Unknown event type: $event (expected message, close, or error)"; | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | # Internal: trigger error callbacks | ||||||||||||||||||||||||||||
| 329 | 7 | ▶ | async sub _trigger_error { | ||||||||||||||||||||||||||
|
Subroutine
_trigger_error: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 330 | 7 | my ($self, $error) = @_; | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 7 | for my $cb (@{$self->{_on_error}}) { | |||||||||||||||||||||||||||
| 333 | 8 | eval { | |||||||||||||||||||||||||||
| 334 | 8 | my $r = $cb->($error); | |||||||||||||||||||||||||||
| 335 | 8 | ▶ | if (blessed($r) && $r->isa('Future')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($r) and $r->isa("Future")
MC/DC50%1/2
builtin::blessed($r) and $r->isa("Future")
builtin::blessed($r)
$r->isa("Future")
Truth table66%2/3
A and B
A= builtin::blessed($r)B= $r->isa("Future")
| |||||||||||||||||||||||||||||
| 336 | 2 | await $r; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | }; | ||||||||||||||||||||||||||||
| 339 | 8 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | 1 | warn "PAGI::WebSocket on_error callback error: $@"; | |||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | # If no error handlers registered, warn | ||||||||||||||||||||||||||||
| 345 | 7 | ▶ | if (!@{$self->{_on_error}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | 1 | warn "PAGI::WebSocket error: $error"; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | 22 | } | |||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | # Accept the WebSocket connection | ||||||||||||||||||||||||||||
| 351 | 95 | ▶ | async sub accept { | ||||||||||||||||||||||||||
|
Subroutine
accept: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 352 | 95 | my ($self, %opts) = @_; | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 95 | my $event = { | |||||||||||||||||||||||||||
| 355 | type => 'websocket.accept', | ||||||||||||||||||||||||||||
| 356 | }; | ||||||||||||||||||||||||||||
| 357 | 95 | ▶ | $event->{subprotocol} = $opts{subprotocol} if exists $opts{subprotocol}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | 95 | ▶ | $event->{headers} = $opts{headers} if exists $opts{headers}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 95 | await $self->{send}->($event); | |||||||||||||||||||||||||||
| 361 | 95 | $self->_set_state('connected'); | |||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 95 | return $self; | |||||||||||||||||||||||||||
| 364 | 22 | } | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | # Close the WebSocket connection | ||||||||||||||||||||||||||||
| 367 | 19 | ▶ | async sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 368 | 19 | my ($self, $code, $reason) = @_; | |||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | # Idempotent - don't send close twice | ||||||||||||||||||||||||||||
| 371 | 19 | ▶ | return if $self->is_closed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 16 | ▶ | $code //= 1000; | ||||||||||||||||||||||||||
|
Condition100%2/2
$code //= 1000
MC/DC100%1/1
$code //= 1000
$code
Truth table100%2/2
A
A= $code
| |||||||||||||||||||||||||||||
| 374 | 16 | ▶ | $reason //= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$reason //= ""
MC/DC100%1/1
$reason //= ""
$reason
Truth table100%2/2
A
A= $reason
| |||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 16 | await $self->{send}->({ | |||||||||||||||||||||||||||
| 377 | type => 'websocket.close', | ||||||||||||||||||||||||||||
| 378 | code => $code, | ||||||||||||||||||||||||||||
| 379 | reason => $reason, | ||||||||||||||||||||||||||||
| 380 | }); | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 16 | $self->_set_closed($code, $reason); | |||||||||||||||||||||||||||
| 383 | 16 | await $self->_run_close_callbacks; | |||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 16 | return $self; | |||||||||||||||||||||||||||
| 386 | 22 | } | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | # Whether the server advertised the WebSocket denial-response extension. | ||||||||||||||||||||||||||||
| 389 | # See L<PAGI::Spec::Www/"WebSocket Denial Response">. | ||||||||||||||||||||||||||||
| 390 | sub supports_denial_response { | ||||||||||||||||||||||||||||
| 391 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
supports_denial_response: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 392 | 5 | ▶ | return $self->{scope}{extensions}{'websocket.http.response'} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | # Reject the handshake with a custom HTTP response (status/headers/body) instead | ||||||||||||||||||||||||||||
| 396 | # of the bare 403. Falls back to a plain close when the server does not advertise | ||||||||||||||||||||||||||||
| 397 | # the extension. Valid only before accept. | ||||||||||||||||||||||||||||
| 398 | # See L<PAGI::Spec::Www/"WebSocket Denial Response">. | ||||||||||||||||||||||||||||
| 399 | 2 | ▶ | async sub deny { | ||||||||||||||||||||||||||
|
Subroutine
deny: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 400 | 2 | my ($self, %opts) = @_; | |||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 2 | ▶ | my $status = $opts{status} // 403; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"status"} // 403
MC/DC0%0/1
$opts{"status"} // 403
$opts{"status"}
Truth table50%1/2
A
A= $opts{"status"}
| |||||||||||||||||||||||||||||
| 403 | 2 | ▶ | my $headers = $opts{headers} // []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"headers"} // []
MC/DC100%1/1
$opts{"headers"} // []
$opts{"headers"}
Truth table100%2/2
A
A= $opts{"headers"}
| |||||||||||||||||||||||||||||
| 404 | 2 | ▶ | my $body = defined $opts{body} ? $opts{body} : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 2 | ▶ | if (!$self->supports_denial_response) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 407 | 1 | await $self->{send}->({ type => 'websocket.close', code => 1008, reason => '' }); | |||||||||||||||||||||||||||
| 408 | 1 | $self->_set_closed(1008, ''); | |||||||||||||||||||||||||||
| 409 | 1 | return $self; | |||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | await $self->{send}->({ | ||||||||||||||||||||||||||||
| 413 | type => 'websocket.http.response.start', | ||||||||||||||||||||||||||||
| 414 | status => $status, | ||||||||||||||||||||||||||||
| 415 | headers => $headers, | ||||||||||||||||||||||||||||
| 416 | 1 | }); | |||||||||||||||||||||||||||
| 417 | 1 | await $self->{send}->({ | |||||||||||||||||||||||||||
| 418 | type => 'websocket.http.response.body', | ||||||||||||||||||||||||||||
| 419 | body => $body, | ||||||||||||||||||||||||||||
| 420 | more => 0, | ||||||||||||||||||||||||||||
| 421 | }); | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 1 | $self->_set_closed($status, ''); | |||||||||||||||||||||||||||
| 424 | 1 | return $self; | |||||||||||||||||||||||||||
| 425 | 22 | } | |||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | # Send text message | ||||||||||||||||||||||||||||
| 428 | 7 | ▶ | async sub send_text { | ||||||||||||||||||||||||||
|
Subroutine
send_text: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 429 | 7 | my ($self, $text) = @_; | |||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 7 | ▶ | croak "Cannot send on closed WebSocket" if $self->is_closed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 6 | await $self->{send}->({ | |||||||||||||||||||||||||||
| 434 | type => 'websocket.send', | ||||||||||||||||||||||||||||
| 435 | text => $text, | ||||||||||||||||||||||||||||
| 436 | }); | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 6 | return $self; | |||||||||||||||||||||||||||
| 439 | 22 | } | |||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | # Send binary message | ||||||||||||||||||||||||||||
| 442 | 3 | ▶ | async sub send_bytes { | ||||||||||||||||||||||||||
|
Subroutine
send_bytes: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 443 | 3 | my ($self, $bytes) = @_; | |||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 3 | ▶ | croak "Cannot send on closed WebSocket" if $self->is_closed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 2 | await $self->{send}->({ | |||||||||||||||||||||||||||
| 448 | type => 'websocket.send', | ||||||||||||||||||||||||||||
| 449 | bytes => $bytes, | ||||||||||||||||||||||||||||
| 450 | }); | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 2 | return $self; | |||||||||||||||||||||||||||
| 453 | 22 | } | |||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | # Send JSON-encoded message | ||||||||||||||||||||||||||||
| 456 | 6 | ▶ | async sub send_json { | ||||||||||||||||||||||||||
|
Subroutine
send_json: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 457 | 6 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | 6 | ▶ | croak "Cannot send on closed WebSocket" if $self->is_closed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 5 | my $json = JSON::MaybeXS::encode_json($data); | |||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | 5 | await $self->{send}->({ | |||||||||||||||||||||||||||
| 464 | type => 'websocket.send', | ||||||||||||||||||||||||||||
| 465 | text => $json, | ||||||||||||||||||||||||||||
| 466 | }); | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 5 | return $self; | |||||||||||||||||||||||||||
| 469 | 22 | } | |||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | # Safe send methods - return bool instead of throwing | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | 6 | ▶ | async sub try_send_text { | ||||||||||||||||||||||||||
|
Subroutine
try_send_text: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 474 | 6 | my ($self, $text) = @_; | |||||||||||||||||||||||||||
| 475 | 6 | ▶ | return 0 if $self->is_closed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | 5 | eval { | |||||||||||||||||||||||||||
| 478 | 5 | await $self->{send}->({ | |||||||||||||||||||||||||||
| 479 | type => 'websocket.send', | ||||||||||||||||||||||||||||
| 480 | text => $text, | ||||||||||||||||||||||||||||
| 481 | }); | ||||||||||||||||||||||||||||
| 482 | }; | ||||||||||||||||||||||||||||
| 483 | 5 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 484 | 1 | $self->_set_closed(1006, 'Connection lost'); | |||||||||||||||||||||||||||
| 485 | 1 | return 0; | |||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||
| 487 | 4 | return 1; | |||||||||||||||||||||||||||
| 488 | 22 | } | |||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | 6 | ▶ | async sub try_send_bytes { | ||||||||||||||||||||||||||
|
Subroutine
try_send_bytes: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 491 | 6 | my ($self, $bytes) = @_; | |||||||||||||||||||||||||||
| 492 | 6 | ▶ | return 0 if $self->is_closed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | 5 | eval { | |||||||||||||||||||||||||||
| 495 | 5 | await $self->{send}->({ | |||||||||||||||||||||||||||
| 496 | type => 'websocket.send', | ||||||||||||||||||||||||||||
| 497 | bytes => $bytes, | ||||||||||||||||||||||||||||
| 498 | }); | ||||||||||||||||||||||||||||
| 499 | }; | ||||||||||||||||||||||||||||
| 500 | 5 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 501 | 1 | $self->_set_closed(1006, 'Connection lost'); | |||||||||||||||||||||||||||
| 502 | 1 | return 0; | |||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||
| 504 | 4 | return 1; | |||||||||||||||||||||||||||
| 505 | 22 | } | |||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | 6 | ▶ | async sub try_send_json { | ||||||||||||||||||||||||||
|
Subroutine
try_send_json: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 508 | 6 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 509 | 6 | ▶ | return 0 if $self->is_closed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | 5 | my $json = JSON::MaybeXS::encode_json($data); | |||||||||||||||||||||||||||
| 512 | 5 | eval { | |||||||||||||||||||||||||||
| 513 | 5 | await $self->{send}->({ | |||||||||||||||||||||||||||
| 514 | type => 'websocket.send', | ||||||||||||||||||||||||||||
| 515 | text => $json, | ||||||||||||||||||||||||||||
| 516 | }); | ||||||||||||||||||||||||||||
| 517 | }; | ||||||||||||||||||||||||||||
| 518 | 5 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 519 | 1 | $self->_set_closed(1006, 'Connection lost'); | |||||||||||||||||||||||||||
| 520 | 1 | return 0; | |||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||
| 522 | 4 | return 1; | |||||||||||||||||||||||||||
| 523 | 22 | } | |||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | # Silent send methods - no-op when closed | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | 3 | ▶ | async sub send_text_if_connected { | ||||||||||||||||||||||||||
|
Subroutine
send_text_if_connected: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 528 | 3 | my ($self, $text) = @_; | |||||||||||||||||||||||||||
| 529 | 3 | ▶ | return unless $self->is_connected; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 530 | 2 | await $self->try_send_text($text); | |||||||||||||||||||||||||||
| 531 | 2 | return; | |||||||||||||||||||||||||||
| 532 | 22 | } | |||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | 3 | ▶ | async sub send_bytes_if_connected { | ||||||||||||||||||||||||||
|
Subroutine
send_bytes_if_connected: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 535 | 3 | my ($self, $bytes) = @_; | |||||||||||||||||||||||||||
| 536 | 3 | ▶ | return unless $self->is_connected; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 537 | 2 | await $self->try_send_bytes($bytes); | |||||||||||||||||||||||||||
| 538 | 2 | return; | |||||||||||||||||||||||||||
| 539 | 22 | } | |||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | 3 | ▶ | async sub send_json_if_connected { | ||||||||||||||||||||||||||
|
Subroutine
send_json_if_connected: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 542 | 3 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 543 | 3 | ▶ | return unless $self->is_connected; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 544 | 2 | await $self->try_send_json($data); | |||||||||||||||||||||||||||
| 545 | 2 | return; | |||||||||||||||||||||||||||
| 546 | 22 | } | |||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | # Receive methods | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | 69 | ▶ | async sub receive { | ||||||||||||||||||||||||||
|
Subroutine
receive: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 551 | 69 | my ($self) = @_; | |||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | 69 | ▶ | return undef if $self->is_closed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | 64 | while (1) { | |||||||||||||||||||||||||||
| 556 | 99 | my $event = await $self->{receive}->(); | |||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | 98 | ▶ | if (!defined($event) || $event->{type} eq 'websocket.disconnect') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $event or $event->{"type"} eq "websocket.disconnect"
MC/DC50%1/2
not defined $event or $event->{"type"} eq "websocket.disconnect"
not defined $event
$event->{"type"} eq "websocket.disconnect"
Truth table66%2/3
not(A) or B
A= not defined $eventB= $event->{"type"} eq "websocket.disconnect"
| |||||||||||||||||||||||||||||
| 559 | # 1005 = No Status Rcvd (RFC 6455) | ||||||||||||||||||||||||||||
| 560 | 27 | ▶ | my $code = $event->{code} // 1005; | ||||||||||||||||||||||||||
|
Condition100%2/2
$event->{"code"} // 1005
MC/DC100%1/1
$event->{"code"} // 1005
$event->{"code"}
Truth table100%2/2
A
A= $event->{"code"}
| |||||||||||||||||||||||||||||
| 561 | 27 | ▶ | my $reason = $event->{reason} // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$event->{"reason"} // ""
MC/DC100%1/1
$event->{"reason"} // ""
$event->{"reason"}
Truth table100%2/2
A
A= $event->{"reason"}
| |||||||||||||||||||||||||||||
| 562 | 27 | $self->_set_closed($code, $reason); | |||||||||||||||||||||||||||
| 563 | 27 | await $self->_run_close_callbacks; | |||||||||||||||||||||||||||
| 564 | 27 | return undef; | |||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | # Skip connect events - they're handled by accept() | ||||||||||||||||||||||||||||
| 568 | 71 | ▶ | next if $event->{type} eq 'websocket.connect'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | 36 | return $event; | |||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||
| 572 | 22 | } | |||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | 28 | ▶ | async sub receive_text { | ||||||||||||||||||||||||||
|
Subroutine
receive_text: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 575 | 28 | my ($self) = @_; | |||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | 28 | while (1) { | |||||||||||||||||||||||||||
| 578 | 30 | my $event = await $self->receive; | |||||||||||||||||||||||||||
| 579 | 30 | ▶ | return undef unless $event; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | # Skip non-receive events and binary frames | ||||||||||||||||||||||||||||
| 582 | 20 | ▶ | next unless $event->{type} eq 'websocket.receive'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 583 | 20 | ▶ | next unless exists $event->{text}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | 18 | return $event->{text}; | |||||||||||||||||||||||||||
| 586 | } | ||||||||||||||||||||||||||||
| 587 | 22 | } | |||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | 5 | ▶ | async sub receive_bytes { | ||||||||||||||||||||||||||
|
Subroutine
receive_bytes: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 590 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | 5 | while (1) { | |||||||||||||||||||||||||||
| 593 | 5 | my $event = await $self->receive; | |||||||||||||||||||||||||||
| 594 | 5 | ▶ | return undef unless $event; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | # Skip non-receive events and text frames | ||||||||||||||||||||||||||||
| 597 | 3 | ▶ | next unless $event->{type} eq 'websocket.receive'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 598 | 3 | ▶ | next unless exists $event->{bytes}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | 3 | return $event->{bytes}; | |||||||||||||||||||||||||||
| 601 | } | ||||||||||||||||||||||||||||
| 602 | 22 | } | |||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | 4 | ▶ | async sub receive_json { | ||||||||||||||||||||||||||
|
Subroutine
receive_json: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 605 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | 4 | my $text = await $self->receive_text; | |||||||||||||||||||||||||||
| 608 | 4 | ▶ | return undef unless defined $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | 3 | return JSON::MaybeXS::decode_json($text); | |||||||||||||||||||||||||||
| 611 | 22 | } | |||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | # Iteration helpers | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | 2 | ▶ | async sub each_message { | ||||||||||||||||||||||||||
|
Subroutine
each_message: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 616 | 2 | my ($self, $callback) = @_; | |||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | 2 | ▶ | while (my $event = await $self->receive) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 619 | 5 | ▶ | next unless $event->{type} eq 'websocket.receive'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 620 | 5 | await $callback->($event); | |||||||||||||||||||||||||||
| 621 | } | ||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | 2 | return; | |||||||||||||||||||||||||||
| 624 | 22 | } | |||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | 6 | ▶ | async sub each_text { | ||||||||||||||||||||||||||
|
Subroutine
each_text: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 627 | 6 | my ($self, $callback) = @_; | |||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | 6 | ▶ | while (my $text = await $self->receive_text) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 630 | 9 | await $callback->($text); | |||||||||||||||||||||||||||
| 631 | } | ||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | 5 | return; | |||||||||||||||||||||||||||
| 634 | 22 | } | |||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | 1 | ▶ | async sub each_bytes { | ||||||||||||||||||||||||||
|
Subroutine
each_bytes: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 637 | 1 | my ($self, $callback) = @_; | |||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | 1 | ▶ | while (my $bytes = await $self->receive_bytes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 640 | 1 | await $callback->($bytes); | |||||||||||||||||||||||||||
| 641 | } | ||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | 1 | return; | |||||||||||||||||||||||||||
| 644 | 22 | } | |||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | 3 | ▶ | async sub each_json { | ||||||||||||||||||||||||||
|
Subroutine
each_json: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 647 | 3 | my ($self, $callback) = @_; | |||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||
| 649 | 3 | while (1) { | |||||||||||||||||||||||||||
| 650 | 6 | my $text = await $self->receive_text; | |||||||||||||||||||||||||||
| 651 | 6 | ▶ | last unless defined $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | 3 | my $data = JSON::MaybeXS::decode_json($text); | |||||||||||||||||||||||||||
| 654 | 3 | await $callback->($data); | |||||||||||||||||||||||||||
| 655 | } | ||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | 3 | return; | |||||||||||||||||||||||||||
| 658 | 22 | } | |||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||
| 660 | # Callback-based event loop (alternative to each_* iteration) | ||||||||||||||||||||||||||||
| 661 | 9 | ▶ | async sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 662 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | 9 | while (1) { | |||||||||||||||||||||||||||
| 665 | 16 | my $event = eval { await $self->receive }; | |||||||||||||||||||||||||||
| 666 | 16 | ▶ | if (my $err = $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 667 | 1 | warn "PAGI::WebSocket receive error: $err"; | |||||||||||||||||||||||||||
| 668 | 1 | last; | |||||||||||||||||||||||||||
| 669 | } | ||||||||||||||||||||||||||||
| 670 | 15 | ▶ | last unless $event; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | 7 | ▶ | next unless $event->{type} eq 'websocket.receive'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | 7 | ▶ | my $data = $event->{text} // $event->{bytes}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$event->{"text"} // $event->{"bytes"}
MC/DC0%0/2
$event->{"text"} // $event->{"bytes"}
$event->{"text"}
$event->{"bytes"}
Truth table33%1/3
A // B
A= $event->{"text"}B= $event->{"bytes"}
| |||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | 7 | for my $cb (@{$self->{_on_message}}) { | |||||||||||||||||||||||||||
| 677 | 8 | eval { | |||||||||||||||||||||||||||
| 678 | 8 | my $r = $cb->($data, $event); | |||||||||||||||||||||||||||
| 679 | # Await if callback returns a Future | ||||||||||||||||||||||||||||
| 680 | 5 | ▶ | if (blessed($r) && $r->isa('Future')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($r) and $r->isa("Future")
MC/DC50%1/2
builtin::blessed($r) and $r->isa("Future")
builtin::blessed($r)
$r->isa("Future")
Truth table66%2/3
A and B
A= builtin::blessed($r)B= $r->isa("Future")
| |||||||||||||||||||||||||||||
| 681 | 1 | await $r; | |||||||||||||||||||||||||||
| 682 | } | ||||||||||||||||||||||||||||
| 683 | }; | ||||||||||||||||||||||||||||
| 684 | 8 | ▶ | if (my $err = $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 685 | 3 | await $self->_trigger_error($err); | |||||||||||||||||||||||||||
| 686 | } | ||||||||||||||||||||||||||||
| 687 | } | ||||||||||||||||||||||||||||
| 688 | } | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | 9 | return; | |||||||||||||||||||||||||||
| 691 | 22 | } | |||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | # Keepalive support using WebSocket protocol-level ping/pong (RFC 6455) | ||||||||||||||||||||||||||||
| 694 | # Sends websocket.keepalive event to server - loop-agnostic, server handles timers | ||||||||||||||||||||||||||||
| 695 | 6 | ▶ | async sub keepalive { | ||||||||||||||||||||||||||
|
Subroutine
keepalive: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 696 | 6 | my ($self, $interval, $timeout) = @_; | |||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||
| 698 | 6 | ▶ | $interval //= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$interval //= 0
MC/DC0%0/1
$interval //= 0
$interval
Truth table50%1/2
A
A= $interval
| |||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||
| 700 | 6 | my $event = { | |||||||||||||||||||||||||||
| 701 | type => 'websocket.keepalive', | ||||||||||||||||||||||||||||
| 702 | interval => $interval, | ||||||||||||||||||||||||||||
| 703 | }; | ||||||||||||||||||||||||||||
| 704 | 6 | ▶ | $event->{timeout} = $timeout if defined $timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||
| 706 | 6 | await $self->{send}->($event); | |||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||
| 708 | 6 | return $self; | |||||||||||||||||||||||||||
| 709 | 22 | } | |||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||
| 711 | 22 | 1; | |||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||
| 713 | __END__ | ||||||||||||||||||||||||||||