Patch from Jesper Krogh to keep Engine::HTTP from crashing when used with IE
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / FastCGI.pm
CommitLineData
d1d9793f 1package Catalyst::Engine::FastCGI;
ffb41d94 2
3use strict;
fbcc39ad 4use base 'Catalyst::Engine::CGI';
6f409682 5eval "use FCGI";
6die "Please install FCGI\n" if $@;
ffb41d94 7
8=head1 NAME
9
fbcc39ad 10Catalyst::Engine::FastCGI - FastCGI Engine
ffb41d94 11
12=head1 DESCRIPTION
13
fbcc39ad 14This is the FastCGI engine.
ffb41d94 15
e2fd5b5f 16=head1 OVERLOADED METHODS
17
fbcc39ad 18This class overloads some methods from C<Catalyst::Engine::CGI>.
e2fd5b5f 19
b5ecfcf0 20=head2 $self->run($c, $listen, { option => value, ... })
5898abae 21
22Starts the FastCGI server. If C<$listen> is set, then it specifies a
23location to listen for FastCGI requests;
24
25 Form Meaning
26 /path listen via Unix sockets on /path
27 :port listen via TCP on port on all interfaces
28 hostname:port listen via TCP on port bound to hostname
29
30Options may also be specified;
31
32 Option Meaning
33 leave_umask Set to 1 to disable setting umask to 0
34 for socket open
35 nointr Do not allow the listener to be
36 interrupted by Ctrl+C
37 nproc Specify a number of processes for
38 FCGI::ProcManager
dc2fc680 39 pidfile Specify a filename for the pid file
526b698a 40 manager Specify a FCGI::ProcManager sub-class
41 detach Detach from console
e2fd5b5f 42
43=cut
44
fbcc39ad 45sub run {
5898abae 46 my ( $self, $class, $listen, $options ) = @_;
47
5db46287 48 my $sock = 0;
5898abae 49 if ($listen) {
50 my $old_umask = umask;
51 unless ( $options->{leave_umask} ) {
52 umask(0);
53 }
54 $sock = FCGI::OpenSocket( $listen, 100 )
55 or die "failed to open FastCGI socket; $!";
56 unless ( $options->{leave_umask} ) {
57 umask($old_umask);
58 }
59 }
6f2e0021 60 elsif ( $^O ne 'MSWin32' ) {
5898abae 61 -S STDIN
62 or die "STDIN is not a socket; specify a listen location";
63 }
64
65 $options ||= {};
6f409682 66
84528885 67 my %env;
e2fd5b5f 68
5898abae 69 my $request =
84528885 70 FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%env, $sock,
5898abae 71 ( $options->{nointr} ? 0 : &FCGI::FAIL_ACCEPT_ON_INTR ),
72 );
73
74 my $proc_manager;
6f409682 75
76 if ($listen) {
526b698a 77 $options->{manager} ||= "FCGI::ProcManager";
78 $options->{nproc} ||= 1;
b5ecfcf0 79
526b698a 80 $self->daemon_fork() if $options->{detach};
b5ecfcf0 81
526b698a 82 if ( $options->{manager} ) {
83 eval "use $options->{manager}; 1" or die $@;
84
b5ecfcf0 85 $proc_manager = $options->{manager}->new(
86 {
526b698a 87 n_processes => $options->{nproc},
88 pid_fname => $options->{pidfile},
b5ecfcf0 89 }
90 );
91
526b698a 92 # detach *before* the ProcManager inits
93 $self->daemon_detach() if $options->{detach};
94
95 $proc_manager->pm_manage();
dc2fc680 96 }
526b698a 97 elsif ( $options->{detach} ) {
98 $self->daemon_detach();
b5ecfcf0 99 }
5898abae 100 }
e2fd5b5f 101
fbcc39ad 102 while ( $request->Accept >= 0 ) {
5898abae 103 $proc_manager && $proc_manager->pm_pre_dispatch();
d7334071 104
105 # If we're running under Lighttpd, swap PATH_INFO and SCRIPT_NAME
106 # http://lists.rawmode.org/pipermail/catalyst/2006-June/008361.html
107 # Thanks to Mark Blythe for this fix
108 if ( $env{SERVER_SOFTWARE} && $env{SERVER_SOFTWARE} =~ /lighttpd/ ) {
3b6a7b7f 109 $env{PATH_INFO} ||= delete $env{SCRIPT_NAME};
d7334071 110 }
111
84528885 112 $class->handle_request( env => \%env );
d7334071 113
9f778270 114 $proc_manager && $proc_manager->pm_post_dispatch();
fbcc39ad 115 }
e2fd5b5f 116}
117
b5ecfcf0 118=head2 $self->write($c, $buffer)
e2fd5b5f 119
120=cut
121
fbcc39ad 122sub write {
123 my ( $self, $c, $buffer ) = @_;
4f5ebacd 124
fbcc39ad 125 unless ( $self->{_prepared_write} ) {
4f5ebacd 126 $self->prepare_write($c);
fbcc39ad 127 $self->{_prepared_write} = 1;
128 }
4f5ebacd 129
fbcc39ad 130 # FastCGI does not stream data properly if using 'print $handle',
131 # but a syswrite appears to work properly.
4f5ebacd 132 *STDOUT->syswrite($buffer);
e2fd5b5f 133}
134
b5ecfcf0 135=head2 $self->daemon_fork()
526b698a 136
137Performs the first part of daemon initialisation. Specifically,
138forking. STDERR, etc are still connected to a terminal.
139
140=cut
141
142sub daemon_fork {
143 require POSIX;
144 fork && exit;
145}
146
b5ecfcf0 147=head2 $self->daemon_detach( )
526b698a 148
149Performs the second part of daemon initialisation. Specifically,
150disassociates from the terminal.
151
152However, this does B<not> change the current working directory to "/",
153as normal daemons do. It also does not close all open file
154descriptors (except STDIN, STDOUT and STDERR, which are re-opened from
155F</dev/null>).
156
157=cut
158
159sub daemon_detach {
160 my $self = shift;
161 print "FastCGI daemon started (pid $$)\n";
b5ecfcf0 162 open STDIN, "+</dev/null" or die $!;
163 open STDOUT, ">&STDIN" or die $!;
164 open STDERR, ">&STDIN" or die $!;
526b698a 165 POSIX::setsid();
166}
167
198b0efa 1681;
169__END__
170
198b0efa 171=head1 WEB SERVER CONFIGURATIONS
172
d7d72ad9 173=head2 Standalone FastCGI Server
174
175In server mode the application runs as a standalone server and accepts
176connections from a web server. The application can be on the same machine as
177the web server, on a remote machine, or even on multiple remote machines.
178Advantages of this method include running the Catalyst application as a
179different user than the web server, and the ability to set up a scalable
180server farm.
198b0efa 181
d7d72ad9 182To start your application in server mode, install the FCGI::ProcManager
183module and then use the included fastcgi.pl script.
198b0efa 184
d7d72ad9 185 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
198b0efa 186
d7d72ad9 187Command line options for fastcgi.pl include:
188
189 -d -daemon Daemonize the server.
190 -p -pidfile Write a pidfile with the pid of the process manager.
191 -l -listen Listen on a socket path, hostname:port, or :port.
192 -n -nproc The number of processes started to handle requests.
198b0efa 193
d7d72ad9 194See below for the specific web server configurations for using the external
195server.
198b0efa 196
d7d72ad9 197=head2 Apache 1.x, 2.x
198
199Apache requires the mod_fastcgi module. The same module supports both
200Apache 1 and 2.
201
202There are three ways to run your application under FastCGI on Apache: server,
203static, and dynamic.
204
205=head3 Standalone server mode
206
207 FastCgiExternalServer /tmp/myapp -socket /tmp/myapp.socket
208 Alias /myapp/ /tmp/myapp/
209
210 # Or, run at the root
211 Alias / /tmp/myapp/
212
213 # Optionally, rewrite the path when accessed without a trailing slash
214 RewriteRule ^/myapp$ myapp/ [R]
215
216The FastCgiExternalServer directive tells Apache that when serving /tmp/myapp
217to use the FastCGI application listenting on the socket /tmp/mapp.socket.
218Note that /tmp/myapp does not need to exist -- it's a virtual file name.
219
220It's likely that Apache is not configured to serve files in /tmp, so the
221Alias directive maps the url path /myapp/ to the (virtual) file that runs the
222FastCGI application. The trailing slashes are important as their use will
223correctly set the PATH_INFO environment variable used by Catalyst to
224determine the request path. If you would like to be able to access your app
225without a trailing slash (http://server/myapp), you can use the above
226RewriteRule directive.
227
228=head3 Static mode
229
230The term 'static' is misleading, but in static mode Apache uses its own
231FastCGI Process Manager to start the application processes. This happens at
232Apache startup time. In this case you do not run your application's
233fastcgi.pl script -- that is done by Apache. Apache then maps URIs to the
234FastCGI script to run your application.
235
236 FastCgiServer /path/to/myapp/script/myapp_fastcgi.pl -processes 3
237 Alias /myapp/ /path/to/myapp/script/myapp_fastcgi.pl/
198b0efa 238
d7d72ad9 239FastCgiServer tells Apache to start three processes of your application at
240startup. The Alias command maps a path to the FastCGI application. Again,
241the trailing slashes are important.
198b0efa 242
d7d72ad9 243=head3 Dynamic mode
244
245In FastCGI dynamic mode, Apache will run your application on demand,
246typically by requesting a file with a specific extension (e.g. .fcgi). ISPs
247often use this type of setup to provide FastCGI support to many customers.
248
249In this mode it is often enough to place or link your *_fastcgi.pl script in
250your cgi-bin directory with the extension of .fcgi. In dynamic mode Apache
251must be able to run your application as a CGI script so ExecCGI must be
252enabled for the directory.
253
254 AddHandler fastcgi-script .fcgi
255
256The above tells Apache to run any .fcgi file as a FastCGI application.
257
258Here is a complete example:
259
260 <VirtualHost *:80>
261 ServerName www.myapp.com
262 DocumentRoot /path/to/MyApp
263
264 # Allow CGI script to run
265 <Directory /path/to/MyApp>
266 Options +ExecCGI
267 </Directory>
268
269 # Tell Apache this is a FastCGI application
270 <Files myapp_fastcgi.pl>
271 SetHandler fastcgi-script
272 </Files>
198b0efa 273 </VirtualHost>
d7d72ad9 274
275Then a request for /script/myapp_fastcgi.pl will run the
276application.
198b0efa 277
278For more information on using FastCGI under Apache, visit
279L<http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html>
280
281=head2 Lighttpd
282
d7d72ad9 283These configurations were tested with Lighttpd 1.4.7.
284
285=head3 Standalone server mode
286
287 server.document-root = "/var/www/MyApp/root"
288
289 fastcgi.server = (
290 "" => (
291 "MyApp" => (
292 "socket" => "/tmp/myapp.socket",
293 "check-local" => "disable"
294 )
295 )
296 )
297
298=head3 Static mode
198b0efa 299
300 server.document-root = "/var/www/MyApp/root"
301
302 fastcgi.server = (
303 "" => (
304 "MyApp" => (
305 "socket" => "/tmp/myapp.socket",
306 "check-local" => "disable",
307 "bin-path" => "/var/www/MyApp/script/myapp_fastcgi.pl",
308 "min-procs" => 2,
309 "max-procs" => 5,
310 "idle-timeout" => 20
311 )
312 )
313 )
314
d7d72ad9 315Note that in newer versions of lighttpd, the min-procs and idle-timeout
316values are disabled. The above example would start 5 processes.
25810c41 317
d7d72ad9 318=head3 Non-root configuration
25810c41 319
d7d72ad9 320You can also run your application at any non-root location with either of the
321above modes.
198b0efa 322
323 fastcgi.server = (
d7d72ad9 324 "/myapp" => (
198b0efa 325 "MyApp" => (
d7d72ad9 326 # same as above
198b0efa 327 )
328 )
329 )
330
331For more information on using FastCGI under Lighttpd, visit
332L<http://www.lighttpd.net/documentation/fastcgi.html>
333
334=head2 IIS
335
336It is possible to run Catalyst under IIS with FastCGI, but we do not
337yet have detailed instructions.
338
fbcc39ad 339=head1 SEE ALSO
e2fd5b5f 340
fbcc39ad 341L<Catalyst>, L<FCGI>.
cd3bb248 342
fbcc39ad 343=head1 AUTHORS
ffb41d94 344
fbcc39ad 345Sebastian Riedel, <sri@cpan.org>
ffb41d94 346
fbcc39ad 347Christian Hansen, <ch@ngmedia.com>
ffb41d94 348
fbcc39ad 349Andy Grundman, <andy@hybridized.org>
ffb41d94 350
d7d72ad9 351=head1 THANKS
352
353Bill Moseley, for documentation updates and testing.
354
ffb41d94 355=head1 COPYRIGHT
356
357This program is free software, you can redistribute it and/or modify it under
358the same terms as Perl itself.
359
360=cut