… | |
… | |
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"; |
… | |
… | |
224 | |
226 | |
225 | sub handle { |
227 | sub handle { |
226 | my $self = shift; |
228 | my $self = shift; |
227 | my $fh = $self->{fh}; |
229 | my $fh = $self->{fh}; |
228 | |
230 | |
|
|
231 | my $host; |
|
|
232 | |
229 | $fh->timeout($::REQ_TIMEOUT); |
233 | $fh->timeout($::REQ_TIMEOUT); |
230 | while() { |
234 | while() { |
231 | $self->{reqs}++; |
235 | $self->{reqs}++; |
232 | |
236 | |
233 | # read request and parse first line |
237 | # read request and parse first line |
… | |
… | |
289 | |
293 | |
290 | $self->{h}{$h} = substr $v, 1 |
294 | $self->{h}{$h} = substr $v, 1 |
291 | while ($h, $v) = each %hdr; |
295 | while ($h, $v) = each %hdr; |
292 | } |
296 | } |
293 | |
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) { |
294 | $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 | } |
295 | |
324 | |
296 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
325 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
297 | |
326 | |
298 | eval { |
327 | eval { |
299 | $self->map_uri; |
328 | $self->map_uri; |
… | |
… | |
302 | |
331 | |
303 | $self->eoconn; |
332 | $self->eoconn; |
304 | |
333 | |
305 | die if $@ && !ref $@; |
334 | die if $@ && !ref $@; |
306 | |
335 | |
307 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
336 | last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; |
308 | |
337 | |
309 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
|
|
310 | $fh->timeout($::PER_TIMEOUT); |
338 | $fh->timeout($::PER_TIMEOUT); |
311 | } |
339 | } |
312 | } |
340 | } |
313 | |
341 | |
314 | # uri => path mapping |
342 | # uri => path mapping |
315 | sub map_uri { |
343 | sub map_uri { |
316 | my $self = shift; |
344 | my $self = shift; |
317 | my $host = $self->{h}{host} || "default"; |
345 | my $host = $self->{server_name}; |
318 | my $uri = $self->{uri}; |
346 | my $uri = $self->{uri}; |
319 | |
347 | |
320 | # some massaging, also makes it more secure |
348 | # some massaging, also makes it more secure |
321 | $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; |
322 | $uri =~ s%//+%/%g; |
350 | $uri =~ s%//+%/%g; |
… | |
… | |
332 | $self->{path} = "$::DOCROOT/$host$uri"; |
360 | $self->{path} = "$::DOCROOT/$host$uri"; |
333 | |
361 | |
334 | $self->access_check; |
362 | $self->access_check; |
335 | } |
363 | } |
336 | |
364 | |
337 | sub server_address { |
|
|
338 | my $self = shift; |
|
|
339 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
|
|
340 | or $self->err(500, "unable to get socket name"); |
|
|
341 | ((inet_ntoa $iaddr), $port); |
|
|
342 | } |
|
|
343 | |
|
|
344 | sub server_host { |
|
|
345 | my $self = shift; |
|
|
346 | if (exists $self->{h}{host}) { |
|
|
347 | return $self->{h}{host}; |
|
|
348 | } else { |
|
|
349 | return (($self->server_address)[0]); |
|
|
350 | } |
|
|
351 | } |
|
|
352 | |
|
|
353 | sub server_hostport { |
|
|
354 | my $self = shift; |
|
|
355 | my ($host, $port); |
|
|
356 | if (exists $self->{h}{host}) { |
|
|
357 | ($host, $port) = ($self->{h}{host}, $self->{server_port}); |
|
|
358 | } else { |
|
|
359 | ($host, $port) = $self->server_address; |
|
|
360 | } |
|
|
361 | $port = $port == 80 ? "" : ":$port"; |
|
|
362 | $host.$port; |
|
|
363 | } |
|
|
364 | |
|
|
365 | sub _cgi { |
365 | sub _cgi { |
366 | my $self = shift; |
366 | my $self = shift; |
367 | my $path = shift; |
367 | my $path = shift; |
368 | my $fh; |
368 | my $fh; |
369 | |
369 | |
370 | # no two-way xxx supported |
370 | # no two-way xxx supported |
371 | if (0 == fork) { |
371 | if (0 == fork) { |
372 | open STDOUT, ">&".fileno($self->{fh}); |
372 | open STDOUT, ">&".fileno($self->{fh}); |
373 | if (chdir $::DOCROOT) { |
373 | if (chdir $::DOCROOT) { |
374 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
374 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
375 | $ENV{HTTP_HOST} = $self->server_host; |
375 | $ENV{HTTP_HOST} = $self->{server_name}; |
376 | $ENV{HTTP_PORT} = $self->{server_host}; |
376 | $ENV{HTTP_PORT} = $self->{server_port}; |
377 | $ENV{SCRIPT_NAME} = $self->{name}; |
377 | $ENV{SCRIPT_NAME} = $self->{name}; |
378 | exec $path; |
378 | exec $path; |
379 | } |
379 | } |
380 | Coro::State::_exit(0); |
380 | Coro::State::_exit(0); |
381 | } else { |
381 | } else { |
|
|
382 | die; |
382 | } |
383 | } |
|
|
384 | } |
|
|
385 | |
|
|
386 | sub server_hostport { |
|
|
387 | $_[0]{server_port} == 80 |
|
|
388 | ? $_[0]{server_name} |
|
|
389 | : "$_[0]{server_name}:$_[0]{server_port}"; |
383 | } |
390 | } |
384 | |
391 | |
385 | sub respond { |
392 | sub respond { |
386 | my $self = shift; |
393 | my $self = shift; |
387 | my $path = $self->{path}; |
394 | my $path = $self->{path}; |
… | |
… | |
397 | |
404 | |
398 | if (-d _ && -r _) { |
405 | if (-d _ && -r _) { |
399 | # directory |
406 | # directory |
400 | if ($path !~ /\/$/) { |
407 | if ($path !~ /\/$/) { |
401 | # create a redirect to get the trailing "/" |
408 | # create a redirect to get the trailing "/" |
402 | my $host = $self->server_hostport; |
409 | # we don't try to avoid the :80 |
403 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
410 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
404 | } else { |
411 | } else { |
405 | $ims < $self->{stat}[9] |
412 | $ims < $self->{stat}[9] |
406 | or $self->err(304, "not modified"); |
413 | or $self->err(304, "not modified"); |
407 | |
414 | |
408 | if (-r "$path/index.html") { |
415 | if (-r "$path/index.html") { |
… | |
… | |
460 | $self->err(416, "not satisfiable", $hdr, ""); |
467 | $self->err(416, "not satisfiable", $hdr, ""); |
461 | |
468 | |
462 | satisfiable: |
469 | satisfiable: |
463 | # check for segmented downloads |
470 | # check for segmented downloads |
464 | if ($l && $::NO_SEGMENTED) { |
471 | if ($l && $::NO_SEGMENTED) { |
|
|
472 | my $delay = 60; |
465 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
473 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
|
|
474 | if ($delay <= 0) { |
466 | $self->err(400, "segmented downloads are not allowed", |
475 | $self->err(400, "segmented downloads are not allowed", |
467 | { "Content-Type" => "text/html", Connection => "close" }, <<EOF); |
476 | { "Content-Type" => "text/html", Connection => "close" }, <<EOF); |
468 | <html> |
477 | <html> |
469 | <head> |
478 | <head> |
470 | <title>Segmented downloads are not allowed</title> |
479 | <title>Segmented downloads are not allowed</title> |
471 | </head> |
480 | </head> |
472 | <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000"> |
481 | <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000"> |
… | |
… | |
474 | <p>Segmented downloads are not allowed on this server. Please refer to the |
483 | <p>Segmented downloads are not allowed on this server. Please refer to the |
475 | <a href="http://www.goof.com/pcg/marc/animefaq.html#segmented_downloads">FAQ</a>.</p> |
484 | <a href="http://www.goof.com/pcg/marc/animefaq.html#segmented_downloads">FAQ</a>.</p> |
476 | |
485 | |
477 | </body></html> |
486 | </body></html> |
478 | EOF |
487 | EOF |
479 | EOF |
488 | } else { |
|
|
489 | Coro::Event::do_timer(after => 3); $delay -= 3; |
|
|
490 | } |
480 | } |
491 | } |
481 | } |
492 | } |
482 | |
493 | |
483 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
494 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
484 | @code = (206, "partial content"); |
495 | @code = (206, "partial content"); |
… | |
… | |
512 | while ($h > 0) { |
523 | while ($h > 0) { |
513 | if (0) { |
524 | if (0) { |
514 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
525 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
515 | or last; |
526 | or last; |
516 | } else { |
527 | } else { |
|
|
528 | undef $buf; |
|
|
529 | $aio_requests->down; |
517 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
530 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
518 | $buf, 0, sub { |
531 | $buf, 0, sub { |
519 | $r = $_[0]; |
532 | $r = $_[0]; |
520 | $current->ready; |
533 | $current->ready; |
521 | }); |
534 | }); |
522 | &Coro::schedule; |
535 | &Coro::schedule; |
|
|
536 | $aio_requests->up; |
523 | last unless $r; |
537 | last unless $r; |
524 | } |
538 | } |
525 | my $w = $self->{fh}->syswrite($buf) |
539 | my $w = $self->{fh}->syswrite($buf) |
526 | or last; |
540 | or last; |
527 | $::written += $w; |
541 | $::written += $w; |