File Coverage

blib/lib/Net/Async/FastCGI.pm
Criterion Covered Total %
statement 34 37 91.8
branch 4 4 100.0
condition n/a
subroutine 10 11 90.9
pod 3 3 100.0
total 51 55 92.7


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