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'; |
37553dc8 |
6 | use FindBin; |
7 | use File::Find; |
8 | use File::Spec; |
fbcc39ad |
9 | use HTTP::Status; |
10 | use NEXT; |
11 | use Socket; |
45374ac6 |
12 | |
13 | =head1 NAME |
14 | |
ca61af20 |
15 | Catalyst::Engine::HTTP - Catalyst HTTP Engine |
45374ac6 |
16 | |
17 | =head1 SYNOPSIS |
18 | |
ca61af20 |
19 | A script using the Catalyst::Engine::HTTP module might look like: |
45374ac6 |
20 | |
21 | #!/usr/bin/perl -w |
22 | |
ca61af20 |
23 | BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP' } |
45374ac6 |
24 | |
25 | use strict; |
26 | use lib '/path/to/MyApp/lib'; |
27 | use MyApp; |
28 | |
29 | MyApp->run; |
30 | |
31 | =head1 DESCRIPTION |
32 | |
33 | This is the Catalyst engine specialized for development and testing. |
34 | |
fbcc39ad |
35 | =head1 METHODS |
36 | |
37 | =over 4 |
38 | |
39 | =item $self->finalize_headers($c) |
40 | |
41 | =cut |
42 | |
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); |
51 | } |
52 | |
53 | =item $self->finalize_read($c) |
54 | |
55 | =cut |
56 | |
57 | sub finalize_read { |
58 | my ( $self, $c ) = @_; |
59 | |
60 | # Never ever remove this, it would result in random length output |
61 | # streams if STDIN eq STDOUT (like in the HTTP engine) |
4f5ebacd |
62 | *STDIN->blocking(1); |
fbcc39ad |
63 | |
64 | return $self->NEXT::finalize_read($c); |
65 | } |
66 | |
67 | =item $self->prepare_read($c) |
68 | |
69 | =cut |
70 | |
71 | sub prepare_read { |
72 | my ( $self, $c ) = @_; |
73 | |
74 | # Set the input handle to non-blocking |
4f5ebacd |
75 | *STDIN->blocking(0); |
fbcc39ad |
76 | |
77 | return $self->NEXT::prepare_read($c); |
78 | } |
79 | |
80 | =item $self->read_chunk($c, $buffer, $length) |
81 | |
82 | =cut |
83 | |
84 | sub read_chunk { |
85 | my $self = shift; |
86 | my $c = shift; |
87 | |
88 | # support for non-blocking IO |
4f5ebacd |
89 | my $rin = ''; |
90 | vec( $rin, *STDIN->fileno, 1 ) = 1; |
fbcc39ad |
91 | |
92 | READ: |
93 | { |
94 | select( $rin, undef, undef, undef ); |
4f5ebacd |
95 | my $rc = *STDIN->sysread(@_); |
fbcc39ad |
96 | if ( defined $rc ) { |
97 | return $rc; |
98 | } |
99 | else { |
100 | next READ if $! == EWOULDBLOCK; |
101 | return; |
102 | } |
103 | } |
104 | } |
105 | |
106 | =item run |
107 | |
108 | =cut |
109 | |
110 | # A very very simple HTTP server that initializes a CGI environment |
111 | sub run { |
37553dc8 |
112 | my ( $self, $class, $port, $host, $options ) = @_; |
fbcc39ad |
113 | |
4eeca0f2 |
114 | $options ||= {}; |
115 | |
37553dc8 |
116 | # Setup restarter |
117 | my $restarter; |
118 | if ( $options->{restart} ) { |
119 | my $parent = $$; |
120 | unless ( $restarter = fork ) { |
121 | |
7a1df403 |
122 | # Prepare |
123 | close STDIN; |
124 | close STDOUT; |
125 | |
37553dc8 |
126 | # Index parent directory |
127 | my $dir = File::Spec->catdir( $FindBin::Bin, '..' ); |
128 | |
129 | my $regex = $options->{restart_regex}; |
7a1df403 |
130 | my $one = _index( $dir, $regex ); |
131 | RESTART: while (1) { |
31b426c0 |
132 | sleep $options->{restart_delay} || 1; |
133 | |
134 | # check if our parent has died |
135 | exit if ( getppid == 1 ); |
136 | |
7a1df403 |
137 | my $two = _index( $dir, $regex ); |
138 | my $changes = _compare_index( $one, $two ); |
139 | if (@$changes) { |
37553dc8 |
140 | $one = $two; |
7a1df403 |
141 | |
142 | # Test modified pm's |
143 | for my $file (@$changes) { |
144 | next unless $file =~ /\.pm$/; |
145 | if ( my $error = _test($file) ) { |
146 | print STDERR |
6844bc1c |
147 | qq/File "$file" modified, not restarting\n\n/; |
7a1df403 |
148 | print STDERR '*' x 80, "\n"; |
149 | print STDERR $error; |
150 | print STDERR '*' x 80, "\n"; |
151 | next RESTART; |
152 | } |
153 | } |
154 | |
155 | # Restart |
156 | my $files = join ', ', @$changes; |
ee110ef5 |
157 | print STDERR qq/"$files" modified, restarting\n\n/; |
7a1df403 |
158 | kill( 1, $parent ); |
159 | exit; |
37553dc8 |
160 | } |
161 | } |
162 | } |
163 | } |
31b426c0 |
164 | |
165 | our $GOT_HUP; |
166 | local $GOT_HUP = 0; |
167 | |
168 | local $SIG{HUP} = sub { $GOT_HUP = 1; }; |
169 | local $SIG{CHLD} = 'IGNORE'; |
fbcc39ad |
170 | |
171 | # Handle requests |
172 | |
173 | # Setup socket |
174 | $host = $host ? inet_aton($host) : INADDR_ANY; |
bd357f39 |
175 | socket( HTTPDaemon, PF_INET, SOCK_STREAM, getprotobyname('tcp') ) |
176 | || die "Couldn't assign TCP socket: $!"; |
177 | setsockopt( HTTPDaemon, SOL_SOCKET, SO_REUSEADDR, pack( "l", 1 ) ) |
178 | || die "Couldn't set TCP socket options: $!"; |
179 | bind( HTTPDaemon, sockaddr_in( $port, $host ) ) |
180 | || die "Couldn't bind socket to $port on $host: $!"; |
181 | listen( HTTPDaemon, SOMAXCONN ) |
182 | || die "Couldn't listen to socket on $port on $host: $!"; |
fbcc39ad |
183 | my $url = 'http://'; |
184 | if ( $host eq INADDR_ANY ) { |
185 | require Sys::Hostname; |
186 | $url .= lc Sys::Hostname::hostname(); |
187 | } |
188 | else { |
189 | $url .= gethostbyaddr( $host, AF_INET ) || inet_ntoa($host); |
190 | } |
191 | $url .= ":$port"; |
192 | print "You can connect to your server at $url\n"; |
193 | my $pid = undef; |
194 | while ( accept( Remote, HTTPDaemon ) ) { |
195 | |
196 | # Fork |
37553dc8 |
197 | if ( $options->{fork} ) { next if $pid = fork } |
fbcc39ad |
198 | |
199 | close HTTPDaemon if defined $pid; |
200 | |
201 | # Ignore broken pipes as an HTTP server should |
202 | local $SIG{PIPE} = sub { close Remote }; |
4f5ebacd |
203 | local $SIG{HUP} = ( defined $pid ? 'IGNORE' : $SIG{HUP} ); |
fbcc39ad |
204 | |
205 | local *STDIN = \*Remote; |
206 | local *STDOUT = \*Remote; |
207 | select STDOUT; |
208 | |
209 | # Request data |
210 | my $remote_sockaddr = getpeername( \*Remote ); |
211 | my ( undef, $iaddr ) = sockaddr_in($remote_sockaddr); |
212 | my $peername = gethostbyaddr( $iaddr, AF_INET ) || "localhost"; |
213 | my $peeraddr = inet_ntoa($iaddr) || "127.0.0.1"; |
214 | my $local_sockaddr = getsockname( \*Remote ); |
215 | my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr); |
216 | my $localname = gethostbyaddr( $localiaddr, AF_INET ) |
217 | || "localhost"; |
218 | my $localaddr = inet_ntoa($localiaddr) || "127.0.0.1"; |
219 | |
220 | STDIN->blocking(1); |
221 | |
222 | # Parse request line |
223 | my $line = $self->_get_line( \*STDIN ); |
224 | next |
225 | unless my ( $method, $uri, $protocol ) = |
226 | $line =~ m/\A(\w+)\s+(\S+)(?:\s+HTTP\/(\d+(?:\.\d+)?))?\z/; |
227 | |
228 | # We better be careful and just use 1.0 |
229 | $protocol = '1.0'; |
230 | |
231 | my ( $path, $query_string ) = split /\?/, $uri, 2; |
232 | |
233 | # Initialize CGI environment |
234 | local %ENV = ( |
235 | PATH_INFO => $path || '', |
236 | QUERY_STRING => $query_string || '', |
237 | REMOTE_ADDR => $peeraddr, |
238 | REMOTE_HOST => $peername, |
239 | REQUEST_METHOD => $method || '', |
240 | SERVER_NAME => $localname, |
241 | SERVER_PORT => $port, |
242 | SERVER_PROTOCOL => "HTTP/$protocol", |
243 | %ENV, |
244 | ); |
245 | |
246 | # Parse headers |
247 | if ( $protocol >= 1 ) { |
248 | while (1) { |
249 | my $line = $self->_get_line( \*STDIN ); |
250 | last if $line eq ''; |
251 | next |
252 | unless my ( $name, $value ) = |
253 | $line =~ m/\A(\w(?:-?\w+)*):\s(.+)\z/; |
254 | |
255 | $name = uc $name; |
256 | $name = 'COOKIE' if $name eq 'COOKIES'; |
257 | $name =~ tr/-/_/; |
258 | $name = 'HTTP_' . $name |
259 | unless $name =~ m/\A(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)\z/; |
260 | if ( exists $ENV{$name} ) { |
261 | $ENV{$name} .= "; $value"; |
262 | } |
263 | else { |
264 | $ENV{$name} = $value; |
265 | } |
266 | } |
267 | } |
268 | |
269 | # Pass flow control to Catalyst |
270 | $class->handle_request; |
271 | exit if defined $pid; |
272 | } |
273 | continue { |
274 | close Remote; |
275 | } |
276 | close HTTPDaemon; |
37553dc8 |
277 | |
60c38e3e |
278 | if ($GOT_HUP) { |
279 | $SIG{CHLD} = 'DEFAULT'; |
6844bc1c |
280 | wait; |
281 | exec {$0}( ( ( -x $0 ) ? () : ($^X) ), $0, @{ $options->{argv} } ); |
60c38e3e |
282 | } |
fbcc39ad |
283 | } |
284 | |
37553dc8 |
285 | sub _compare_index { |
286 | my ( $one, $two ) = @_; |
287 | my %clone = %$two; |
7a1df403 |
288 | my @changes; |
37553dc8 |
289 | while ( my ( $key, $val ) = each %$one ) { |
7a1df403 |
290 | if ( !$clone{$key} || ( $clone{$key} ne $val ) ) { |
291 | push @changes, $key; |
292 | } |
37553dc8 |
293 | delete $clone{$key}; |
294 | } |
7a1df403 |
295 | for my $key ( keys %clone ) { push @changes, $key } |
296 | return \@changes; |
37553dc8 |
297 | } |
298 | |
fbcc39ad |
299 | sub _get_line { |
300 | my ( $self, $handle ) = @_; |
301 | |
302 | my $line = ''; |
303 | |
304 | while ( sysread( $handle, my $byte, 1 ) ) { |
305 | last if $byte eq "\012"; # eol |
306 | $line .= $byte; |
307 | } |
308 | |
309 | 1 while $line =~ s/\s\z//; |
310 | |
311 | return $line; |
312 | } |
313 | |
ee110ef5 |
314 | # The list of files/directories we check for modification |
6f40578b |
315 | our $file_index; |
316 | |
37553dc8 |
317 | sub _index { |
318 | my ( $dir, $regex ) = @_; |
6f40578b |
319 | |
320 | if ( ref $file_index ) { |
ee110ef5 |
321 | # don't run a File::Find, but just check file/dir mod times |
6f40578b |
322 | my %index = %{$file_index}; |
323 | foreach my $file ( keys %index ) { |
324 | if ( my @stat = stat $file ) { |
325 | $index{$file} = $stat[9]; |
326 | } |
327 | else { |
328 | delete $index{$file}; |
329 | } |
330 | } |
331 | return \%index; |
332 | } |
333 | else { |
ee110ef5 |
334 | # first time, run a File::Find to locate files and dirs to watch |
6f40578b |
335 | my $index = {}; |
336 | finddepth( |
337 | { |
338 | wanted => sub { |
339 | my $file = File::Spec->rel2abs($File::Find::name); |
ee110ef5 |
340 | $file =~ s{/script/..}{}; |
6f40578b |
341 | return unless $file =~ /$regex/; |
342 | return unless -f $file; |
343 | my $time = ( stat $file )[9]; |
344 | $index->{$file} = $time; |
ee110ef5 |
345 | |
346 | # also watch the directory the file is in |
347 | my $cur_dir = File::Spec->rel2abs($File::Find::dir); |
348 | $cur_dir =~ s{/script/..}{}; |
349 | unless ( $index->{$cur_dir} ) { |
350 | my $time = ( stat $cur_dir )[9]; |
351 | $index->{$cur_dir} = $time; |
352 | } |
6f40578b |
353 | }, |
354 | no_chdir => 1 |
37553dc8 |
355 | }, |
6f40578b |
356 | $dir |
357 | ); |
358 | $file_index = $index; |
359 | return $file_index; |
360 | } |
37553dc8 |
361 | } |
362 | |
7a1df403 |
363 | sub _test { |
364 | my $file = shift; |
6844bc1c |
365 | delete $INC{$file}; |
ee110ef5 |
366 | |
367 | # if the file has been deleted, don't try to test it |
368 | return 0 unless -f $file; |
369 | |
7a1df403 |
370 | local $SIG{__WARN__} = sub { }; |
371 | open my $olderr, '>&STDERR'; |
372 | open STDERR, '>', File::Spec->devnull; |
373 | eval "require '$file'"; |
374 | open STDERR, '>&', $olderr; |
375 | return $@ if $@; |
376 | return 0; |
377 | } |
378 | |
fbcc39ad |
379 | =back |
380 | |
45374ac6 |
381 | =head1 SEE ALSO |
382 | |
fbcc39ad |
383 | L<Catalyst>, L<Catalyst::Engine>. |
384 | |
385 | =head1 AUTHORS |
386 | |
387 | Sebastian Riedel, <sri@cpan.org> |
388 | |
389 | Dan Kubb, <dan.kubb-cpan@onautopilot.com> |
45374ac6 |
390 | |
fbcc39ad |
391 | =head1 THANKS |
45374ac6 |
392 | |
fbcc39ad |
393 | Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent. |
45374ac6 |
394 | |
395 | =head1 COPYRIGHT |
396 | |
397 | This program is free software, you can redistribute it and/or modify it under |
398 | the same terms as Perl itself. |
399 | |
400 | =cut |
401 | |
45374ac6 |
402 | 1; |