10Sstevel@tonic-gatepackage CGI::Fast; 20Sstevel@tonic-gate 30Sstevel@tonic-gate# See the bottom of this file for the POD documentation. Search for the 40Sstevel@tonic-gate# string '=head'. 50Sstevel@tonic-gate 60Sstevel@tonic-gate# You can run this file through either pod2man or pod2html to produce pretty 70Sstevel@tonic-gate# documentation in manual or html file format (these utilities are part of the 80Sstevel@tonic-gate# Perl 5 distribution). 90Sstevel@tonic-gate 100Sstevel@tonic-gate# Copyright 1995,1996, Lincoln D. Stein. All rights reserved. 110Sstevel@tonic-gate# It may be used and modified freely, but I do request that this copyright 120Sstevel@tonic-gate# notice remain attached to the file. You may modify this module as you 130Sstevel@tonic-gate# wish, but if you redistribute a modified version, please attach a note 140Sstevel@tonic-gate# listing the modifications you have made. 150Sstevel@tonic-gate 16*6287Sps156622$CGI::Fast::VERSION='1.07'; 170Sstevel@tonic-gate 180Sstevel@tonic-gateuse CGI; 190Sstevel@tonic-gateuse FCGI; 200Sstevel@tonic-gate@ISA = ('CGI'); 210Sstevel@tonic-gate 220Sstevel@tonic-gate# workaround for known bug in libfcgi 230Sstevel@tonic-gatewhile (($ignore) = each %ENV) { } 240Sstevel@tonic-gate 250Sstevel@tonic-gate# override the initialization behavior so that 260Sstevel@tonic-gate# state is NOT maintained between invocations 270Sstevel@tonic-gatesub save_request { 280Sstevel@tonic-gate # no-op 290Sstevel@tonic-gate} 300Sstevel@tonic-gate 310Sstevel@tonic-gate# If ENV{FCGI_SOCKET_PATH} is specified, we maintain a FCGI Request handle 320Sstevel@tonic-gate# in this package variable. 330Sstevel@tonic-gateuse vars qw($Ext_Request); 340Sstevel@tonic-gateBEGIN { 350Sstevel@tonic-gate # If ENV{FCGI_SOCKET_PATH} is given, explicitly open the socket, 360Sstevel@tonic-gate # and keep the request handle around from which to call Accept(). 370Sstevel@tonic-gate if ($ENV{FCGI_SOCKET_PATH}) { 380Sstevel@tonic-gate my $path = $ENV{FCGI_SOCKET_PATH}; 390Sstevel@tonic-gate my $backlog = $ENV{FCGI_LISTEN_QUEUE} || 100; 400Sstevel@tonic-gate my $socket = FCGI::OpenSocket( $path, $backlog ); 410Sstevel@tonic-gate $Ext_Request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, 420Sstevel@tonic-gate \%ENV, $socket, 1 ); 430Sstevel@tonic-gate } 440Sstevel@tonic-gate} 450Sstevel@tonic-gate 460Sstevel@tonic-gate# New is slightly different in that it calls FCGI's 470Sstevel@tonic-gate# accept() method. 480Sstevel@tonic-gatesub new { 490Sstevel@tonic-gate my ($self, $initializer, @param) = @_; 500Sstevel@tonic-gate unless (defined $initializer) { 510Sstevel@tonic-gate if ($Ext_Request) { 520Sstevel@tonic-gate return undef unless $Ext_Request->Accept() >= 0; 530Sstevel@tonic-gate } else { 540Sstevel@tonic-gate return undef unless FCGI::accept() >= 0; 550Sstevel@tonic-gate } 560Sstevel@tonic-gate } 57*6287Sps156622 CGI->_reset_globals; 580Sstevel@tonic-gate return $CGI::Q = $self->SUPER::new($initializer, @param); 590Sstevel@tonic-gate} 600Sstevel@tonic-gate 610Sstevel@tonic-gate1; 620Sstevel@tonic-gate 630Sstevel@tonic-gate=head1 NAME 640Sstevel@tonic-gate 650Sstevel@tonic-gateCGI::Fast - CGI Interface for Fast CGI 660Sstevel@tonic-gate 670Sstevel@tonic-gate=head1 SYNOPSIS 680Sstevel@tonic-gate 690Sstevel@tonic-gate use CGI::Fast qw(:standard); 700Sstevel@tonic-gate $COUNTER = 0; 710Sstevel@tonic-gate while (new CGI::Fast) { 720Sstevel@tonic-gate print header; 730Sstevel@tonic-gate print start_html("Fast CGI Rocks"); 740Sstevel@tonic-gate print 750Sstevel@tonic-gate h1("Fast CGI Rocks"), 760Sstevel@tonic-gate "Invocation number ",b($COUNTER++), 770Sstevel@tonic-gate " PID ",b($$),".", 780Sstevel@tonic-gate hr; 790Sstevel@tonic-gate print end_html; 800Sstevel@tonic-gate } 810Sstevel@tonic-gate 820Sstevel@tonic-gate=head1 DESCRIPTION 830Sstevel@tonic-gate 840Sstevel@tonic-gateCGI::Fast is a subclass of the CGI object created by 850Sstevel@tonic-gateCGI.pm. It is specialized to work well with the Open Market 860Sstevel@tonic-gateFastCGI standard, which greatly speeds up CGI scripts by 870Sstevel@tonic-gateturning them into persistently running server processes. Scripts 880Sstevel@tonic-gatethat perform time-consuming initialization processes, such as 890Sstevel@tonic-gateloading large modules or opening persistent database connections, 900Sstevel@tonic-gatewill see large performance improvements. 910Sstevel@tonic-gate 920Sstevel@tonic-gate=head1 OTHER PIECES OF THE PUZZLE 930Sstevel@tonic-gate 940Sstevel@tonic-gateIn order to use CGI::Fast you'll need a FastCGI-enabled Web 95*6287Sps156622server. See http://www.fastcgi.com/ for details. 960Sstevel@tonic-gate 970Sstevel@tonic-gate=head1 WRITING FASTCGI PERL SCRIPTS 980Sstevel@tonic-gate 990Sstevel@tonic-gateFastCGI scripts are persistent: one or more copies of the script 1000Sstevel@tonic-gateare started up when the server initializes, and stay around until 1010Sstevel@tonic-gatethe server exits or they die a natural death. After performing 1020Sstevel@tonic-gatewhatever one-time initialization it needs, the script enters a 1030Sstevel@tonic-gateloop waiting for incoming connections, processing the request, and 1040Sstevel@tonic-gatewaiting some more. 1050Sstevel@tonic-gate 1060Sstevel@tonic-gateA typical FastCGI script will look like this: 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate #!/usr/local/bin/perl # must be a FastCGI version of perl! 1090Sstevel@tonic-gate use CGI::Fast; 1100Sstevel@tonic-gate &do_some_initialization(); 1110Sstevel@tonic-gate while ($q = new CGI::Fast) { 1120Sstevel@tonic-gate &process_request($q); 1130Sstevel@tonic-gate } 1140Sstevel@tonic-gate 1150Sstevel@tonic-gateEach time there's a new request, CGI::Fast returns a 1160Sstevel@tonic-gateCGI object to your loop. The rest of the time your script 1170Sstevel@tonic-gatewaits in the call to new(). When the server requests that 1180Sstevel@tonic-gateyour script be terminated, new() will return undef. You can 1190Sstevel@tonic-gateof course exit earlier if you choose. A new version of the 1200Sstevel@tonic-gatescript will be respawned to take its place (this may be 1210Sstevel@tonic-gatenecessary in order to avoid Perl memory leaks in long-running 1220Sstevel@tonic-gatescripts). 1230Sstevel@tonic-gate 1240Sstevel@tonic-gateCGI.pm's default CGI object mode also works. Just modify the loop 1250Sstevel@tonic-gatethis way: 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate while (new CGI::Fast) { 1280Sstevel@tonic-gate &process_request; 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate 1310Sstevel@tonic-gateCalls to header(), start_form(), etc. will all operate on the 1320Sstevel@tonic-gatecurrent request. 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate=head1 INSTALLING FASTCGI SCRIPTS 1350Sstevel@tonic-gate 1360Sstevel@tonic-gateSee the FastCGI developer's kit documentation for full details. On 1370Sstevel@tonic-gatethe Apache server, the following line must be added to srm.conf: 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate AddType application/x-httpd-fcgi .fcgi 1400Sstevel@tonic-gate 1410Sstevel@tonic-gateFastCGI scripts must end in the extension .fcgi. For each script you 1420Sstevel@tonic-gateinstall, you must add something like the following to srm.conf: 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate FastCgiServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -processes 2 1450Sstevel@tonic-gate 1460Sstevel@tonic-gateThis instructs Apache to launch two copies of file_upload.fcgi at 1470Sstevel@tonic-gatestartup time. 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate=head1 USING FASTCGI SCRIPTS AS CGI SCRIPTS 1500Sstevel@tonic-gate 1510Sstevel@tonic-gateAny script that works correctly as a FastCGI script will also work 1520Sstevel@tonic-gatecorrectly when installed as a vanilla CGI script. However it will 1530Sstevel@tonic-gatenot see any performance benefit. 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate=head1 EXTERNAL FASTCGI SERVER INVOCATION 1560Sstevel@tonic-gate 1570Sstevel@tonic-gateFastCGI supports a TCP/IP transport mechanism which allows FastCGI scripts to run 1580Sstevel@tonic-gateexternal to the webserver, perhaps on a remote machine. To configure the 1590Sstevel@tonic-gatewebserver to connect to an external FastCGI server, you would add the following 1600Sstevel@tonic-gateto your srm.conf: 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate FastCgiExternalServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -host sputnik:8888 1630Sstevel@tonic-gate 1640Sstevel@tonic-gateTwo environment variables affect how the C<CGI::Fast> object is created, 1650Sstevel@tonic-gateallowing C<CGI::Fast> to be used as an external FastCGI server. (See C<FCGI> 1660Sstevel@tonic-gatedocumentation for C<FCGI::OpenSocket> for more information.) 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate=over 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate=item FCGI_SOCKET_PATH 1710Sstevel@tonic-gate 1720Sstevel@tonic-gateThe address (TCP/IP) or path (UNIX Domain) of the socket the external FastCGI 1730Sstevel@tonic-gatescript to which bind an listen for incoming connections from the web server. 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate=item FCGI_LISTEN_QUEUE 1760Sstevel@tonic-gate 1770Sstevel@tonic-gateMaximum length of the queue of pending connections. 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate=back 1800Sstevel@tonic-gate 1810Sstevel@tonic-gateFor example: 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate #!/usr/local/bin/perl # must be a FastCGI version of perl! 1840Sstevel@tonic-gate use CGI::Fast; 1850Sstevel@tonic-gate &do_some_initialization(); 1860Sstevel@tonic-gate $ENV{FCGI_SOCKET_PATH} = "sputnik:8888"; 1870Sstevel@tonic-gate $ENV{FCGI_LISTEN_QUEUE} = 100; 1880Sstevel@tonic-gate while ($q = new CGI::Fast) { 1890Sstevel@tonic-gate &process_request($q); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate=head1 CAVEATS 1930Sstevel@tonic-gate 1940Sstevel@tonic-gateI haven't tested this very much. 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate=head1 AUTHOR INFORMATION 1970Sstevel@tonic-gate 1980Sstevel@tonic-gateCopyright 1996-1998, Lincoln D. Stein. All rights reserved. 1990Sstevel@tonic-gate 2000Sstevel@tonic-gateThis library is free software; you can redistribute it and/or modify 2010Sstevel@tonic-gateit under the same terms as Perl itself. 2020Sstevel@tonic-gate 2030Sstevel@tonic-gateAddress bug reports and comments to: lstein@cshl.org 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate=head1 BUGS 2060Sstevel@tonic-gate 2070Sstevel@tonic-gateThis section intentionally left blank. 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate=head1 SEE ALSO 2100Sstevel@tonic-gate 2110Sstevel@tonic-gateL<CGI::Carp>, L<CGI> 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate=cut 214