1 package Catalyst::Engine::HTTP;
4 use base 'Catalyst::Engine::CGI';
5 use Errno 'EWOULDBLOCK';
12 Catalyst::Engine::HTTP - Catalyst HTTP Engine
16 A script using the Catalyst::Engine::HTTP module might look like:
20 BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP' }
23 use lib '/path/to/MyApp/lib';
30 This is the Catalyst engine specialized for development and testing.
36 =item $self->finalize_headers($c)
40 sub finalize_headers {
41 my ( $self, $c ) = @_;
42 my $protocol = $c->request->protocol;
43 my $status = $c->response->status;
44 my $message = status_message($status);
45 print "$protocol $status $message\015\012";
46 $c->response->headers->date(time);
47 $self->NEXT::finalize_headers($c);
50 =item $self->finalize_read($c)
55 my ( $self, $c ) = @_;
57 # Never ever remove this, it would result in random length output
58 # streams if STDIN eq STDOUT (like in the HTTP engine)
59 $c->request->handle->blocking(1);
61 return $self->NEXT::finalize_read($c);
64 =item $self->prepare_read($c)
69 my ( $self, $c ) = @_;
71 # Set the input handle to non-blocking
72 $c->request->handle->blocking(0);
74 return $self->NEXT::prepare_read($c);
77 =item $self->read_chunk($c, $buffer, $length)
85 # support for non-blocking IO
86 my $handle = $c->request->handle;
88 vec( $rin, $handle->fileno, 1 ) = 1;
92 select( $rin, undef, undef, undef );
93 my $rc = $handle->sysread(@_);
98 next READ if $! == EWOULDBLOCK;
108 # A very very simple HTTP server that initializes a CGI environment
110 my ( $self, $class, $port, $host, $fork ) = @_;
115 local $SIG{HUP} = sub { $GOT_HUP = 1; };
117 local $SIG{CHLD} = 'IGNORE';
122 $host = $host ? inet_aton($host) : INADDR_ANY;
123 socket( HTTPDaemon, PF_INET, SOCK_STREAM, getprotobyname('tcp') );
124 setsockopt( HTTPDaemon, SOL_SOCKET, SO_REUSEADDR, pack( "l", 1 ) );
125 bind( HTTPDaemon, sockaddr_in( $port, $host ) );
126 listen( HTTPDaemon, SOMAXCONN );
128 if ( $host eq INADDR_ANY ) {
129 require Sys::Hostname;
130 $url .= lc Sys::Hostname::hostname();
133 $url .= gethostbyaddr( $host, AF_INET ) || inet_ntoa($host);
136 print "You can connect to your server at $url\n";
138 while ( accept( Remote, HTTPDaemon ) ) {
141 if ($fork) { next if $pid = fork }
143 close HTTPDaemon if defined $pid;
145 # Ignore broken pipes as an HTTP server should
146 local $SIG{PIPE} = sub { close Remote };
147 local $SIG{HUP} = (defined $pid ? 'IGNORE' : $SIG{HUP});
149 local *STDIN = \*Remote;
150 local *STDOUT = \*Remote;
154 my $remote_sockaddr = getpeername( \*Remote );
155 my ( undef, $iaddr ) = sockaddr_in($remote_sockaddr);
156 my $peername = gethostbyaddr( $iaddr, AF_INET ) || "localhost";
157 my $peeraddr = inet_ntoa($iaddr) || "127.0.0.1";
158 my $local_sockaddr = getsockname( \*Remote );
159 my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr);
160 my $localname = gethostbyaddr( $localiaddr, AF_INET )
162 my $localaddr = inet_ntoa($localiaddr) || "127.0.0.1";
167 my $line = $self->_get_line( \*STDIN );
169 unless my ( $method, $uri, $protocol ) =
170 $line =~ m/\A(\w+)\s+(\S+)(?:\s+HTTP\/(\d+(?:\.\d+)?))?\z/;
172 # We better be careful and just use 1.0
175 my ( $path, $query_string ) = split /\?/, $uri, 2;
177 # Initialize CGI environment
179 PATH_INFO => $path || '',
180 QUERY_STRING => $query_string || '',
181 REMOTE_ADDR => $peeraddr,
182 REMOTE_HOST => $peername,
183 REQUEST_METHOD => $method || '',
184 SERVER_NAME => $localname,
185 SERVER_PORT => $port,
186 SERVER_PROTOCOL => "HTTP/$protocol",
191 if ( $protocol >= 1 ) {
193 my $line = $self->_get_line( \*STDIN );
196 unless my ( $name, $value ) =
197 $line =~ m/\A(\w(?:-?\w+)*):\s(.+)\z/;
200 $name = 'COOKIE' if $name eq 'COOKIES';
202 $name = 'HTTP_' . $name
203 unless $name =~ m/\A(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)\z/;
204 if ( exists $ENV{$name} ) {
205 $ENV{$name} .= "; $value";
208 $ENV{$name} = $value;
213 # Pass flow control to Catalyst
214 $class->handle_request;
215 exit if defined $pid;
221 exec {$0}( ( ( -x $0 ) ? () : ($^X) ), $0, @ARGV ) if $GOT_HUP;
225 my ( $self, $handle ) = @_;
229 while ( sysread( $handle, my $byte, 1 ) ) {
230 last if $byte eq "\012"; # eol
234 1 while $line =~ s/\s\z//;
243 L<Catalyst>, L<Catalyst::Engine>.
247 Sebastian Riedel, <sri@cpan.org>
249 Dan Kubb, <dan.kubb-cpan@onautopilot.com>
253 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
257 This program is free software, you can redistribute it and/or modify it under
258 the same terms as Perl itself.