Updated Catalyst::Test to use HTTP::Request::AsCGI
[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';
5use FCGI;
ffb41d94 6
7=head1 NAME
8
fbcc39ad 9Catalyst::Engine::FastCGI - FastCGI Engine
ffb41d94 10
11=head1 DESCRIPTION
12
fbcc39ad 13This is the FastCGI engine.
ffb41d94 14
e2fd5b5f 15=head1 OVERLOADED METHODS
16
fbcc39ad 17This class overloads some methods from C<Catalyst::Engine::CGI>.
e2fd5b5f 18
19=over 4
20
5898abae 21=item $self->run($c, $listen, { option => value, ... })
22
23Starts the FastCGI server. If C<$listen> is set, then it specifies a
24location to listen for FastCGI requests;
25
26 Form Meaning
27 /path listen via Unix sockets on /path
28 :port listen via TCP on port on all interfaces
29 hostname:port listen via TCP on port bound to hostname
30
31Options may also be specified;
32
33 Option Meaning
34 leave_umask Set to 1 to disable setting umask to 0
35 for socket open
36 nointr Do not allow the listener to be
37 interrupted by Ctrl+C
38 nproc Specify a number of processes for
39 FCGI::ProcManager
e2fd5b5f 40
41=cut
42
fbcc39ad 43sub run {
5898abae 44 my ( $self, $class, $listen, $options ) = @_;
45
46 my $sock;
47 if ($listen) {
48 my $old_umask = umask;
49 unless ( $options->{leave_umask} ) {
50 umask(0);
51 }
52 $sock = FCGI::OpenSocket( $listen, 100 )
53 or die "failed to open FastCGI socket; $!";
54 unless ( $options->{leave_umask} ) {
55 umask($old_umask);
56 }
57 }
58 else {
59 -S STDIN
60 or die "STDIN is not a socket; specify a listen location";
61 }
62
63 $options ||= {};
84528885 64
65 my %env;
e2fd5b5f 66
5898abae 67 my $request =
84528885 68 FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%env, $sock,
5898abae 69 ( $options->{nointr} ? 0 : &FCGI::FAIL_ACCEPT_ON_INTR ),
70 );
71
72 my $proc_manager;
73
74 if ( $listen and ( $options->{nproc} || 1 ) > 1 ) {
75 require FCGI::ProcManager;
76 $proc_manager =
77 FCGI::ProcManager->new( { n_processes => $options->{nproc} } );
78 $proc_manager->pm_manage();
79 }
e2fd5b5f 80
fbcc39ad 81 while ( $request->Accept >= 0 ) {
5898abae 82 $proc_manager && $proc_manager->pm_pre_dispatch();
84528885 83 $class->handle_request( env => \%env );
5898abae 84 $proc_manager && $proc_manager->pm_pre_dispatch();
fbcc39ad 85 }
e2fd5b5f 86}
87
fbcc39ad 88=item $self->write($c, $buffer)
e2fd5b5f 89
90=cut
91
fbcc39ad 92sub write {
93 my ( $self, $c, $buffer ) = @_;
4f5ebacd 94
fbcc39ad 95 unless ( $self->{_prepared_write} ) {
4f5ebacd 96 $self->prepare_write($c);
fbcc39ad 97 $self->{_prepared_write} = 1;
98 }
4f5ebacd 99
fbcc39ad 100 # FastCGI does not stream data properly if using 'print $handle',
101 # but a syswrite appears to work properly.
4f5ebacd 102 *STDOUT->syswrite($buffer);
e2fd5b5f 103}
104
198b0efa 1051;
106__END__
107
fbcc39ad 108=back
e2fd5b5f 109
198b0efa 110=head1 WEB SERVER CONFIGURATIONS
111
112=head2 Apache 1.x, 2.x
113
114Apache requires the mod_fastcgi module. The following config will let Apache
115control the running of your FastCGI processes.
116
117 # Launch the FastCGI processes
118 FastCgiIpcDir /tmp
8f753f10 119 FastCgiServer /var/www/MyApp/script/myapp_fastcgi.pl -idle-timeout 300 -processes 5
198b0efa 120
121 <VirtualHost *>
122 ScriptAlias / /var/www/MyApp/script/myapp_fastcgi.pl/
123 </VirtualHost>
124
125You can also tell Apache to connect to an external FastCGI server:
126
127 # Start the external server (requires FCGI::ProcManager)
128 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
129
130 # Note that the path used in FastCgiExternalServer can be any path
131 FastCgiIpcDir /tmp
132 FastCgiExternalServer /tmp/myapp_fastcgi.pl -socket /tmp/myapp.socket
133
134 <VirtualHost *>
135 ScriptAlias / /tmp/myapp_fastcgi.pl/
136 </VirtualHost>
137
138For more information on using FastCGI under Apache, visit
139L<http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html>
140
141=head2 Lighttpd
142
143This configuration was tested with Lighttpd 1.4.7.
144
145 server.document-root = "/var/www/MyApp/root"
146
147 fastcgi.server = (
148 "" => (
149 "MyApp" => (
150 "socket" => "/tmp/myapp.socket",
151 "check-local" => "disable",
152 "bin-path" => "/var/www/MyApp/script/myapp_fastcgi.pl",
153 "min-procs" => 2,
154 "max-procs" => 5,
155 "idle-timeout" => 20
156 )
157 )
158 )
159
25810c41 160You can also run your application at any non-root location.
161
162 fastcgi.server = (
163 "/myapp" => (
164 "MyApp" => (
165 # same as above
166 )
167 )
168 )
169
170You can also use an external server:
198b0efa 171
172 # Start the external server (requires FCGI::ProcManager)
173 $ script/myapp_fastcgi.pl -l /tmp/myapp.socket -n 5
174
175 server.document-root = "/var/www/MyApp/root"
176
177 fastcgi.server = (
178 "" => (
179 "MyApp" => (
180 "socket" => "/tmp/myapp.socket",
181 "check-local" => "disable"
182 )
183 )
184 )
185
186For more information on using FastCGI under Lighttpd, visit
187L<http://www.lighttpd.net/documentation/fastcgi.html>
188
189=head2 IIS
190
191It is possible to run Catalyst under IIS with FastCGI, but we do not
192yet have detailed instructions.
193
fbcc39ad 194=head1 SEE ALSO
e2fd5b5f 195
fbcc39ad 196L<Catalyst>, L<FCGI>.
cd3bb248 197
fbcc39ad 198=head1 AUTHORS
ffb41d94 199
fbcc39ad 200Sebastian Riedel, <sri@cpan.org>
ffb41d94 201
fbcc39ad 202Christian Hansen, <ch@ngmedia.com>
ffb41d94 203
fbcc39ad 204Andy Grundman, <andy@hybridized.org>
ffb41d94 205
206=head1 COPYRIGHT
207
208This program is free software, you can redistribute it and/or modify it under
209the same terms as Perl itself.
210
211=cut