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   929169 use strict;
  5         13  
  5         186  
2 5     5   25 use warnings;
  5         5  
  5         218  
3 5     5   523 use Future::AsyncAwait;
  5         2997  
  5         45  
4 5     5   2433 use JSON::MaybeXS (); # for pretty output (optional)
  5         33700  
  5         2906  
5              
6 8     8 0 16 async sub drain_body {
7 8         12 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       40 last unless $event->{more};
13             }
14             }
15              
16 17     17 0 33 async sub app {
17 17         35 my ($scope, $receive, $send) = @_;
18              
19             # Handle lifespan scope
20 17 100       47 if ($scope->{type} eq 'lifespan') {
21 9         10 while (1) {
22 18         400 my $event = await $receive->();
23 18 100       1108 if ($event->{type} eq 'lifespan.startup') {
    50          
24 9         31 await $send->({ type => 'lifespan.startup.complete' });
25             }
26             elsif ($event->{type} eq 'lifespan.shutdown') {
27 9         45 await $send->({ type => 'lifespan.shutdown.complete' });
28 9         254 last;
29             }
30             }
31 9         60 return;
32             }
33              
34 8 50       28 die "Unsupported scope type: $scope->{type}" unless $scope->{type} eq 'http';
35 8         28 await drain_body($receive);
36              
37 8         188 my $tls = $scope->{extensions}{tls};
38 8         12 my $body;
39 8 100       16 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     80 });
45             }
46             else {
47 4         12 $body = "Connection is not using TLS";
48             }
49              
50 8         208 await $send->({
51             type => 'http.response.start',
52             status => 200,
53             headers => [ [ 'content-type', 'text/plain' ] ],
54             });
55              
56 8         288 await $send->({ type => 'http.response.body', body => $body, more => 0 });
57             }
58              
59             \&app;