Cache the IP address => hostname lookups which could be performed multiple times...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / HTTP.pm
CommitLineData
ca61af20 1package Catalyst::Engine::HTTP;
45374ac6 2
7fa2c9c1 3use Moose;
4extends 'Catalyst::Engine::CGI';
0fc2d522 5
4bb8bd62 6use Data::Dump qw(dump);
fbcc39ad 7use Errno 'EWOULDBLOCK';
055ff026 8use HTTP::Date ();
06744540 9use HTTP::Headers;
fbcc39ad 10use HTTP::Status;
fbcc39ad 11use Socket;
6c7a1d2f 12use IO::Socket::INET ();
b5ecfcf0 13use IO::Select ();
45374ac6 14
333123ef 15use constant CHUNKSIZE => 64 * 1024;
16use constant DEBUG => $ENV{CATALYST_HTTP_DEBUG} || 0;
4bb8bd62 17
aa17df21 18use namespace::clean -except => 'meta';
19
02570318 20has options => ( is => 'rw' );
21has _keepalive => ( is => 'rw', predicate => '_is_keepalive', clearer => '_clear_keepalive' );
22has _write_error => ( is => 'rw', predicate => '_has_write_error' );
23
11c270bd 24# Refactoring note - could/should Eliminate all instances of $self->{inputbuf},
25# which I haven't touched as it is used as an lvalue in a lot of places, and I guess
26# doing it differently could be expensive.. Feel free to refactor and NYTProf :)
27
45374ac6 28=head1 NAME
29
ca61af20 30Catalyst::Engine::HTTP - Catalyst HTTP Engine
45374ac6 31
32=head1 SYNOPSIS
33
ca61af20 34A script using the Catalyst::Engine::HTTP module might look like:
45374ac6 35
36 #!/usr/bin/perl -w
37
ca61af20 38 BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP' }
45374ac6 39
40 use strict;
41 use lib '/path/to/MyApp/lib';
42 use MyApp;
43
44 MyApp->run;
45
46=head1 DESCRIPTION
47
48This is the Catalyst engine specialized for development and testing.
49
fbcc39ad 50=head1 METHODS
51
b5ecfcf0 52=head2 $self->finalize_headers($c)
fbcc39ad 53
54=cut
55
56sub finalize_headers {
57 my ( $self, $c ) = @_;
58 my $protocol = $c->request->protocol;
59 my $status = $c->response->status;
60 my $message = status_message($status);
7fa2c9c1 61 my $res_headers = $c->response->headers;
62
06744540 63 my @headers;
64 push @headers, "$protocol $status $message";
7fa2c9c1 65
66 $res_headers->header( Date => HTTP::Date::time2str(time) );
67 $res_headers->header( Status => $status );
68
7f3c5736 69 # Should we keep the connection open?
70 my $connection = $c->request->header('Connection');
b0ad47c1 71 if ( $self->options->{keepalive}
72 && $connection
7f3c5736 73 && $connection =~ /^keep-alive$/i
74 ) {
7fa2c9c1 75 $res_headers->header( Connection => 'keep-alive' );
02570318 76 $self->_keepalive(1);
7f3c5736 77 }
78 else {
7fa2c9c1 79 $res_headers->header( Connection => 'close' );
7f3c5736 80 }
7fa2c9c1 81
82 push @headers, $res_headers->as_string("\x0D\x0A");
83
06744540 84 # Buffer the headers so they are sent with the first write() call
85 # This reduces the number of TCP packets we are sending
02570318 86 $self->_header_buf( join("\x0D\x0A", @headers, '') );
fbcc39ad 87}
88
b5ecfcf0 89=head2 $self->finalize_read($c)
fbcc39ad 90
91=cut
92
1c6a35b1 93before finalize_read => sub {
fbcc39ad 94 # Never ever remove this, it would result in random length output
95 # streams if STDIN eq STDOUT (like in the HTTP engine)
4f5ebacd 96 *STDIN->blocking(1);
4090e3bb 97};
fbcc39ad 98
b5ecfcf0 99=head2 $self->prepare_read($c)
fbcc39ad 100
101=cut
102
1c6a35b1 103before prepare_read => sub {
fbcc39ad 104 # Set the input handle to non-blocking
4f5ebacd 105 *STDIN->blocking(0);
4090e3bb 106};
fbcc39ad 107
b5ecfcf0 108=head2 $self->read_chunk($c, $buffer, $length)
fbcc39ad 109
110=cut
111
112sub read_chunk {
113 my $self = shift;
114 my $c = shift;
b0ad47c1 115
4bb8bd62 116 # If we have any remaining data in the input buffer, send it back first
117 if ( $_[0] = delete $self->{inputbuf} ) {
118 my $read = length( $_[0] );
119 DEBUG && warn "read_chunk: Read $read bytes from previous input buffer\n";
120 return $read;
121 }
fbcc39ad 122
123 # support for non-blocking IO
4f5ebacd 124 my $rin = '';
125 vec( $rin, *STDIN->fileno, 1 ) = 1;
fbcc39ad 126
127 READ:
128 {
129 select( $rin, undef, undef, undef );
4f5ebacd 130 my $rc = *STDIN->sysread(@_);
fbcc39ad 131 if ( defined $rc ) {
4bb8bd62 132 DEBUG && warn "read_chunk: Read $rc bytes from socket\n";
fbcc39ad 133 return $rc;
134 }
135 else {
136 next READ if $! == EWOULDBLOCK;
137 return;
138 }
139 }
140}
141
00c99324 142=head2 $self->write($c, $buffer)
143
e512dd24 144Writes the buffer to the client.
00c99324 145
146=cut
147
4090e3bb 148around write => sub {
149 my $orig = shift;
4bb8bd62 150 my ( $self, $c, $buffer ) = @_;
7fa2c9c1 151
85d9fce6 152 # Avoid 'print() on closed filehandle Remote' warnings when using IE
153 return unless *STDOUT->opened();
154
85d9fce6 155 # Prepend the headers if they have not yet been sent
02570318 156 if ( $self->_has_header_buf ) {
eb511a78 157 $self->_warn_on_write_error(
26901354 158 $self->$orig($c, $self->_clear_header_buf)
eb511a78 159 );
4bb8bd62 160 }
7fa2c9c1 161
eb511a78 162 $self->_warn_on_write_error($self->$orig($c, $buffer));
163};
7fa2c9c1 164
eb511a78 165sub _warn_on_write_error {
166 my ($self, $ret) = @_;
e512dd24 167 if ( !defined $ret ) {
02570318 168 $self->_write_error($!);
e2b0ddd3 169 DEBUG && warn "write: Failed to write response ($!)\n";
4bb8bd62 170 }
9f3ebd8a 171 else {
172 DEBUG && warn "write: Wrote response ($ret bytes)\n";
173 }
4bb8bd62 174 return $ret;
eb511a78 175}
00c99324 176
b5ecfcf0 177=head2 run
fbcc39ad 178
179=cut
180
181# A very very simple HTTP server that initializes a CGI environment
182sub run {
37553dc8 183 my ( $self, $class, $port, $host, $options ) = @_;
fbcc39ad 184
4eeca0f2 185 $options ||= {};
63182b08 186
02570318 187 $self->options($options);
1cf1c56a 188
e1576f62 189 if ($options->{background}) {
190 my $child = fork;
191 die "Can't fork: $!" unless defined($child);
44c6d25a 192 return $child if $child;
e1576f62 193 }
194
57a87bb3 195 my $restart = 0;
6a5aa41c 196 local $SIG{CHLD} = 'IGNORE';
fbcc39ad 197
1cf1c56a 198 my $allowed = $options->{allowed} || { '127.0.0.1' => '255.255.255.255' };
6c7a1d2f 199 my $addr = $host ? inet_aton($host) : INADDR_ANY;
200 if ( $addr eq INADDR_ANY ) {
fbcc39ad 201 require Sys::Hostname;
6c7a1d2f 202 $host = lc Sys::Hostname::hostname();
fbcc39ad 203 }
204 else {
6c7a1d2f 205 $host = gethostbyaddr( $addr, AF_INET ) || inet_ntoa($addr);
fbcc39ad 206 }
6c7a1d2f 207
208 # Handle requests
209
210 # Setup socket
211 my $daemon = IO::Socket::INET->new(
212 Listen => SOMAXCONN,
213 LocalAddr => inet_ntoa($addr),
214 LocalPort => $port,
215 Proto => 'tcp',
216 ReuseAddr => 1,
217 Type => SOCK_STREAM,
218 )
41e1a0b0 219 or die "Couldn't create daemon: $@";
6c7a1d2f 220
63182b08 221 $port = $daemon->sockport();
222
6c7a1d2f 223 my $url = "http://$host";
224 $url .= ":$port" unless $port == 80;
225
fbcc39ad 226 print "You can connect to your server at $url\n";
fbcc39ad 227
e1576f62 228 if ($options->{background}) {
229 open STDIN, "+</dev/null" or die $!;
230 open STDOUT, ">&STDIN" or die $!;
231 open STDERR, ">&STDIN" or die $!;
232 if ( $^O !~ /MSWin32/ ) {
233 require POSIX;
234 POSIX::setsid()
235 or die "Can't start a new session: $!";
236 }
237 }
238
239 if (my $pidfile = $options->{pidfile}) {
240 if (! open PIDFILE, "> $pidfile") {
241 warn("Cannot open: $pidfile: $!");
242 }
243 print PIDFILE "$$\n";
244 close PIDFILE;
245 }
246
4bb8bd62 247 my $pid = undef;
7fa2c9c1 248
4bb8bd62 249 # Ignore broken pipes as an HTTP server should
250 local $SIG{PIPE} = 'IGNORE';
7fa2c9c1 251
b095458a 252 # Restart on HUP
7fa2c9c1 253 local $SIG{HUP} = sub {
b095458a 254 $restart = 1;
255 warn "Restarting server on SIGHUP...\n";
256 };
7fa2c9c1 257
4bb8bd62 258 LISTEN:
259 while ( !$restart ) {
7fa2c9c1 260 while ( accept( Remote, $daemon ) ) {
4bb8bd62 261 DEBUG && warn "New connection\n";
fbcc39ad 262
4bb8bd62 263 select Remote;
fbcc39ad 264
4bb8bd62 265 Remote->blocking(1);
7fa2c9c1 266
7f3c5736 267 # Read until we see all headers
4bb8bd62 268 $self->{inputbuf} = '';
7fa2c9c1 269
7f3c5736 270 if ( !$self->_read_headers ) {
271 # Error reading, give up
059c085b 272 close Remote;
7f3c5736 273 next LISTEN;
4bb8bd62 274 }
fbcc39ad 275
4bb8bd62 276 my ( $method, $uri, $protocol ) = $self->_parse_request_line;
7fa2c9c1 277
4bb8bd62 278 DEBUG && warn "Parsed request: $method $uri $protocol\n";
7fa2c9c1 279 next unless $method;
57a87bb3 280
4bb8bd62 281 unless ( uc($method) eq 'RESTART' ) {
57a87bb3 282
4bb8bd62 283 # Fork
7fa2c9c1 284 if ( $options->{fork} ) {
1b45d7e5 285 if ( $pid = fork ) {
286 DEBUG && warn "Forked child $pid\n";
287 next;
288 }
289 }
6c7a1d2f 290
4bb8bd62 291 $self->_handler( $class, $port, $method, $uri, $protocol );
63182b08 292
02570318 293 if ( $self->_has_write_error ) {
4bb8bd62 294 close Remote;
63182b08 295
1b45d7e5 296 if ( !defined $pid ) {
297 next LISTEN;
298 }
4bb8bd62 299 }
fbcc39ad 300
1b45d7e5 301 if ( defined $pid ) {
302 # Child process, close connection and exit
303 DEBUG && warn "Child process exiting\n";
304 $daemon->close;
305 exit;
306 }
1cf1c56a 307 }
4bb8bd62 308 else {
309 my $sockdata = $self->_socket_data( \*Remote );
310 my $ipaddr = _inet_addr( $sockdata->{peeraddr} );
311 my $ready = 0;
312 foreach my $ip ( keys %$allowed ) {
313 my $mask = $allowed->{$ip};
314 $ready = ( $ipaddr & _inet_addr($mask) ) == _inet_addr($ip);
315 last if $ready;
316 }
317 if ($ready) {
318 $restart = 1;
319 last;
320 }
1cf1c56a 321 }
4bb8bd62 322 }
323 continue {
324 close Remote;
325 }
fbcc39ad 326 }
63182b08 327
6c7a1d2f 328 $daemon->close;
63182b08 329
4bb8bd62 330 DEBUG && warn "Shutting down\n";
37553dc8 331
57a87bb3 332 if ($restart) {
60c38e3e 333 $SIG{CHLD} = 'DEFAULT';
6844bc1c 334 wait;
e37e3977 335
336 ### if the standalone server was invoked with perl -I .. we will loose
337 ### those include dirs upon re-exec. So add them to PERL5LIB, so they
338 ### are available again for the exec'ed process --kane
339 use Config;
63182b08 340 $ENV{PERL5LIB} .= join $Config{path_sep}, @INC;
341
30b70903 342 exec $^X, $0, @{ $options->{argv} || [] };
60c38e3e 343 }
57a87bb3 344
345 exit;
fbcc39ad 346}
347
6c7a1d2f 348sub _handler {
349 my ( $self, $class, $port, $method, $uri, $protocol ) = @_;
350
6c7a1d2f 351 local *STDIN = \*Remote;
352 local *STDOUT = \*Remote;
353
354 # We better be careful and just use 1.0
355 $protocol = '1.0';
356
357 my $sockdata = $self->_socket_data( \*Remote );
358 my %copy_of_env = %ENV;
359
360 my $sel = IO::Select->new;
361 $sel->add( \*STDIN );
b0ad47c1 362
3bcb3aae 363 REQUEST:
683762ca 364 while (1) {
365 my ( $path, $query_string ) = split /\?/, $uri, 2;
b0ad47c1 366
5e2186c5 367 # URI is not the same as path. Remove scheme, domain name and port from it
368 $path =~ s{^https?://[^/?#]+}{};
369
7f3c5736 370 # Initialize CGI environment
371 local %ENV = (
372 PATH_INFO => $path || '',
373 QUERY_STRING => $query_string || '',
374 REMOTE_ADDR => $sockdata->{peeraddr},
7f3c5736 375 REQUEST_METHOD => $method || '',
376 SERVER_NAME => $sockdata->{localname},
377 SERVER_PORT => $port,
378 SERVER_PROTOCOL => "HTTP/$protocol",
379 %copy_of_env,
380 );
cf26c39c 381
7f3c5736 382 # Parse headers
383 if ( $protocol >= 1 ) {
384 $self->_parse_headers;
385 }
6c7a1d2f 386
7f3c5736 387 # Pass flow control to Catalyst
60f8e66a 388 {
389 # FIXME: don't ignore SIGCHLD while handling requests so system()
390 # et al. work within actions. it might be a little risky to do that
391 # this far out, but then again it's only the dev server anyway.
392 local $SIG{CHLD} = 'DEFAULT';
393
8317b3d1 394 $class->handle_request( env => \%ENV );
60f8e66a 395 }
b0ad47c1 396
7f3c5736 397 DEBUG && warn "Request done\n";
b0ad47c1 398
7f3c5736 399 # Allow keepalive requests, this is a hack but we'll support it until
400 # the next major release.
02570318 401 if ( $self->_is_keepalive ) {
402 $self->_clear_keepalive;
b0ad47c1 403
7f3c5736 404 DEBUG && warn "Reusing previous connection for keep-alive request\n";
b0ad47c1 405
406 if ( $sel->can_read(1) ) {
7f3c5736 407 if ( !$self->_read_headers ) {
408 # Error reading, give up
409 last REQUEST;
410 }
411
412 ( $method, $uri, $protocol ) = $self->_parse_request_line;
b0ad47c1 413
7f3c5736 414 DEBUG && warn "Parsed request: $method $uri $protocol\n";
b0ad47c1 415
7f3c5736 416 # Force HTTP/1.0
417 $protocol = '1.0';
b0ad47c1 418
7f3c5736 419 next REQUEST;
420 }
b0ad47c1 421
7f3c5736 422 DEBUG && warn "No keep-alive request within 1 second\n";
423 }
b0ad47c1 424
7f3c5736 425 last REQUEST;
426 }
b0ad47c1 427
7f3c5736 428 DEBUG && warn "Closing connection\n";
06744540 429
430 close Remote;
3bcb3aae 431}
432
7f3c5736 433sub _read_headers {
434 my $self = shift;
7fa2c9c1 435
7f3c5736 436 while (1) {
437 my $read = sysread Remote, my $buf, CHUNKSIZE;
7fa2c9c1 438
059c085b 439 if ( !defined $read ) {
440 next if $! == EWOULDBLOCK;
441 DEBUG && warn "Error reading headers: $!\n";
442 return;
7fa2c9c1 443 } elsif ( $read == 0 ) {
059c085b 444 DEBUG && warn "EOF\n";
7f3c5736 445 return;
446 }
7fa2c9c1 447
7f3c5736 448 DEBUG && warn "Read $read bytes\n";
449 $self->{inputbuf} .= $buf;
450 last if $self->{inputbuf} =~ /(\x0D\x0A?\x0D\x0A?|\x0A\x0D?\x0A\x0D?)/s;
451 }
7fa2c9c1 452
7f3c5736 453 return 1;
454}
455
4bb8bd62 456sub _parse_request_line {
457 my $self = shift;
6c7a1d2f 458
7fa2c9c1 459 # Parse request line
dd3ae38d 460 # Leading CRLF sometimes sent by buggy IE versions
461 if ( $self->{inputbuf} !~ s/^(?:\x0D\x0A)?(\w+)[ \t]+(\S+)(?:[ \t]+(HTTP\/\d+\.\d+))?[^\012]*\012// ) {
4bb8bd62 462 return ();
463 }
7fa2c9c1 464
4bb8bd62 465 my $method = $1;
466 my $uri = $2;
467 my $proto = $3 || 'HTTP/0.9';
7fa2c9c1 468
4bb8bd62 469 return ( $method, $uri, $proto );
6c7a1d2f 470}
471
4bb8bd62 472sub _parse_headers {
473 my $self = shift;
7fa2c9c1 474
4bb8bd62 475 # Copy the buffer for header parsing, and remove the header block
476 # from the content buffer.
477 my $buf = $self->{inputbuf};
478 $self->{inputbuf} =~ s/.*?(\x0D\x0A?\x0D\x0A?|\x0A\x0D?\x0A\x0D?)//s;
7fa2c9c1 479
4bb8bd62 480 # Parse headers
481 my $headers = HTTP::Headers->new;
482 my ($key, $val);
483 HEADER:
484 while ( $buf =~ s/^([^\012]*)\012// ) {
485 $_ = $1;
486 s/\015$//;
487 if ( /^([\w\-~]+)\s*:\s*(.*)/ ) {
488 $headers->push_header( $key, $val ) if $key;
489 ($key, $val) = ($1, $2);
490 }
491 elsif ( /^\s+(.*)/ ) {
492 $val .= " $1";
493 }
494 else {
495 last HEADER;
496 }
497 }
498 $headers->push_header( $key, $val ) if $key;
b0ad47c1 499
4bb8bd62 500 DEBUG && warn "Parsed headers: " . dump($headers) . "\n";
6c7a1d2f 501
4bb8bd62 502 # Convert headers into ENV vars
503 $headers->scan( sub {
504 my ( $key, $val ) = @_;
b0ad47c1 505
4bb8bd62 506 $key = uc $key;
507 $key = 'COOKIE' if $key eq 'COOKIES';
508 $key =~ tr/-/_/;
509 $key = 'HTTP_' . $key
510 unless $key =~ m/\A(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)\z/;
b0ad47c1 511
4bb8bd62 512 if ( exists $ENV{$key} ) {
513 $ENV{$key} .= ", $val";
514 }
515 else {
516 $ENV{$key} = $val;
517 }
518 } );
6c7a1d2f 519}
520
521sub _socket_data {
522 my ( $self, $handle ) = @_;
523
8b9d0298 524 my $remote_sockaddr = getpeername($handle);
b0ad47c1 525 my ( undef, $iaddr ) = $remote_sockaddr
526 ? sockaddr_in($remote_sockaddr)
3150d774 527 : (undef, undef);
b0ad47c1 528
8b9d0298 529 my $local_sockaddr = getsockname($handle);
6c7a1d2f 530 my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr);
531
8b9d0298 532 # This mess is necessary to keep IE from crashing the server
6c7a1d2f 533 my $data = {
b0ad47c1 534 peeraddr => $iaddr
8b9d0298 535 ? ( inet_ntoa($iaddr) || '127.0.0.1' )
536 : '127.0.0.1',
13e46699 537 localname => _gethostbyaddr( $localiaddr ),
99d93ba8 538 localaddr => inet_ntoa($localiaddr) || '127.0.0.1',
6c7a1d2f 539 };
540
541 return $data;
542}
543
13e46699 544{ # If you have a crappy DNS server then these can be slow, so cache 'em
545 my %hostname_cache;
546 sub _gethostbyaddr {
547 my $ip = shift;
548 $hostname_cache{$ip} ||= gethostbyaddr( $ip, AF_INET ) || 'localhost';
549 }
550}
551
1cf1c56a 552sub _inet_addr { unpack "N*", inet_aton( $_[0] ) }
553
02570318 554=head2 options
555
556Options hash passed to the http engine to control things like if keepalive
557is supported.
558
45374ac6 559=head1 SEE ALSO
560
2f381252 561L<Catalyst>, L<Catalyst::Engine>
fbcc39ad 562
563=head1 AUTHORS
564
2f381252 565Catalyst Contributors, see Catalyst.pm
4bb8bd62 566
fbcc39ad 567=head1 THANKS
45374ac6 568
fbcc39ad 569Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
45374ac6 570
571=head1 COPYRIGHT
572
536bee89 573This library is free software. You can redistribute it and/or modify it under
45374ac6 574the same terms as Perl itself.
575
576=cut
577
45374ac6 5781;