File Coverage

blib/lib/Net/Async/HTTP/Server/Protocol.pm
Criterion Covered Total %
statement 47 49 95.9
branch 10 12 83.3
condition 5 11 45.4
subroutine 10 10 100.0
pod 2 2 100.0
total 74 84 88.1


line stmt bran cond sub pod time code
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, 2013-2023 -- leonerd@leonerd.org.uk
5              
6             package Net::Async::HTTP::Server::Protocol 0.15;
7              
8 13     13   170 use v5.14;
  13         41  
9 13     13   65 use warnings;
  13         24  
  13         892  
10 13     13   95 use base qw( IO::Async::Stream );
  13         27  
  13         4094  
11              
12 13     13   137326 use Carp;
  13         26  
  13         822  
13 13     13   70 use Scalar::Util qw( weaken );
  13         21  
  13         631  
14              
15 13     13   6424 use HTTP::Request;
  13         260968  
  13         6653  
16              
17             my $CRLF = "\x0d\x0a";
18              
19             sub on_read
20             {
21 56     56 1 35425 my $self = shift;
22 56         175 my ( $buffref, $eof ) = @_;
23              
24 56 100       271 return 0 if $eof;
25              
26 50 100       875 return 0 unless $$buffref =~ s/^(.*?$CRLF$CRLF)//s;
27 26         142 my $header = $1;
28              
29 26         289 my $request = HTTP::Request->parse( $header );
30 26 50 33     91958 unless( $request and defined $request->protocol and $request->protocol =~ m/^HTTP/ ) {
      33        
31 0         0 $self->close_now;
32 0         0 return 0;
33             }
34              
35 26   100     869 my $request_body_len = $request->content_length || 0;
36              
37 26         2288 $self->debug_printf( "REQUEST %s %s", $request->method, $request->uri->path );
38              
39             return sub {
40 27     27   4482 my ( undef, $buffref, $eof ) = @_;
41              
42 27 100       141 return 0 unless length($$buffref) >= $request_body_len;
43              
44 25         337 $request->add_content( substr( $$buffref, 0, $request_body_len, "" ) );
45              
46 25         553 push @{ $self->{requests} }, my $req = $self->parent->make_request( $self, $request );
  25         166  
47 25         89 weaken( $self->{requests}[-1] );
48              
49 25         85 $self->parent->_received_request( $req );
50              
51 25         404 return undef;
52 26         1882 };
53             }
54              
55             sub on_closed
56             {
57 8     8 1 16 my $self = shift;
58              
59 8   33     31 $_ and $_->_close for @{ $self->{requests} };
  8         40  
60 8         16 undef @{ $self->{requests} };
  8         25  
61             }
62              
63             sub _flush_requests
64             {
65 61     61   586 my $self = shift;
66              
67 61         117 my $queue = $self->{requests};
68 61         191 while( @$queue ) {
69 62         110 my $req = $queue->[0];
70 62 50       177 $req or shift @$queue, next;
71              
72 62         167 my $is_done = $req->_write_to_stream( $self );
73              
74 62 100       586 $is_done ? shift @$queue : return;
75             }
76             }
77              
78             0x55AA;