xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/CGI/Fast.pm (revision 6287:9a1f5d2c8dd8)
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
16$CGI::Fast::VERSION='1.07';
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
31# If ENV{FCGI_SOCKET_PATH} is specified, we maintain a FCGI Request handle
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
46# New is slightly different in that it calls FCGI's
47# accept() method.
48sub new {
49     my ($self, $initializer, @param) = @_;
50     unless (defined $initializer) {
51	if ($Ext_Request) {
52          return undef unless $Ext_Request->Accept() >= 0;
53	} else {
54         return undef unless FCGI::accept() >= 0;
55     }
56     }
57     CGI->_reset_globals;
58     return $CGI::Q = $self->SUPER::new($initializer, @param);
59}
60
611;
62
63=head1 NAME
64
65CGI::Fast - CGI Interface for Fast CGI
66
67=head1 SYNOPSIS
68
69    use CGI::Fast qw(:standard);
70    $COUNTER = 0;
71    while (new CGI::Fast) {
72	print header;
73	print start_html("Fast CGI Rocks");
74	print
75	    h1("Fast CGI Rocks"),
76	    "Invocation number ",b($COUNTER++),
77            " PID ",b($$),".",
78	    hr;
79        print end_html;
80    }
81
82=head1 DESCRIPTION
83
84CGI::Fast is a subclass of the CGI object created by
85CGI.pm.  It is specialized to work well with the Open Market
86FastCGI standard, which greatly speeds up CGI scripts by
87turning them into persistently running server processes.  Scripts
88that perform time-consuming initialization processes, such as
89loading large modules or opening persistent database connections,
90will see large performance improvements.
91
92=head1 OTHER PIECES OF THE PUZZLE
93
94In order to use CGI::Fast you'll need a FastCGI-enabled Web
95server. See http://www.fastcgi.com/ for details.
96
97=head1 WRITING FASTCGI PERL SCRIPTS
98
99FastCGI scripts are persistent: one or more copies of the script
100are started up when the server initializes, and stay around until
101the server exits or they die a natural death.  After performing
102whatever one-time initialization it needs, the script enters a
103loop waiting for incoming connections, processing the request, and
104waiting some more.
105
106A typical FastCGI script will look like this:
107
108    #!/usr/local/bin/perl    # must be a FastCGI version of perl!
109    use CGI::Fast;
110    &do_some_initialization();
111    while ($q = new CGI::Fast) {
112	&process_request($q);
113    }
114
115Each time there's a new request, CGI::Fast returns a
116CGI object to your loop.  The rest of the time your script
117waits in the call to new().  When the server requests that
118your script be terminated, new() will return undef.  You can
119of course exit earlier if you choose.  A new version of the
120script will be respawned to take its place (this may be
121necessary in order to avoid Perl memory leaks in long-running
122scripts).
123
124CGI.pm's default CGI object mode also works.  Just modify the loop
125this way:
126
127    while (new CGI::Fast) {
128	&process_request;
129    }
130
131Calls to header(), start_form(), etc. will all operate on the
132current request.
133
134=head1 INSTALLING FASTCGI SCRIPTS
135
136See the FastCGI developer's kit documentation for full details.  On
137the Apache server, the following line must be added to srm.conf:
138
139    AddType application/x-httpd-fcgi .fcgi
140
141FastCGI scripts must end in the extension .fcgi.  For each script you
142install, you must add something like the following to srm.conf:
143
144    FastCgiServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -processes 2
145
146This instructs Apache to launch two copies of file_upload.fcgi at
147startup time.
148
149=head1 USING FASTCGI SCRIPTS AS CGI SCRIPTS
150
151Any script that works correctly as a FastCGI script will also work
152correctly when installed as a vanilla CGI script.  However it will
153not see any performance benefit.
154
155=head1 EXTERNAL FASTCGI SERVER INVOCATION
156
157FastCGI supports a TCP/IP transport mechanism which allows FastCGI scripts to run
158external to the webserver, perhaps on a remote machine.  To configure the
159webserver to connect to an external FastCGI server, you would add the following
160to your srm.conf:
161
162    FastCgiExternalServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -host sputnik:8888
163
164Two environment variables affect how the C<CGI::Fast> object is created,
165allowing C<CGI::Fast> to be used as an external FastCGI server.  (See C<FCGI>
166documentation for C<FCGI::OpenSocket> for more information.)
167
168=over
169
170=item FCGI_SOCKET_PATH
171
172The address (TCP/IP) or path (UNIX Domain) of the socket the external FastCGI
173script to which bind an listen for incoming connections from the web server.
174
175=item FCGI_LISTEN_QUEUE
176
177Maximum length of the queue of pending connections.
178
179=back
180
181For example:
182
183    #!/usr/local/bin/perl    # must be a FastCGI version of perl!
184    use CGI::Fast;
185    &do_some_initialization();
186    $ENV{FCGI_SOCKET_PATH} = "sputnik:8888";
187    $ENV{FCGI_LISTEN_QUEUE} = 100;
188    while ($q = new CGI::Fast) {
189	&process_request($q);
190    }
191
192=head1 CAVEATS
193
194I haven't tested this very much.
195
196=head1 AUTHOR INFORMATION
197
198Copyright 1996-1998, Lincoln D. Stein.  All rights reserved.
199
200This library is free software; you can redistribute it and/or modify
201it under the same terms as Perl itself.
202
203Address bug reports and comments to: lstein@cshl.org
204
205=head1 BUGS
206
207This section intentionally left blank.
208
209=head1 SEE ALSO
210
211L<CGI::Carp>, L<CGI>
212
213=cut
214