File Coverage

blib/lib/Net/Async/FastCGI/Protocol.pm
Criterion Covered Total %
statement 38 38 100.0
branch 6 8 75.0
condition n/a
subroutine 8 8 100.0
pod 1 3 33.3
total 53 57 92.9


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, 2010-2024 -- leonerd@leonerd.org.uk
5              
6             package Net::Async::FastCGI::Protocol 0.26;
7              
8 18     18   260 use v5.14;
  18         77  
9 18     18   100 use warnings;
  18         30  
  18         1620  
10              
11 18     18   101 use base qw( IO::Async::Protocol::Stream );
  18         37  
  18         9998  
12              
13 18         1965 use Net::FastCGI::Constant qw(
14             FCGI_UNKNOWN_TYPE
15 18     18   56558 );
  18         20143  
16              
17 18         9234 use Net::FastCGI::Protocol qw(
18             parse_header
19             build_record
20             build_unknown_type_body
21 18     18   8783 );
  18         143663  
22              
23             sub on_read
24             {
25 103     103 1 68267 my $self = shift;
26 103         314 my ( $buffref, $handleclosed ) = @_;
27              
28 103         197 my $blen = length $$buffref;
29              
30 103 100       367 if( $handleclosed ) {
31             # Abort
32 1         3 my $fcgi = $self->{fcgi};
33 1         29 $fcgi->remove_child( $self );
34 1         286 return;
35             }
36              
37             # Do we have a record header yet?
38 102 50       247 return 0 unless( $blen >= 8 );
39              
40             # Excellent - parse it
41 102         375 my ( $type, $reqid, $contentlen, $padlen ) = parse_header( $$buffref );
42              
43             # Do we have enough for a complete record?
44 102 50       1386 return 0 unless( $blen >= 8 + $contentlen + $padlen );
45              
46 102         287 substr( $$buffref, 0, 8, "" ); # Header
47              
48 102         476 my $rec = {
49             type => $type,
50             reqid => $reqid,
51             len => $contentlen,
52             plen => $padlen,
53             };
54              
55 102         441 $rec->{content} = substr( $$buffref, 0, $contentlen, "" );
56              
57 102         259 substr( $$buffref, 0, $rec->{plen}, "" ); # Padding
58              
59 102 100       289 if( $reqid == 0 ) {
60 4         51 $self->on_mgmt_record( $type, $rec );
61             }
62             else {
63 98         387 $self->on_record( $reqid, $rec );
64             }
65              
66 102         3098 return 1;
67             }
68              
69             sub on_mgmt_record
70             {
71 1     1 0 1 my $self = shift;
72 1         22 my ( $type, $rec ) = @_;
73              
74 1         14 $self->write_record( { type => FCGI_UNKNOWN_TYPE, reqid => 0 }, build_unknown_type_body( $type ) );
75             }
76              
77             sub write_record
78             {
79 71     71 0 155 my $self = shift;
80 71         256 my ( $rec, $content ) = @_;
81              
82 71         346 $self->write( build_record( $rec->{type}, $rec->{reqid}, $content ) );
83             }
84              
85             0x55AA;