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 ) = @_;
120 local $SIG{CHLD} = 'IGNORE';
122 my $allowed = $options->{allowed} || { '127.0.0.1' => '255.255.255.255' };
123 my $addr = $host ? inet_aton($host) : INADDR_ANY;
124 if ( $addr eq INADDR_ANY ) {
125 require Sys::Hostname;
126 $host = lc Sys::Hostname::hostname();
129 $host = gethostbyaddr( $addr, AF_INET ) || inet_ntoa($addr);
135 my $daemon = IO::Socket::INET->new(
137 LocalAddr => inet_ntoa($addr),
143 or die "Couldn't create daemon: $!";
145 my $url = "http://$host";
146 $url .= ":$port" unless $port == 80;
148 print "You can connect to your server at $url\n";
150 $self->_keep_alive( $options->{keepalive} || 0 );
154 while ( accept( Remote, $daemon ) )
155 { # TODO: get while ( my $remote = $daemon->accept ) to work
164 unless my ( $method, $uri, $protocol ) =
165 $self->_parse_request_line( \*Remote );
167 unless ( uc($method) eq 'RESTART' ) {
170 if ( $options->{fork} ) { next if $pid = fork }
172 $self->_handler( $class, $port, $method, $uri, $protocol );
174 $daemon->close if defined $pid;
178 my $sockdata = $self->_socket_data( \*Remote );
179 my $ipaddr = _inet_addr( $sockdata->{peeraddr} );
181 foreach my $ip ( keys %$allowed ) {
182 my $mask = $allowed->{$ip};
183 $ready = ( $ipaddr & _inet_addr($mask) ) == _inet_addr($ip);
192 exit if defined $pid;
200 $SIG{CHLD} = 'DEFAULT';
203 ### if the standalone server was invoked with perl -I .. we will loose
204 ### those include dirs upon re-exec. So add them to PERL5LIB, so they
205 ### are available again for the exec'ed process --kane
207 $ENV{PERL5LIB} .= join $Config{path_sep}, @INC;
209 exec $^X . ' "' . $0 . '" ' . join( ' ', @{ $options->{argv} } );
216 my ( $self, $class, $port, $method, $uri, $protocol ) = @_;
218 # Ignore broken pipes as an HTTP server should
219 local $SIG{PIPE} = sub { close Remote };
221 local *STDIN = \*Remote;
222 local *STDOUT = \*Remote;
224 # We better be careful and just use 1.0
227 my $sockdata = $self->_socket_data( \*Remote );
228 my %copy_of_env = %ENV;
230 my $sel = IO::Select->new;
231 $sel->add( \*STDIN );
234 my ( $path, $query_string ) = split /\?/, $uri, 2;
236 # Initialize CGI environment
238 PATH_INFO => $path || '',
239 QUERY_STRING => $query_string || '',
240 REMOTE_ADDR => $sockdata->{peeraddr},
241 REMOTE_HOST => $sockdata->{peername},
242 REQUEST_METHOD => $method || '',
243 SERVER_NAME => $sockdata->{localname},
244 SERVER_PORT => $port,
245 SERVER_PROTOCOL => "HTTP/$protocol",
250 if ( $protocol >= 1 ) {
252 my $line = $self->_get_line( \*STDIN );
255 unless my ( $name, $value ) =
256 $line =~ m/\A(\w(?:-?\w+)*):\s(.+)\z/;
259 $name = 'COOKIE' if $name eq 'COOKIES';
261 $name = 'HTTP_' . $name
262 unless $name =~ m/\A(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)\z/;
263 if ( exists $ENV{$name} ) {
264 $ENV{$name} .= "; $value";
267 $ENV{$name} = $value;
272 # Pass flow control to Catalyst
273 $class->handle_request;
275 my $connection = lc $ENV{HTTP_CONNECTION};
277 unless $self->_keep_alive()
278 && index( $connection, 'keep-alive' ) > -1
279 && index( $connection, 'te' ) == -1 # opera stuff
280 && $sel->can_read(5);
283 unless ( $method, $uri, $protocol ) =
284 $self->_parse_request_line( \*STDIN );
291 my ( $self, $keepalive ) = @_;
293 my $r = $self->{_keepalive} || 0;
294 $self->{_keepalive} = $keepalive if defined $keepalive;
300 sub _parse_request_line {
301 my ( $self, $handle ) = @_;
304 my $line = $self->_get_line($handle);
306 unless my ( $method, $uri, $protocol ) =
307 $line =~ m/\A(\w+)\s+(\S+)(?:\s+HTTP\/(\d+(?:\.\d+)?))?\z/;
308 return ( $method, $uri, $protocol );
312 my ( $self, $handle ) = @_;
314 my $remote_sockaddr = getpeername($handle);
315 my ( undef, $iaddr ) = sockaddr_in($remote_sockaddr);
316 my $local_sockaddr = getsockname($handle);
317 my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr);
320 peername => gethostbyaddr( $iaddr, AF_INET ) || "localhost",
321 peeraddr => inet_ntoa($iaddr) || "127.0.0.1",
322 localname => gethostbyaddr( $localiaddr, AF_INET ) || "localhost",
323 localaddr => inet_ntoa($localiaddr) || "127.0.0.1",
330 my ( $self, $handle ) = @_;
334 while ( sysread( $handle, my $byte, 1 ) ) {
335 last if $byte eq "\012"; # eol
339 1 while $line =~ s/\s\z//;
344 sub _inet_addr { unpack "N*", inet_aton( $_[0] ) }
348 L<Catalyst>, L<Catalyst::Engine>.
352 Sebastian Riedel, <sri@cpan.org>
354 Dan Kubb, <dan.kubb-cpan@onautopilot.com>
356 Sascha Kiefer, <esskar@cpan.org>
360 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
364 This program is free software, you can redistribute it and/or modify it under
365 the same terms as Perl itself.