File Coverage

examples/08-tls-introspection/app.pl
Criterion Covered Total %
statement 37 37 100.0
branch 11 16 68.7
condition 1 2 50.0
subroutine 6 6 100.0
pod 0 2 0.0
total 55 63 87.3


line stmt bran cond sub pod time code
1 5     5   863248 use strict;
  5         6  
  5         234  
2 5     5   16 use warnings;
  5         9  
  5         204  
3 5     5   410 use Future::AsyncAwait;
  5         3273  
  5         44  
4 5     5   2217 use JSON::MaybeXS (); # for pretty output (optional)
  5         32326  
  5         2841  
5              
6 8     8 0 12 async sub drain_body {
7 8         16 my ($receive) = @_;
8              
9 8         12 while (1) {
10 8         16 my $event = await $receive->();
11 8 50       148 last if $event->{type} ne 'http.request';
12 8 50       44 last unless $event->{more};
13             }
14             }
15              
16 17     17 0 35 async sub app {
17 17         26 my ($scope, $receive, $send) = @_;
18              
19             # Handle lifespan scope
20 17 100       53 if ($scope->{type} eq 'lifespan') {
21 9         10 while (1) {
22 18         254 my $event = await $receive->();
23 18 100       1110 if ($event->{type} eq 'lifespan.startup') {
    50          
24 9         32 await $send->({ type => 'lifespan.startup.complete' });
25             }
26             elsif ($event->{type} eq 'lifespan.shutdown') {
27 9         42 await $send->({ type => 'lifespan.shutdown.complete' });
28 9         250 last;
29             }
30             }
31 9         37 return;
32             }
33              
34 8 50       24 die "Unsupported scope type: $scope->{type}" unless $scope->{type} eq 'http';
35 8         32 await drain_body($receive);
36              
37 8         180 my $tls = $scope->{extensions}{tls};
38 8         16 my $body;
39 8 100       32 if ($tls) {
40             $body = "TLS info:\n" . JSON::MaybeXS->new->pretty(1)->encode({
41             tls_version => sprintf('0x%04x', $tls->{tls_version} // 0),
42             cipher_suite => defined $tls->{cipher_suite} ? sprintf('0x%04x', $tls->{cipher_suite}) : undef,
43             client_cert => $tls->{client_cert_name},
44 4 50 50     36 });
45             }
46             else {
47 4         4 $body = "Connection is not using TLS";
48             }
49              
50 8         204 await $send->({
51             type => 'http.response.start',
52             status => 200,
53             headers => [ [ 'content-type', 'text/plain' ] ],
54             });
55              
56 8         252 await $send->({ type => 'http.response.body', body => $body, more => 0 });
57             }
58              
59             \&app;