4 # A way to say "use warnings" that's compatible with even older perls.
5 # making it local will not affect the code that loads this module
6 # and since we're not in a BLOCK, warnings are enabled until the EOF
9 # See the bottom of this file for the POD documentation. Search for the
12 # You can run this file through either pod2man or pod2html to produce pretty
13 # documentation in manual or html file format (these utilities are part of the
14 # Perl 5 distribution).
16 # Copyright 1995,1996, Lincoln D. Stein. All rights reserved.
17 # It may be used and modified freely, but I do request that this copyright
18 # notice remain attached to the file. You may modify this module as you
19 # wish, but if you redistribute a modified version, please attach a note
20 # listing the modifications you have made.
22 $CGI::Fast::VERSION='1.08';
26 # use vars works like "our", but is compatible with older Perls.
33 # workaround for known bug in libfcgi
34 while (($ignore) = each %ENV) { }
36 # override the initialization behavior so that
37 # state is NOT maintained between invocations
42 # If ENV{FCGI_SOCKET_PATH} is specified, we maintain a FCGI Request handle
43 # in this package variable.
44 use vars qw($Ext_Request);
46 # If ENV{FCGI_SOCKET_PATH} is given, explicitly open the socket,
47 # and keep the request handle around from which to call Accept().
48 if ($ENV{FCGI_SOCKET_PATH}) {
49 my $path = $ENV{FCGI_SOCKET_PATH};
50 my $backlog = $ENV{FCGI_LISTEN_QUEUE} || 100;
51 my $socket = FCGI::OpenSocket( $path, $backlog );
52 $Ext_Request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR,
57 # New is slightly different in that it calls FCGI's
60 my ($self, $initializer, @param) = @_;
61 unless (defined $initializer) {
63 return undef unless $Ext_Request->Accept() >= 0;
65 return undef unless FCGI::accept() >= 0;
69 $self->_setup_symbols(@CGI::SAVED_SYMBOLS) if @CGI::SAVED_SYMBOLS;
70 return $CGI::Q = $self->SUPER::new($initializer, @param);
77 CGI::Fast - CGI Interface for Fast CGI
81 use CGI::Fast qw(:standard);
83 while (new CGI::Fast) {
85 print start_html("Fast CGI Rocks");
88 "Invocation number ",b($COUNTER++),
96 CGI::Fast is a subclass of the CGI object created by CGI.pm. It is
97 specialized to work well FCGI module, which greatly speeds up CGI
98 scripts by turning them into persistently running server processes.
99 Scripts that perform time-consuming initialization processes, such as
100 loading large modules or opening persistent database connections, will
101 see large performance improvements.
103 =head1 OTHER PIECES OF THE PUZZLE
105 In order to use CGI::Fast you'll need the FCGI module. See
106 http://www.cpan.org/ for details.
108 =head1 WRITING FASTCGI PERL SCRIPTS
110 FastCGI scripts are persistent: one or more copies of the script
111 are started up when the server initializes, and stay around until
112 the server exits or they die a natural death. After performing
113 whatever one-time initialization it needs, the script enters a
114 loop waiting for incoming connections, processing the request, and
117 A typical FastCGI script will look like this:
121 &do_some_initialization();
122 while ($q = new CGI::Fast) {
123 &process_request($q);
126 Each time there's a new request, CGI::Fast returns a
127 CGI object to your loop. The rest of the time your script
128 waits in the call to new(). When the server requests that
129 your script be terminated, new() will return undef. You can
130 of course exit earlier if you choose. A new version of the
131 script will be respawned to take its place (this may be
132 necessary in order to avoid Perl memory leaks in long-running
135 CGI.pm's default CGI object mode also works. Just modify the loop
138 while (new CGI::Fast) {
142 Calls to header(), start_form(), etc. will all operate on the
145 =head1 INSTALLING FASTCGI SCRIPTS
147 See the FastCGI developer's kit documentation for full details. On
148 the Apache server, the following line must be added to srm.conf:
150 AddType application/x-httpd-fcgi .fcgi
152 FastCGI scripts must end in the extension .fcgi. For each script you
153 install, you must add something like the following to srm.conf:
155 FastCgiServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -processes 2
157 This instructs Apache to launch two copies of file_upload.fcgi at
160 =head1 USING FASTCGI SCRIPTS AS CGI SCRIPTS
162 Any script that works correctly as a FastCGI script will also work
163 correctly when installed as a vanilla CGI script. However it will
164 not see any performance benefit.
166 =head1 EXTERNAL FASTCGI SERVER INVOCATION
168 FastCGI supports a TCP/IP transport mechanism which allows FastCGI scripts to run
169 external to the webserver, perhaps on a remote machine. To configure the
170 webserver to connect to an external FastCGI server, you would add the following
173 FastCgiExternalServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -host sputnik:8888
175 Two environment variables affect how the C<CGI::Fast> object is created,
176 allowing C<CGI::Fast> to be used as an external FastCGI server. (See C<FCGI>
177 documentation for C<FCGI::OpenSocket> for more information.)
181 =item FCGI_SOCKET_PATH
183 The address (TCP/IP) or path (UNIX Domain) of the socket the external FastCGI
184 script to which bind an listen for incoming connections from the web server.
186 =item FCGI_LISTEN_QUEUE
188 Maximum length of the queue of pending connections.
194 #!/usr/local/bin/perl # must be a FastCGI version of perl!
196 &do_some_initialization();
197 $ENV{FCGI_SOCKET_PATH} = "sputnik:8888";
198 $ENV{FCGI_LISTEN_QUEUE} = 100;
199 while ($q = new CGI::Fast) {
200 &process_request($q);
205 I haven't tested this very much.
207 =head1 AUTHOR INFORMATION
209 Copyright 1996-1998, Lincoln D. Stein. All rights reserved.
211 This library is free software; you can redistribute it and/or modify
212 it under the same terms as Perl itself.
214 Address bug reports and comments to: lstein@cshl.org
218 This section intentionally left blank.