blib/lib/PAGI/WebSocket.pm

filter: Statementstmt 100% 398 / 398 Branchbran 92.4% 135 / 146 Conditioncond 82.2% 65 / 79 MC/DCmcdc 66.6% 30 / 45 Subroutinesub 100% 73 / 73 Podpod 100% 58 / 58 totaltotal 94.9% 759 / 799 CC 114 SCAR 14.8
CC
114
Cov
96%
query_params
13.0
new
10.0
run
9.1
CRAP 4.4
« blib/lib/PAGI/Middleware/CSRF.pm ↑ Summary blib/lib/PAGI/Middleware/Session.pm »
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
BranchTrueFalseTotal
unless $scope and ref $scope eq "HASH" 2 154 156
Condition100%3/3
$scope and ref $scope eq "HASH"
!ll&&!rl&&r
11154
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"
ABresult
0X0
100
111
18 unless $scope && ref($scope) eq 'HASH';
19 154 ▶ croak "PAGI::WebSocket requires receive coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless $receive and ref $receive eq "CODE" 2 152 154
Condition100%3/3
$receive and ref $receive eq "CODE"
!ll&&!rl&&r
11152
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"
ABresult
0X0
100
111
20 unless $receive && ref($receive) eq 'CODE';
21 152 ▶ croak "PAGI::WebSocket requires send coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless $send and ref $send eq "CODE" 2 150 152
Condition100%3/3
$send and ref $send eq "CODE"
!ll&&!rl&&r
11150
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless ($scope->{"type"} // "") eq "websocket" 1 149 150
Condition50%1/2
$scope->{"type"} // ""
l!l
1500
MC/DC0%0/1
$scope->{"type"} // ""
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
Aresult
11
00
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
BranchTrueFalseTotal
if $scope->{"pagi.websocket"} 0 149 149
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"}
l!l&&r!l&&!r
110
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"}
ABresult
1X1
011
000
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"} // ""
l!l
181
MC/DC100%1/1
+(shift())->{"scope"}{"query_string"} // ""
+(shift())->{"scope"}{"query_string"}
Truth table100%2/2
A
A= +(shift())->{"scope"}{"query_string"}
Aresult
11
00
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"
l!l
11
MC/DC100%1/1
+(shift())->{"scope"}{"scheme"} // "ws"
+(shift())->{"scope"}{"scheme"}
Truth table100%2/2
A
A= +(shift())->{"scope"}{"scheme"}
Aresult
11
00
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"
l!l
21
MC/DC100%1/1
+(shift())->{"scope"}{"http_version"} // "1.1"
+(shift())->{"scope"}{"http_version"}
Truth table100%2/2
A
A= +(shift())->{"scope"}{"http_version"}
Aresult
11
00
57 3 ▶sub subprotocols { shift->{scope}{subprotocols} // [] }
Subroutine subprotocols: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
+(shift())->{"scope"}{"subprotocols"} // []
l!l
21
MC/DC100%1/1
+(shift())->{"scope"}{"subprotocols"} // []
+(shift())->{"scope"}{"subprotocols"}
Truth table100%2/2
A
A= +(shift())->{"scope"}{"subprotocols"}
Aresult
11
00
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"} // {}
l!l
42
MC/DC100%1/1
$self->{"scope"}{"state"} // {}
$self->{"scope"}{"state"}
Truth table100%2/2
A
A= $self->{"scope"}{"state"}
Aresult
11
00
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"} // {}
l!l
11
MC/DC100%1/1
$self->{"scope"}{"path_params"} // {}
$self->{"scope"}{"path_params"}
Truth table100%2/2
A
A= $self->{"scope"}{"path_params"}
Aresult
11
00
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"} // {}
l!l
51
MC/DC100%1/1
$self->{"scope"}{"path_params"} // {}
$self->{"scope"}{"path_params"}
Truth table100%2/2
A
A= $self->{"scope"}{"path_params"}
Aresult
11
00
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
BranchTrueFalseTotal
unless defined $str 0 58 58
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
BranchTrueFalseTotal
unless defined $str 0 50 50
94 50 ▶ my $flag = $strict ? FB_CROAK : FB_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
$strict ? : 2 48 50
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
l!l
131
MC/DC100%1/1
delete $opts{"strict"} // 0
delete $opts{"strict"}
Truth table100%2/2
A
A= delete $opts{"strict"}
Aresult
11
00
104 32 ▶ my $raw = delete $opts{raw} // 0;
Condition100%2/2
delete $opts{"raw"} // 0
l!l
626
MC/DC100%1/1
delete $opts{"raw"} // 0
delete $opts{"raw"}
Truth table100%2/2
A
A= delete $opts{"raw"}
Aresult
11
00
105 32 ▶ croak("Unknown options to query_params: " . join(', ', keys %opts)) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 31 32
106  
107 31 ▶ my $cache_key = $raw ? 'pagi.websocket.query.raw' : ($strict ? 'pagi.websocket.query.strict' : 'pagi.websocket.query');
Branch: 2 branches
BranchTrueFalseTotal
$raw ? : 6 25 31
$strict ? : 1 24 25
108 31 ▶ return $self->{scope}{$cache_key} if $self->{scope}{$cache_key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"scope"}{$cache_key} 14 17 31
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
BranchTrueFalseTotal
unless length $part 0 29 29
115 29 my ($key, $val) = split /=/, $part, 2;
116 29 ▶ $key //= '';
Condition50%1/2
$key //= ""
l!l
290
MC/DC0%0/1
$key //= ""
$key
Truth table50%1/2
A
A= $key
Aresult
11
00
117 29 ▶ $val //= '';
Condition100%2/2
$val //= ""
l!l
281
MC/DC100%1/1
$val //= ""
$val
Truth table100%2/2
A
A= $val
Aresult
11
00
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
BranchTrueFalseTotal
$raw ? : 4 25 29
125 29 ▶ my $val_final = $raw ? $val_decoded : _decode_utf8($val_decoded, $strict);
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 4 25 29
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"} // []
l!l
40
MC/DC0%0/1
$self->{"scope"}{"headers"} // []
$self->{"scope"}{"headers"}
Truth table50%1/2
A
A= $self->{"scope"}{"headers"}
Aresult
11
00
158 20 ▶ if (lc($pair->[0]) eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $pair->[0] eq $name) 4 16 20
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
BranchTrueFalseTotal
if $self->{"scope"}{"pagi.request.headers"} 1 2 3
169  
170 2 my @pairs;
171 2 ▶ for my $pair (@{$self->{scope}{headers} // []}) {
Condition50%1/2
$self->{"scope"}{"headers"} // []
l!l
20
MC/DC0%0/1
$self->{"scope"}{"headers"} // []
$self->{"scope"}{"headers"}
Truth table50%1/2
A
A= $self->{"scope"}{"headers"}
Aresult
11
00
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
BranchTrueFalseTotal
unless $t 1 1 2
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
BranchTrueFalseTotal
unless $t 1 1 2
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
BranchTrueFalseTotal
unless $t 1 1 2
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
BranchTrueFalseTotal
if $t and $t->can("on_high_water") 1 1 2
Condition66%2/3
$t and $t->can("on_high_water")
!ll&&!rl&&r
011
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $t and $t->can("on_drain") 1 1 2
Condition66%2/3
$t and $t->can("on_drain")
!ll&&!rl&&r
011
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $t 1 2 3
241 2 my $high = $t->high_water_mark;
242 2 ▶ return 1 unless defined $high;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $high 0 2 2
243 2 ▶ return $t->buffered_amount < $high ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$t->buffered_amount < $high ? : 1 1 2
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
l!l
491
MC/DC100%1/1
$code // 1005
$code
Truth table100%2/2
A
A= $code
Aresult
11
00
256 50 ▶ $self->{_close_reason} = $reason // '';
Condition100%2/2
$reason // ""
l!l
491
MC/DC100%1/1
$reason // ""
$reason
Truth table100%2/2
A
A= $reason
Aresult
11
00
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
BranchTrueFalseTotal
if $self->{"_close_callbacks_ran"} 0 43 43
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
BranchTrueFalseTotal
if (builtin::blessed($r) and $r->isa("Future")) 10 5 15
Condition100%3/3
builtin::blessed($r) and $r->isa("Future")
!ll&&!rl&&r
4110
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")
ABresult
0X0
100
111
282 10 await $r;
283 }
284 };
285 15 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 14 15
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
BranchTrueFalseTotal
if ($event eq 'message') { } 3 5 8
elsif ($event eq 'close') { } 2 3 5
elsif ($event eq 'error') { } 2 1 3
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
BranchTrueFalseTotal
if (builtin::blessed($r) and $r->isa("Future")) 2 6 8
Condition66%2/3
builtin::blessed($r) and $r->isa("Future")
!ll&&!rl&&r
602
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")
ABresult
0X0
100
111
336 2 await $r;
337 }
338 };
339 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 7 8
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
BranchTrueFalseTotal
if (not @{$self->{"_on_error"};}) 1 6 7
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
BranchTrueFalseTotal
if exists $opts{"subprotocol"} 2 93 95
358 95 ▶ $event->{headers} = $opts{headers} if exists $opts{headers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"headers"} 2 93 95
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
BranchTrueFalseTotal
if $self->is_closed 3 16 19
372  
373 16 ▶ $code //= 1000;
Condition100%2/2
$code //= 1000
l!l
313
MC/DC100%1/1
$code //= 1000
$code
Truth table100%2/2
A
A= $code
Aresult
11
00
374 16 ▶ $reason //= '';
Condition100%2/2
$reason //= ""
l!l
313
MC/DC100%1/1
$reason //= ""
$reason
Truth table100%2/2
A
A= $reason
Aresult
11
00
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
BranchTrueFalseTotal
$self->{'scope'}{'extensions'}{'websocket.http.response'} ? : 2 3 5
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
l!l
20
MC/DC0%0/1
$opts{"status"} // 403
$opts{"status"}
Truth table50%1/2
A
A= $opts{"status"}
Aresult
11
00
403 2 ▶ my $headers = $opts{headers} // [];
Condition100%2/2
$opts{"headers"} // []
l!l
11
MC/DC100%1/1
$opts{"headers"} // []
$opts{"headers"}
Truth table100%2/2
A
A= $opts{"headers"}
Aresult
11
00
404 2 ▶ my $body = defined $opts{body} ? $opts{body} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'body'} ? : 1 1 2
405  
406 2 ▶ if (!$self->supports_denial_response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->supports_denial_response) 1 1 2
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
BranchTrueFalseTotal
if $self->is_closed 1 6 7
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
BranchTrueFalseTotal
if $self->is_closed 1 2 3
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
BranchTrueFalseTotal
if $self->is_closed 1 5 6
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
BranchTrueFalseTotal
if $self->is_closed 1 5 6
476  
477 5 eval {
478 5 await $self->{send}->({
479 type => 'websocket.send',
480 text => $text,
481 });
482 };
483 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 4 5
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
BranchTrueFalseTotal
if $self->is_closed 1 5 6
493  
494 5 eval {
495 5 await $self->{send}->({
496 type => 'websocket.send',
497 bytes => $bytes,
498 });
499 };
500 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 4 5
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
BranchTrueFalseTotal
if $self->is_closed 1 5 6
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
BranchTrueFalseTotal
if ($@) 1 4 5
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
BranchTrueFalseTotal
unless $self->is_connected 1 2 3
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
BranchTrueFalseTotal
unless $self->is_connected 1 2 3
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
BranchTrueFalseTotal
unless $self->is_connected 1 2 3
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
BranchTrueFalseTotal
if $self->is_closed 5 64 69
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
BranchTrueFalseTotal
if (not defined $event or $event->{"type"} eq "websocket.disconnect") 27 71 98
Condition66%2/3
not defined $event or $event->{"type"} eq "websocket.disconnect"
l!l&&r!l&&!r
02771
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"
ABresult
1X1
011
000
559 # 1005 = No Status Rcvd (RFC 6455)
560 27 ▶ my $code = $event->{code} // 1005;
Condition100%2/2
$event->{"code"} // 1005
l!l
198
MC/DC100%1/1
$event->{"code"} // 1005
$event->{"code"}
Truth table100%2/2
A
A= $event->{"code"}
Aresult
11
00
561 27 ▶ my $reason = $event->{reason} // '';
Condition100%2/2
$event->{"reason"} // ""
l!l
423
MC/DC100%1/1
$event->{"reason"} // ""
$event->{"reason"}
Truth table100%2/2
A
A= $event->{"reason"}
Aresult
11
00
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
BranchTrueFalseTotal
if $event->{"type"} eq "websocket.connect" 35 36 71
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
BranchTrueFalseTotal
unless $event 10 20 30
580  
581 # Skip non-receive events and binary frames
582 20 ▶ next unless $event->{type} eq 'websocket.receive';
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"type"} eq "websocket.receive" 0 20 20
583 20 ▶ next unless exists $event->{text};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $event->{"text"} 2 18 20
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
BranchTrueFalseTotal
unless $event 2 3 5
595  
596 # Skip non-receive events and text frames
597 3 ▶ next unless $event->{type} eq 'websocket.receive';
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"type"} eq "websocket.receive" 0 3 3
598 3 ▶ next unless exists $event->{bytes};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $event->{"bytes"} 0 3 3
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
BranchTrueFalseTotal
unless defined $text 1 3 4
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
BranchTrueFalseTotal
if (my $event = XXX) 5 2 7
619 5 ▶ next unless $event->{type} eq 'websocket.receive';
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"type"} eq "websocket.receive" 0 5 5
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
BranchTrueFalseTotal
if (my $text = XXX) 9 5 14
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
BranchTrueFalseTotal
if (my $bytes = XXX) 1 1 2
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
BranchTrueFalseTotal
unless defined $text 3 3 6
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
BranchTrueFalseTotal
if (my $err = $@) 1 15 16
667 1 warn "PAGI::WebSocket receive error: $err";
668 1 last;
669 }
670 15 ▶ last unless $event;
Branch: 1 branch
BranchTrueFalseTotal
unless $event 8 7 15
671  
672 7 ▶ next unless $event->{type} eq 'websocket.receive';
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"type"} eq "websocket.receive" 0 7 7
673  
674 7 ▶ my $data = $event->{text} // $event->{bytes};
Condition33%1/3
$event->{"text"} // $event->{"bytes"}
l!l&&r!l&&!r
700
MC/DC0%0/2
$event->{"text"} // $event->{"bytes"}
$event->{"text"} $event->{"bytes"}
Truth table33%1/3
A // B
A= $event->{"text"}B= $event->{"bytes"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (builtin::blessed($r) and $r->isa("Future")) 1 4 5
Condition66%2/3
builtin::blessed($r) and $r->isa("Future")
!ll&&!rl&&r
401
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")
ABresult
0X0
100
111
681 1 await $r;
682 }
683 };
684 8 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 3 5 8
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
l!l
60
MC/DC0%0/1
$interval //= 0
$interval
Truth table50%1/2
A
A= $interval
Aresult
11
00
699  
700 6 my $event = {
701 type => 'websocket.keepalive',
702 interval => $interval,
703 };
704 6 ▶ $event->{timeout} = $timeout if defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout 2 4 6
705  
706 6 await $self->{send}->($event);
707  
708 6 return $self;
709 22 }
710  
711 22 1;
712  
713 __END__
« blib/lib/PAGI/Middleware/CSRF.pm ↑ Summary blib/lib/PAGI/Middleware/Session.pm »