| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | last unless $event->{more}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$supports_fullflush and not $is_last
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
| |||||||||||||||||||||||||||||
| 44 | 2 | await $send->({ type => 'http.fullflush' }); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 1 | } | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | \&app; # Return coderef when loaded via do | |||||||||||||||||||||||||||