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 n/a
total 55 61 90.1


line stmt bran cond sub pod time code
1 4     4   877520 use strict;
  4         8  
  4         136  
2 4     4   12 use warnings;
  4         8  
  4         148  
3 4     4   16 use Future::AsyncAwait;
  4         4  
  4         48  
4 4     4   1952 use JSON::MaybeXS (); # for pretty output (optional)
  4         26952  
  4         2288  
5              
6 8     8   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       176 last if $event->{type} ne 'http.request';
12 8 50       48 last unless $event->{more};
13             }
14             }
15              
16 16     16   32 async sub app {
17 16         36 my ($scope, $receive, $send) = @_;
18              
19             # Handle lifespan scope
20 16 100       60 if ($scope->{type} eq 'lifespan') {
21 8         12 while (1) {
22 16         240 my $event = await $receive->();
23 16 100       992 if ($event->{type} eq 'lifespan.startup') {
    50          
24 8         28 await $send->({ type => 'lifespan.startup.complete' });
25             }
26             elsif ($event->{type} eq 'lifespan.shutdown') {
27 8         36 await $send->({ type => 'lifespan.shutdown.complete' });
28 8         232 last;
29             }
30             }
31 8         32 return;
32             }
33              
34 8 50       24 die "Unsupported scope type: $scope->{type}" unless $scope->{type} eq 'http';
35 8         28 await drain_body($receive);
36              
37 8         200 my $tls = $scope->{extensions}{tls};
38 8         8 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     40 });
45             }
46             else {
47 4         12 $body = "Connection is not using TLS";
48             }
49              
50 8         248 await $send->({
51             type => 'http.response.start',
52             status => 200,
53             headers => [ [ 'content-type', 'text/plain' ] ],
54             });
55              
56 8         300 await $send->({ type => 'http.response.body', body => $body, more => 0 });
57             }
58              
59             \&app;