1 package Catalyst::Engine::HTTP;
4 use base 'Catalyst::Engine::CGI';
5 use Errno 'EWOULDBLOCK';
15 Catalyst::Engine::HTTP - Catalyst HTTP Engine
19 A script using the Catalyst::Engine::HTTP module might look like:
23 BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP' }
26 use lib '/path/to/MyApp/lib';
33 This is the Catalyst engine specialized for development and testing.
39 =item $self->finalize_headers($c)
43 sub finalize_headers {
44 my ( $self, $c ) = @_;
45 my $protocol = $c->request->protocol;
46 my $status = $c->response->status;
47 my $message = status_message($status);
48 print "$protocol $status $message\015\012";
49 $c->response->headers->date(time);
50 $self->NEXT::finalize_headers($c);
53 =item $self->finalize_read($c)
58 my ( $self, $c ) = @_;
60 # Never ever remove this, it would result in random length output
61 # streams if STDIN eq STDOUT (like in the HTTP engine)
64 return $self->NEXT::finalize_read($c);
67 =item $self->prepare_read($c)
72 my ( $self, $c ) = @_;
74 # Set the input handle to non-blocking
77 return $self->NEXT::prepare_read($c);
80 =item $self->read_chunk($c, $buffer, $length)
88 # support for non-blocking IO
90 vec( $rin, *STDIN->fileno, 1 ) = 1;
94 select( $rin, undef, undef, undef );
95 my $rc = *STDIN->sysread(@_);
100 next READ if $! == EWOULDBLOCK;
110 # A very very simple HTTP server that initializes a CGI environment
112 my ( $self, $class, $port, $host, $options ) = @_;
117 local $SIG{HUP} = sub { $GOT_HUP = 1; };
118 local $SIG{CHLD} = 'IGNORE';
122 if ( $options->{restart} ) {
124 unless ( $restarter = fork ) {
126 # Index parent directory
127 my $dir = File::Spec->catdir( $FindBin::Bin, '..' );
129 my $regex = $options->{restart_regex};
130 my $one = _index( $dir, $regex );
132 sleep $options->{restart_delay};
133 my $two = _index( $dir, $regex );
134 if ( my $file = _compare_index( $one, $two ) ) {
135 print STDERR qq/File "$file" modified, restarting\n/;
146 $host = $host ? inet_aton($host) : INADDR_ANY;
147 socket( HTTPDaemon, PF_INET, SOCK_STREAM, getprotobyname('tcp') );
148 setsockopt( HTTPDaemon, SOL_SOCKET, SO_REUSEADDR, pack( "l", 1 ) );
149 bind( HTTPDaemon, sockaddr_in( $port, $host ) );
150 listen( HTTPDaemon, SOMAXCONN );
152 if ( $host eq INADDR_ANY ) {
153 require Sys::Hostname;
154 $url .= lc Sys::Hostname::hostname();
157 $url .= gethostbyaddr( $host, AF_INET ) || inet_ntoa($host);
160 print "You can connect to your server at $url\n";
162 while ( accept( Remote, HTTPDaemon ) ) {
165 if ( $options->{fork} ) { next if $pid = fork }
167 close HTTPDaemon if defined $pid;
169 # Ignore broken pipes as an HTTP server should
170 local $SIG{PIPE} = sub { close Remote };
171 local $SIG{HUP} = ( defined $pid ? 'IGNORE' : $SIG{HUP} );
173 local *STDIN = \*Remote;
174 local *STDOUT = \*Remote;
178 my $remote_sockaddr = getpeername( \*Remote );
179 my ( undef, $iaddr ) = sockaddr_in($remote_sockaddr);
180 my $peername = gethostbyaddr( $iaddr, AF_INET ) || "localhost";
181 my $peeraddr = inet_ntoa($iaddr) || "127.0.0.1";
182 my $local_sockaddr = getsockname( \*Remote );
183 my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr);
184 my $localname = gethostbyaddr( $localiaddr, AF_INET )
186 my $localaddr = inet_ntoa($localiaddr) || "127.0.0.1";
191 my $line = $self->_get_line( \*STDIN );
193 unless my ( $method, $uri, $protocol ) =
194 $line =~ m/\A(\w+)\s+(\S+)(?:\s+HTTP\/(\d+(?:\.\d+)?))?\z/;
196 # We better be careful and just use 1.0
199 my ( $path, $query_string ) = split /\?/, $uri, 2;
201 # Initialize CGI environment
203 PATH_INFO => $path || '',
204 QUERY_STRING => $query_string || '',
205 REMOTE_ADDR => $peeraddr,
206 REMOTE_HOST => $peername,
207 REQUEST_METHOD => $method || '',
208 SERVER_NAME => $localname,
209 SERVER_PORT => $port,
210 SERVER_PROTOCOL => "HTTP/$protocol",
215 if ( $protocol >= 1 ) {
217 my $line = $self->_get_line( \*STDIN );
220 unless my ( $name, $value ) =
221 $line =~ m/\A(\w(?:-?\w+)*):\s(.+)\z/;
224 $name = 'COOKIE' if $name eq 'COOKIES';
226 $name = 'HTTP_' . $name
227 unless $name =~ m/\A(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)\z/;
228 if ( exists $ENV{$name} ) {
229 $ENV{$name} .= "; $value";
232 $ENV{$name} = $value;
237 # Pass flow control to Catalyst
238 $class->handle_request;
239 exit if defined $pid;
247 $SIG{CHLD} = 'DEFAULT';
248 exec {$0}( ( ( -x $0 ) ? () : ($^X) ), $0, @ARGV );
253 my ( $one, $two ) = @_;
255 while ( my ( $key, $val ) = each %$one ) {
256 return $key if ( !$clone{$key} || ( $clone{$key} ne $val ) );
260 return join ' ', keys %clone;
266 my ( $self, $handle ) = @_;
270 while ( sysread( $handle, my $byte, 1 ) ) {
271 last if $byte eq "\012"; # eol
275 1 while $line =~ s/\s\z//;
281 my ( $dir, $regex ) = @_;
286 my $file = File::Spec->rel2abs($File::Find::name);
287 return unless $file =~ /$regex/;
288 return unless -f $file;
289 my $time = ( stat $file )[9];
290 $index{$file} = $time;
303 L<Catalyst>, L<Catalyst::Engine>.
307 Sebastian Riedel, <sri@cpan.org>
309 Dan Kubb, <dan.kubb-cpan@onautopilot.com>
313 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
317 This program is free software, you can redistribute it and/or modify it under
318 the same terms as Perl itself.