1 package Catalyst::Engine::HTTP;
4 use base 'Catalyst::Engine::CGI';
5 use Errno 'EWOULDBLOCK';
11 require Catalyst::Engine::HTTP::Restarter;
12 require Catalyst::Engine::HTTP::Restarter::Watcher;
16 Catalyst::Engine::HTTP - Catalyst HTTP Engine
20 A script using the Catalyst::Engine::HTTP module might look like:
24 BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP' }
27 use lib '/path/to/MyApp/lib';
34 This is the Catalyst engine specialized for development and testing.
40 =item $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 $self->NEXT::finalize_headers($c);
54 =item $self->finalize_read($c)
59 my ( $self, $c ) = @_;
61 # Never ever remove this, it would result in random length output
62 # streams if STDIN eq STDOUT (like in the HTTP engine)
65 return $self->NEXT::finalize_read($c);
68 =item $self->prepare_read($c)
73 my ( $self, $c ) = @_;
75 # Set the input handle to non-blocking
78 return $self->NEXT::prepare_read($c);
81 =item $self->read_chunk($c, $buffer, $length)
89 # support for non-blocking IO
91 vec( $rin, *STDIN->fileno, 1 ) = 1;
95 select( $rin, undef, undef, undef );
96 my $rc = *STDIN->sysread(@_);
101 next READ if $! == EWOULDBLOCK;
111 # A very very simple HTTP server that initializes a CGI environment
113 my ( $self, $class, $port, $host, $options ) = @_;
118 local $SIG{CHLD} = 'IGNORE';
120 my $allowed = $options->{allowed} || { '127.0.0.1' => '255.255.255.255' };
125 $host = $host ? inet_aton($host) : INADDR_ANY;
126 socket( HTTPDaemon, PF_INET, SOCK_STREAM, getprotobyname('tcp') )
127 || die "Couldn't assign TCP socket: $!";
128 setsockopt( HTTPDaemon, SOL_SOCKET, SO_REUSEADDR, pack( "l", 1 ) )
129 || die "Couldn't set TCP socket options: $!";
130 bind( HTTPDaemon, sockaddr_in( $port, $host ) )
131 || die "Couldn't bind socket to $port on $host: $!";
132 listen( HTTPDaemon, SOMAXCONN )
133 || die "Couldn't listen to socket on $port on $host: $!";
135 if ( $host eq INADDR_ANY ) {
136 require Sys::Hostname;
137 $url .= lc Sys::Hostname::hostname();
140 $url .= gethostbyaddr( $host, AF_INET ) || inet_ntoa($host);
143 print "You can connect to your server at $url\n";
147 while ( accept( Remote, HTTPDaemon ) ) {
152 my $remote_sockaddr = getpeername( \*Remote );
153 my ( undef, $iaddr ) = sockaddr_in($remote_sockaddr);
154 my $peername = gethostbyaddr( $iaddr, AF_INET ) || "localhost";
155 my $peeraddr = inet_ntoa($iaddr) || "127.0.0.1";
156 my $local_sockaddr = getsockname( \*Remote );
157 my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr);
158 my $localname = gethostbyaddr( $localiaddr, AF_INET )
160 my $localaddr = inet_ntoa($localiaddr) || "127.0.0.1";
165 my $line = $self->_get_line( \*Remote );
167 unless my ( $method, $uri, $protocol ) =
168 $line =~ m/\A(\w+)\s+(\S+)(?:\s+HTTP\/(\d+(?:\.\d+)?))?\z/;
170 unless ( uc($method) eq 'RESTART' ) {
173 if ( $options->{fork} ) { next if $pid = fork }
175 close HTTPDaemon if defined $pid;
177 # Ignore broken pipes as an HTTP server should
178 local $SIG{PIPE} = sub { close Remote };
180 local *STDIN = \*Remote;
181 local *STDOUT = \*Remote;
183 # We better be careful and just use 1.0
186 my ( $path, $query_string ) = split /\?/, $uri, 2;
188 # Initialize CGI environment
190 PATH_INFO => $path || '',
191 QUERY_STRING => $query_string || '',
192 REMOTE_ADDR => $peeraddr,
193 REMOTE_HOST => $peername,
194 REQUEST_METHOD => $method || '',
195 SERVER_NAME => $localname,
196 SERVER_PORT => $port,
197 SERVER_PROTOCOL => "HTTP/$protocol",
202 if ( $protocol >= 1 ) {
204 my $line = $self->_get_line( \*STDIN );
207 unless my ( $name, $value ) =
208 $line =~ m/\A(\w(?:-?\w+)*):\s(.+)\z/;
211 $name = 'COOKIE' if $name eq 'COOKIES';
213 $name = 'HTTP_' . $name
214 unless $name =~ m/\A(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)\z/;
215 if ( exists $ENV{$name} ) {
216 $ENV{$name} .= "; $value";
219 $ENV{$name} = $value;
224 # Pass flow control to Catalyst
225 $class->handle_request;
228 my $ipaddr = _inet_addr($peeraddr);
230 while ( my ( $ip, $mask ) = each %$allowed and not $ready ) {
231 $ready = ( $ipaddr & _inet_addr($mask) ) == _inet_addr($ip);
239 exit if defined $pid;
247 $SIG{CHLD} = 'DEFAULT';
249 exec $^X . ' "' . $0 . '" ' . join( ' ', @{ $options->{argv} } );
256 my ( $self, $handle ) = @_;
260 while ( sysread( $handle, my $byte, 1 ) ) {
261 last if $byte eq "\012"; # eol
265 1 while $line =~ s/\s\z//;
270 sub _inet_addr { unpack "N*", inet_aton( $_[0] ) }
276 L<Catalyst>, L<Catalyst::Engine>.
280 Sebastian Riedel, <sri@cpan.org>
282 Dan Kubb, <dan.kubb-cpan@onautopilot.com>
286 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
290 This program is free software, you can redistribute it and/or modify it under
291 the same terms as Perl itself.