… | |
… | |
83 | use Convert::Scalar 'weaken'; |
83 | use Convert::Scalar 'weaken'; |
84 | use Linux::AIO; |
84 | use Linux::AIO; |
85 | |
85 | |
86 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
86 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
87 | |
87 | |
|
|
88 | my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4; |
|
|
89 | |
88 | Event->io(fd => Linux::AIO::poll_fileno, |
90 | Event->io(fd => Linux::AIO::poll_fileno, |
89 | poll => 'r', async => 1, |
91 | poll => 'r', async => 1, |
90 | cb => \&Linux::AIO::poll_cb); |
92 | cb => \&Linux::AIO::poll_cb); |
91 | |
93 | |
92 | our %conn; # $conn{ip}{self} => connobj |
94 | our %conn; # $conn{ip}{self} => connobj |
… | |
… | |
143 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
145 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
144 | } |
146 | } |
145 | |
147 | |
146 | sub slog { |
148 | sub slog { |
147 | my $self = shift; |
149 | my $self = shift; |
148 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
150 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
149 | } |
151 | } |
150 | |
152 | |
151 | sub response { |
153 | sub response { |
152 | my ($self, $code, $msg, $hdr, $content) = @_; |
154 | my ($self, $code, $msg, $hdr, $content) = @_; |
153 | my $res = "HTTP/1.1 $code $msg\015\012"; |
155 | my $res = "HTTP/1.1 $code $msg\015\012"; |
154 | |
156 | |
155 | #$res .= "Connection: close\015\012"; |
157 | $self->{h}{connection} ||= $hdr->{Connection}; |
|
|
158 | |
156 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
159 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
157 | |
160 | |
158 | while (my ($h, $v) = each %$hdr) { |
161 | while (my ($h, $v) = each %$hdr) { |
159 | $res .= "$h: $v\015\012" |
162 | $res .= "$h: $v\015\012" |
160 | } |
163 | } |
… | |
… | |
198 | { |
201 | { |
199 | "Content-Type" => "text/html", |
202 | "Content-Type" => "text/html", |
200 | "Retry-After" => $::BLOCKTIME, |
203 | "Retry-After" => $::BLOCKTIME, |
201 | "Warning" => "Please do NOT retry, you have been blocked", |
204 | "Warning" => "Please do NOT retry, you have been blocked", |
202 | "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", |
205 | "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", |
|
|
206 | "Connection" => "close", |
203 | }, |
207 | }, |
204 | <<EOF); |
208 | <<EOF); |
205 | <html> |
209 | <html> |
206 | <head> |
210 | <head> |
207 | <title>Too many connections</title> |
211 | <title>Too many connections</title> |
… | |
… | |
212 | may retry at</p> |
216 | may retry at</p> |
213 | |
217 | |
214 | <p><blockquote>$time.</blockquote></p> |
218 | <p><blockquote>$time.</blockquote></p> |
215 | |
219 | |
216 | <p>Until then, each new access will renew the block. You might want to have a |
220 | <p>Until then, each new access will renew the block. You might want to have a |
217 | look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p> |
221 | look at the <a href="http://www.goof.com/pcg/marc/animefaq.html#connectionlimit">FAQ</a>.</p> |
218 | |
222 | |
219 | </body></html> |
223 | </body></html> |
220 | EOF |
224 | EOF |
221 | } |
225 | } |
222 | |
226 | |
223 | sub handle { |
227 | sub handle { |
224 | my $self = shift; |
228 | my $self = shift; |
225 | my $fh = $self->{fh}; |
229 | my $fh = $self->{fh}; |
|
|
230 | |
|
|
231 | my $host; |
226 | |
232 | |
227 | $fh->timeout($::REQ_TIMEOUT); |
233 | $fh->timeout($::REQ_TIMEOUT); |
228 | while() { |
234 | while() { |
229 | $self->{reqs}++; |
235 | $self->{reqs}++; |
230 | |
236 | |
… | |
… | |
287 | |
293 | |
288 | $self->{h}{$h} = substr $v, 1 |
294 | $self->{h}{$h} = substr $v, 1 |
289 | while ($h, $v) = each %hdr; |
295 | while ($h, $v) = each %hdr; |
290 | } |
296 | } |
291 | |
297 | |
|
|
298 | # find out server name and port |
|
|
299 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
|
|
300 | $host = $1; |
|
|
301 | } else { |
|
|
302 | $host = $self->{h}{host}; |
|
|
303 | } |
|
|
304 | |
|
|
305 | if (defined $host) { |
292 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
306 | $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; |
|
|
307 | } else { |
|
|
308 | ($self->{server_port}, $host) |
|
|
309 | = unpack_sockaddr_in $self->{fh}->getsockname |
|
|
310 | or $self->err(500, "unable to get socket name"); |
|
|
311 | $host = inet_ntoa $host; |
|
|
312 | } |
|
|
313 | |
|
|
314 | $self->{server_name} = $host; |
|
|
315 | |
|
|
316 | # remote id should be unique per user |
|
|
317 | $self->{remote_id} = $self->{remote_addr}; |
|
|
318 | |
|
|
319 | if (exists $self->{h}{"client-ip"}) { |
|
|
320 | $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]"; |
|
|
321 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
322 | $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
323 | } |
293 | |
324 | |
294 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
325 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
295 | |
326 | |
296 | eval { |
327 | eval { |
297 | $self->map_uri; |
328 | $self->map_uri; |
… | |
… | |
300 | |
331 | |
301 | $self->eoconn; |
332 | $self->eoconn; |
302 | |
333 | |
303 | die if $@ && !ref $@; |
334 | die if $@ && !ref $@; |
304 | |
335 | |
305 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
336 | last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; |
306 | |
337 | |
307 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
|
|
308 | $fh->timeout($::PER_TIMEOUT); |
338 | $fh->timeout($::PER_TIMEOUT); |
309 | } |
339 | } |
310 | } |
340 | } |
311 | |
341 | |
312 | # uri => path mapping |
342 | # uri => path mapping |
313 | sub map_uri { |
343 | sub map_uri { |
314 | my $self = shift; |
344 | my $self = shift; |
315 | my $host = $self->{h}{host} || "default"; |
345 | my $host = $self->{server_name}; |
316 | my $uri = $self->{uri}; |
346 | my $uri = $self->{uri}; |
317 | |
347 | |
318 | # some massaging, also makes it more secure |
348 | # some massaging, also makes it more secure |
319 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
349 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
320 | $uri =~ s%//+%/%g; |
350 | $uri =~ s%//+%/%g; |
… | |
… | |
330 | $self->{path} = "$::DOCROOT/$host$uri"; |
360 | $self->{path} = "$::DOCROOT/$host$uri"; |
331 | |
361 | |
332 | $self->access_check; |
362 | $self->access_check; |
333 | } |
363 | } |
334 | |
364 | |
335 | sub server_address { |
|
|
336 | my $self = shift; |
|
|
337 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
|
|
338 | or $self->err(500, "unable to get socket name"); |
|
|
339 | ((inet_ntoa $iaddr), $port); |
|
|
340 | } |
|
|
341 | |
|
|
342 | sub server_host { |
|
|
343 | my $self = shift; |
|
|
344 | if (exists $self->{h}{host}) { |
|
|
345 | return $self->{h}{host}; |
|
|
346 | } else { |
|
|
347 | return (($self->server_address)[0]); |
|
|
348 | } |
|
|
349 | } |
|
|
350 | |
|
|
351 | sub server_hostport { |
|
|
352 | my $self = shift; |
|
|
353 | my ($host, $port); |
|
|
354 | if (exists $self->{h}{host}) { |
|
|
355 | ($host, $port) = ($self->{h}{host}, $self->{server_port}); |
|
|
356 | } else { |
|
|
357 | ($host, $port) = $self->server_address; |
|
|
358 | } |
|
|
359 | $port = $port == 80 ? "" : ":$port"; |
|
|
360 | $host.$port; |
|
|
361 | } |
|
|
362 | |
|
|
363 | sub _cgi { |
365 | sub _cgi { |
364 | my $self = shift; |
366 | my $self = shift; |
365 | my $path = shift; |
367 | my $path = shift; |
366 | my $fh; |
368 | my $fh; |
367 | |
369 | |
368 | # no two-way xxx supported |
370 | # no two-way xxx supported |
369 | if (0 == fork) { |
371 | if (0 == fork) { |
370 | open STDOUT, ">&".fileno($self->{fh}); |
372 | open STDOUT, ">&".fileno($self->{fh}); |
371 | if (chdir $::DOCROOT) { |
373 | if (chdir $::DOCROOT) { |
372 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
374 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
373 | $ENV{HTTP_HOST} = $self->server_host; |
375 | $ENV{HTTP_HOST} = $self->{server_name}; |
374 | $ENV{HTTP_PORT} = $self->{server_host}; |
376 | $ENV{HTTP_PORT} = $self->{server_port}; |
375 | $ENV{SCRIPT_NAME} = $self->{name}; |
377 | $ENV{SCRIPT_NAME} = $self->{name}; |
376 | exec $path; |
378 | exec $path; |
377 | } |
379 | } |
378 | Coro::State::_exit(0); |
380 | Coro::State::_exit(0); |
379 | } else { |
381 | } else { |
|
|
382 | die; |
380 | } |
383 | } |
|
|
384 | } |
|
|
385 | |
|
|
386 | sub server_hostport { |
|
|
387 | $_[0]{server_port} == 80 |
|
|
388 | ? $_[0]{server_name} |
|
|
389 | : "$_[0]{server_name}:$_[0]{server_port}"; |
381 | } |
390 | } |
382 | |
391 | |
383 | sub respond { |
392 | sub respond { |
384 | my $self = shift; |
393 | my $self = shift; |
385 | my $path = $self->{path}; |
394 | my $path = $self->{path}; |
… | |
… | |
395 | |
404 | |
396 | if (-d _ && -r _) { |
405 | if (-d _ && -r _) { |
397 | # directory |
406 | # directory |
398 | if ($path !~ /\/$/) { |
407 | if ($path !~ /\/$/) { |
399 | # create a redirect to get the trailing "/" |
408 | # create a redirect to get the trailing "/" |
400 | my $host = $self->server_hostport; |
409 | # we don't try to avoid the :80 |
401 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
410 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
402 | } else { |
411 | } else { |
403 | $ims < $self->{stat}[9] |
412 | $ims < $self->{stat}[9] |
404 | or $self->err(304, "not modified"); |
413 | or $self->err(304, "not modified"); |
405 | |
414 | |
406 | if (-r "$path/index.html") { |
415 | if (-r "$path/index.html") { |
… | |
… | |
458 | $self->err(416, "not satisfiable", $hdr, ""); |
467 | $self->err(416, "not satisfiable", $hdr, ""); |
459 | |
468 | |
460 | satisfiable: |
469 | satisfiable: |
461 | # check for segmented downloads |
470 | # check for segmented downloads |
462 | if ($l && $::NO_SEGMENTED) { |
471 | if ($l && $::NO_SEGMENTED) { |
|
|
472 | my $delay = 60; |
463 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
473 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
|
|
474 | if ($delay <= 0) { |
464 | $self->err(400, "segmented downloads are not allowed"); |
475 | $self->err(400, "segmented downloads are not allowed", |
|
|
476 | { "Content-Type" => "text/html", Connection => "close" }, <<EOF); |
|
|
477 | <html> |
|
|
478 | <head> |
|
|
479 | <title>Segmented downloads are not allowed</title> |
|
|
480 | </head> |
|
|
481 | <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000"> |
|
|
482 | |
|
|
483 | <p>Segmented downloads are not allowed on this server. Please refer to the |
|
|
484 | <a href="http://www.goof.com/pcg/marc/animefaq.html#segmented_downloads">FAQ</a>.</p> |
|
|
485 | |
|
|
486 | </body></html> |
|
|
487 | EOF |
|
|
488 | } else { |
|
|
489 | Coro::Event::do_timer(after => 3); $delay -= 3; |
|
|
490 | } |
465 | } |
491 | } |
466 | } |
492 | } |
467 | |
493 | |
468 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
494 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
469 | @code = (206, "partial content"); |
495 | @code = (206, "partial content"); |
… | |
… | |
497 | while ($h > 0) { |
523 | while ($h > 0) { |
498 | if (0) { |
524 | if (0) { |
499 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
525 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
500 | or last; |
526 | or last; |
501 | } else { |
527 | } else { |
|
|
528 | undef $buf; |
|
|
529 | $aio_requests->down; |
502 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
530 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
503 | $buf, 0, sub { |
531 | $buf, 0, sub { |
504 | $r = $_[0]; |
532 | $r = $_[0]; |
505 | $current->ready; |
533 | $current->ready; |
506 | }); |
534 | }); |
507 | &Coro::schedule; |
535 | &Coro::schedule; |
|
|
536 | $aio_requests->up; |
508 | last unless $r; |
537 | last unless $r; |
509 | } |
538 | } |
510 | my $w = $self->{fh}->syswrite($buf) |
539 | my $w = $self->{fh}->syswrite($buf) |
511 | or last; |
540 | or last; |
512 | $::written += $w; |
541 | $::written += $w; |