blib/lib/Net/Prometheus/_FutureIO.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 98 CC 6 SCAR 30.2
CC
6
Cov
0%
serve
42.0
start
2.0
BEGIN
2.0
CRAP 20.5
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2022-2024 -- leonerd@leonerd.org.uk
5  
6 package Net::Prometheus::_FutureIO 0.16;
7  
8 use v5.20;
9 use warnings;
10  
11 use feature qw( signatures );
12 no warnings qw( experimental::signatures );
13  
14 use Future::IO 0.11;
15 use Future::Utils qw( repeat );
16  
17 # TODO: Consider if we want to use Future::AsyncAwait to make this all a lot neater
18  
19 my %running_clients;
20  
21 sub start ( $pkg, $prometheus, $listensock )
22 {
23 Future::IO->HAVE_MULTIPLE_FILEHANDLES or
24 die "Net::Prometheus::_FutureIO requires a Future::IO implementation that supports multiple filehandles\n";
25  
26 return ( repeat {
27 return Future::IO->accept( $listensock )->then( sub ( $clientsock ) {
28 my $fileno = $clientsock->fileno;
29  
30 my $f = $pkg->serve( $prometheus, $clientsock );
31 $running_clients{$fileno} = $f;
32  
33 $f->on_done( sub ( @ ) {
34 delete $running_clients{$fileno};
35 });
36 $f->on_fail( sub ( $err, @ ) {
37 warn "Net::Prometheus builtin HTTP server failed for [$fileno]: $err";
38 delete $running_clients{$fileno};
39 });
40  
41 return Future->done;
42 });
43 } while => sub ( $f ) { !$f->failure } )->on_fail( sub ( $err, @ ) {
44 warn "Net::Prometheus builtin HTTP server crashed: $err";
45 });
46 }
47  
48 my %HTTP_CODES = (
49 200 => "OK",
50 400 => "Bad Request",
51 405 => "Method Not Allowed",
52 );
53  
54 sub serve ( $pkg, $prometheus, $fh )
55 {
56 my $buf = "";
57 my $f = repeat {
58 Future::IO->sysread( $fh, 8192 )->then( sub ( $more ) {
59 $buf .= $more;
60 Future->done;
61 } );
62 } until => sub ( $f ) { $f->failure or $buf =~ m/\x0d\x0a\x0d\x0a/ };
63  
64 # Parse request and generate a response code
65 $f = $f->then( sub ( @ ) {
66 my ( $req ) = $buf =~ m/^(.*\x0d\x0a\x0d\x0a)/s;
67 ( my ( $firstline, $headers ) = split m/\x0d\x0a/, $req, 2 ) == 2 or
68 return Future->done( 400 );
69  
70 my ( $method, $path, $proto ) = split m/\s+/, $firstline;
71  
72 return Future->done( 400 ) unless $proto =~ m(^HTTP/1\.[01]$);
73  
74 return Future->done( 200 ) if $method eq "GET";
75 return Future->done( 200, 1 ) if $method eq "HEAD";
76 return Future->done( 405 );
77 });
78  
79 # Render an actual response and send it
80 $f = $f->then( sub ( $code, $is_head = undef ) {
81 my $body = "";
82 $body .= $prometheus->render if $code == 200;
83  
84 my $response = "HTTP/1.0 $code $HTTP_CODES{$code}\n";
85 $response .= "Content-Type: text/plain\n";
86 $response .= sprintf "Content-Length: %d\n", length $body;
87 $response .= "\n";
88  
89 $response =~ s/\n/\x0d\x0a/g;
90  
91 $response .= $body unless $is_head;
92  
93 return Future::IO->syswrite( $fh, $response );
94 });
95  
96 return $f;
97 }
98  
99 0x55AA;