File Coverage

blib/lib/Net/Async/FastCGI.pm
Criterion Covered Total %
statement 32 35 91.4
branch 2 2 100.0
condition n/a
subroutine 10 11 90.9
pod 3 3 100.0
total 47 51 92.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, 2005-2024 -- leonerd@leonerd.org.uk
5              
6             package Net::Async::FastCGI 0.26;
7              
8 18     18   4443194 use v5.14;
  18         109  
9 18     18   6062 use warnings;
  18         54  
  18         1329  
10              
11 18     18   137 use Carp;
  18         41  
  18         1676  
12              
13 18     18   154 use base qw( IO::Async::Listener );
  18         70  
  18         11127  
14             IO::Async::Listener->VERSION( '0.35' );
15              
16 18     18   763562 use Net::Async::FastCGI::ServerProtocol;
  18         118  
  18         7652  
17              
18             # The FCGI_GET_VALUES request might ask for our maximally supported number of
19             # concurrent connections or requests. We don't really have an inbuilt maximum,
20             # so just respond these large numbers
21             our $MAX_CONNS = 1024;
22             our $MAX_REQS = 1024;
23              
24             =head1 NAME
25              
26             C - use FastCGI with L
27              
28             =head1 SYNOPSIS
29              
30             As an adapter:
31              
32             use Net::Async::FastCGI;
33             use IO::Async::Loop;
34              
35             my $loop = IO::Async::Loop->new();
36              
37             my $fastcgi = Net::Async::FastCGI->new(
38             on_request => sub {
39             my ( $fastcgi, $req ) = @_;
40              
41             # Handle the request here
42             }
43             );
44              
45             $loop->add( $fastcgi );
46              
47             $fastcgi->listen(
48             service => 1234,
49             on_resolve_error => sub { die "Cannot resolve - $_[-1]\n" },
50             on_listen_error => sub { die "Cannot listen - $_[-1]\n" },
51             );
52              
53             $loop->run;
54              
55             As a subclass:
56              
57             package MyFastCGIResponder;
58             use base qw( Net::Async::FastCGI );
59              
60             sub on_request
61             {
62             my $self = shift;
63             my ( $req ) = @_;
64              
65             # Handle the request here
66             }
67              
68             ...
69              
70             use IO::Async::Loop;
71              
72             my $loop = IO::Async::Loop->new();
73              
74             my $fastcgi;
75             $loop->add( $fastcgi = MyFastCGIResponder->new( service => 1234 ) );
76              
77             $fastcgi->listen(
78             service => 1234,
79             on_resolve_error => sub { die "Cannot resolve - $_[-1]\n" },
80             on_listen_error => sub { die "Cannot listen - $_[-1]\n" },
81             );
82              
83             $loop->run;
84              
85             =head1 DESCRIPTION
86              
87             This module allows a program to respond asynchronously to FastCGI requests,
88             as part of a program based on L. An object in this class represents
89             a single FastCGI responder that the webserver is configured to communicate
90             with. It can handle multiple outstanding requests at a time, responding to
91             each as data is provided by the program. Individual outstanding requests that
92             have been started but not yet finished, are represented by instances of
93             L.
94              
95             =cut
96              
97             =head1 EVENTS
98              
99             The following events are invoked, either using subclass methods or CODE
100             references in parameters:
101              
102             =head2 on_request $req
103              
104             Invoked when a new FastCGI request is received. It will be passed a new
105             L object.
106              
107             =cut
108              
109             =head1 PARAMETERS
110              
111             The following named parameters may be passed to C or C:
112              
113             =over 8
114              
115             =item on_request => CODE
116              
117             CODE references for C event handler.
118              
119             =item default_encoding => STRING
120              
121             Sets the default encoding used by all new requests. If not supplied then
122             C will apply.
123              
124             =item stream_stdin => BOOL
125              
126             If true, requests will expect to handling streaming of stdin data. In this
127             mode, the C event handler will be invoked once parameters for a
128             new request have been received, even if the stdin stream is not yet complete.
129              
130             =back
131              
132             =cut
133              
134             sub _init
135             {
136 17     17   4585766 my $self = shift;
137 17         70 my ( $params ) = @_;
138 17         162 $self->SUPER::_init( $params );
139              
140 17         368 $params->{default_encoding} = "UTF-8";
141             }
142              
143             sub configure
144             {
145 21     21 1 728 my $self = shift;
146 21         75 my %params = @_;
147              
148 21         83 foreach (qw( on_request default_encoding stream_stdin )) {
149             exists $params{$_} and
150 63 100       377 $self->{$_} = delete $params{$_};
151             }
152              
153 21         167 $self->SUPER::configure( %params );
154             }
155              
156             sub on_stream
157             {
158 17     17 1 97318 my $self = shift;
159 17         47 my ( $stream ) = @_;
160              
161             $self->add_child( Net::Async::FastCGI::ServerProtocol->new(
162             transport => $stream,
163             fcgi => $self,
164             stream_stdin => $self->{stream_stdin},
165 17         396 ) );
166             }
167              
168             =head1 METHODS
169              
170             =cut
171              
172             =head2 listen
173              
174             $fcgi->listen( %args );
175              
176             Start listening for connections on a socket, creating it first if necessary.
177              
178             This method may be called in either of the following ways. To listen on an
179             existing socket filehandle:
180              
181             =over 4
182              
183             =item handle => IO
184              
185             An IO handle referring to a listen-mode socket. This is now deprecated; use
186             the C key to the C or C methods instead.
187              
188             =back
189              
190             Or, to create the listening socket or sockets:
191              
192             =over 4
193              
194             =item service => STRING
195              
196             Port number or service name to listen on.
197              
198             =item host => STRING
199              
200             Optional. If supplied, the hostname will be resolved into a set of addresses,
201             and one listening socket will be created for each address. If not, then all
202             available addresses will be used.
203              
204             =back
205              
206             This method may also require C or C
207             callbacks for error handling - see L for more detail.
208              
209             =cut
210              
211             sub listen
212             {
213 0     0 1 0 my $self = shift;
214 0         0 my %args = @_;
215              
216 0         0 $self->SUPER::listen( %args, socktype => 'stream' );
217             }
218              
219             sub _request_ready
220             {
221 24     24   52 my $self = shift;
222 24         68 my ( $req ) = @_;
223              
224 24         288 $self->invoke_event( on_request => $req );
225              
226 24         374 $req->_start;
227             }
228              
229             sub _default_encoding
230             {
231 24     24   51 my $self = shift;
232 24         160 return $self->{default_encoding};
233             }
234              
235             =head1 Limits in FCGI_GET_VALUES
236              
237             The C FastCGI request can enquire of the responder the
238             maximum number of connections or requests it can support. Because this module
239             puts no fundamental limit on these values, it will return some arbitrary
240             numbers. These are given in package variables:
241              
242             $Net::Async::FastCGI::MAX_CONNS = 1024;
243             $Net::Async::FastCGI::MAX_REQS = 1024;
244              
245             These variables are provided in case the containing application wishes to make
246             the library return different values in the request. These values are not
247             actually used by the library, other than to fill in the values in response of
248             C.
249              
250             =head1 Using a socket on STDIN
251              
252             When running a local FastCGI responder, the webserver will create a new INET
253             socket connected to the script's STDIN file handle. To use the socket in this
254             case, it should be passed as the C argument.
255              
256             =head1 SEE ALSO
257              
258             =over 4
259              
260             =item *
261              
262             L - Fast CGI drop-in replacement of L; single-threaded,
263             blocking mode.
264              
265             =item *
266              
267             L - The Common Gateway
268             Interface Specification
269              
270             =item *
271              
272             L - FastCGI Specification
273              
274             =back
275              
276             =head1 AUTHOR
277              
278             Paul Evans
279              
280             =cut
281              
282             0x55AA;