Re: Patch lint for grep { /.../ } and grep /.../,
[p5sagit/p5-mst-13.2.git] / lib / CGI / Fast.pm
CommitLineData
54310121 1package CGI::Fast;
2
3# See the bottom of this file for the POD documentation. Search for the
4# string '=head'.
5
6# You can run this file through either pod2man or pod2html to produce pretty
7# documentation in manual or html file format (these utilities are part of the
8# Perl 5 distribution).
9
10# Copyright 1995,1996, Lincoln D. Stein. All rights reserved.
11# It may be used and modified freely, but I do request that this copyright
12# notice remain attached to the file. You may modify this module as you
13# wish, but if you redistribute a modified version, please attach a note
14# listing the modifications you have made.
15
55b5d700 16$CGI::Fast::VERSION='1.06';
54310121 17
18use CGI;
19use FCGI;
20@ISA = ('CGI');
21
22# workaround for known bug in libfcgi
23while (($ignore) = each %ENV) { }
24
25# override the initialization behavior so that
26# state is NOT maintained between invocations
27sub save_request {
28 # no-op
29}
30
8f3ccfa2 31# If ENV{FCGI_SOCKET_PATH} is specified, we maintain a FCGI Request handle
69c89ae7 32# in this package variable.
33use vars qw($Ext_Request);
34BEGIN {
35 # If ENV{FCGI_SOCKET_PATH} is given, explicitly open the socket,
36 # and keep the request handle around from which to call Accept().
37 if ($ENV{FCGI_SOCKET_PATH}) {
38 my $path = $ENV{FCGI_SOCKET_PATH};
39 my $backlog = $ENV{FCGI_LISTEN_QUEUE} || 100;
40 my $socket = FCGI::OpenSocket( $path, $backlog );
41 $Ext_Request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR,
42 \%ENV, $socket, 1 );
43 }
44}
45
54310121 46# New is slightly different in that it calls FCGI's
47# accept() method.
48sub new {
71f3e297 49 my ($self, $initializer, @param) = @_;
50 unless (defined $initializer) {
69c89ae7 51 if ($Ext_Request) {
52 return undef unless $Ext_Request->Accept() >= 0;
53 } else {
71f3e297 54 return undef unless FCGI::accept() >= 0;
55 }
69c89ae7 56 }
71f3e297 57 return $CGI::Q = $self->SUPER::new($initializer, @param);
54310121 58}
59
601;
61
62=head1 NAME
63
64CGI::Fast - CGI Interface for Fast CGI
65
66=head1 SYNOPSIS
67
68 use CGI::Fast qw(:standard);
69 $COUNTER = 0;
70 while (new CGI::Fast) {
71 print header;
72 print start_html("Fast CGI Rocks");
73 print
74 h1("Fast CGI Rocks"),
75 "Invocation number ",b($COUNTER++),
76 " PID ",b($$),".",
77 hr;
78 print end_html;
79 }
80
81=head1 DESCRIPTION
82
83CGI::Fast is a subclass of the CGI object created by
84CGI.pm. It is specialized to work well with the Open Market
85FastCGI standard, which greatly speeds up CGI scripts by
86turning them into persistently running server processes. Scripts
87that perform time-consuming initialization processes, such as
88loading large modules or opening persistent database connections,
89will see large performance improvements.
90
91=head1 OTHER PIECES OF THE PUZZLE
92
93In order to use CGI::Fast you'll need a FastCGI-enabled Web
55b5d700 94server. See http://www.fastcgi.com/ for details.
54310121 95
96=head1 WRITING FASTCGI PERL SCRIPTS
97
98FastCGI scripts are persistent: one or more copies of the script
99are started up when the server initializes, and stay around until
100the server exits or they die a natural death. After performing
101whatever one-time initialization it needs, the script enters a
102loop waiting for incoming connections, processing the request, and
103waiting some more.
104
105A typical FastCGI script will look like this:
106
107 #!/usr/local/bin/perl # must be a FastCGI version of perl!
108 use CGI::Fast;
109 &do_some_initialization();
110 while ($q = new CGI::Fast) {
111 &process_request($q);
112 }
113
114Each time there's a new request, CGI::Fast returns a
115CGI object to your loop. The rest of the time your script
116waits in the call to new(). When the server requests that
117your script be terminated, new() will return undef. You can
118of course exit earlier if you choose. A new version of the
119script will be respawned to take its place (this may be
120necessary in order to avoid Perl memory leaks in long-running
121scripts).
122
123CGI.pm's default CGI object mode also works. Just modify the loop
124this way:
125
126 while (new CGI::Fast) {
127 &process_request;
128 }
129
130Calls to header(), start_form(), etc. will all operate on the
131current request.
132
133=head1 INSTALLING FASTCGI SCRIPTS
134
135See the FastCGI developer's kit documentation for full details. On
136the Apache server, the following line must be added to srm.conf:
137
138 AddType application/x-httpd-fcgi .fcgi
139
140FastCGI scripts must end in the extension .fcgi. For each script you
141install, you must add something like the following to srm.conf:
142
69c89ae7 143 FastCgiServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -processes 2
54310121 144
145This instructs Apache to launch two copies of file_upload.fcgi at
146startup time.
147
148=head1 USING FASTCGI SCRIPTS AS CGI SCRIPTS
149
150Any script that works correctly as a FastCGI script will also work
151correctly when installed as a vanilla CGI script. However it will
152not see any performance benefit.
153
69c89ae7 154=head1 EXTERNAL FASTCGI SERVER INVOCATION
155
156FastCGI supports a TCP/IP transport mechanism which allows FastCGI scripts to run
157external to the webserver, perhaps on a remote machine. To configure the
158webserver to connect to an external FastCGI server, you would add the following
159to your srm.conf:
160
161 FastCgiExternalServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -host sputnik:8888
162
163Two environment variables affect how the C<CGI::Fast> object is created,
164allowing C<CGI::Fast> to be used as an external FastCGI server. (See C<FCGI>
165documentation for C<FCGI::OpenSocket> for more information.)
166
167=over
168
169=item FCGI_SOCKET_PATH
170
171The address (TCP/IP) or path (UNIX Domain) of the socket the external FastCGI
8f3ccfa2 172script to which bind an listen for incoming connections from the web server.
69c89ae7 173
174=item FCGI_LISTEN_QUEUE
175
176Maximum length of the queue of pending connections.
177
178=back
179
180For example:
181
182 #!/usr/local/bin/perl # must be a FastCGI version of perl!
183 use CGI::Fast;
184 &do_some_initialization();
185 $ENV{FCGI_SOCKET_PATH} = "sputnik:8888";
186 $ENV{FCGI_LISTEN_QUEUE} = 100;
187 while ($q = new CGI::Fast) {
188 &process_request($q);
189 }
190
54310121 191=head1 CAVEATS
192
193I haven't tested this very much.
194
195=head1 AUTHOR INFORMATION
196
71f3e297 197Copyright 1996-1998, Lincoln D. Stein. All rights reserved.
54310121 198
71f3e297 199This library is free software; you can redistribute it and/or modify
200it under the same terms as Perl itself.
201
202Address bug reports and comments to: lstein@cshl.org
54310121 203
204=head1 BUGS
205
206This section intentionally left blank.
207
208=head1 SEE ALSO
209
210L<CGI::Carp>, L<CGI>
3cb6de81 211
54310121 212=cut