Commit | Line | Data |
ca61af20 |
1 | package Catalyst::Engine::HTTP; |
45374ac6 |
2 | |
3 | use strict; |
fbcc39ad |
4 | use base 'Catalyst::Engine::CGI'; |
5 | use Errno 'EWOULDBLOCK'; |
6 | use HTTP::Status; |
7 | use NEXT; |
8 | use Socket; |
6c7a1d2f |
9 | use IO::Socket::INET (); |
10 | use IO::Select (); |
45374ac6 |
11 | |
71fd2e0f |
12 | # For PAR |
13 | require Catalyst::Engine::HTTP::Restarter; |
14 | require Catalyst::Engine::HTTP::Restarter::Watcher; |
15 | |
45374ac6 |
16 | =head1 NAME |
17 | |
ca61af20 |
18 | Catalyst::Engine::HTTP - Catalyst HTTP Engine |
45374ac6 |
19 | |
20 | =head1 SYNOPSIS |
21 | |
ca61af20 |
22 | A script using the Catalyst::Engine::HTTP module might look like: |
45374ac6 |
23 | |
24 | #!/usr/bin/perl -w |
25 | |
ca61af20 |
26 | BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP' } |
45374ac6 |
27 | |
28 | use strict; |
29 | use lib '/path/to/MyApp/lib'; |
30 | use MyApp; |
31 | |
32 | MyApp->run; |
33 | |
34 | =head1 DESCRIPTION |
35 | |
36 | This is the Catalyst engine specialized for development and testing. |
37 | |
fbcc39ad |
38 | =head1 METHODS |
39 | |
40 | =over 4 |
41 | |
42 | =item $self->finalize_headers($c) |
43 | |
44 | =cut |
45 | |
46 | sub finalize_headers { |
47 | my ( $self, $c ) = @_; |
48 | my $protocol = $c->request->protocol; |
49 | my $status = $c->response->status; |
50 | my $message = status_message($status); |
51 | print "$protocol $status $message\015\012"; |
52 | $c->response->headers->date(time); |
6c7a1d2f |
53 | $c->response->headers->header( |
54 | Connection => $self->_keep_alive ? 'keep-alive' : 'close' ); |
fbcc39ad |
55 | $self->NEXT::finalize_headers($c); |
56 | } |
57 | |
58 | =item $self->finalize_read($c) |
59 | |
60 | =cut |
61 | |
62 | sub finalize_read { |
63 | my ( $self, $c ) = @_; |
64 | |
65 | # Never ever remove this, it would result in random length output |
66 | # streams if STDIN eq STDOUT (like in the HTTP engine) |
4f5ebacd |
67 | *STDIN->blocking(1); |
fbcc39ad |
68 | |
69 | return $self->NEXT::finalize_read($c); |
70 | } |
71 | |
72 | =item $self->prepare_read($c) |
73 | |
74 | =cut |
75 | |
76 | sub prepare_read { |
77 | my ( $self, $c ) = @_; |
78 | |
79 | # Set the input handle to non-blocking |
4f5ebacd |
80 | *STDIN->blocking(0); |
fbcc39ad |
81 | |
82 | return $self->NEXT::prepare_read($c); |
83 | } |
84 | |
85 | =item $self->read_chunk($c, $buffer, $length) |
86 | |
87 | =cut |
88 | |
89 | sub read_chunk { |
90 | my $self = shift; |
91 | my $c = shift; |
92 | |
93 | # support for non-blocking IO |
4f5ebacd |
94 | my $rin = ''; |
95 | vec( $rin, *STDIN->fileno, 1 ) = 1; |
fbcc39ad |
96 | |
97 | READ: |
98 | { |
99 | select( $rin, undef, undef, undef ); |
4f5ebacd |
100 | my $rc = *STDIN->sysread(@_); |
fbcc39ad |
101 | if ( defined $rc ) { |
102 | return $rc; |
103 | } |
104 | else { |
105 | next READ if $! == EWOULDBLOCK; |
106 | return; |
107 | } |
108 | } |
109 | } |
110 | |
111 | =item run |
112 | |
113 | =cut |
114 | |
115 | # A very very simple HTTP server that initializes a CGI environment |
116 | sub run { |
37553dc8 |
117 | my ( $self, $class, $port, $host, $options ) = @_; |
fbcc39ad |
118 | |
4eeca0f2 |
119 | $options ||= {}; |
1cf1c56a |
120 | |
57a87bb3 |
121 | my $restart = 0; |
31b426c0 |
122 | local $SIG{CHLD} = 'IGNORE'; |
fbcc39ad |
123 | |
1cf1c56a |
124 | my $allowed = $options->{allowed} || { '127.0.0.1' => '255.255.255.255' }; |
6c7a1d2f |
125 | my $addr = $host ? inet_aton($host) : INADDR_ANY; |
126 | if ( $addr eq INADDR_ANY ) { |
fbcc39ad |
127 | require Sys::Hostname; |
6c7a1d2f |
128 | $host = lc Sys::Hostname::hostname(); |
fbcc39ad |
129 | } |
130 | else { |
6c7a1d2f |
131 | $host = gethostbyaddr( $addr, AF_INET ) || inet_ntoa($addr); |
fbcc39ad |
132 | } |
6c7a1d2f |
133 | |
134 | # Handle requests |
135 | |
136 | # Setup socket |
137 | my $daemon = IO::Socket::INET->new( |
138 | Listen => SOMAXCONN, |
139 | LocalAddr => inet_ntoa($addr), |
140 | LocalPort => $port, |
141 | Proto => 'tcp', |
142 | ReuseAddr => 1, |
143 | Type => SOCK_STREAM, |
144 | ) |
145 | or die "Couldn't create daemon: $!"; |
146 | |
147 | my $url = "http://$host"; |
148 | $url .= ":$port" unless $port == 80; |
149 | |
fbcc39ad |
150 | print "You can connect to your server at $url\n"; |
fbcc39ad |
151 | |
6c7a1d2f |
152 | $self->_keep_alive( $options->{keepalive} || 0 ); |
153 | |
57a87bb3 |
154 | my $parent = $$; |
155 | my $pid = undef; |
6c7a1d2f |
156 | while ( accept( Remote, $daemon ) ) |
157 | { # TODO: get while ( my $remote = $daemon->accept ) to work |
fbcc39ad |
158 | |
57a87bb3 |
159 | select Remote; |
fbcc39ad |
160 | |
161 | # Request data |
fbcc39ad |
162 | |
57a87bb3 |
163 | Remote->blocking(1); |
fbcc39ad |
164 | |
fbcc39ad |
165 | next |
166 | unless my ( $method, $uri, $protocol ) = |
6c7a1d2f |
167 | $self->_parse_request_line( \*Remote ); |
fbcc39ad |
168 | |
57a87bb3 |
169 | unless ( uc($method) eq 'RESTART' ) { |
170 | |
171 | # Fork |
172 | if ( $options->{fork} ) { next if $pid = fork } |
173 | |
6c7a1d2f |
174 | $self->_handler( $class, $port, $method, $uri, $protocol ); |
175 | |
176 | $daemon->close if defined $pid; |
fbcc39ad |
177 | |
1cf1c56a |
178 | } |
179 | else { |
6c7a1d2f |
180 | my $sockdata = $self->_socket_data( \*Remote ); |
181 | my $ipaddr = _inet_addr( $sockdata->{peeraddr} ); |
182 | my $ready = 0; |
1cf1c56a |
183 | while ( my ( $ip, $mask ) = each %$allowed and not $ready ) { |
184 | $ready = ( $ipaddr & _inet_addr($mask) ) == _inet_addr($ip); |
185 | } |
186 | if ($ready) { |
57a87bb3 |
187 | $restart = 1; |
1cf1c56a |
188 | last; |
189 | } |
190 | } |
57a87bb3 |
191 | |
fbcc39ad |
192 | exit if defined $pid; |
193 | } |
194 | continue { |
195 | close Remote; |
196 | } |
6c7a1d2f |
197 | $daemon->close; |
37553dc8 |
198 | |
57a87bb3 |
199 | if ($restart) { |
60c38e3e |
200 | $SIG{CHLD} = 'DEFAULT'; |
6844bc1c |
201 | wait; |
1cf1c56a |
202 | exec $^X . ' "' . $0 . '" ' . join( ' ', @{ $options->{argv} } ); |
60c38e3e |
203 | } |
57a87bb3 |
204 | |
205 | exit; |
fbcc39ad |
206 | } |
207 | |
6c7a1d2f |
208 | sub _handler { |
209 | my ( $self, $class, $port, $method, $uri, $protocol ) = @_; |
210 | |
211 | # Ignore broken pipes as an HTTP server should |
212 | local $SIG{PIPE} = sub { close Remote }; |
213 | |
214 | local *STDIN = \*Remote; |
215 | local *STDOUT = \*Remote; |
216 | |
217 | # We better be careful and just use 1.0 |
218 | $protocol = '1.0'; |
219 | |
220 | my $sockdata = $self->_socket_data( \*Remote ); |
221 | my %copy_of_env = %ENV; |
222 | |
223 | my $sel = IO::Select->new; |
224 | $sel->add( \*STDIN ); |
225 | |
226 | while (1) { |
227 | my ( $path, $query_string ) = split /\?/, $uri, 2; |
228 | |
229 | # Initialize CGI environment |
230 | local %ENV = ( |
231 | PATH_INFO => $path || '', |
232 | QUERY_STRING => $query_string || '', |
233 | REMOTE_ADDR => $sockdata->{peeraddr}, |
234 | REMOTE_HOST => $sockdata->{peername}, |
235 | REQUEST_METHOD => $method || '', |
236 | SERVER_NAME => $sockdata->{localname}, |
237 | SERVER_PORT => $port, |
238 | SERVER_PROTOCOL => "HTTP/$protocol", |
239 | %copy_of_env, |
240 | ); |
241 | |
242 | # Parse headers |
243 | if ( $protocol >= 1 ) { |
244 | while (1) { |
245 | my $line = $self->_get_line( \*STDIN ); |
246 | last if $line eq ''; |
247 | next |
248 | unless my ( $name, $value ) = |
249 | $line =~ m/\A(\w(?:-?\w+)*):\s(.+)\z/; |
250 | |
251 | $name = uc $name; |
252 | $name = 'COOKIE' if $name eq 'COOKIES'; |
253 | $name =~ tr/-/_/; |
254 | $name = 'HTTP_' . $name |
255 | unless $name =~ m/\A(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)\z/; |
256 | if ( exists $ENV{$name} ) { |
257 | $ENV{$name} .= "; $value"; |
258 | } |
259 | else { |
260 | $ENV{$name} = $value; |
261 | } |
262 | } |
263 | } |
264 | |
265 | # Pass flow control to Catalyst |
266 | $class->handle_request; |
267 | |
268 | my $connection = lc $ENV{HTTP_CONNECTION}; |
269 | last |
270 | unless $self->_keep_alive() |
271 | && index( $connection, 'keep-alive' ) > -1 |
272 | && index( $connection, 'te' ) == -1 # opera stuff |
273 | && $sel->can_read(5); |
274 | |
275 | last |
276 | unless ( $method, $uri, $protocol ) = |
277 | $self->_parse_request_line( \*STDIN ); |
278 | } |
279 | |
280 | close Remote; |
281 | } |
282 | |
283 | sub _keep_alive { |
284 | my ( $self, $keepalive ) = @_; |
285 | |
286 | my $r = $self->{_keepalive} || 0; |
287 | $self->{_keepalive} = $keepalive if defined $keepalive; |
288 | |
289 | return $r; |
290 | |
291 | } |
292 | |
293 | sub _parse_request_line { |
294 | my ( $self, $handle ) = @_; |
295 | |
296 | # Parse request line |
297 | my $line = $self->_get_line($handle); |
298 | return () |
299 | unless my ( $method, $uri, $protocol ) = |
300 | $line =~ m/\A(\w+)\s+(\S+)(?:\s+HTTP\/(\d+(?:\.\d+)?))?\z/; |
301 | return ( $method, $uri, $protocol ); |
302 | } |
303 | |
304 | sub _socket_data { |
305 | my ( $self, $handle ) = @_; |
306 | |
307 | my $remote_sockaddr = getpeername($handle); |
308 | my ( undef, $iaddr ) = sockaddr_in($remote_sockaddr); |
309 | my $local_sockaddr = getsockname($handle); |
310 | my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr); |
311 | |
312 | my $data = { |
313 | peername => gethostbyaddr( $iaddr, AF_INET ) || "localhost", |
314 | peeraddr => inet_ntoa($iaddr) || "127.0.0.1", |
315 | localname => gethostbyaddr( $localiaddr, AF_INET ) || "localhost", |
316 | localaddr => inet_ntoa($localiaddr) || "127.0.0.1", |
317 | }; |
318 | |
319 | return $data; |
320 | } |
321 | |
fbcc39ad |
322 | sub _get_line { |
323 | my ( $self, $handle ) = @_; |
324 | |
325 | my $line = ''; |
326 | |
327 | while ( sysread( $handle, my $byte, 1 ) ) { |
328 | last if $byte eq "\012"; # eol |
329 | $line .= $byte; |
330 | } |
331 | |
332 | 1 while $line =~ s/\s\z//; |
333 | |
334 | return $line; |
335 | } |
336 | |
1cf1c56a |
337 | sub _inet_addr { unpack "N*", inet_aton( $_[0] ) } |
338 | |
fbcc39ad |
339 | =back |
340 | |
45374ac6 |
341 | =head1 SEE ALSO |
342 | |
fbcc39ad |
343 | L<Catalyst>, L<Catalyst::Engine>. |
344 | |
345 | =head1 AUTHORS |
346 | |
347 | Sebastian Riedel, <sri@cpan.org> |
348 | |
349 | Dan Kubb, <dan.kubb-cpan@onautopilot.com> |
45374ac6 |
350 | |
6c7a1d2f |
351 | Sascha Kiefer, <esskar@cpan.org> |
352 | |
fbcc39ad |
353 | =head1 THANKS |
45374ac6 |
354 | |
fbcc39ad |
355 | Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent. |
45374ac6 |
356 | |
357 | =head1 COPYRIGHT |
358 | |
359 | This program is free software, you can redistribute it and/or modify it under |
360 | the same terms as Perl itself. |
361 | |
362 | =cut |
363 | |
45374ac6 |
364 | 1; |