… | |
… | |
77 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
77 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
78 | |
78 | |
79 | Event->io(fd => Linux::AIO::poll_fileno, |
79 | Event->io(fd => Linux::AIO::poll_fileno, |
80 | poll => 'r', async => 1, |
80 | poll => 'r', async => 1, |
81 | cb => \&Linux::AIO::poll_cb); |
81 | cb => \&Linux::AIO::poll_cb); |
82 | my $scheduler = Event->idle( |
|
|
83 | max => 0, min => 0, prio => 4, parked => 1, |
|
|
84 | cb => \&Coro::schedule); |
|
|
85 | |
82 | |
86 | our %conn; # $conn{ip}{fh} => connobj |
83 | our %conn; # $conn{ip}{fh} => connobj |
87 | our %blocked; |
84 | our %blocked; |
88 | our %mimetype; |
85 | our %mimetype; |
89 | |
86 | |
… | |
… | |
152 | } |
149 | } |
153 | $res .= "\015\012"; |
150 | $res .= "\015\012"; |
154 | |
151 | |
155 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
152 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
156 | |
153 | |
157 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
154 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
158 | |
155 | |
159 | $self->{written} += |
156 | $self->{written} += |
160 | print {$self->{fh}} $res; |
157 | print {$self->{fh}} $res; |
161 | } |
158 | } |
162 | |
159 | |
… | |
… | |
274 | |
271 | |
275 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
272 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
276 | |
273 | |
277 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
274 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
278 | |
275 | |
|
|
276 | eval { |
279 | $self->map_uri; |
277 | $self->map_uri; |
280 | $self->respond; |
278 | $self->respond; |
|
|
279 | }; |
|
|
280 | |
|
|
281 | die if $@ && !ref $@; |
281 | |
282 | |
282 | $self->eoconn; |
283 | $self->eoconn; |
283 | |
284 | |
284 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
285 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
285 | |
286 | |
… | |
… | |
380 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
381 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
381 | } else { |
382 | } else { |
382 | $ims < $self->{stat}[9] |
383 | $ims < $self->{stat}[9] |
383 | or $self->err(304, "not modified"); |
384 | or $self->err(304, "not modified"); |
384 | |
385 | |
385 | if ($self->{method} eq "GET") { |
|
|
386 | if (-r "$path/index.html") { |
386 | if (-r "$path/index.html") { |
387 | $self->{path} .= "/index.html"; |
387 | $self->{path} .= "/index.html"; |
388 | $self->handle_file; |
388 | $self->handle_file; |
389 | } else { |
389 | } else { |
390 | $self->handle_dir; |
390 | $self->handle_dir; |
391 | } |
|
|
392 | } |
391 | } |
393 | } |
392 | } |
394 | } elsif (-f _ && -r _) { |
393 | } elsif (-f _ && -r _) { |
395 | -x _ and $self->err(403, "forbidden"); |
394 | -x _ and $self->err(403, "forbidden"); |
396 | $self->handle_file; |
395 | $self->handle_file; |
… | |
… | |
432 | goto ignore; |
431 | goto ignore; |
433 | } |
432 | } |
434 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
433 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
435 | } |
434 | } |
436 | $hdr->{"Content-Range"} = "bytes */$length"; |
435 | $hdr->{"Content-Range"} = "bytes */$length"; |
|
|
436 | $hdr->{"Content-Length"} = $length; |
437 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
437 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
438 | $self->err(416, "not satisfiable", $hdr); |
438 | $self->err(416, "not satisfiable", $hdr, ""); |
439 | |
439 | |
440 | satisfiable: |
440 | satisfiable: |
441 | # check for segmented downloads |
441 | # check for segmented downloads |
442 | if ($l && $::NO_SEGMENTED) { |
442 | if ($l && $::NO_SEGMENTED) { |
443 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
443 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
… | |
… | |
483 | } else { |
483 | } else { |
484 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
484 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
485 | $buf, 0, sub { |
485 | $buf, 0, sub { |
486 | $r = $_[0]; |
486 | $r = $_[0]; |
487 | $current->ready; |
487 | $current->ready; |
488 | $scheduler->now; |
|
|
489 | }); |
488 | }); |
490 | &Coro::schedule; |
489 | &Coro::schedule; |
491 | last unless $r; |
490 | last unless $r; |
492 | } |
491 | } |
493 | my $w = $self->{fh}->syswrite($buf) |
492 | my $w = $self->{fh}->syswrite($buf) |