File: FastCGI.pm

package info (click to toggle)
libnet-async-fastcgi-perl 0.26-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: perl: 2,308; makefile: 2
file content (282 lines) | stat: -rw-r--r-- 6,254 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#  You may distribute under the terms of either the GNU General Public License
#  or the Artistic License (the same terms as Perl itself)
#
#  (C) Paul Evans, 2005-2024 -- leonerd@leonerd.org.uk

package Net::Async::FastCGI 0.26;

use v5.14;
use warnings;

use Carp;

use base qw( IO::Async::Listener );
IO::Async::Listener->VERSION( '0.35' );

use Net::Async::FastCGI::ServerProtocol;

# The FCGI_GET_VALUES request might ask for our maximally supported number of
# concurrent connections or requests. We don't really have an inbuilt maximum,
# so just respond these large numbers
our $MAX_CONNS = 1024;
our $MAX_REQS  = 1024;

=head1 NAME

C<Net::Async::FastCGI> - use FastCGI with L<IO::Async>

=head1 SYNOPSIS

As an adapter:

   use Net::Async::FastCGI;
   use IO::Async::Loop;

   my $loop = IO::Async::Loop->new();

   my $fastcgi = Net::Async::FastCGI->new(
      on_request => sub {
         my ( $fastcgi, $req ) = @_;

         # Handle the request here
      }
   );

   $loop->add( $fastcgi );

   $fastcgi->listen(
      service => 1234,
      on_resolve_error => sub { die "Cannot resolve - $_[-1]\n" },
      on_listen_error  => sub { die "Cannot listen - $_[-1]\n" },
   );

   $loop->run;

As a subclass:

   package MyFastCGIResponder;
   use base qw( Net::Async::FastCGI );

   sub on_request
   {
      my $self = shift;
      my ( $req ) = @_;

      # Handle the request here
   }

   ...

   use IO::Async::Loop;

   my $loop = IO::Async::Loop->new();

   my $fastcgi;
   $loop->add( $fastcgi = MyFastCGIResponder->new( service => 1234 ) );

   $fastcgi->listen(
      service => 1234,
      on_resolve_error => sub { die "Cannot resolve - $_[-1]\n" },
      on_listen_error  => sub { die "Cannot listen - $_[-1]\n" },
   );

   $loop->run;

=head1 DESCRIPTION

This module allows a program to respond asynchronously to FastCGI requests,
as part of a program based on L<IO::Async>. An object in this class represents
a single FastCGI responder that the webserver is configured to communicate
with. It can handle multiple outstanding requests at a time, responding to
each as data is provided by the program. Individual outstanding requests that
have been started but not yet finished, are represented by instances of
L<Net::Async::FastCGI::Request>.

=cut

=head1 EVENTS

The following events are invoked, either using subclass methods or CODE
references in parameters:

=head2 on_request $req

Invoked when a new FastCGI request is received. It will be passed a new
L<Net::Async::FastCGI::Request> object.

=cut

=head1 PARAMETERS

The following named parameters may be passed to C<new> or C<configure>:

=over 8

=item on_request => CODE

CODE references for C<on_request> event handler.

=item default_encoding => STRING

Sets the default encoding used by all new requests. If not supplied then
C<UTF-8> will apply.

=item stream_stdin => BOOL

If true, requests will expect to handling streaming of stdin data. In this
mode, the C<on_request> event handler will be invoked once parameters for a
new request have been received, even if the stdin stream is not yet complete.

=back

=cut

sub _init
{
   my $self = shift;
   my ( $params ) = @_;
   $self->SUPER::_init( $params );

   $params->{default_encoding} = "UTF-8";
}

sub configure
{
   my $self = shift;
   my %params = @_;

   foreach (qw( on_request default_encoding stream_stdin )) {
      exists $params{$_} and
         $self->{$_} = delete $params{$_};
   }

   $self->SUPER::configure( %params );
}

sub on_stream
{
   my $self = shift;
   my ( $stream ) = @_;

   $self->add_child( Net::Async::FastCGI::ServerProtocol->new(
      transport    => $stream,
      fcgi         => $self,
      stream_stdin => $self->{stream_stdin},
   ) );
}

=head1 METHODS

=cut

=head2 listen

   $fcgi->listen( %args );

Start listening for connections on a socket, creating it first if necessary.

This method may be called in either of the following ways. To listen on an
existing socket filehandle:

=over 4

=item handle => IO

An IO handle referring to a listen-mode socket. This is now deprecated; use
the C<handle> key to the C<new> or C<configure> methods instead.

=back

Or, to create the listening socket or sockets:

=over 4

=item service => STRING

Port number or service name to listen on.

=item host => STRING

Optional. If supplied, the hostname will be resolved into a set of addresses,
and one listening socket will be created for each address. If not, then all
available addresses will be used.

=back

This method may also require C<on_listen_error> or C<on_resolve_error>
callbacks for error handling - see L<IO::Async::Listener> for more detail.

=cut

sub listen
{
   my $self = shift;
   my %args = @_;

   $self->SUPER::listen( %args, socktype => 'stream' );
}

sub _request_ready
{
   my $self = shift;
   my ( $req ) = @_;

   $self->invoke_event( on_request => $req );

   $req->_start;
}

sub _default_encoding
{
   my $self = shift;
   return $self->{default_encoding};
}

=head1 Limits in FCGI_GET_VALUES

The C<FCGI_GET_VALUES> FastCGI request can enquire of the responder the
maximum number of connections or requests it can support. Because this module
puts no fundamental limit on these values, it will return some arbitrary
numbers. These are given in package variables:

   $Net::Async::FastCGI::MAX_CONNS = 1024;
   $Net::Async::FastCGI::MAX_REQS  = 1024;

These variables are provided in case the containing application wishes to make
the library return different values in the request. These values are not
actually used by the library, other than to fill in the values in response of
C<FCGI_GET_VALUES>.

=head1 Using a socket on STDIN

When running a local FastCGI responder, the webserver will create a new INET
socket connected to the script's STDIN file handle. To use the socket in this
case, it should be passed as the C<handle> argument.

=head1 SEE ALSO

=over 4

=item *

L<CGI::Fast> - Fast CGI drop-in replacement of L<CGI>; single-threaded,
blocking mode.

=item *

L<http://hoohoo.ncsa.uiuc.edu/cgi/interface.html> - The Common Gateway
Interface Specification

=item *

L<http://www.fastcgi.com/devkit/doc/fcgi-spec.html> - FastCGI Specification

=back

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;