examples/duplex-http-stream/app.pl

filter: Statementstmt 100% 38 / 38 Branchbran 75.0% 6 / 8 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 90.7% 59 / 65 CC 7 SCAR 9.8
CC
7
Cov
92%
__ANON__
5.1
__ANON__
3.0
__ANON__
1.0
CRAP 2.7
« examples/custom-middleware/lib/Example/Middleware/RequestTimer.pm ↑ Summary examples/sse-custom-events/app.pl »
1 1 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Future::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use PAGI::Nano;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Full-duplex over a single HTTP request: read the streaming request body while
9 # concurrently streaming the response. This is the HTTP-streaming analog of
10 # examples/bidirectional-websocket — two branches raced with wait_any, one
11 # echoing client input, one pushing server ticks unsolicited.
12 #
13 # Caveats (this is why WebSocket exists): no message framing (it's a byte stream
14 # both ways), browsers can't do full-duplex on a single fetch, and HTTP/1.1
15 # proxies may buffer the request before forwarding. It is a fine fit for
16 # non-browser / service-to-service / HTTP-2 clients.
17 #
18 # pagi-server app.pl
19 #
20 # # Plain POST sends NO request body, so there is nothing to echo; the
21 # # connection drives the stream, so ticks keep coming until you stop reading:
22 # curl -N -XPOST --max-time 4 http://127.0.0.1:5000/duplex
23 #
24 # # To actually send a body and see echoes, stream stdin as the body with
25 # # `-T -` (so you get echo: lines interleaved with ticks):
26 # ( printf 'hello\n'; sleep 1; printf 'world\n'; sleep 2 ) \
27 # | curl -N -T - -XPOST -H 'Transfer-Encoding: chunked' http://127.0.0.1:5000/duplex
28 #
29 # # Note: typing into `curl -T -` interactively lags by one line — curl holds
30 # # the line you just typed in its upload buffer until its next read. That is
31 # # a curl artifact, not the server: the server echoes each chunk within ~40ms
32 # # of receiving it. For a real-time wire proof use the raw-socket probe, which
33 # # flushes each chunk immediately:
34 # perl probe.pl 5000
35  
36 1 ▶my $app = app {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 1 ▶ post '/duplex' => async sub ($c) {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 1 my $in = $c->req->body_stream;
39 1 ▶ $c->response->stream(async sub ($w) {
Subroutine __ANON__: called CC=5 SCAR=16.2
40 # Echo request-body chunks as they arrive, concurrently. This branch
41 # ends when the client finishes its body — but that must NOT end the
42 # stream: the client can stop sending and keep receiving. So it runs
43 # alongside the ticker, it does not drive termination.
44 1 ▶ my $echoer = (async sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
45 1 ▶ while (defined(my $chunk = await $in->next_chunk)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $chunk = <custom op await>)) 1 1 2
46 1 ▶ next unless length $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless length $chunk 0 1 1
47 1 await $w->write("echo: $chunk\n");
48 }
49 1 })->();
50  
51 # The stream lives as long as the client stays connected, pushing a
52 # tick a second. A ?ticks=N cap bounds the run for demos and for
53 # in-process tests (PAGI::Test::Client models a client that stays
54 # connected and cannot disconnect mid-response, so an unbounded
55 # connection-driven stream would never complete there). Without the
56 # cap, the connection drives termination — see probe.pl against
57 # pagi-server for the real full-duplex run.
58 1 my $max = $c->req->query_param('ticks');
59 1 my $n = 0;
60 1 ▶ while (!$c->is_disconnected && (!defined $max || $n < $max)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c->is_disconnected and !defined($max) || $n < $max) 1 1 2
Condition66%4/6
not $c->is_disconnected and !defined($max) || $n < $max
!ll&&!rl&&r
011
not defined $max or $n < $max
l!l&&r!l&&!r
011
MC/DC33%1/3
not $c->is_disconnected and !defined($max) || $n < $max
not $c->is_disconnected not defined $max $n < $max
Truth table50%2/4
not(A) and not(B) or C
A= not $c->is_disconnectedB= not defined $maxC= $n < $max
ABCresult
0XX0
1000
11X1
1011
61 1 await $w->write('tick ' . (++$n) . "\n");
62 1 await Future::IO->sleep(1);
63 }
64  
65 1 ▶ $echoer->cancel unless $echoer->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $echoer->is_ready 0 1 1
66 1 await $w->close;
67 1 });
68 1 };
69 1 };
70  
71 1 $app;
« examples/custom-middleware/lib/Example/Middleware/RequestTimer.pm ↑ Summary examples/sse-custom-events/app.pl »