3 # See the bottom of this file for the POD documentation. Search for the
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).
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.
16 # The most recent version and complete docs are available at:
17 # http://www.genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html
18 # ftp://ftp-genome.wi.mit.edu/pub/software/WWW/
19 $CGI::Fast::VERSION='1.04';
25 # workaround for known bug in libfcgi
26 while (($ignore) = each %ENV) { }
28 # override the initialization behavior so that
29 # state is NOT maintained between invocations
34 # If ENV{FCGI_SOCKET_PATH} is specified, we maintain a FCGI Request handle
35 # in this package variable.
36 use vars qw($Ext_Request);
38 # If ENV{FCGI_SOCKET_PATH} is given, explicitly open the socket,
39 # and keep the request handle around from which to call Accept().
40 if ($ENV{FCGI_SOCKET_PATH}) {
41 my $path = $ENV{FCGI_SOCKET_PATH};
42 my $backlog = $ENV{FCGI_LISTEN_QUEUE} || 100;
43 my $socket = FCGI::OpenSocket( $path, $backlog );
44 $Ext_Request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR,
49 # New is slightly different in that it calls FCGI's
52 my ($self, $initializer, @param) = @_;
53 unless (defined $initializer) {
55 return undef unless $Ext_Request->Accept() >= 0;
57 return undef unless FCGI::accept() >= 0;
60 return $CGI::Q = $self->SUPER::new($initializer, @param);
67 CGI::Fast - CGI Interface for Fast CGI
71 use CGI::Fast qw(:standard);
73 while (new CGI::Fast) {
75 print start_html("Fast CGI Rocks");
78 "Invocation number ",b($COUNTER++),
86 CGI::Fast is a subclass of the CGI object created by
87 CGI.pm. It is specialized to work well with the Open Market
88 FastCGI standard, which greatly speeds up CGI scripts by
89 turning them into persistently running server processes. Scripts
90 that perform time-consuming initialization processes, such as
91 loading large modules or opening persistent database connections,
92 will see large performance improvements.
94 =head1 OTHER PIECES OF THE PUZZLE
96 In order to use CGI::Fast you'll need a FastCGI-enabled Web
97 server. Open Market's server is FastCGI-savvy. There are also
98 freely redistributable FastCGI modules for NCSA httpd 1.5 and Apache.
99 FastCGI-enabling modules for Microsoft Internet Information Server and
100 Netscape Communications Server have been announced.
102 In addition, you'll need a version of the Perl interpreter that has
103 been linked with the FastCGI I/O library. Precompiled binaries are
104 available for several platforms, including DEC Alpha, HP-UX and
105 SPARC/Solaris, or you can rebuild Perl from source with patches
106 provided in the FastCGI developer's kit. The FastCGI Perl interpreter
107 can be used in place of your normal Perl without ill consequences.
109 You can find FastCGI modules for Apache and NCSA httpd, precompiled
110 Perl interpreters, and the FastCGI developer's kit all at URL:
112 http://www.fastcgi.com/
114 =head1 WRITING FASTCGI PERL SCRIPTS
116 FastCGI scripts are persistent: one or more copies of the script
117 are started up when the server initializes, and stay around until
118 the server exits or they die a natural death. After performing
119 whatever one-time initialization it needs, the script enters a
120 loop waiting for incoming connections, processing the request, and
123 A typical FastCGI script will look like this:
125 #!/usr/local/bin/perl # must be a FastCGI version of perl!
127 &do_some_initialization();
128 while ($q = new CGI::Fast) {
129 &process_request($q);
132 Each time there's a new request, CGI::Fast returns a
133 CGI object to your loop. The rest of the time your script
134 waits in the call to new(). When the server requests that
135 your script be terminated, new() will return undef. You can
136 of course exit earlier if you choose. A new version of the
137 script will be respawned to take its place (this may be
138 necessary in order to avoid Perl memory leaks in long-running
141 CGI.pm's default CGI object mode also works. Just modify the loop
144 while (new CGI::Fast) {
148 Calls to header(), start_form(), etc. will all operate on the
151 =head1 INSTALLING FASTCGI SCRIPTS
153 See the FastCGI developer's kit documentation for full details. On
154 the Apache server, the following line must be added to srm.conf:
156 AddType application/x-httpd-fcgi .fcgi
158 FastCGI scripts must end in the extension .fcgi. For each script you
159 install, you must add something like the following to srm.conf:
161 FastCgiServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -processes 2
163 This instructs Apache to launch two copies of file_upload.fcgi at
166 =head1 USING FASTCGI SCRIPTS AS CGI SCRIPTS
168 Any script that works correctly as a FastCGI script will also work
169 correctly when installed as a vanilla CGI script. However it will
170 not see any performance benefit.
172 =head1 EXTERNAL FASTCGI SERVER INVOCATION
174 FastCGI supports a TCP/IP transport mechanism which allows FastCGI scripts to run
175 external to the webserver, perhaps on a remote machine. To configure the
176 webserver to connect to an external FastCGI server, you would add the following
179 FastCgiExternalServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -host sputnik:8888
181 Two environment variables affect how the C<CGI::Fast> object is created,
182 allowing C<CGI::Fast> to be used as an external FastCGI server. (See C<FCGI>
183 documentation for C<FCGI::OpenSocket> for more information.)
187 =item FCGI_SOCKET_PATH
189 The address (TCP/IP) or path (UNIX Domain) of the socket the external FastCGI
190 script to which bind an listen for incoming connections from the web server.
192 =item FCGI_LISTEN_QUEUE
194 Maximum length of the queue of pending connections.
200 #!/usr/local/bin/perl # must be a FastCGI version of perl!
202 &do_some_initialization();
203 $ENV{FCGI_SOCKET_PATH} = "sputnik:8888";
204 $ENV{FCGI_LISTEN_QUEUE} = 100;
205 while ($q = new CGI::Fast) {
206 &process_request($q);
211 I haven't tested this very much.
213 =head1 AUTHOR INFORMATION
215 Copyright 1996-1998, Lincoln D. Stein. All rights reserved.
217 This library is free software; you can redistribute it and/or modify
218 it under the same terms as Perl itself.
220 Address bug reports and comments to: lstein@cshl.org
224 This section intentionally left blank.