README
1# NAME
2
3CGI::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
34CGI::Fast is a subclass of the CGI object created by CGI.pm. It is
35specialized to work with the FCGI module, which greatly speeds up CGI
36scripts by turning them into persistently running server processes.
37Scripts that perform time-consuming initialization processes, such as
38loading large modules or opening persistent database connections, will
39see large performance improvements.
40
41Note that as CGI::Fast is based on CGI.pm it is no longer advised as
42a 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)
43for more information about this
44
45# OTHER PIECES OF THE PUZZLE
46
47In order to use CGI::Fast you'll need the FCGI module. See
48http://www.cpan.org/ for details.
49
50# WRITING FASTCGI PERL SCRIPTS
51
52FastCGI scripts are persistent: one or more copies of the script
53are started up when the server initializes, and stay around until
54the server exits or they die a natural death. After performing
55whatever one-time initialization it needs, the script enters a
56loop waiting for incoming connections, processing the request, and
57waiting some more.
58
59A 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
68Each time there's a new request, CGI::Fast returns a
69CGI object to your loop. The rest of the time your script
70waits in the call to new(). When the server requests that
71your script be terminated, new() will return undef. You can
72of course exit earlier if you choose. A new version of the
73script will be respawned to take its place (this may be
74necessary in order to avoid Perl memory leaks in long-running
75scripts).
76
77CGI.pm's default CGI object mode also works. Just modify the loop
78this way:
79
80 while (CGI::Fast->new) {
81 process_request();
82 }
83
84Calls to header(), start\_form(), etc. will all operate on the
85current request.
86
87# INSTALLING FASTCGI SCRIPTS
88
89See the FastCGI developer's kit documentation for full details. On
90the Apache server, the following line must be added to srm.conf:
91
92 AddType application/x-httpd-fcgi .fcgi
93
94FastCGI scripts must end in the extension .fcgi. For each script you
95install, you must add something like the following to srm.conf:
96
97 FastCgiServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -processes 2
98
99This instructs Apache to launch two copies of file\_upload.fcgi at
100startup time.
101
102# USING FASTCGI SCRIPTS AS CGI SCRIPTS
103
104Any script that works correctly as a FastCGI script will also work
105correctly when installed as a vanilla CGI script. However it will
106not see any performance benefit.
107
108# EXTERNAL FASTCGI SERVER INVOCATION
109
110FastCGI supports a TCP/IP transport mechanism which allows FastCGI scripts to run
111external to the webserver, perhaps on a remote machine. To configure the
112webserver to connect to an external FastCGI server, you would add the following
113to your srm.conf:
114
115 FastCgiExternalServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -host sputnik:8888
116
117Two environment variables affect how the `CGI::Fast` object is created,
118allowing `CGI::Fast` to be used as an external FastCGI server. (See `FCGI`
119documentation for `FCGI::OpenSocket` for more information.)
120
121You can set these as ENV variables or imports in the use CGI::Fast statement.
122If the ENV variables are set then these will be favoured so you can override
123the 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
138For 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
153Or:
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
167Note the importance of having use CGI after use CGI::Fast as this will
168prevent any CGI import pragmas being overwritten by CGI::Fast. You can
169use CGI::Fast as a drop in replacement like so:
170
171 use CGI::Fast qw/ :standard /
172
173# FILE HANDLES
174
175FCGI defaults to using STDOUT and STDERR as its output filehandles - this
176may lead to unexpected redirect of output if you migrate scripts from CGI.pm
177to CGI::Fast. To get around this you can use the file\_handles method, which
178you must do **before** the first call to CGI::Fast->new. For example using
179IO::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
190Overriding STDIN using the `fcgi_input_file_handle` key is also possible,
191however doing so is likely to break at least POST requests.
192
193# CAVEATS
194
195I haven't tested this very much.
196
197# LICENSE
198
199Copyright 1996-1998, Lincoln D. Stein. All rights reserved. Currently
200maintained by Lee Johnson
201
202This library is free software; you can redistribute it and/or modify
203it under the same terms as Perl itself.
204
205Address bug reports and comments to:
206
207 https://github.com/leejo/cgi-fast
208
209# BUGS
210
211This section intentionally left blank.
212
213# SEE ALSO
214
215[CGI::Carp](https://metacpan.org/pod/CGI::Carp), [CGI](https://metacpan.org/pod/CGI)
216
README.md
1# NAME
2
3CGI::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
34CGI::Fast is a subclass of the CGI object created by CGI.pm. It is
35specialized to work with the FCGI module, which greatly speeds up CGI
36scripts by turning them into persistently running server processes.
37Scripts that perform time-consuming initialization processes, such as
38loading large modules or opening persistent database connections, will
39see large performance improvements.
40
41Note that as CGI::Fast is based on CGI.pm it is no longer advised as
42a 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)
43for more information about this
44
45# OTHER PIECES OF THE PUZZLE
46
47In order to use CGI::Fast you'll need the FCGI module. See
48http://www.cpan.org/ for details.
49
50# WRITING FASTCGI PERL SCRIPTS
51
52FastCGI scripts are persistent: one or more copies of the script
53are started up when the server initializes, and stay around until
54the server exits or they die a natural death. After performing
55whatever one-time initialization it needs, the script enters a
56loop waiting for incoming connections, processing the request, and
57waiting some more.
58
59A 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
68Each time there's a new request, CGI::Fast returns a
69CGI object to your loop. The rest of the time your script
70waits in the call to new(). When the server requests that
71your script be terminated, new() will return undef. You can
72of course exit earlier if you choose. A new version of the
73script will be respawned to take its place (this may be
74necessary in order to avoid Perl memory leaks in long-running
75scripts).
76
77CGI.pm's default CGI object mode also works. Just modify the loop
78this way:
79
80 while (CGI::Fast->new) {
81 process_request();
82 }
83
84Calls to header(), start\_form(), etc. will all operate on the
85current request.
86
87# INSTALLING FASTCGI SCRIPTS
88
89See the FastCGI developer's kit documentation for full details. On
90the Apache server, the following line must be added to srm.conf:
91
92 AddType application/x-httpd-fcgi .fcgi
93
94FastCGI scripts must end in the extension .fcgi. For each script you
95install, you must add something like the following to srm.conf:
96
97 FastCgiServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -processes 2
98
99This instructs Apache to launch two copies of file\_upload.fcgi at
100startup time.
101
102# USING FASTCGI SCRIPTS AS CGI SCRIPTS
103
104Any script that works correctly as a FastCGI script will also work
105correctly when installed as a vanilla CGI script. However it will
106not see any performance benefit.
107
108# EXTERNAL FASTCGI SERVER INVOCATION
109
110FastCGI supports a TCP/IP transport mechanism which allows FastCGI scripts to run
111external to the webserver, perhaps on a remote machine. To configure the
112webserver to connect to an external FastCGI server, you would add the following
113to your srm.conf:
114
115 FastCgiExternalServer /usr/etc/httpd/fcgi-bin/file_upload.fcgi -host sputnik:8888
116
117Two environment variables affect how the `CGI::Fast` object is created,
118allowing `CGI::Fast` to be used as an external FastCGI server. (See `FCGI`
119documentation for `FCGI::OpenSocket` for more information.)
120
121You can set these as ENV variables or imports in the use CGI::Fast statement.
122If the ENV variables are set then these will be favoured so you can override
123the 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
138For 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
153Or:
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
167Note the importance of having use CGI after use CGI::Fast as this will
168prevent any CGI import pragmas being overwritten by CGI::Fast. You can
169use CGI::Fast as a drop in replacement like so:
170
171 use CGI::Fast qw/ :standard /
172
173# FILE HANDLES
174
175FCGI defaults to using STDOUT and STDERR as its output filehandles - this
176may lead to unexpected redirect of output if you migrate scripts from CGI.pm
177to CGI::Fast. To get around this you can use the file\_handles method, which
178you must do **before** the first call to CGI::Fast->new. For example using
179IO::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
190Overriding STDIN using the `fcgi_input_file_handle` key is also possible,
191however doing so is likely to break at least POST requests.
192
193# CAVEATS
194
195I haven't tested this very much.
196
197# LICENSE
198
199Copyright 1996-1998, Lincoln D. Stein. All rights reserved. Currently
200maintained by Lee Johnson
201
202This library is free software; you can redistribute it and/or modify
203it under the same terms as Perl itself.
204
205Address bug reports and comments to:
206
207 https://github.com/leejo/cgi-fast
208
209# BUGS
210
211This section intentionally left blank.
212
213# SEE ALSO
214
215[CGI::Carp](https://metacpan.org/pod/CGI::Carp), [CGI](https://metacpan.org/pod/CGI)
216