1 package Catalyst::Engine::HTTP;
4 use base 'Catalyst::Engine::CGI';
5 use Errno 'EWOULDBLOCK';
10 use IO::Socket::INET ();
14 require Catalyst::Engine::HTTP::Restarter;
15 require Catalyst::Engine::HTTP::Restarter::Watcher;
19 Catalyst::Engine::HTTP - Catalyst HTTP Engine
23 A script using the Catalyst::Engine::HTTP module might look like:
27 BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP' }
30 use lib '/path/to/MyApp/lib';
37 This is the Catalyst engine specialized for development and testing.
41 =head2 $self->finalize_headers($c)
45 sub finalize_headers {
46 my ( $self, $c ) = @_;
47 my $protocol = $c->request->protocol;
48 my $status = $c->response->status;
49 my $message = status_message($status);
51 print "$protocol $status $message\015\012";
53 $c->response->headers->header( Date => HTTP::Date::time2str(time) );
54 $c->response->headers->header(
55 Connection => $self->_keep_alive ? 'keep-alive' : 'close' );
57 $c->response->headers->header( Status => $status );
59 # Avoid 'print() on closed filehandle Remote' warnings when using IE
60 print $c->response->headers->as_string("\015\012") if *STDOUT->opened();
61 print "\015\012" if *STDOUT->opened();
64 =head2 $self->finalize_read($c)
69 my ( $self, $c ) = @_;
71 # Never ever remove this, it would result in random length output
72 # streams if STDIN eq STDOUT (like in the HTTP engine)
75 return $self->NEXT::finalize_read($c);
78 =head2 $self->prepare_read($c)
83 my ( $self, $c ) = @_;
85 # Set the input handle to non-blocking
88 return $self->NEXT::prepare_read($c);
91 =head2 $self->read_chunk($c, $buffer, $length)
99 # support for non-blocking IO
101 vec( $rin, *STDIN->fileno, 1 ) = 1;
105 select( $rin, undef, undef, undef );
106 my $rc = *STDIN->sysread(@_);
111 next READ if $! == EWOULDBLOCK;
117 =head2 $self->write($c, $buffer)
119 Writes the buffer to the client. Can only be called once for a request.
124 # Avoid 'print() on closed filehandle Remote' warnings when using IE
125 return unless *STDOUT->opened();
127 shift->NEXT::write( @_ );
134 # A very very simple HTTP server that initializes a CGI environment
136 my ( $self, $class, $port, $host, $options ) = @_;
140 if ($options->{background}) {
142 die "Can't fork: $!" unless defined($child);
147 local $SIG{CHLD} = 'IGNORE';
149 my $allowed = $options->{allowed} || { '127.0.0.1' => '255.255.255.255' };
150 my $addr = $host ? inet_aton($host) : INADDR_ANY;
151 if ( $addr eq INADDR_ANY ) {
152 require Sys::Hostname;
153 $host = lc Sys::Hostname::hostname();
156 $host = gethostbyaddr( $addr, AF_INET ) || inet_ntoa($addr);
162 my $daemon = IO::Socket::INET->new(
164 LocalAddr => inet_ntoa($addr),
170 or die "Couldn't create daemon: $!";
172 my $url = "http://$host";
173 $url .= ":$port" unless $port == 80;
175 print "You can connect to your server at $url\n";
177 if ($options->{background}) {
178 open STDIN, "+</dev/null" or die $!;
179 open STDOUT, ">&STDIN" or die $!;
180 open STDERR, ">&STDIN" or die $!;
181 if ( $^O !~ /MSWin32/ ) {
184 or die "Can't start a new session: $!";
188 if (my $pidfile = $options->{pidfile}) {
189 if (! open PIDFILE, "> $pidfile") {
190 warn("Cannot open: $pidfile: $!");
192 print PIDFILE "$$\n";
196 $self->_keep_alive( $options->{keepalive} || 0 );
199 while ( accept( Remote, $daemon ) )
200 { # TODO: get while ( my $remote = $daemon->accept ) to work
209 unless my ( $method, $uri, $protocol ) =
210 $self->_parse_request_line( \*Remote );
212 unless ( uc($method) eq 'RESTART' ) {
215 if ( $options->{fork} ) { next if $pid = fork }
217 $self->_handler( $class, $port, $method, $uri, $protocol );
219 $daemon->close if defined $pid;
223 my $sockdata = $self->_socket_data( \*Remote );
224 my $ipaddr = _inet_addr( $sockdata->{peeraddr} );
226 foreach my $ip ( keys %$allowed ) {
227 my $mask = $allowed->{$ip};
228 $ready = ( $ipaddr & _inet_addr($mask) ) == _inet_addr($ip);
237 exit if defined $pid;
245 $SIG{CHLD} = 'DEFAULT';
248 ### if the standalone server was invoked with perl -I .. we will loose
249 ### those include dirs upon re-exec. So add them to PERL5LIB, so they
250 ### are available again for the exec'ed process --kane
252 $ENV{PERL5LIB} .= join $Config{path_sep}, @INC;
254 exec $^X . ' "' . $0 . '" ' . join( ' ', @{ $options->{argv} } );
261 my ( $self, $class, $port, $method, $uri, $protocol ) = @_;
263 # Ignore broken pipes as an HTTP server should
264 local $SIG{PIPE} = sub { close Remote };
266 local *STDIN = \*Remote;
267 local *STDOUT = \*Remote;
269 # We better be careful and just use 1.0
272 my $sockdata = $self->_socket_data( \*Remote );
273 my %copy_of_env = %ENV;
275 my $sel = IO::Select->new;
276 $sel->add( \*STDIN );
279 my ( $path, $query_string ) = split /\?/, $uri, 2;
281 # Initialize CGI environment
283 PATH_INFO => $path || '',
284 QUERY_STRING => $query_string || '',
285 REMOTE_ADDR => $sockdata->{peeraddr},
286 REMOTE_HOST => $sockdata->{peername},
287 REQUEST_METHOD => $method || '',
288 SERVER_NAME => $sockdata->{localname},
289 SERVER_PORT => $port,
290 SERVER_PROTOCOL => "HTTP/$protocol",
295 if ( $protocol >= 1 ) {
297 my $line = $self->_get_line( \*STDIN );
300 unless my ( $name, $value ) =
301 $line =~ m/\A(\w(?:-?\w+)*):\s(.+)\z/;
304 $name = 'COOKIE' if $name eq 'COOKIES';
306 $name = 'HTTP_' . $name
307 unless $name =~ m/\A(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)\z/;
308 if ( exists $ENV{$name} ) {
309 $ENV{$name} .= ", $value";
312 $ENV{$name} = $value;
317 # Pass flow control to Catalyst
318 $class->handle_request;
320 my $connection = lc $ENV{HTTP_CONNECTION};
322 unless $self->_keep_alive()
323 && index( $connection, 'keep-alive' ) > -1
324 && index( $connection, 'te' ) == -1 # opera stuff
325 && $sel->can_read(5);
328 unless ( $method, $uri, $protocol ) =
329 $self->_parse_request_line( \*STDIN );
336 my ( $self, $keepalive ) = @_;
338 my $r = $self->{_keepalive} || 0;
339 $self->{_keepalive} = $keepalive if defined $keepalive;
345 sub _parse_request_line {
346 my ( $self, $handle ) = @_;
349 my $line = $self->_get_line($handle);
351 unless my ( $method, $uri, $protocol ) =
352 $line =~ m/\A(\w+)\s+(\S+)(?:\s+HTTP\/(\d+(?:\.\d+)?))?\z/;
353 return ( $method, $uri, $protocol );
357 my ( $self, $handle ) = @_;
359 my $remote_sockaddr = getpeername($handle);
360 my ( undef, $iaddr ) = $remote_sockaddr
361 ? sockaddr_in($remote_sockaddr)
364 my $local_sockaddr = getsockname($handle);
365 my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr);
367 # This mess is necessary to keep IE from crashing the server
370 ? ( gethostbyaddr( $iaddr, AF_INET ) || 'localhost' )
373 ? ( inet_ntoa($iaddr) || '127.0.0.1' )
375 localname => gethostbyaddr( $localiaddr, AF_INET ) || 'localhost',
376 localaddr => inet_ntoa($localiaddr) || '127.0.0.1',
383 my ( $self, $handle ) = @_;
387 while ( sysread( $handle, my $byte, 1 ) ) {
388 last if $byte eq "\012"; # eol
392 1 while $line =~ s/\s\z//;
397 sub _inet_addr { unpack "N*", inet_aton( $_[0] ) }
401 L<Catalyst>, L<Catalyst::Engine>.
405 Sebastian Riedel, <sri@cpan.org>
407 Dan Kubb, <dan.kubb-cpan@onautopilot.com>
409 Sascha Kiefer, <esskar@cpan.org>
413 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
417 This program is free software, you can redistribute it and/or modify it under
418 the same terms as Perl itself.