| 1 |
# NAME |
| 2 |
|
| 3 |
CGI::Fast - CGI Interface for Fast CGI |
| 4 |
|
| 5 |
<div> |
| 6 |
|
| 7 |
<a href='https://travis-ci.org/leejo/cgi-fast?branch=master'><img src='https://travis-ci.org/leejo/cgi-fast.svg?branch=master' alt='Build Status' /></a> |
| 8 |
<a href='https://coveralls.io/r/leejo/cgi-fast?branch=master'><img src='https://coveralls.io/repos/leejo/cgi-fast/badge.png?branch=master' alt='Coverage Status' /></a> |
| 9 |
</div> |
| 10 |
|
| 11 |
# SYNOPSIS |
| 12 |
|
| 13 |
use CGI::Fast |
| 14 |
socket_path => '9000', |
| 15 |
socket_perm => 0777, |
| 16 |
listen_queue => 50; |
| 17 |
|
| 18 |
use CGI qw/ :standard /; |
| 19 |
|
| 20 |
$COUNTER = 0; |
| 21 |
|
| 22 |
# optional, will default to STDOUT, STDERR |
| 23 |
CGI::Fast->file_handles({ |
| 24 |
fcgi_output_file_handle => IO::Handle->new, |
| 25 |
fcgi_error_file_handle => IO::Handle->new, |
| 26 |
}); |
| 27 |
|
| 28 |
while ($q = CGI::Fast->new) { |
| 29 |
process_request($q); |
| 30 |
} |
| 31 |
|
| 32 |
# DESCRIPTION |
| 33 |
|
| 34 |
CGI::Fast is a subclass of the CGI object created by CGI.pm. It is |
| 35 |
specialized to work with the FCGI module, which greatly speeds up CGI |
| 36 |
scripts by turning them into persistently running server processes. |
| 37 |
Scripts that perform time-consuming initialization processes, such as |
| 38 |
loading large modules or opening persistent database connections, will |
| 39 |
see large performance improvements. |
| 40 |
|
| 41 |
Note that as CGI::Fast is based on CGI.pm it is no longer advised as |
| 42 |
a way to write Perl web apps. See [https://metacpan.org/pod/CGI#CGI.pm-HAS-BEEN-REMOVED-FROM-THE-PERL-CORE](https://metacpan.org/pod/CGI#CGI.pm-HAS-BEEN-REMOVED-FROM-THE-PERL-CORE) |
| 43 |
for more information about this |
| 44 |
|
| 45 |
# OTHER PIECES OF THE PUZZLE |
| 46 |
|
| 47 |
In order to use CGI::Fast you'll need the FCGI module. See |
| 48 |
http://www.cpan.org/ for details. |
| 49 |
|
| 50 |
# WRITING FASTCGI PERL SCRIPTS |
| 51 |
|
| 52 |
FastCGI scripts are persistent: one or more copies of the script |
| 53 |
are started up when the server initializes, and stay around until |
| 54 |
the server exits or they die a natural death. After performing |
| 55 |
whatever one-time initialization it needs, the script enters a |
| 56 |
loop waiting for incoming connections, processing the request, and |
| 57 |
waiting some more. |
| 58 |
|
| 59 |
A typical FastCGI script will look like this: |
| 60 |
|
| 61 |
#!perl |
| 62 |
use CGI::Fast; |
| 63 |
do_some_initialization(); |
| 64 |
while ($q = CGI::Fast->new) { |
| 65 |
process_request($q); |
| 66 |
} |
| 67 |
|
| 68 |
Each time there's a new request, CGI::Fast returns a |
| 69 |
CGI object to your loop. The rest of the time your script |
| 70 |
waits in the call to new(). When the server requests that |
| 71 |
your script be terminated, new() will return undef. You can |
| 72 |
of course exit earlier if you choose. A new version of the |
| 73 |
script will be respawned to take its place (this may be |
| 74 |
necessary in order to avoid Perl memory leaks in long-running |
| 75 |
scripts). |
| 76 |
|
| 77 |
CGI.pm's default CGI object mode also works. Just modify the loop |
| 78 |
this way: |
| 79 |
|
| 80 |
while (CGI::Fast->new) { |
| 81 |
process_request(); |
| 82 |
} |
| 83 |
|
| 84 |
Calls to header(), start\_form(), etc. will all operate on the |
| 85 |
current request. |
| 86 |
|
| 87 |
# INSTALLING FASTCGI SCRIPTS |
| 88 |
|
| 89 |
See the FastCGI developer's kit documentation for full details. On |
| 90 |
the Apache server, the following line must be added to srm.conf: |
| 91 |
|
| 92 |
AddType application/x-httpd-fcgi .fcgi |
| 93 |
|
| 94 |
FastCGI scripts must end in the extension .fcgi. For each script you |
| 95 |
install, you must add something like the following to srm.conf: |
| 96 |
|
| 97 |
FastCgiServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -processes 2 |
| 98 |
|
| 99 |
This instructs Apache to launch two copies of file\_upload.fcgi at |
| 100 |
startup time. |
| 101 |
|
| 102 |
# USING FASTCGI SCRIPTS AS CGI SCRIPTS |
| 103 |
|
| 104 |
Any script that works correctly as a FastCGI script will also work |
| 105 |
correctly when installed as a vanilla CGI script. However it will |
| 106 |
not see any performance benefit. |
| 107 |
|
| 108 |
# EXTERNAL FASTCGI SERVER INVOCATION |
| 109 |
|
| 110 |
FastCGI supports a TCP/IP transport mechanism which allows FastCGI scripts to run |
| 111 |
external to the webserver, perhaps on a remote machine. To configure the |
| 112 |
webserver to connect to an external FastCGI server, you would add the following |
| 113 |
to your srm.conf: |
| 114 |
|
| 115 |
FastCgiExternalServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -host sputnik:8888 |
| 116 |
|
| 117 |
Two environment variables affect how the `CGI::Fast` object is created, |
| 118 |
allowing `CGI::Fast` to be used as an external FastCGI server. (See `FCGI` |
| 119 |
documentation for `FCGI::OpenSocket` for more information.) |
| 120 |
|
| 121 |
You can set these as ENV variables or imports in the use CGI::Fast statement. |
| 122 |
If the ENV variables are set then these will be favoured so you can override |
| 123 |
the import statements on the command line, etc. |
| 124 |
|
| 125 |
- FCGI\_SOCKET\_PATH / socket\_path |
| 126 |
|
| 127 |
The address (TCP/IP) or path (UNIX Domain) of the socket the external FastCGI |
| 128 |
script to which bind an listen for incoming connections from the web server. |
| 129 |
|
| 130 |
- FCGI\_SOCKET\_PERM / socket\_perm |
| 131 |
|
| 132 |
Permissions for UNIX Domain socket. |
| 133 |
|
| 134 |
- FCGI\_LISTEN\_QUEUE / listen\_queue |
| 135 |
|
| 136 |
Maximum length of the queue of pending connections, defaults to 100. |
| 137 |
|
| 138 |
For example: |
| 139 |
|
| 140 |
use CGI::Fast |
| 141 |
socket_path => "sputnik:8888", |
| 142 |
listen_queue => "50" |
| 143 |
; |
| 144 |
|
| 145 |
use CGI qw/ :standard /; |
| 146 |
|
| 147 |
do_some_initialization(); |
| 148 |
|
| 149 |
while ($q = CGI::Fast->new) { |
| 150 |
process_request($q); |
| 151 |
} |
| 152 |
|
| 153 |
Or: |
| 154 |
|
| 155 |
use CGI::Fast; |
| 156 |
use CGI qw/ :standard /; |
| 157 |
|
| 158 |
do_some_initialization(); |
| 159 |
|
| 160 |
$ENV{FCGI_SOCKET_PATH} = "sputnik:8888"; |
| 161 |
$ENV{FCGI_LISTEN_QUEUE} = 50; |
| 162 |
|
| 163 |
while ($q = CGI::Fast->new) { |
| 164 |
process_request($q); |
| 165 |
} |
| 166 |
|
| 167 |
Note the importance of having use CGI after use CGI::Fast as this will |
| 168 |
prevent any CGI import pragmas being overwritten by CGI::Fast. You can |
| 169 |
use CGI::Fast as a drop in replacement like so: |
| 170 |
|
| 171 |
use CGI::Fast qw/ :standard / |
| 172 |
|
| 173 |
# FILE HANDLES |
| 174 |
|
| 175 |
FCGI defaults to using STDOUT and STDERR as its output filehandles - this |
| 176 |
may lead to unexpected redirect of output if you migrate scripts from CGI.pm |
| 177 |
to CGI::Fast. To get around this you can use the file\_handles method, which |
| 178 |
you must do **before** the first call to CGI::Fast->new. For example using |
| 179 |
IO::Handle: |
| 180 |
|
| 181 |
CGI::Fast->file_handles({ |
| 182 |
fcgi_output_file_handle => IO::Handle->new, |
| 183 |
fcgi_error_file_handle => IO::Handle->new, |
| 184 |
}); |
| 185 |
|
| 186 |
while (CGI::Fast->new) { |
| 187 |
.. |
| 188 |
} |
| 189 |
|
| 190 |
Overriding STDIN using the `fcgi_input_file_handle` key is also possible, |
| 191 |
however doing so is likely to break at least POST requests. |
| 192 |
|
| 193 |
# CAVEATS |
| 194 |
|
| 195 |
I haven't tested this very much. |
| 196 |
|
| 197 |
# LICENSE |
| 198 |
|
| 199 |
Copyright 1996-1998, Lincoln D. Stein. All rights reserved. Currently |
| 200 |
maintained by Lee Johnson |
| 201 |
|
| 202 |
This library is free software; you can redistribute it and/or modify |
| 203 |
it under the same terms as Perl itself. |
| 204 |
|
| 205 |
# BUGS |
| 206 |
|
| 207 |
Address bug reports and comments to: |
| 208 |
|
| 209 |
https://github.com/leejo/cgi-fast |
| 210 |
|
| 211 |
# SEE ALSO |
| 212 |
|
| 213 |
[CGI::Carp](https://metacpan.org/pod/CGI::Carp), [CGI](https://metacpan.org/pod/CGI) |