Net/Async/FastCGI.pm

filter: Statementstmt 92.3% 36 / 39 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 100% 3 / 3 totaltotal 92.7% 51 / 55 CC 3 SCAR 4.3
CC
3
Cov
93%
configure
3.0
listen
2.0
BEGIN
1.0
CRAP 1.5
« Net/Async/FastCGI/Protocol.pm ↑ Summary
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 ▶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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 18 ▶use base qw( IO::Async::Listener );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 IO::Async::Listener->VERSION( '0.35' );
15  
16 18 ▶use Net::Async::FastCGI::ServerProtocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 18 our $MAX_CONNS = 1024;
22 18 our $MAX_REQS = 1024;
23  
24 =head1 NAME
25  
26 C<Net::Async::FastCGI> - use FastCGI with L<IO::Async>
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<IO::Async>. 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<Net::Async::FastCGI::Request>.
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<Net::Async::FastCGI::Request> object.
106  
107 =cut
108  
109 =head1 PARAMETERS
110  
111 The following named parameters may be passed to C<new> or C<configure>:
112  
113 =over 8
114  
115 =item on_request => CODE
116  
117 CODE references for C<on_request> 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<UTF-8> 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<on_request> 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 17 ▶{
Subroutine _init: called CC=1 SCAR=0.0
136 17 my $self = shift;
137 17 my ( $params ) = @_;
138 17 $self->SUPER::_init( $params );
139  
140 17 $params->{default_encoding} = "UTF-8";
141 }
142  
143 sub configure
144 21 ▶{
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
145 21 my $self = shift;
146 21 my %params = @_;
147  
148 21 foreach (qw( on_request default_encoding stream_stdin )) {
149 exists $params{$_} and
150 63 ▶ $self->{$_} = delete $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
exists $params{$_} and $self->{$_} = delete $params{$_} 34 29 63
151 }
152  
153 21 $self->SUPER::configure( %params );
154 }
155  
156 sub on_stream
157 17 ▶{
Subroutine on_stream: called CC=1 SCAR=0.0
Pod documented
158 17 my $self = shift;
159 17 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 ) );
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<handle> key to the C<new> or C<configure> 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<on_listen_error> or C<on_resolve_error>
207 callbacks for error handling - see L<IO::Async::Listener> for more detail.
208  
209 =cut
210  
211 sub listen
212 0 ▶{
Subroutine listen: not called CC=1 SCAR=6.9
Pod documented
213 0 my $self = shift;
214 0 my %args = @_;
215  
216 0 $self->SUPER::listen( %args, socktype => 'stream' );
217 }
218  
219 sub _request_ready
220 24 ▶{
Subroutine _request_ready: called CC=1 SCAR=0.0
221 24 my $self = shift;
222 24 my ( $req ) = @_;
223  
224 24 $self->invoke_event( on_request => $req );
225  
226 24 $req->_start;
227 }
228  
229 sub _default_encoding
230 24 ▶{
Subroutine _default_encoding: called CC=1 SCAR=0.0
231 24 my $self = shift;
232 24 return $self->{default_encoding};
233 }
234  
235 =head1 Limits in FCGI_GET_VALUES
236  
237 The C<FCGI_GET_VALUES> 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<FCGI_GET_VALUES>.
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<handle> argument.
255  
256 =head1 SEE ALSO
257  
258 =over 4
259  
260 =item *
261  
262 L<CGI::Fast> - Fast CGI drop-in replacement of L<CGI>; single-threaded,
263 blocking mode.
264  
265 =item *
266  
267 L<http://hoohoo.ncsa.uiuc.edu/cgi/interface.html> - The Common Gateway
268 Interface Specification
269  
270 =item *
271  
272 L<http://www.fastcgi.com/devkit/doc/fcgi-spec.html> - FastCGI Specification
273  
274 =back
275  
276 =head1 AUTHOR
277  
278 Paul Evans <leonerd@leonerd.org.uk>
279  
280 =cut
281  
282 18 0x55AA;
« Net/Async/FastCGI/Protocol.pm ↑ Summary