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)
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
74 return $self->NEXT::prepare_read($c);
77 =item $self->read_chunk($c, $buffer, $length)
85 # support for non-blocking IO
87 vec( $rin, *STDIN->fileno, 1 ) = 1;
91 select( $rin, undef, undef, undef );
92 my $rc = *STDIN->sysread(@_);
97 next READ if $! == EWOULDBLOCK;
107 # A very very simple HTTP server that initializes a CGI environment
109 my ( $self, $class, $port, $host, $fork ) = @_;
114 local $SIG{HUP} = sub { $GOT_HUP = 1; };
116 local $SIG{CHLD} = 'IGNORE';
121 $host = $host ? inet_aton($host) : INADDR_ANY;
122 socket( HTTPDaemon, PF_INET, SOCK_STREAM, getprotobyname('tcp') );
123 setsockopt( HTTPDaemon, SOL_SOCKET, SO_REUSEADDR, pack( "l", 1 ) );
124 bind( HTTPDaemon, sockaddr_in( $port, $host ) );
125 listen( HTTPDaemon, SOMAXCONN );
127 if ( $host eq INADDR_ANY ) {
128 require Sys::Hostname;
129 $url .= lc Sys::Hostname::hostname();
132 $url .= gethostbyaddr( $host, AF_INET ) || inet_ntoa($host);
135 print "You can connect to your server at $url\n";
137 while ( accept( Remote, HTTPDaemon ) ) {
140 if ($fork) { next if $pid = fork }
142 close HTTPDaemon if defined $pid;
144 # Ignore broken pipes as an HTTP server should
145 local $SIG{PIPE} = sub { close Remote };
146 local $SIG{HUP} = ( defined $pid ? 'IGNORE' : $SIG{HUP} );
148 local *STDIN = \*Remote;
149 local *STDOUT = \*Remote;
153 my $remote_sockaddr = getpeername( \*Remote );
154 my ( undef, $iaddr ) = sockaddr_in($remote_sockaddr);
155 my $peername = gethostbyaddr( $iaddr, AF_INET ) || "localhost";
156 my $peeraddr = inet_ntoa($iaddr) || "127.0.0.1";
157 my $local_sockaddr = getsockname( \*Remote );
158 my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr);
159 my $localname = gethostbyaddr( $localiaddr, AF_INET )
161 my $localaddr = inet_ntoa($localiaddr) || "127.0.0.1";
166 my $line = $self->_get_line( \*STDIN );
168 unless my ( $method, $uri, $protocol ) =
169 $line =~ m/\A(\w+)\s+(\S+)(?:\s+HTTP\/(\d+(?:\.\d+)?))?\z/;
171 # We better be careful and just use 1.0
174 my ( $path, $query_string ) = split /\?/, $uri, 2;
176 # Initialize CGI environment
178 PATH_INFO => $path || '',
179 QUERY_STRING => $query_string || '',
180 REMOTE_ADDR => $peeraddr,
181 REMOTE_HOST => $peername,
182 REQUEST_METHOD => $method || '',
183 SERVER_NAME => $localname,
184 SERVER_PORT => $port,
185 SERVER_PROTOCOL => "HTTP/$protocol",
190 if ( $protocol >= 1 ) {
192 my $line = $self->_get_line( \*STDIN );
195 unless my ( $name, $value ) =
196 $line =~ m/\A(\w(?:-?\w+)*):\s(.+)\z/;
199 $name = 'COOKIE' if $name eq 'COOKIES';
201 $name = 'HTTP_' . $name
202 unless $name =~ m/\A(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)\z/;
203 if ( exists $ENV{$name} ) {
204 $ENV{$name} .= "; $value";
207 $ENV{$name} = $value;
212 # Pass flow control to Catalyst
213 $class->handle_request;
214 exit if defined $pid;
220 exec {$0}( ( ( -x $0 ) ? () : ($^X) ), $0, @ARGV ) if $GOT_HUP;
224 my ( $self, $handle ) = @_;
228 while ( sysread( $handle, my $byte, 1 ) ) {
229 last if $byte eq "\012"; # eol
233 1 while $line =~ s/\s\z//;
242 L<Catalyst>, L<Catalyst::Engine>.
246 Sebastian Riedel, <sri@cpan.org>
248 Dan Kubb, <dan.kubb-cpan@onautopilot.com>
252 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
256 This program is free software, you can redistribute it and/or modify it under
257 the same terms as Perl itself.