… | |
… | |
76 | |
76 | |
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 | |
82 | |
83 | our %conn; # $conn{ip}{fh} => connobj |
83 | our %conn; # $conn{ip}{self} => connobj |
|
|
84 | our %uri; # $uri{ip}{uri}{self} |
84 | our %blocked; |
85 | our %blocked; |
85 | our %mimetype; |
86 | our %mimetype; |
86 | |
87 | |
87 | sub read_mimetypes { |
88 | sub read_mimetypes { |
88 | local *M; |
89 | local *M; |
… | |
… | |
127 | delete $conn{$self->{remote_addr}}{$self*1}; |
128 | delete $conn{$self->{remote_addr}}{$self*1}; |
128 | } |
129 | } |
129 | |
130 | |
130 | # end of connection |
131 | # end of connection |
131 | sub eoconn { |
132 | sub eoconn { |
|
|
133 | my $self = shift; |
132 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
134 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
133 | } |
135 | } |
134 | |
136 | |
135 | sub slog { |
137 | sub slog { |
136 | my $self = shift; |
138 | my $self = shift; |
… | |
… | |
149 | } |
151 | } |
150 | $res .= "\015\012"; |
152 | $res .= "\015\012"; |
151 | |
153 | |
152 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
154 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
153 | |
155 | |
154 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
156 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
155 | |
157 | |
156 | $self->{written} += |
158 | $self->{written} += |
157 | print {$self->{fh}} $res; |
159 | print {$self->{fh}} $res; |
158 | } |
160 | } |
159 | |
161 | |
… | |
… | |
271 | |
273 | |
272 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
274 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
273 | |
275 | |
274 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
276 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
275 | |
277 | |
|
|
278 | eval { |
276 | $self->map_uri; |
279 | $self->map_uri; |
277 | $self->respond; |
280 | $self->respond; |
|
|
281 | }; |
278 | |
282 | |
279 | $self->eoconn; |
283 | $self->eoconn; |
|
|
284 | |
|
|
285 | die if $@ && !ref $@; |
280 | |
286 | |
281 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
287 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
282 | |
288 | |
283 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
289 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
284 | $fh->timeout($::PER_TIMEOUT); |
290 | $fh->timeout($::PER_TIMEOUT); |
… | |
… | |
377 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
383 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
378 | } else { |
384 | } else { |
379 | $ims < $self->{stat}[9] |
385 | $ims < $self->{stat}[9] |
380 | or $self->err(304, "not modified"); |
386 | or $self->err(304, "not modified"); |
381 | |
387 | |
382 | if ($self->{method} eq "GET") { |
|
|
383 | if (-r "$path/index.html") { |
388 | if (-r "$path/index.html") { |
384 | $self->{path} .= "/index.html"; |
389 | $self->{path} .= "/index.html"; |
385 | $self->handle_file; |
390 | $self->handle_file; |
386 | } else { |
391 | } else { |
387 | $self->handle_dir; |
392 | $self->handle_dir; |
388 | } |
|
|
389 | } |
393 | } |
390 | } |
394 | } |
391 | } elsif (-f _ && -r _) { |
395 | } elsif (-f _ && -r _) { |
392 | -x _ and $self->err(403, "forbidden"); |
396 | -x _ and $self->err(403, "forbidden"); |
393 | $self->handle_file; |
397 | $self->handle_file; |
… | |
… | |
426 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
430 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
427 | } else { |
431 | } else { |
428 | ($l, $h) = (0, $length - 1); |
432 | ($l, $h) = (0, $length - 1); |
429 | goto ignore; |
433 | goto ignore; |
430 | } |
434 | } |
431 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
435 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
432 | } |
436 | } |
433 | $hdr->{"Content-Range"} = "bytes */$length"; |
437 | $hdr->{"Content-Range"} = "bytes */$length"; |
|
|
438 | $hdr->{"Content-Length"} = $length; |
434 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
439 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
435 | $self->err(416, "not satisfiable", $hdr); |
440 | $self->err(416, "not satisfiable", $hdr, ""); |
436 | |
441 | |
437 | satisfiable: |
442 | satisfiable: |
438 | # check for segmented downloads |
443 | # check for segmented downloads |
439 | if ($l && $::NO_SEGMENTED) { |
444 | if ($l && $::NO_SEGMENTED) { |
440 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
445 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
441 | Coro::Event::do_timer(after => 15); |
|
|
442 | |
|
|
443 | $self->err(400, "segmented downloads are not allowed"); |
446 | $self->err(400, "segmented downloads are not allowed"); |
444 | } |
447 | } |
445 | } |
448 | } |
446 | |
449 | |
447 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
450 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |