File Coverage

examples/03-request-body/app.pl
Criterion Covered Total %
statement 25 25 100.0
branch 6 8 75.0
condition 1 2 50.0
subroutine 5 5 100.0
pod n/a
total 37 40 92.5


line stmt bran cond sub pod time code
1 1     1   216323 use strict;
  1         3  
  1         41  
2 1     1   6 use warnings;
  1         2  
  1         45  
3 1     1   5 use Future::AsyncAwait;
  1         3  
  1         11  
4              
5 2     2   5 async sub read_body {
6 2         4 my ($receive) = @_;
7              
8 2         7 my $body = '';
9 2         4 while (1) {
10 2         7 my $event = await $receive->();
11 2 50       79 last if $event->{type} ne 'http.request';
12 2   50     10 $body .= $event->{body} // '';
13 2 50       20 last unless $event->{more};
14             }
15 2         12 return $body;
16             }
17              
18 4     4   8 async sub app {
19 4         12 my ($scope, $receive, $send) = @_;
20              
21 4 100       70 die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'http';
22              
23 2         9 my $body = await read_body($receive);
24 2 100       88 my $message = length $body
25             ? "You sent: $body"
26             : "No body provided";
27              
28 2         19 await $send->({
29             type => 'http.response.start',
30             status => 200,
31             headers => [ [ 'content-type', 'text/plain' ] ],
32             });
33              
34 2         106 await $send->({ type => 'http.response.body', body => $message, more => 0 });
35             }
36              
37             \&app;