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' );
54 $c->response->header( Status => $c->response->status );
56 # Avoid 'print() on closed filehandle Remote' warnings when using IE
57 print $c->response->headers->as_string("\015\012") if *STDOUT->opened();
58 print "\015\012" if *STDOUT->opened();
61 =head2 $self->finalize_read($c)
66 my ( $self, $c ) = @_;
68 # Never ever remove this, it would result in random length output
69 # streams if STDIN eq STDOUT (like in the HTTP engine)
72 return $self->NEXT::finalize_read($c);
75 =head2 $self->prepare_read($c)
80 my ( $self, $c ) = @_;
82 # Set the input handle to non-blocking
85 return $self->NEXT::prepare_read($c);
88 =head2 $self->read_chunk($c, $buffer, $length)
96 # support for non-blocking IO
98 vec( $rin, *STDIN->fileno, 1 ) = 1;
102 select( $rin, undef, undef, undef );
103 my $rc = *STDIN->sysread(@_);
108 next READ if $! == EWOULDBLOCK;
114 =head2 $self->write($c, $buffer)
116 Writes the buffer to the client. Can only be called once for a request.
121 # Avoid 'print() on closed filehandle Remote' warnings when using IE
122 return unless *STDOUT->opened();
124 shift->NEXT::write( @_ );
131 # A very very simple HTTP server that initializes a CGI environment
133 my ( $self, $class, $port, $host, $options ) = @_;
137 if ($options->{background}) {
139 die "Can't fork: $!" unless defined($child);
144 local $SIG{CHLD} = 'IGNORE';
146 my $allowed = $options->{allowed} || { '127.0.0.1' => '255.255.255.255' };
147 my $addr = $host ? inet_aton($host) : INADDR_ANY;
148 if ( $addr eq INADDR_ANY ) {
149 require Sys::Hostname;
150 $host = lc Sys::Hostname::hostname();
153 $host = gethostbyaddr( $addr, AF_INET ) || inet_ntoa($addr);
159 my $daemon = IO::Socket::INET->new(
161 LocalAddr => inet_ntoa($addr),
167 or die "Couldn't create daemon: $!";
169 my $url = "http://$host";
170 $url .= ":$port" unless $port == 80;
172 print "You can connect to your server at $url\n";
174 if ($options->{background}) {
175 open STDIN, "+</dev/null" or die $!;
176 open STDOUT, ">&STDIN" or die $!;
177 open STDERR, ">&STDIN" or die $!;
178 if ( $^O !~ /MSWin32/ ) {
181 or die "Can't start a new session: $!";
185 if (my $pidfile = $options->{pidfile}) {
186 if (! open PIDFILE, "> $pidfile") {
187 warn("Cannot open: $pidfile: $!");
189 print PIDFILE "$$\n";
193 $self->_keep_alive( $options->{keepalive} || 0 );
196 while ( accept( Remote, $daemon ) )
197 { # TODO: get while ( my $remote = $daemon->accept ) to work
206 unless my ( $method, $uri, $protocol ) =
207 $self->_parse_request_line( \*Remote );
209 unless ( uc($method) eq 'RESTART' ) {
212 if ( $options->{fork} ) { next if $pid = fork }
214 $self->_handler( $class, $port, $method, $uri, $protocol );
216 $daemon->close if defined $pid;
220 my $sockdata = $self->_socket_data( \*Remote );
221 my $ipaddr = _inet_addr( $sockdata->{peeraddr} );
223 foreach my $ip ( keys %$allowed ) {
224 my $mask = $allowed->{$ip};
225 $ready = ( $ipaddr & _inet_addr($mask) ) == _inet_addr($ip);
234 exit if defined $pid;
242 $SIG{CHLD} = 'DEFAULT';
245 ### if the standalone server was invoked with perl -I .. we will loose
246 ### those include dirs upon re-exec. So add them to PERL5LIB, so they
247 ### are available again for the exec'ed process --kane
249 $ENV{PERL5LIB} .= join $Config{path_sep}, @INC;
251 exec $^X . ' "' . $0 . '" ' . join( ' ', @{ $options->{argv} } );
258 my ( $self, $class, $port, $method, $uri, $protocol ) = @_;
260 # Ignore broken pipes as an HTTP server should
261 local $SIG{PIPE} = sub { close Remote };
263 local *STDIN = \*Remote;
264 local *STDOUT = \*Remote;
266 # We better be careful and just use 1.0
269 my $sockdata = $self->_socket_data( \*Remote );
270 my %copy_of_env = %ENV;
272 my $sel = IO::Select->new;
273 $sel->add( \*STDIN );
276 my ( $path, $query_string ) = split /\?/, $uri, 2;
278 # Initialize CGI environment
280 PATH_INFO => $path || '',
281 QUERY_STRING => $query_string || '',
282 REMOTE_ADDR => $sockdata->{peeraddr},
283 REMOTE_HOST => $sockdata->{peername},
284 REQUEST_METHOD => $method || '',
285 SERVER_NAME => $sockdata->{localname},
286 SERVER_PORT => $port,
287 SERVER_PROTOCOL => "HTTP/$protocol",
292 if ( $protocol >= 1 ) {
294 my $line = $self->_get_line( \*STDIN );
297 unless my ( $name, $value ) =
298 $line =~ m/\A(\w(?:-?\w+)*):\s(.+)\z/;
301 $name = 'COOKIE' if $name eq 'COOKIES';
303 $name = 'HTTP_' . $name
304 unless $name =~ m/\A(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)\z/;
305 if ( exists $ENV{$name} ) {
306 $ENV{$name} .= ", $value";
309 $ENV{$name} = $value;
314 # Pass flow control to Catalyst
315 $class->handle_request;
317 my $connection = lc $ENV{HTTP_CONNECTION};
319 unless $self->_keep_alive()
320 && index( $connection, 'keep-alive' ) > -1
321 && index( $connection, 'te' ) == -1 # opera stuff
322 && $sel->can_read(5);
325 unless ( $method, $uri, $protocol ) =
326 $self->_parse_request_line( \*STDIN );
333 my ( $self, $keepalive ) = @_;
335 my $r = $self->{_keepalive} || 0;
336 $self->{_keepalive} = $keepalive if defined $keepalive;
342 sub _parse_request_line {
343 my ( $self, $handle ) = @_;
346 my $line = $self->_get_line($handle);
348 unless my ( $method, $uri, $protocol ) =
349 $line =~ m/\A(\w+)\s+(\S+)(?:\s+HTTP\/(\d+(?:\.\d+)?))?\z/;
350 return ( $method, $uri, $protocol );
354 my ( $self, $handle ) = @_;
356 my $remote_sockaddr = getpeername($handle);
357 my ( undef, $iaddr ) = $remote_sockaddr
358 ? sockaddr_in($remote_sockaddr)
361 my $local_sockaddr = getsockname($handle);
362 my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr);
364 # This mess is necessary to keep IE from crashing the server
367 ? ( gethostbyaddr( $iaddr, AF_INET ) || 'localhost' )
370 ? ( inet_ntoa($iaddr) || '127.0.0.1' )
372 localname => gethostbyaddr( $localiaddr, AF_INET ) || 'localhost',
373 localaddr => inet_ntoa($localiaddr) || '127.0.0.1',
380 my ( $self, $handle ) = @_;
384 while ( sysread( $handle, my $byte, 1 ) ) {
385 last if $byte eq "\012"; # eol
389 1 while $line =~ s/\s\z//;
394 sub _inet_addr { unpack "N*", inet_aton( $_[0] ) }
398 L<Catalyst>, L<Catalyst::Engine>.
402 Sebastian Riedel, <sri@cpan.org>
404 Dan Kubb, <dan.kubb-cpan@onautopilot.com>
406 Sascha Kiefer, <esskar@cpan.org>
410 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
414 This program is free software, you can redistribute it and/or modify it under
415 the same terms as Perl itself.