blib/lib/PAGI/SSE.pm

filter: Statementstmt 91.6% 382 / 417 Branchbran 70.8% 136 / 192 Conditioncond 56.0% 56 / 100 MC/DCmcdc 33.9% 19 / 56 Subroutinesub 94.2% 66 / 70 Podpod 91.6% 44 / 48 totaltotal 79.6% 703 / 883 CC 145 SCAR 22.8
CC
145
Cov
81%
every
45.1
__ANON__
38.1
query_params
13.5
CRAP 9.8
« lib/PAGI/Middleware/SecurityHeaders.pm ↑ Summary lib/PAGI/Middleware/Auth/Bearer.pm »
1 package PAGI::SSE;
2 24 $PAGI::SSE::VERSION = '0.002002';
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Encode qw(decode FB_CROAK FB_DEFAULT LEAVE_SRC);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 111 ▶sub new {
Subroutine new: called CC=10 SCAR=23.2
Pod documented
15 111 my ($class, $scope, $receive, $send) = @_;
16  
17 111 ▶ croak "PAGI::SSE requires scope hashref"
Branch: 1 branch
BranchTrueFalseTotal
unless $scope and ref $scope eq "HASH" 1 110 111
Condition66%2/3
$scope and ref $scope eq "HASH"
!ll&&!rl&&r
10110
MC/DC50%1/2
$scope and ref $scope eq "HASH"
$scope ref $scope eq "HASH"
Truth table66%2/3
A and B
A= $scopeB= ref $scope eq "HASH"
ABresult
0X0
100
111
18 unless $scope && ref($scope) eq 'HASH';
19 110 ▶ croak "PAGI::SSE requires receive coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless $receive and ref $receive eq "CODE" 1 109 110
Condition66%2/3
$receive and ref $receive eq "CODE"
!ll&&!rl&&r
10109
MC/DC50%1/2
$receive and ref $receive eq "CODE"
$receive ref $receive eq "CODE"
Truth table66%2/3
A and B
A= $receiveB= ref $receive eq "CODE"
ABresult
0X0
100
111
20 unless $receive && ref($receive) eq 'CODE';
21 109 ▶ croak "PAGI::SSE requires send coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless $send and ref $send eq "CODE" 1 108 109
Condition66%2/3
$send and ref $send eq "CODE"
!ll&&!rl&&r
10108
MC/DC50%1/2
$send and ref $send eq "CODE"
$send ref $send eq "CODE"
Truth table66%2/3
A and B
A= $sendB= ref $send eq "CODE"
ABresult
0X0
100
111
22 unless $send && ref($send) eq 'CODE';
23 croak "PAGI::SSE requires scope type 'sse', got '$scope->{type}'"
24 108 ▶ unless ($scope->{type} // '') eq 'sse';
Branch: 1 branch
BranchTrueFalseTotal
unless ($scope->{"type"} // "") eq "sse" 1 107 108
Condition50%1/2
$scope->{"type"} // ""
l!l
1080
MC/DC0%0/1
$scope->{"type"} // ""
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
Aresult
11
00
25  
26 # Return existing SSE object if one was already created for this scope
27 # This ensures consistent state (is_started, is_closed, callbacks) if
28 # multiple code paths create SSE objects from the same scope.
29 107 ▶ return $scope->{'pagi.sse'} if $scope->{'pagi.sse'};
Branch: 1 branch
BranchTrueFalseTotal
if $scope->{"pagi.sse"} 1 106 107
30  
31 106 my $self = bless {
32 scope => $scope,
33 receive => $receive,
34 send => $send,
35 _state => 'pending', # pending -> started -> closed
36 _on_close => [],
37 _on_error => [],
38 _disconnect_reason => undef, # Set when disconnect received
39 }, $class;
40  
41 # Cache in scope for reuse (weakened to avoid circular reference leak)
42 106 $scope->{'pagi.sse'} = $self;
43 106 Scalar::Util::weaken($scope->{'pagi.sse'});
44  
45 106 return $self;
46 }
47  
48 # Scope property accessors
49 5 ▶sub scope { shift->{scope} }
Subroutine scope: called CC=1 SCAR=0.0
Pod documented
50 2 ▶sub path { shift->{scope}{path} // '/' }
Subroutine path: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"scope"}{"path"} // "/"
l!l
20
MC/DC0%0/1
+(shift())->{"scope"}{"path"} // "/"
+(shift())->{"scope"}{"path"}
Truth table50%1/2
A
A= +(shift())->{"scope"}{"path"}
Aresult
11
00
51 0 ▶sub raw_path { my $s = shift; $s->{scope}{raw_path} // $s->{scope}{path} // '/' }
Subroutine raw_path: not called CC=3 SCAR=24.8
Pod documented
Condition0%0/5
($s->{"scope"}{"raw_path"} // $s->{"scope"}{"path"}) // "/"
l!l
00
$s->{"scope"}{"raw_path"} // $s->{"scope"}{"path"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($s->{"scope"}{"raw_path"} // $s->{"scope"}{"path"}) // "/"
$s->{"scope"}{"raw_path"} $s->{"scope"}{"path"}
Truth table0%0/3
A // B
A= $s->{"scope"}{"raw_path"}B= $s->{"scope"}{"path"}
ABresult
1X1
011
000
52 5 ▶sub query_string { shift->{scope}{query_string} // '' }
Subroutine query_string: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"scope"}{"query_string"} // ""
l!l
50
MC/DC0%0/1
+(shift())->{"scope"}{"query_string"} // ""
+(shift())->{"scope"}{"query_string"}
Truth table50%1/2
A
A= +(shift())->{"scope"}{"query_string"}
Aresult
11
00
53  
54 # URL decode helper (handles + as space per application/x-www-form-urlencoded)
55 12 ▶sub _url_decode {
Subroutine _url_decode: called CC=2 SCAR=7.0
56 12 my ($str) = @_;
57 12 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 12 12
58 12 $str =~ s/\+/ /g;
59 12 $str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
60 12 return $str;
61 }
62  
63 # UTF-8 decode helper with strict/lenient mode
64 8 ▶sub _decode_utf8 {
Subroutine _decode_utf8: called CC=3 SCAR=11.3
65 8 my ($str, $strict) = @_;
66 8 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 8 8
67 8 ▶ my $flag = $strict ? FB_CROAK : FB_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
$strict ? : 0 8 8
68 8 $flag |= LEAVE_SRC;
69 8 return decode('UTF-8', $str, $flag);
70 }
71  
72 # Query params as Hash::MultiValue (cached in scope)
73 # Options: strict => 1 (croak on invalid UTF-8), raw => 1 (skip UTF-8 decoding)
74 6 ▶sub query_params {
Subroutine query_params: called CC=13 SCAR=26.0
Pod documented
75 6 my ($self, %opts) = @_;
76 6 ▶ my $strict = delete $opts{strict} // 0;
Condition50%1/2
delete $opts{"strict"} // 0
l!l
06
MC/DC0%0/1
delete $opts{"strict"} // 0
delete $opts{"strict"}
Truth table50%1/2
A
A= delete $opts{"strict"}
Aresult
11
00
77 6 ▶ my $raw = delete $opts{raw} // 0;
Condition100%2/2
delete $opts{"raw"} // 0
l!l
24
MC/DC100%1/1
delete $opts{"raw"} // 0
delete $opts{"raw"}
Truth table100%2/2
A
A= delete $opts{"raw"}
Aresult
11
00
78 6 ▶ croak("Unknown options to query_params: " . join(', ', keys %opts)) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 6 6
79  
80 6 ▶ my $cache_key = $raw ? 'pagi.sse.query.raw' : ($strict ? 'pagi.sse.query.strict' : 'pagi.sse.query');
Branch: 2 branches
BranchTrueFalseTotal
$raw ? : 2 4 6
$strict ? : 0 4 4
81 6 ▶ return $self->{scope}{$cache_key} if $self->{scope}{$cache_key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"scope"}{$cache_key} 2 4 6
82  
83 4 my $qs = $self->query_string;
84 4 my @pairs;
85  
86 4 for my $part (split /[&;]/, $qs) {
87 6 ▶ next unless length $part;
Branch: 1 branch
BranchTrueFalseTotal
unless length $part 0 6 6
88 6 my ($key, $val) = split /=/, $part, 2;
89 6 ▶ $key //= '';
Condition50%1/2
$key //= ""
l!l
60
MC/DC0%0/1
$key //= ""
$key
Truth table50%1/2
A
A= $key
Aresult
11
00
90 6 ▶ $val //= '';
Condition50%1/2
$val //= ""
l!l
60
MC/DC0%0/1
$val //= ""
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
91  
92 # URL decode (handles + as space)
93 6 my $key_decoded = _url_decode($key);
94 6 my $val_decoded = _url_decode($val);
95  
96 # UTF-8 decode unless raw mode
97 6 ▶ my $key_final = $raw ? $key_decoded : _decode_utf8($key_decoded, $strict);
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 2 4 6
98 6 ▶ my $val_final = $raw ? $val_decoded : _decode_utf8($val_decoded, $strict);
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 2 4 6
99  
100 6 push @pairs, $key_final, $val_final;
101 }
102  
103 4 $self->{scope}{$cache_key} = Hash::MultiValue->new(@pairs);
104 4 return $self->{scope}{$cache_key};
105 }
106  
107 # Raw query params (no UTF-8 decoding)
108 1 ▶sub raw_query_params {
Subroutine raw_query_params: called CC=1 SCAR=0.0
Pod documented
109 1 my $self = shift;
110 1 return $self->query_params(raw => 1);
111 }
112  
113 # Single query param accessor
114 4 ▶sub query_param {
Subroutine query_param: called CC=1 SCAR=0.0
Pod documented
115 4 my ($self, $name, %opts) = @_;
116 4 return $self->query_params(%opts)->get($name);
117 }
118  
119 # Raw single query param
120 1 ▶sub raw_query_param {
Subroutine raw_query_param: called CC=1 SCAR=0.0
Pod documented
121 1 my ($self, $name) = @_;
122 1 return $self->query_param($name, raw => 1);
123 }
124  
125 1 ▶sub scheme { shift->{scope}{scheme} // 'http' }
Subroutine scheme: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"scope"}{"scheme"} // "http"
l!l
10
MC/DC0%0/1
+(shift())->{"scope"}{"scheme"} // "http"
+(shift())->{"scope"}{"scheme"}
Truth table50%1/2
A
A= +(shift())->{"scope"}{"scheme"}
Aresult
11
00
126 1 ▶sub http_version { shift->{scope}{http_version} // '1.1' }
Subroutine http_version: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"scope"}{"http_version"} // "1.1"
l!l
10
MC/DC0%0/1
+(shift())->{"scope"}{"http_version"} // "1.1"
+(shift())->{"scope"}{"http_version"}
Truth table50%1/2
A
A= +(shift())->{"scope"}{"http_version"}
Aresult
11
00
127 0 ▶sub client { shift->{scope}{client} }
Subroutine client: not called CC=1 SCAR=6.9
Pod undocumented
128 0 ▶sub server { shift->{scope}{server} }
Subroutine server: not called CC=1 SCAR=6.9
Pod undocumented
129  
130  
131 # Application state (injected by PAGI::Lifespan, read-only)
132 4 ▶sub state {
Subroutine state: called CC=2 SCAR=6.9
Pod documented
133 4 my $self = shift;
134 4 ▶ return $self->{scope}{state} // {};
Condition100%2/2
$self->{"scope"}{"state"} // {}
l!l
22
MC/DC100%1/1
$self->{"scope"}{"state"} // {}
$self->{"scope"}{"state"}
Truth table100%2/2
A
A= $self->{"scope"}{"state"}
Aresult
11
00
135 }
136  
137 # Path parameter accessors - captured from URL path by router
138 # Stored in scope->{path_params} for router-agnostic access
139 2 ▶sub path_params {
Subroutine path_params: called CC=2 SCAR=6.9
Pod documented
140 2 my ($self) = @_;
141 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
142 }
143  
144 6 ▶sub path_param {
Subroutine path_param: called CC=2 SCAR=6.9
Pod documented
145 6 my ($self, $name) = @_;
146 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
147 6 return $params->{$name};
148 }
149  
150 # Connection state accessors
151 4 ▶sub connection_state { shift->{_state} }
Subroutine connection_state: called CC=1 SCAR=0.0
Pod documented
152  
153 132 ▶sub is_started {
Subroutine is_started: called CC=1 SCAR=0.0
Pod documented
154 132 my $self = shift;
155 132 return $self->{_state} eq 'started';
156 }
157  
158 167 ▶sub is_closed {
Subroutine is_closed: called CC=1 SCAR=0.0
Pod documented
159 167 my $self = shift;
160 167 return $self->{_state} eq 'closed';
161 }
162  
163 # True while the stream is live (started and not yet closed/disconnected).
164 # Mirrors PAGI::WebSocket->is_connected (state eq 'connected'): an SSE scope
165 # carries no pagi.connection (spec: N/A for sse), so connection liveness is
166 # derived from this object's own state machine, not the base Context method.
167 11 ▶sub is_connected {
Subroutine is_connected: called CC=1 SCAR=0.0
Pod documented
168 11 my $self = shift;
169 11 return $self->{_state} eq 'started';
170 }
171  
172 # Disconnect reason - why the connection closed
173 # Common values: 'client_closed', 'write_error', 'send_timeout', 'idle_timeout'
174 0 ▶sub disconnect_reason {
Subroutine disconnect_reason: not called CC=1 SCAR=6.9
Pod documented
175 0 my $self = shift;
176 0 return $self->{_disconnect_reason};
177 }
178  
179 # Outbound flow-control introspection (delegates to the pagi.transport handle)
180 2 ▶sub buffered_amount {
Subroutine buffered_amount: called CC=2 SCAR=6.9
Pod documented
181 2 my $self = shift;
182 2 my $t = $self->{scope}{'pagi.transport'};
183 2 ▶ return 0 unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 1 1 2
184 1 return $t->buffered_amount;
185 }
186  
187 2 ▶sub high_water_mark {
Subroutine high_water_mark: called CC=2 SCAR=6.9
Pod documented
188 2 my $self = shift;
189 2 my $t = $self->{scope}{'pagi.transport'};
190 2 ▶ return undef unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 1 1 2
191 1 return $t->high_water_mark;
192 }
193  
194 2 ▶sub low_water_mark {
Subroutine low_water_mark: called CC=2 SCAR=6.9
Pod documented
195 2 my $self = shift;
196 2 my $t = $self->{scope}{'pagi.transport'};
197 2 ▶ return undef unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 1 1 2
198 1 return $t->low_water_mark;
199 }
200  
201 2 ▶sub on_high_water {
Subroutine on_high_water: called CC=3 SCAR=11.0
Pod documented
202 2 my ($self, $cb) = @_;
203 2 my $t = $self->{scope}{'pagi.transport'};
204 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
205 2 return $self;
206 }
207  
208 2 ▶sub on_drain {
Subroutine on_drain: called CC=3 SCAR=11.0
Pod documented
209 2 my ($self, $cb) = @_;
210 2 my $t = $self->{scope}{'pagi.transport'};
211 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
212 2 return $self;
213 }
214  
215 3 ▶sub is_writable {
Subroutine is_writable: called CC=4 SCAR=13.9
Pod documented
216 3 my $self = shift;
217 3 my $t = $self->{scope}{'pagi.transport'};
218 3 ▶ return 1 unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 1 2 3
219 2 my $high = $t->high_water_mark;
220 2 ▶ return 1 unless defined $high;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $high 0 2 2
221 2 ▶ return $t->buffered_amount < $high ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$t->buffered_amount < $high ? : 1 1 2
222 }
223  
224 # Internal state setters
225 66 ▶sub _set_state {
Subroutine _set_state: called CC=1 SCAR=0.0
226 66 my ($self, $state) = @_;
227 66 $self->{_state} = $state;
228 }
229  
230 34 ▶sub _set_closed {
Subroutine _set_closed: called CC=3 SCAR=11.0
231 34 my ($self) = @_;
232 34 $self->{_state} = 'closed';
233  
234 # Wake a parked run() (which races this future against $receive) so a close
235 # from deep in a helper actually ends the stream.
236 $self->{_closed_future}->done
237 34 ▶ if $self->{_closed_future} && !$self->{_closed_future}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed_future"} and not $self->{"_closed_future"}->is_ready 1 33 34
Condition100%3/3
$self->{"_closed_future"} and not $self->{"_closed_future"}->is_ready
!ll&&!rl&&r
22111
MC/DC100%2/2
$self->{"_closed_future"} and not $self->{"_closed_future"}->is_ready
$self->{"_closed_future"} not $self->{"_closed_future"}->is_ready
Truth table100%3/3
A and not(B)
A= $self->{"_closed_future"}B= not $self->{"_closed_future"}->is_ready
ABresult
0X0
100
111
238 }
239  
240 # Start the SSE stream
241 59 ▶async sub start {
Subroutine start: called CC=5 SCAR=16.1
Pod documented
242 59 my ($self, %opts) = @_;
243  
244 # Idempotent - don't start twice
245 59 ▶ return $self if $self->is_started || $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_started or $self->is_closed 2 57 59
Condition66%2/3
$self->is_started or $self->is_closed
l!l&&r!l&&!r
2057
MC/DC50%1/2
$self->is_started or $self->is_closed
$self->is_started $self->is_closed
Truth table66%2/3
A or B
A= $self->is_startedB= $self->is_closed
ABresult
1X1
011
000
246  
247 my $event = {
248 type => 'sse.start',
249 57 ▶ status => $opts{status} // 200,
Condition100%2/2
$opts{"status"} // 200
l!l
255
MC/DC100%1/1
$opts{"status"} // 200
$opts{"status"}
Truth table100%2/2
A
A= $opts{"status"}
Aresult
11
00
250 };
251 57 ▶ $event->{headers} = $opts{headers} if exists $opts{headers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"headers"} 2 55 57
252  
253 57 await $self->{send}->($event);
254 57 $self->_set_state('started');
255  
256 57 return $self;
257 24 }
258  
259 # Enable keepalive - sends sse.keepalive event to server
260 # Server handles the timer; this is loop-agnostic
261 8 ▶async sub keepalive {
Subroutine keepalive: called CC=3 SCAR=11.0
Pod documented
262 8 my ($self, $interval, $comment) = @_;
263  
264 8 ▶ await $self->{send}->({
Condition75%3/4
$interval // 0
l!l
80
$comment // ""
l!l
26
MC/DC0%0/1
$interval // 0
$interval
MC/DC100%1/1
$comment // ""
$comment
Truth table50%1/2
A
A= $interval
Aresult
11
00
Truth table100%2/2
A
A= $comment
Aresult
11
00
265 type => 'sse.keepalive',
266 interval => $interval // 0,
267 comment => $comment // '',
268 });
269  
270 8 return $self;
271 24 }
272  
273 # Single header lookup (case-insensitive, returns last value)
274 9 ▶sub header {
Subroutine header: called CC=4 SCAR=13.9
Pod documented
275 9 my ($self, $name) = @_;
276 9 $name = lc($name);
277 9 my $value;
278 9 ▶ for my $pair (@{$self->{scope}{headers} // []}) {
Condition50%1/2
$self->{"scope"}{"headers"} // []
l!l
90
MC/DC0%0/1
$self->{"scope"}{"headers"} // []
$self->{"scope"}{"headers"}
Truth table50%1/2
A
A= $self->{"scope"}{"headers"}
Aresult
11
00
279 15 ▶ if (lc($pair->[0]) eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $pair->[0] eq $name) 6 9 15
280 6 $value = $pair->[1];
281 }
282 }
283 9 return $value;
284 }
285  
286 # All headers as Hash::MultiValue (cached in scope)
287 2 ▶sub headers {
Subroutine headers: called CC=4 SCAR=14.0
Pod documented
288 2 my $self = shift;
289 2 ▶ return $self->{scope}{'pagi.request.headers'} if $self->{scope}{'pagi.request.headers'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"scope"}{"pagi.request.headers"} 0 2 2
290  
291 2 my @pairs;
292 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
293 5 push @pairs, lc($pair->[0]), $pair->[1];
294 }
295  
296 2 $self->{scope}{'pagi.request.headers'} = Hash::MultiValue->new(@pairs);
297 2 return $self->{scope}{'pagi.request.headers'};
298 }
299  
300 # All values for a header
301 2 ▶sub header_all {
Subroutine header_all: called CC=1 SCAR=0.0
Pod documented
302 2 my ($self, $name) = @_;
303 2 return $self->headers->get_all(lc($name));
304 }
305  
306 # Get Last-Event-ID header from client (for reconnection)
307 6 ▶sub last_event_id {
Subroutine last_event_id: called CC=1 SCAR=0.0
Pod documented
308 6 my ($self) = @_;
309 6 return $self->header('last-event-id');
310 }
311  
312 # Send data-only event
313 12 ▶async sub send {
Subroutine send: called CC=3 SCAR=11.0
Pod documented
314 12 my ($self, $data) = @_;
315  
316 12 ▶ croak "Cannot send on closed SSE connection" if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 1 11 12
317  
318 # Auto-start if not started
319 11 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 2 9 11
320  
321 11 await $self->{send}->({
322 type => 'sse.send',
323 data => $data,
324 });
325  
326 11 return $self;
327 24 }
328  
329 # Send JSON-encoded data
330 5 ▶async sub send_json {
Subroutine send_json: called CC=3 SCAR=11.0
Pod documented
331 5 my ($self, $data) = @_;
332  
333 5 ▶ croak "Cannot send on closed SSE connection" if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 5 5
334  
335 5 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 1 4 5
336  
337 5 my $json = JSON::MaybeXS::encode_json($data);
338  
339 5 await $self->{send}->({
340 type => 'sse.send',
341 data => $json,
342 });
343  
344 5 return $self;
345 24 }
346  
347 # Send full SSE event with all fields
348 15 ▶async sub send_event {
Subroutine send_event: called CC=8 SCAR=20.8
Pod documented
349 15 my ($self, %opts) = @_;
350  
351 15 ▶ croak "Cannot send on closed SSE connection" if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 15 15
352 15 ▶ croak "send_event requires 'data' parameter" unless exists $opts{data};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"data"} 0 15 15
353  
354 15 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 7 8 15
355  
356 # Auto-encode hashref/arrayref data as JSON
357 15 my $data = $opts{data};
358 15 ▶ if (ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) 13 2 15
359 13 $data = JSON::MaybeXS::encode_json($data);
360 }
361  
362 15 my $event = {
363 type => 'sse.send',
364 data => $data,
365 };
366  
367 15 ▶ $event->{event} = $opts{event} if defined $opts{event};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"event"} 13 2 15
368 15 ▶ $event->{id} = "$opts{id}" if defined $opts{id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"id"} 9 6 15
369 15 ▶ $event->{retry} = int($opts{retry}) if defined $opts{retry};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"retry"} 3 12 15
370  
371 15 await $self->{send}->($event);
372  
373 15 return $self;
374 24 }
375  
376 # Safe send - returns bool instead of throwing
377 10 ▶async sub try_send {
Subroutine try_send: called CC=4 SCAR=13.9
Pod documented
378 10 my ($self, $data) = @_;
379 10 ▶ return 0 if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 1 9 10
380  
381 9 eval {
382 9 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 0 9 9
383 9 await $self->{send}->({
384 type => 'sse.send',
385 data => $data,
386 });
387 };
388 9 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 7 2 9
389 7 $self->_set_closed;
390 7 await $self->_trigger_error($err);
391 7 return 0;
392 }
393 2 return 1;
394 24 }
395  
396 2 ▶async sub try_send_json {
Subroutine try_send_json: called CC=4 SCAR=15.2
Pod documented
397 2 my ($self, $data) = @_;
398 2 ▶ return 0 if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 2 2
399  
400 2 eval {
401 2 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 0 2 2
402 2 my $json = JSON::MaybeXS::encode_json($data);
403 2 await $self->{send}->({
404 type => 'sse.send',
405 data => $json,
406 });
407 };
408 2 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 2 2
409 0 $self->_set_closed;
410 0 await $self->_trigger_error($err);
411 0 return 0;
412 }
413 2 return 1;
414 24 }
415  
416 # Send SSE comment (doesn't trigger onmessage in browser)
417 1 ▶async sub send_comment {
Subroutine send_comment: called CC=3 SCAR=11.2
Pod undocumented
418 1 my ($self, $comment) = @_;
419  
420 1 ▶ croak "Cannot send on closed SSE connection" if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 1 1
421  
422 1 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 0 1 1
423  
424 1 await $self->{send}->({
425 type => 'sse.comment',
426 comment => $comment,
427 });
428  
429 1 return $self;
430 24 }
431  
432 1 ▶async sub try_send_comment {
Subroutine try_send_comment: called CC=4 SCAR=15.5
Pod undocumented
433 1 my ($self, $comment) = @_;
434 1 ▶ return 0 if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 1 1
435  
436 1 eval {
437 1 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 0 1 1
438 1 await $self->{send}->({
439 type => 'sse.comment',
440 comment => $comment,
441 });
442 };
443 1 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 1 1
444 0 $self->_set_closed;
445 0 await $self->_trigger_error($err);
446 0 return 0;
447 }
448 1 return 1;
449 24 }
450  
451 2 ▶async sub try_send_event {
Subroutine try_send_event: called CC=9 SCAR=25.3
Pod documented
452 2 my ($self, %opts) = @_;
453 2 ▶ return 0 if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 2 2
454  
455 2 eval {
456 2 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 0 2 2
457  
458 2 ▶ my $data = $opts{data} // '';
Condition50%1/2
$opts{"data"} // ""
l!l
20
MC/DC0%0/1
$opts{"data"} // ""
$opts{"data"}
Truth table50%1/2
A
A= $opts{"data"}
Aresult
11
00
459 2 ▶ if (ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) 0 2 2
460 0 $data = JSON::MaybeXS::encode_json($data);
461 }
462  
463 2 my $event = {
464 type => 'sse.send',
465 data => $data,
466 };
467 2 ▶ $event->{event} = $opts{event} if defined $opts{event};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"event"} 2 0 2
468 2 ▶ $event->{id} = "$opts{id}" if defined $opts{id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"id"} 0 2 2
469 2 ▶ $event->{retry} = int($opts{retry}) if defined $opts{retry};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"retry"} 0 2 2
470  
471 2 await $self->{send}->($event);
472 };
473 2 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 2 2
474 0 $self->_set_closed;
475 0 await $self->_trigger_error($err);
476 0 return 0;
477 }
478 2 return 1;
479 24 }
480  
481 # Internal: trigger error callbacks
482 7 ▶async sub _trigger_error {
Subroutine _trigger_error: called CC=6 SCAR=17.9
483 7 my ($self, $error) = @_;
484  
485 7 for my $cb (@{$self->{_on_error}}) {
486 7 eval {
487 7 my $r = $cb->($self, $error);
488 6 ▶ if (blessed($r) && $r->isa('Future')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($r) and $r->isa("Future")) 2 4 6
Condition66%2/3
builtin::blessed($r) and $r->isa("Future")
!ll&&!rl&&r
402
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
489 2 await $r;
490 }
491 };
492 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 5 7
493 2 warn "PAGI::SSE on_error callback error: $@";
494 }
495 }
496  
497 # If no error handlers registered, warn
498 7 ▶ if (!@{$self->{_on_error}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$self->{"_on_error"};}) 2 5 7
499 2 warn "PAGI::SSE error: $error";
500 }
501 24 }
502  
503 # Register close callback
504 22 ▶sub on_close {
Subroutine on_close: called CC=1 SCAR=0.0
Pod documented
505 22 my ($self, $callback) = @_;
506 22 push @{$self->{_on_close}}, $callback;
507 22 return $self;
508 }
509  
510 # Register error callback
511 8 ▶sub on_error {
Subroutine on_error: called CC=1 SCAR=0.0
Pod documented
512 8 my ($self, $callback) = @_;
513 8 push @{$self->{_on_error}}, $callback;
514 8 return $self;
515 }
516  
517 # Generic event dispatcher - dispatches to on_close or on_error
518 4 ▶sub on {
Subroutine on: called CC=3 SCAR=11.0
Pod documented
519 4 my ($self, $event, $callback) = @_;
520 4 ▶ if ($event eq 'close') {
Branch: 2 branches
BranchTrueFalseTotal
if ($event eq 'close') { } 2 2 4
elsif ($event eq 'error') { } 1 1 2
521 2 return $self->on_close($callback);
522 }
523 elsif ($event eq 'error') {
524 1 return $self->on_error($callback);
525 }
526 else {
527 1 croak "Unknown event type: $event (expected close or error)";
528 }
529 }
530  
531 # Internal: run all on_close callbacks
532 25 ▶async sub _run_close_callbacks {
Subroutine _run_close_callbacks: called CC=7 SCAR=19.6
533 25 my ($self) = @_;
534  
535 # Only run once
536 25 ▶ return if $self->{_close_callbacks_ran};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_close_callbacks_ran"} 0 25 25
537 25 $self->{_close_callbacks_ran} = 1;
538  
539 25 ▶ my $reason = $self->{_disconnect_reason} // 'unknown';
Condition100%2/2
$self->{"_disconnect_reason"} // "unknown"
l!l
232
MC/DC100%1/1
$self->{"_disconnect_reason"} // "unknown"
$self->{"_disconnect_reason"}
Truth table100%2/2
A
A= $self->{"_disconnect_reason"}
Aresult
11
00
540  
541 25 for my $cb (@{$self->{_on_close}}) {
542 19 eval {
543 19 my $r = $cb->($self, $reason);
544 19 ▶ if (blessed($r) && $r->isa('Future')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($r) and $r->isa("Future")) 5 14 19
Condition100%3/3
builtin::blessed($r) and $r->isa("Future")
!ll&&!rl&&r
1315
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
545 5 await $r;
546 }
547 };
548 19 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 19 19
549 0 warn "PAGI::SSE on_close callback error: $@";
550 }
551 }
552  
553 # Clear all callback arrays to break any closure-based cycles
554 25 $self->{_on_close} = [];
555 25 $self->{_on_error} = [];
556 24 }
557  
558 # Internal: mark closed and fire on_close callbacks for a disconnect that
559 # arrived directly off the wire (not via close()). Shared by run()'s own
560 # disconnect handling and PAGI::Context::SSE's _sync_terminal_disconnect hook
561 # (fired when the disconnect is instead consumed via $ctx->run()). Does NOT
562 # send an sse.close wire event -- the peer is already gone.
563 12 ▶async sub _note_disconnected {
Subroutine _note_disconnected: called CC=2 SCAR=6.9
564 12 my ($self, $code, $reason) = @_;
565 # $code is accepted for signature parity with PAGI::WebSocket's version
566 # (Context::WebSocket and Context::SSE both call this positionally) but
567 # unused -- SSE has no RFC6455-style close code, only a reason.
568  
569 12 ▶ $self->{_disconnect_reason} = $reason // 'client_closed';
Condition100%2/2
$reason // "client_closed"
l!l
111
MC/DC100%1/1
$reason // "client_closed"
$reason
Truth table100%2/2
A
A= $reason
Aresult
11
00
570 12 $self->_set_closed;
571 12 await $self->_run_close_callbacks;
572 12 return;
573 24 }
574  
575 # Close the connection
576 11 ▶async sub close {
Subroutine close: called CC=5 SCAR=16.2
Pod documented
577 11 my ($self, %opts) = @_;
578  
579 11 ▶ return $self if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 11 11
580  
581 # Record the reason so on_close callbacks (and the server access log) see it.
582 11 ▶ $self->{_disconnect_reason} //= $opts{reason} // 'app_closed';
Condition75%3/4
$self->{"_disconnect_reason"} //= $opts{"reason"} // "app_closed"
l!l
011
$opts{"reason"} // "app_closed"
l!l
38
MC/DC0%0/1
$self->{"_disconnect_reason"} //= $opts{"reason"} // "app_closed"
$self->{"_disconnect_reason"}
Truth table50%1/2
A
A= $self->{"_disconnect_reason"}
Aresult
11
00
583  
584 # Tell the server to end the stream now (sse.close). The reason is
585 # server-side only and is never written to the wire.
586 await $self->{send}->({
587 type => 'sse.close',
588 11 ▶ (defined $opts{reason} ? (reason => $opts{reason}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'reason'} ? : 3 8 11
589 });
590  
591 11 $self->_set_closed;
592 11 await $self->_run_close_callbacks;
593  
594 11 return $self;
595 24 }
596  
597 # Wait for disconnect
598 13 ▶async sub run {
Subroutine run: called CC=8 SCAR=21.1
Pod documented
599 13 my ($self) = @_;
600  
601 13 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 1 12 13
602  
603 # Race incoming events against an explicit close() (which resolves this
604 # future via _set_closed), so run() returns when the stream is closed from
605 # anywhere -- not only when the client disconnects.
606 13 ▶ $self->{_closed_future} //= Future->new;
Condition33%1/3
$self->{"_closed_future"} //= "Future"->new
l!l&&r!l&&!r
0130
MC/DC0%0/2
$self->{"_closed_future"} //= "Future"->new
$self->{"_closed_future"} "Future"->new
Truth table33%1/3
A //= B
A= $self->{"_closed_future"}B= "Future"->new
ABresult
1X1
011
000
607  
608 13 ▶ while (!$self->is_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_closed) 13 0 13
609 13 my $event = eval { await Future->wait_any($self->{receive}->(), $self->{_closed_future}) };
610 13 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 1 12 13
611 1 warn "PAGI::SSE receive error: $err";
612 1 last;
613 }
614  
615 12 ▶ last if $self->is_closed; # woken by close()
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 1 11 12
616  
617 11 ▶ my $type = $event->{type} // '';
Condition50%1/2
$event->{"type"} // ""
l!l
110
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table50%1/2
A
A= $event->{"type"}
Aresult
11
00
618  
619 11 ▶ if ($type eq 'sse.disconnect') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "sse.disconnect") 11 0 11
620 11 await $self->_note_disconnected(undef, $event->{reason});
621 11 last;
622 }
623 }
624  
625 13 return;
626 24 }
627  
628 # Internal: run one per-item callback, running on_close cleanup before
629 # re-raising if it dies (idempotent; _run_close_callbacks may already have run).
630 # Takes a thunk so each caller's varying callback arg list stays at the call
631 # site; awaits the thunk's Future and returns its value for callers that use it.
632 12 ▶async sub _guarded_dispatch {
Subroutine _guarded_dispatch: called CC=2 SCAR=6.9
633 12 my ($self, $thunk) = @_;
634  
635 12 my $result;
636 12 my $ok = eval { $result = await $thunk->(); 1 };
637 12 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 11 12
638 1 my $err = $@;
639 1 await $self->_run_close_callbacks; # idempotent; may already have run
640 1 die $err; # re-raise: caller still sees the error
641 }
642  
643 11 return $result;
644 24 }
645  
646 # Iterate over items and send events
647 5 ▶async sub each {
Subroutine each: called CC=10 SCAR=23.5
Pod documented
648 5 my ($self, $source, $callback) = @_;
649  
650 5 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 1 4 5
651  
652 5 my $index = 0;
653  
654 # Handle arrayref
655 5 ▶ if (ref $source eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $source eq 'ARRAY') { } 4 1 5
elsif (ref $source eq 'CODE') { } 1 0 1
656 4 for my $item (@$source) {
657 9 ▶ last if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 9 9
658  
659 9 ▶ my $result = await $self->_guarded_dispatch(sub { $callback->($item, $index++) });
Subroutine __ANON__: called CC=1 SCAR=0.0
660  
661 # If callback returns a hashref, treat as event spec
662 8 ▶ if (ref $result eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result eq "HASH") 2 6 8
663 2 await $self->send_event(%$result);
664 }
665 }
666 }
667 # Handle coderef iterator
668 elsif (ref $source eq 'CODE') {
669 1 ▶ while (!$self->is_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_closed) 4 0 4
670 4 my $item = $source->();
671 4 ▶ last unless defined $item;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 1 3 4
672  
673 3 ▶ my $result = await $self->_guarded_dispatch(sub { $callback->($item, $index++) });
Subroutine __ANON__: called CC=1 SCAR=0.0
674  
675 3 ▶ if (ref $result eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result eq "HASH") 0 3 3
676 0 await $self->send_event(%$result);
677 }
678 }
679 }
680 else {
681 0 croak "each() requires arrayref or coderef, got " . ref($source);
682 }
683  
684 4 return $self;
685 24 }
686  
687 # Periodic callback execution with interval delay
688 # Requires Future::IO to be installed
689 2 ▶async sub every {
Subroutine every: called CC=16 SCAR=38.1
Pod documented
690 2 my ($self, $interval, $callback) = @_;
691  
692 2 ▶ croak "every() requires interval" unless defined $interval && $interval > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $interval and $interval > 0 0 2 2
Condition33%1/3
defined $interval and $interval > 0
!ll&&!rl&&r
002
MC/DC0%0/2
defined $interval and $interval > 0
defined $interval $interval > 0
Truth table33%1/3
A and B
A= defined $intervalB= $interval > 0
ABresult
0X0
100
111
693 2 ▶ croak "every() requires callback coderef" unless ref $callback eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 2 2
694  
695 # Future::IO is required for every() - fail clearly if not available
696 2 ▶ eval { require Future::IO; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Future::IO; 1 } } or croak("every() requires Future::IO to be installed. Install it with: cpanm Future::IO") 2 0 2
697 or croak "every() requires Future::IO to be installed. "
698 . "Install it with: cpanm Future::IO";
699  
700 # Future::IO must be configured with a backend
701 24 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
702 2 ▶ croak "Future::IO backend not configured. Add to your app.pl:\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $Future::IO::IMPL 1 1 2
703 . " use Future::IO::Impl::IOAsync;\n"
704 unless $Future::IO::IMPL;
705  
706 1 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 0 1 1
707  
708 # Start background disconnect monitor
709 1 my $disconnect_future = $self->_watch_for_disconnect;
710  
711 1 ▶ while (!$self->is_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_closed) 1 0 1
712 # Execute the callback
713 0 my $ok = eval { await $callback->(); 1 };
714 1 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 0 1
715 1 my $err = $@;
716 # Callback failed - connection likely closed or error occurred
717 1 $self->_set_closed;
718 1 await $self->_run_close_callbacks;
719 1 ▶ $disconnect_future->cancel if $disconnect_future->can('cancel') && !$disconnect_future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $disconnect_future->can("cancel") and not $disconnect_future->is_ready 1 0 1
Condition33%1/3
$disconnect_future->can("cancel") and not $disconnect_future->is_ready
!ll&&!rl&&r
001
MC/DC0%0/2
$disconnect_future->can("cancel") and not $disconnect_future->is_ready
$disconnect_future->can("cancel") not $disconnect_future->is_ready
Truth table33%1/3
A and not(B)
A= $disconnect_future->can("cancel")B= not $disconnect_future->is_ready
ABresult
0X0
100
111
720 1 die $err; # re-raise: caller still sees the error, matching each()/each_*
721 }
722  
723 # Race between sleep and disconnect detection
724 0 my $sleep_future = Future::IO->sleep($interval);
725 0 my $winner = await Future->wait_any($sleep_future, $disconnect_future);
726  
727 # If disconnect won, exit the loop
728 0 ▶ if ($self->is_closed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_closed) 0 0 0
729 0 ▶ $sleep_future->cancel if $sleep_future->can('cancel') && !$sleep_future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $sleep_future->can("cancel") and not $sleep_future->is_ready 0 0 0
Condition0%0/3
$sleep_future->can("cancel") and not $sleep_future->is_ready
!ll&&!rl&&r
000
MC/DC0%0/2
$sleep_future->can("cancel") and not $sleep_future->is_ready
$sleep_future->can("cancel") not $sleep_future->is_ready
Truth table0%0/3
A and not(B)
A= $sleep_future->can("cancel")B= not $sleep_future->is_ready
ABresult
0X0
100
111
730 0 last;
731 }
732 }
733  
734 # Clean up disconnect monitor if still running
735 0 ▶ $disconnect_future->cancel if $disconnect_future->can('cancel') && !$disconnect_future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $disconnect_future->can("cancel") and not $disconnect_future->is_ready 0 0 0
Condition0%0/3
$disconnect_future->can("cancel") and not $disconnect_future->is_ready
!ll&&!rl&&r
000
MC/DC0%0/2
$disconnect_future->can("cancel") and not $disconnect_future->is_ready
$disconnect_future->can("cancel") not $disconnect_future->is_ready
Truth table0%0/3
A and not(B)
A= $disconnect_future->can("cancel")B= not $disconnect_future->is_ready
ABresult
0X0
100
111
736  
737 0 return $self;
738 24 }
739  
740 # Background future that completes when disconnect is detected
741 1 ▶sub _watch_for_disconnect {
Subroutine _watch_for_disconnect: called CC=1 SCAR=2.3
742 1 my ($self) = @_;
743  
744 1 my $receive = $self->{receive};
745 1 require Scalar::Util;
746 1 my $weak_self = $self;
747 1 Scalar::Util::weaken($weak_self);
748  
749 1 ▶ return (async sub {
Subroutine __ANON__: called CC=8 SCAR=36.4
750 1 ▶ while ($weak_self && !$weak_self->is_closed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weak_self and not $weak_self->is_closed) 1 0 1
Condition33%1/3
$weak_self and not $weak_self->is_closed
!ll&&!rl&&r
001
MC/DC0%0/2
$weak_self and not $weak_self->is_closed
$weak_self not $weak_self->is_closed
Truth table33%1/3
A and not(B)
A= $weak_selfB= not $weak_self->is_closed
ABresult
0X0
100
111
751 1 my $event = eval { await $receive->() };
752 0 ▶ last unless $event;
Branch: 1 branch
BranchTrueFalseTotal
unless $event 0 0 0
753  
754 0 ▶ my $type = $event->{type} // '';
Condition0%0/2
$event->{"type"} // ""
l!l
00
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table0%0/2
A
A= $event->{"type"}
Aresult
11
00
755 0 ▶ if ($type eq 'sse.disconnect') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "sse.disconnect") 0 0 0
756 0 ▶ if ($weak_self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weak_self) 0 0 0
757 0 ▶ $weak_self->{_disconnect_reason} = $event->{reason} // 'client_closed';
Condition0%0/2
$event->{"reason"} // "client_closed"
l!l
00
MC/DC0%0/1
$event->{"reason"} // "client_closed"
$event->{"reason"}
Truth table0%0/2
A
A= $event->{"reason"}
Aresult
11
00
758 0 $weak_self->_set_closed;
759 0 await $weak_self->_run_close_callbacks;
760 }
761 0 last;
762 }
763 }
764 1 })->();
765 }
766  
767 24 1;
768  
769 __END__
« lib/PAGI/Middleware/SecurityHeaders.pm ↑ Summary lib/PAGI/Middleware/Auth/Bearer.pm »