… | |
… | |
9 | # at least on my machine, this thingy serves files |
9 | # at least on my machine, this thingy serves files |
10 | # quite a bit faster than apache, ;) |
10 | # quite a bit faster than apache, ;) |
11 | # and quite a bit slower than thttpd :( |
11 | # and quite a bit slower than thttpd :( |
12 | |
12 | |
13 | $SIG{PIPE} = 'IGNORE'; |
13 | $SIG{PIPE} = 'IGNORE'; |
14 | |
14 | |
|
|
15 | our $accesslog; |
|
|
16 | |
|
|
17 | if ($ACCESS_LOG) { |
|
|
18 | use IO::Handle; |
|
|
19 | open $accesslog, ">>$ACCESS_LOG" |
|
|
20 | or die "$ACCESS_LOG: $!"; |
|
|
21 | $accesslog->autoflush(1); |
|
|
22 | } |
|
|
23 | |
15 | sub slog { |
24 | sub slog { |
16 | my $level = shift; |
25 | my $level = shift; |
17 | my $format = shift; |
26 | my $format = shift; |
18 | printf "---: $format\n", @_; |
27 | printf "---: $format\n", @_; |
19 | } |
28 | } |
… | |
… | |
76 | |
85 | |
77 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
86 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
78 | |
87 | |
79 | Event->io(fd => Linux::AIO::poll_fileno, |
88 | Event->io(fd => Linux::AIO::poll_fileno, |
80 | poll => 'r', async => 1, |
89 | poll => 'r', async => 1, |
81 | cb => \&Linux::AIO::poll_cb ); |
90 | cb => \&Linux::AIO::poll_cb); |
82 | |
91 | |
83 | our %conn; # $conn{ip}{fh} => connobj |
92 | our %conn; # $conn{ip}{self} => connobj |
|
|
93 | our %uri; # $uri{ip}{uri}{self} |
84 | our %blocked; |
94 | our %blocked; |
85 | our %mimetype; |
95 | our %mimetype; |
86 | |
96 | |
87 | sub read_mimetypes { |
97 | sub read_mimetypes { |
88 | local *M; |
98 | local *M; |
… | |
… | |
121 | sub DESTROY { |
131 | sub DESTROY { |
122 | my $self = shift; |
132 | my $self = shift; |
123 | |
133 | |
124 | $::conns--; |
134 | $::conns--; |
125 | |
135 | |
|
|
136 | $self->eoconn; |
126 | delete $conn{$self->{remote_addr}}{$self*1}; |
137 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
138 | } |
|
|
139 | |
|
|
140 | # end of connection |
|
|
141 | sub eoconn { |
|
|
142 | my $self = shift; |
127 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
143 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
128 | } |
144 | } |
129 | |
145 | |
130 | sub slog { |
146 | sub slog { |
131 | my $self = shift; |
147 | my $self = shift; |
… | |
… | |
144 | } |
160 | } |
145 | $res .= "\015\012"; |
161 | $res .= "\015\012"; |
146 | |
162 | |
147 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
163 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
148 | |
164 | |
149 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
165 | my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
|
|
166 | |
|
|
167 | print $accesslog $log if $accesslog; |
|
|
168 | print STDERR $log; |
150 | |
169 | |
151 | $self->{written} += |
170 | $self->{written} += |
152 | print {$self->{fh}} $res; |
171 | print {$self->{fh}} $res; |
153 | } |
172 | } |
154 | |
173 | |
… | |
… | |
171 | sub err_blocked { |
190 | sub err_blocked { |
172 | my $self = shift; |
191 | my $self = shift; |
173 | my $ip = $self->{remote_addr}; |
192 | my $ip = $self->{remote_addr}; |
174 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
193 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
175 | |
194 | |
176 | Coro::Event::do_timer(after => 15); |
195 | Coro::Event::do_timer(after => 20*rand); |
177 | |
196 | |
178 | $self->err(401, "too many connections", |
197 | $self->err(401, "too many connections", |
179 | { |
198 | { |
180 | "Content-Type" => "text/html", |
199 | "Content-Type" => "text/html", |
181 | "Retry-After" => $::BLOCKTIME |
200 | "Retry-After" => $::BLOCKTIME, |
|
|
201 | "Warning" => "Please do NOT retry, you have been blocked", |
|
|
202 | "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", |
182 | }, |
203 | }, |
183 | <<EOF); |
204 | <<EOF); |
184 | <html><p> |
205 | <html> |
|
|
206 | <head> |
|
|
207 | <title>Too many connections</title> |
|
|
208 | </head> |
|
|
209 | <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000"> |
|
|
210 | |
185 | You have been blocked because you opened too many connections. You |
211 | <p>You have been blocked because you opened too many connections. You |
186 | may retry at</p> |
212 | may retry at</p> |
187 | |
213 | |
188 | <p><blockquote>$time.</blockquote></p> |
214 | <p><blockquote>$time.</blockquote></p> |
189 | |
215 | |
190 | <p>Until then, each new access will renew the block. You might want to have a |
216 | <p>Until then, each new access will renew the block. You might want to have a |
191 | look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p> |
217 | look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p> |
192 | </html> |
218 | |
|
|
219 | </body></html> |
193 | EOF |
220 | EOF |
194 | } |
221 | } |
195 | |
222 | |
196 | sub handle { |
223 | sub handle { |
197 | my $self = shift; |
224 | my $self = shift; |
… | |
… | |
238 | |
265 | |
239 | $self->{method} = $1; |
266 | $self->{method} = $1; |
240 | $self->{uri} = $2; |
267 | $self->{uri} = $2; |
241 | $self->{version} = $3; |
268 | $self->{version} = $3; |
242 | |
269 | |
243 | $3 eq "1.0" or $3 eq "1.1" |
270 | $3 =~ /^1\./ |
244 | or $self->err(506, "http protocol version $3 not supported"); |
271 | or $self->err(506, "http protocol version $3 not supported"); |
245 | |
272 | |
246 | # parse headers |
273 | # parse headers |
247 | { |
274 | { |
248 | my (%hdr, $h, $v); |
275 | my (%hdr, $h, $v); |
… | |
… | |
264 | |
291 | |
265 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
292 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
266 | |
293 | |
267 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
294 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
268 | |
295 | |
|
|
296 | eval { |
269 | $self->map_uri; |
297 | $self->map_uri; |
270 | $self->respond; |
298 | $self->respond; |
|
|
299 | }; |
|
|
300 | |
|
|
301 | $self->eoconn; |
|
|
302 | |
|
|
303 | die if $@ && !ref $@; |
271 | |
304 | |
272 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
305 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
273 | |
306 | |
274 | $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
307 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
275 | $fh->timeout($::PER_TIMEOUT); |
308 | $fh->timeout($::PER_TIMEOUT); |
276 | } |
309 | } |
277 | } |
310 | } |
278 | |
311 | |
279 | # uri => path mapping |
312 | # uri => path mapping |
… | |
… | |
368 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
401 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
369 | } else { |
402 | } else { |
370 | $ims < $self->{stat}[9] |
403 | $ims < $self->{stat}[9] |
371 | or $self->err(304, "not modified"); |
404 | or $self->err(304, "not modified"); |
372 | |
405 | |
373 | if ($self->{method} eq "GET") { |
|
|
374 | if (-r "$path/index.html") { |
406 | if (-r "$path/index.html") { |
375 | $self->{path} .= "/index.html"; |
407 | $self->{path} .= "/index.html"; |
376 | $self->handle_file; |
408 | $self->handle_file; |
377 | } else { |
409 | } else { |
378 | $self->handle_dir; |
410 | $self->handle_dir; |
379 | } |
|
|
380 | } |
411 | } |
381 | } |
412 | } |
382 | } elsif (-f _ && -r _) { |
413 | } elsif (-f _ && -r _) { |
383 | -x _ and $self->err(403, "forbidden"); |
414 | -x _ and $self->err(403, "forbidden"); |
384 | $self->handle_file; |
415 | $self->handle_file; |
… | |
… | |
417 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
448 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
418 | } else { |
449 | } else { |
419 | ($l, $h) = (0, $length - 1); |
450 | ($l, $h) = (0, $length - 1); |
420 | goto ignore; |
451 | goto ignore; |
421 | } |
452 | } |
422 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
453 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
423 | } |
454 | } |
424 | $hdr->{"Content-Range"} = "bytes */$length"; |
455 | $hdr->{"Content-Range"} = "bytes */$length"; |
|
|
456 | $hdr->{"Content-Length"} = $length; |
|
|
457 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
425 | $self->err(416, "not satisfiable", $hdr); |
458 | $self->err(416, "not satisfiable", $hdr, ""); |
426 | |
459 | |
427 | satisfiable: |
460 | satisfiable: |
428 | # check for segmented downloads |
461 | # check for segmented downloads |
429 | if ($l && $::NO_SEGMENTED) { |
462 | if ($l && $::NO_SEGMENTED) { |
430 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
463 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
431 | Coro::Event::do_timer(after => 15); |
|
|
432 | |
|
|
433 | $self->err(400, "segmented downloads are not allowed"); |
464 | $self->err(400, "segmented downloads are not allowed"); |
434 | } |
465 | } |
435 | } |
466 | } |
436 | |
467 | |
437 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
468 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
455 | open $fh, "<", $self->{path} |
486 | open $fh, "<", $self->{path} |
456 | or die "$self->{path}: late open failure ($!)"; |
487 | or die "$self->{path}: late open failure ($!)"; |
457 | |
488 | |
458 | $h -= $l - 1; |
489 | $h -= $l - 1; |
459 | |
490 | |
|
|
491 | if (0) { |
|
|
492 | if ($l) { |
|
|
493 | sysseek $fh, $l, 0; |
|
|
494 | } |
|
|
495 | } |
|
|
496 | |
460 | while ($h > 0) { |
497 | while ($h > 0) { |
|
|
498 | if (0) { |
|
|
499 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
|
|
500 | or last; |
|
|
501 | } else { |
461 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
502 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
462 | $buf, 0, sub { |
503 | $buf, 0, sub { |
463 | $r = $_[0]; |
504 | $r = $_[0]; |
464 | $current->ready; |
505 | $current->ready; |
465 | }); |
506 | }); |
466 | &Coro::schedule; |
507 | &Coro::schedule; |
467 | last unless $r; |
508 | last unless $r; |
|
|
509 | } |
468 | my $w = $self->{fh}->syswrite($buf) |
510 | my $w = $self->{fh}->syswrite($buf) |
469 | or last; |
511 | or last; |
470 | $::written += $w; |
512 | $::written += $w; |
471 | $self->{written} += $w; |
513 | $self->{written} += $w; |
472 | $l += $r; |
514 | $l += $r; |