1 package Catalyst::Engine::HTTP;
4 use base 'Catalyst::Engine::CGI';
5 use Errno 'EWOULDBLOCK';
9 use IO::Socket::INET ();
13 require Catalyst::Engine::HTTP::Restarter;
14 require Catalyst::Engine::HTTP::Restarter::Watcher;
18 Catalyst::Engine::HTTP - Catalyst HTTP Engine
22 A script using the Catalyst::Engine::HTTP module might look like:
26 BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP' }
29 use lib '/path/to/MyApp/lib';
36 This is the Catalyst engine specialized for development and testing.
40 =head2 $self->finalize_headers($c)
44 sub finalize_headers {
45 my ( $self, $c ) = @_;
46 my $protocol = $c->request->protocol;
47 my $status = $c->response->status;
48 my $message = status_message($status);
49 print "$protocol $status $message\015\012";
50 $c->response->headers->date(time);
51 $c->response->headers->header(
52 Connection => $self->_keep_alive ? 'keep-alive' : 'close' );
53 $self->NEXT::finalize_headers($c);
56 =head2 $self->finalize_read($c)
61 my ( $self, $c ) = @_;
63 # Never ever remove this, it would result in random length output
64 # streams if STDIN eq STDOUT (like in the HTTP engine)
67 return $self->NEXT::finalize_read($c);
70 =head2 $self->prepare_read($c)
75 my ( $self, $c ) = @_;
77 # Set the input handle to non-blocking
80 return $self->NEXT::prepare_read($c);
83 =head2 $self->read_chunk($c, $buffer, $length)
91 # support for non-blocking IO
93 vec( $rin, *STDIN->fileno, 1 ) = 1;
97 select( $rin, undef, undef, undef );
98 my $rc = *STDIN->sysread(@_);
103 next READ if $! == EWOULDBLOCK;
113 # A very very simple HTTP server that initializes a CGI environment
115 my ( $self, $class, $port, $host, $options ) = @_;
119 if ($options->{background}) {
121 die "Can't fork: $!" unless defined($child);
126 local $SIG{CHLD} = 'IGNORE';
128 my $allowed = $options->{allowed} || { '127.0.0.1' => '255.255.255.255' };
129 my $addr = $host ? inet_aton($host) : INADDR_ANY;
130 if ( $addr eq INADDR_ANY ) {
131 require Sys::Hostname;
132 $host = lc Sys::Hostname::hostname();
135 $host = gethostbyaddr( $addr, AF_INET ) || inet_ntoa($addr);
141 my $daemon = IO::Socket::INET->new(
143 LocalAddr => inet_ntoa($addr),
149 or die "Couldn't create daemon: $!";
151 my $url = "http://$host";
152 $url .= ":$port" unless $port == 80;
154 print "You can connect to your server at $url\n";
156 if ($options->{background}) {
157 open STDIN, "+</dev/null" or die $!;
158 open STDOUT, ">&STDIN" or die $!;
159 open STDERR, ">&STDIN" or die $!;
160 if ( $^O !~ /MSWin32/ ) {
163 or die "Can't start a new session: $!";
167 if (my $pidfile = $options->{pidfile}) {
168 if (! open PIDFILE, "> $pidfile") {
169 warn("Cannot open: $pidfile: $!");
171 print PIDFILE "$$\n";
175 $self->_keep_alive( $options->{keepalive} || 0 );
178 while ( accept( Remote, $daemon ) )
179 { # TODO: get while ( my $remote = $daemon->accept ) to work
188 unless my ( $method, $uri, $protocol ) =
189 $self->_parse_request_line( \*Remote );
191 unless ( uc($method) eq 'RESTART' ) {
194 if ( $options->{fork} ) { next if $pid = fork }
196 $self->_handler( $class, $port, $method, $uri, $protocol );
198 $daemon->close if defined $pid;
202 my $sockdata = $self->_socket_data( \*Remote );
203 my $ipaddr = _inet_addr( $sockdata->{peeraddr} );
205 foreach my $ip ( keys %$allowed ) {
206 my $mask = $allowed->{$ip};
207 $ready = ( $ipaddr & _inet_addr($mask) ) == _inet_addr($ip);
216 exit if defined $pid;
224 $SIG{CHLD} = 'DEFAULT';
227 ### if the standalone server was invoked with perl -I .. we will loose
228 ### those include dirs upon re-exec. So add them to PERL5LIB, so they
229 ### are available again for the exec'ed process --kane
231 $ENV{PERL5LIB} .= join $Config{path_sep}, @INC;
233 exec $^X . ' "' . $0 . '" ' . join( ' ', @{ $options->{argv} } );
240 my ( $self, $class, $port, $method, $uri, $protocol ) = @_;
242 # Ignore broken pipes as an HTTP server should
243 local $SIG{PIPE} = sub { close Remote };
245 local *STDIN = \*Remote;
246 local *STDOUT = \*Remote;
248 # We better be careful and just use 1.0
251 my $sockdata = $self->_socket_data( \*Remote );
252 my %copy_of_env = %ENV;
254 my $sel = IO::Select->new;
255 $sel->add( \*STDIN );
258 my ( $path, $query_string ) = split /\?/, $uri, 2;
260 # Initialize CGI environment
262 PATH_INFO => $path || '',
263 QUERY_STRING => $query_string || '',
264 REMOTE_ADDR => $sockdata->{peeraddr},
265 REMOTE_HOST => $sockdata->{peername},
266 REQUEST_METHOD => $method || '',
267 SERVER_NAME => $sockdata->{localname},
268 SERVER_PORT => $port,
269 SERVER_PROTOCOL => "HTTP/$protocol",
274 if ( $protocol >= 1 ) {
276 my $line = $self->_get_line( \*STDIN );
279 unless my ( $name, $value ) =
280 $line =~ m/\A(\w(?:-?\w+)*):\s(.+)\z/;
283 $name = 'COOKIE' if $name eq 'COOKIES';
285 $name = 'HTTP_' . $name
286 unless $name =~ m/\A(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)\z/;
287 if ( exists $ENV{$name} ) {
288 $ENV{$name} .= "; $value";
291 $ENV{$name} = $value;
296 # Pass flow control to Catalyst
297 $class->handle_request;
299 my $connection = lc $ENV{HTTP_CONNECTION};
301 unless $self->_keep_alive()
302 && index( $connection, 'keep-alive' ) > -1
303 && index( $connection, 'te' ) == -1 # opera stuff
304 && $sel->can_read(5);
307 unless ( $method, $uri, $protocol ) =
308 $self->_parse_request_line( \*STDIN );
315 my ( $self, $keepalive ) = @_;
317 my $r = $self->{_keepalive} || 0;
318 $self->{_keepalive} = $keepalive if defined $keepalive;
324 sub _parse_request_line {
325 my ( $self, $handle ) = @_;
328 my $line = $self->_get_line($handle);
330 unless my ( $method, $uri, $protocol ) =
331 $line =~ m/\A(\w+)\s+(\S+)(?:\s+HTTP\/(\d+(?:\.\d+)?))?\z/;
332 return ( $method, $uri, $protocol );
336 my ( $self, $handle ) = @_;
340 my $remote_sockaddr = getpeername($handle);
341 ( undef, $iaddr ) = sockaddr_in($remote_sockaddr);
342 my $local_sockaddr = getsockname($handle);
343 my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr);
345 # This mess is necessary to keep IE from crashing the server
348 ? ( gethostbyaddr( $iaddr, AF_INET ) || 'localhost' )
351 ? ( inet_ntoa($iaddr) || '127.0.0.1' )
353 localname => gethostbyaddr( $localiaddr, AF_INET ) || 'localhost',
354 localaddr => inet_ntoa($localiaddr) || '127.0.0.1',
361 my ( $self, $handle ) = @_;
365 while ( sysread( $handle, my $byte, 1 ) ) {
366 last if $byte eq "\012"; # eol
370 1 while $line =~ s/\s\z//;
375 sub _inet_addr { unpack "N*", inet_aton( $_[0] ) }
379 L<Catalyst>, L<Catalyst::Engine>.
383 Sebastian Riedel, <sri@cpan.org>
385 Dan Kubb, <dan.kubb-cpan@onautopilot.com>
387 Sascha Kiefer, <esskar@cpan.org>
391 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
395 This program is free software, you can redistribute it and/or modify it under
396 the same terms as Perl itself.