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   940184 use strict;
  5         9  
  5         236  
2 5     5   28 use warnings;
  5         10  
  5         371  
3 5     5   440 use Future::AsyncAwait;
  5         2816  
  5         49  
4 5     5   3070 use JSON::MaybeXS (); # for pretty output (optional)
  5         41673  
  5         2840  
5              
6 8     8 0 12 async sub drain_body {
7 8         16 my ($receive) = @_;
8              
9 8         12 while (1) {
10 8         12 my $event = await $receive->();
11 8 50       152 last if $event->{type} ne 'http.request';
12 8 50       44 last unless $event->{more};
13             }
14             }
15              
16 17     17 0 30 async sub app {
17 17         34 my ($scope, $receive, $send) = @_;
18              
19             # Handle lifespan scope
20 17 100       53 if ($scope->{type} eq 'lifespan') {
21 9         13 while (1) {
22 18         249 my $event = await $receive->();
23 18 100       1253 if ($event->{type} eq 'lifespan.startup') {
    50          
24 9         28 await $send->({ type => 'lifespan.startup.complete' });
25             }
26             elsif ($event->{type} eq 'lifespan.shutdown') {
27 9         41 await $send->({ type => 'lifespan.shutdown.complete' });
28 9         255 last;
29             }
30             }
31 9         45 return;
32             }
33              
34 8 50       24 die "Unsupported scope type: $scope->{type}" unless $scope->{type} eq 'http';
35 8         20 await drain_body($receive);
36              
37 8         196 my $tls = $scope->{extensions}{tls};
38 8         12 my $body;
39 8 100       20 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     44 });
45             }
46             else {
47 4         12 $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         260 await $send->({ type => 'http.response.body', body => $body, more => 0 });
57             }
58              
59             \&app;