| 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, 2005-2024 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package Net::Async::FastCGI::ServerProtocol 0.26; | ||||||||||
| 7 | |||||||||||
| 8 | 18 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 18 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 18 | ▶ | use base qw( Net::Async::FastCGI::Protocol ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 18 | ▶ | use IO::Async::Stream 0.33; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 18 | ▶ | use Net::FastCGI::Constant qw( FCGI_VERSION_1 :type :role :protocol_status ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 18 | ▶ | use Net::FastCGI::Protocol qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | build_params parse_params | ||||||||||
| 17 | parse_begin_request_body | ||||||||||
| 18 | build_end_request_body | ||||||||||
| 19 | 18 | ); | |||||||||
| 20 | |||||||||||
| 21 | 18 | ▶ | use Net::Async::FastCGI::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | sub configure | ||||||||||
| 24 | 26 | ▶ | { | ||||||||
|
Subroutine
configure: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 25 | 26 | my $self = shift; | |||||||||
| 26 | 26 | my %params = @_; | |||||||||
| 27 | |||||||||||
| 28 | 26 | foreach (qw( stream_stdin )) { | |||||||||
| 29 | exists $params{$_} and | ||||||||||
| 30 | 26 | ▶ | $self->{$_} = delete $params{$_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 26 | $self->SUPER::configure( %params ); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | sub _init | ||||||||||
| 37 | 17 | ▶ | { | ||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 17 | my $self = shift; | |||||||||
| 39 | 17 | my ( $params ) = @_; | |||||||||
| 40 | |||||||||||
| 41 | 17 | $self->{fcgi} = delete $params->{fcgi}; | |||||||||
| 42 | 17 | $self->{reqs} = {}; # {$reqid} = $req | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | sub on_closed | ||||||||||
| 46 | 9 | ▶ | { | ||||||||
|
Subroutine
on_closed: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 47 | 9 | my ( $self ) = @_; | |||||||||
| 48 | 9 | $_->_abort for values %{ $self->{reqs} }; | |||||||||
| 49 | |||||||||||
| 50 | # TODO: This might want to live in IO::Async::Protocol | ||||||||||
| 51 | 9 | ▶ | if( my $parent = $self->parent ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 8 | $parent->remove_child( $self ); | |||||||||
| 53 | } | ||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | sub on_mgmt_record | ||||||||||
| 57 | 4 | ▶ | { | ||||||||
|
Subroutine
on_mgmt_record: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 58 | 4 | my $self = shift; | |||||||||
| 59 | 4 | my ( $type, $rec ) = @_; | |||||||||
| 60 | |||||||||||
| 61 | 4 | ▶ | return $self->_get_values( $rec ) if $type == FCGI_GET_VALUES; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 1 | return $self->SUPER::on_mgmt_record( $type, $rec ); | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | sub on_record | ||||||||||
| 67 | 98 | ▶ | { | ||||||||
|
Subroutine
on_record: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 68 | 98 | my $self = shift; | |||||||||
| 69 | 98 | my ( $reqid, $rec ) = @_; | |||||||||
| 70 | |||||||||||
| 71 | 98 | my $type = $rec->{type}; | |||||||||
| 72 | |||||||||||
| 73 | 98 | ▶ | if( $type == FCGI_BEGIN_REQUEST ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 25 | ( my $role, $rec->{flags} ) = parse_begin_request_body( $rec->{content} ); | |||||||||
| 75 | |||||||||||
| 76 | 25 | ▶ | if( $role == FCGI_RESPONDER ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | my $req = Net::Async::FastCGI::Request->new( | ||||||||||
| 78 | conn => $self, | ||||||||||
| 79 | fcgi => $self->{fcgi}, | ||||||||||
| 80 | rec => $rec, | ||||||||||
| 81 | stream_stdin => $self->{stream_stdin}, | ||||||||||
| 82 | 24 | ); | |||||||||
| 83 | 24 | $self->{reqs}->{$reqid} = $req; | |||||||||
| 84 | } | ||||||||||
| 85 | else { | ||||||||||
| 86 | $self->write_record( { type => FCGI_END_REQUEST, reqid => $rec->{reqid} }, | ||||||||||
| 87 | 1 | build_end_request_body( 0, FCGI_UNKNOWN_ROLE ) | |||||||||
| 88 | ); | ||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 25 | return; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | # FastCGI spec says we're supposed to ignore any record apart from | ||||||||||
| 95 | # FCGI_BEGIN_REQUEST on unrecognised request IDs | ||||||||||
| 96 | 73 | ▶ | my $req = $self->{reqs}->{$reqid} or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | |||||||||||
| 98 | 73 | $req->incomingrecord( $rec ); | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | sub _req_needs_flush | ||||||||||
| 102 | 51 | ▶ | { | ||||||||
|
Subroutine
_req_needs_flush: called CC=2 SCAR=6.9
| |||||||||||
| 103 | 51 | my $self = shift; | |||||||||
| 104 | |||||||||||
| 105 | 24 | ▶ | $self->{gensub_queued}++ or $self->write( sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 106 | 24 | my ( $self ) = @_; | |||||||||
| 107 | |||||||||||
| 108 | 24 | undef $self->{gensub_queued}; | |||||||||
| 109 | |||||||||||
| 110 | 24 | my $want_more = 0; | |||||||||
| 111 | |||||||||||
| 112 | 24 | foreach my $req ( values %{ $self->{reqs} } ) { | |||||||||
| 113 | 16 | $req->_flush_streams; | |||||||||
| 114 | 16 | ▶ | $want_more = 1 if $req->_needs_flush; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 24 | ▶ | $self->_req_needs_flush if $want_more; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | |||||||||||
| 119 | 24 | return undef; | |||||||||
| 120 | 51 | ▶ | } ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | sub _removereq | ||||||||||
| 124 | 21 | ▶ | { | ||||||||
|
Subroutine
_removereq: called CC=1 SCAR=0.0
| |||||||||||
| 125 | 21 | my $self = shift; | |||||||||
| 126 | 21 | my ( $reqid ) = @_; | |||||||||
| 127 | |||||||||||
| 128 | 21 | delete $self->{reqs}->{$reqid}; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | sub _get_values | ||||||||||
| 132 | 3 | ▶ | { | ||||||||
|
Subroutine
_get_values: called CC=3 SCAR=11.0
| |||||||||||
| 133 | 3 | my $self = shift; | |||||||||
| 134 | 3 | my ( $rec ) = @_; | |||||||||
| 135 | |||||||||||
| 136 | 3 | my $content = $rec->{content}; | |||||||||
| 137 | |||||||||||
| 138 | 3 | my $ret = ""; | |||||||||
| 139 | |||||||||||
| 140 | 3 | foreach my $name ( keys %{ parse_params( $content ) } ) { | |||||||||
| 141 | 3 | my $value = $self->_get_value( $name ); | |||||||||
| 142 | 3 | ▶ | if( defined $value ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | 3 | $ret .= build_params( { $name => $value } ); | |||||||||
| 144 | } | ||||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | $self->write_record( | ||||||||||
| 148 | { | ||||||||||
| 149 | 3 | type => FCGI_GET_VALUES_RESULT, | |||||||||
| 150 | reqid => 0, | ||||||||||
| 151 | }, | ||||||||||
| 152 | $ret | ||||||||||
| 153 | ); | ||||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | # This is a method so subclasses could hook extra values if they want | ||||||||||
| 157 | sub _get_value | ||||||||||
| 158 | 3 | ▶ | { | ||||||||
|
Subroutine
_get_value: called CC=4 SCAR=14.0
| |||||||||||
| 159 | 3 | my $self = shift; | |||||||||
| 160 | 3 | my ( $name ) = @_; | |||||||||
| 161 | |||||||||||
| 162 | 3 | ▶ | return 1 if $name eq "FCGI_MPXS_CONNS"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | |||||||||||
| 164 | 2 | ▶ | return $Net::Async::FastCGI::MAX_CONNS if $name eq "FCGI_MAX_CONNS"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 165 | 1 | ▶ | return $Net::Async::FastCGI::MAX_REQS if $name eq "FCGI_MAX_REQS"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | |||||||||||
| 167 | 0 | return undef; | |||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | 18 | 0x55AA; | |||||||||