examples/07-extension-fullflush/app.pl

filter: Statementstmt 100% 26 / 26 Branchbran 80.0% 8 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 91.1% 41 / 45 CC 8 SCAR 18.2
CC
8
Cov
92%
app
8.1
BEGIN
1.0
BEGIN
1.0
CRAP 6.1
« blib/lib/PAGI/Server/AppNormalizer.pm ↑ Summary examples/04-websocket-echo/app.pl »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # Demonstrates fullflush extension during streaming response.
6 # The fullflush event forces immediate TCP buffer flush, useful for
7 # Server-Sent Events or real-time streaming where latency matters.
8  
9 2 ▶async sub app {
Subroutine app: called CC=8 SCAR=20.9
10 2 my ($scope, $receive, $send) = @_;
11  
12 2 ▶ die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'http';
Branch: 1 branch
BranchTrueFalseTotal
if $scope->{"type"} ne "http" 1 1 2
13  
14 # Drain request body if present
15 1 while (1) {
16 1 my $event = await $receive->();
17 1 ▶ last if $event->{type} ne 'http.request';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"type"} ne "http.request" 0 1 1
18 1 ▶ last unless $event->{more};
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"more"} 1 0 1
19 }
20  
21 # Check if server supports fullflush extension
22 1 my $supports_fullflush = exists $scope->{extensions}{fullflush};
23  
24 1 await $send->({
25 type => 'http.response.start',
26 status => 200,
27 headers => [ [ 'content-type', 'text/plain' ] ],
28 });
29  
30 # Stream chunks with flush after each (if supported)
31 1 my @chunks = ("Line 1\n", "Line 2\n", "Line 3\n");
32  
33 1 for my $i (0 .. $#chunks) {
34 3 my $is_last = ($i == $#chunks);
35 3 ▶ await $send->({
Branch: 1 branch
BranchTrueFalseTotal
$is_last ? : 1 2 3
36 type => 'http.response.body',
37 body => $chunks[$i],
38 more => $is_last ? 0 : 1,
39 });
40  
41 # Flush immediately after each chunk so client sees it right away
42 # Only send if server advertises support and not the final chunk
43 3 ▶ if ($supports_fullflush && !$is_last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($supports_fullflush and not $is_last) 2 1 3
Condition66%2/3
$supports_fullflush and not $is_last
!ll&&!rl&&r
012
MC/DC50%1/2
$supports_fullflush and not $is_last
$supports_fullflush not $is_last
Truth table66%2/3
A and not(B)
A= $supports_fullflushB= not $is_last
ABresult
0X0
100
111
44 2 await $send->({ type => 'http.fullflush' });
45 }
46 }
47 1 }
48  
49 1 \&app; # Return coderef when loaded via do
« blib/lib/PAGI/Server/AppNormalizer.pm ↑ Summary examples/04-websocket-echo/app.pl »