Almost-minimal filesystem based blog.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
6.1 KiB

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