… | |
… | |
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}{fh} => connobj |
84 | our %blocked; |
84 | our %blocked; |
85 | our %mimetype; |
85 | our %mimetype; |
86 | |
86 | |
… | |
… | |
149 | } |
149 | } |
150 | $res .= "\015\012"; |
150 | $res .= "\015\012"; |
151 | |
151 | |
152 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
152 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
153 | |
153 | |
154 | 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"; |
155 | |
155 | |
156 | $self->{written} += |
156 | $self->{written} += |
157 | print {$self->{fh}} $res; |
157 | print {$self->{fh}} $res; |
158 | } |
158 | } |
159 | |
159 | |
… | |
… | |
176 | sub err_blocked { |
176 | sub err_blocked { |
177 | my $self = shift; |
177 | my $self = shift; |
178 | my $ip = $self->{remote_addr}; |
178 | my $ip = $self->{remote_addr}; |
179 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
179 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
180 | |
180 | |
181 | Coro::Event::do_timer(after => 15); |
181 | Coro::Event::do_timer(after => 20*rand); |
182 | |
182 | |
183 | $self->err(401, "too many connections", |
183 | $self->err(401, "too many connections", |
184 | { |
184 | { |
185 | "Content-Type" => "text/html", |
185 | "Content-Type" => "text/html", |
186 | "Retry-After" => $::BLOCKTIME |
186 | "Retry-After" => $::BLOCKTIME, |
|
|
187 | "Warning" => "Please do NOT retry, you have been blocked", |
|
|
188 | "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", |
187 | }, |
189 | }, |
188 | <<EOF); |
190 | <<EOF); |
189 | <html><p> |
191 | <html><p> |
190 | You have been blocked because you opened too many connections. You |
192 | You have been blocked because you opened too many connections. You |
191 | may retry at</p> |
193 | may retry at</p> |
… | |
… | |
243 | |
245 | |
244 | $self->{method} = $1; |
246 | $self->{method} = $1; |
245 | $self->{uri} = $2; |
247 | $self->{uri} = $2; |
246 | $self->{version} = $3; |
248 | $self->{version} = $3; |
247 | |
249 | |
248 | $3 eq "1.0" or $3 eq "1.1" |
250 | $3 =~ /^1\./ |
249 | or $self->err(506, "http protocol version $3 not supported"); |
251 | or $self->err(506, "http protocol version $3 not supported"); |
250 | |
252 | |
251 | # parse headers |
253 | # parse headers |
252 | { |
254 | { |
253 | my (%hdr, $h, $v); |
255 | my (%hdr, $h, $v); |
… | |
… | |
269 | |
271 | |
270 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
272 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
271 | |
273 | |
272 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
274 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
273 | |
275 | |
|
|
276 | eval { |
274 | $self->map_uri; |
277 | $self->map_uri; |
275 | $self->respond; |
278 | $self->respond; |
|
|
279 | }; |
|
|
280 | |
|
|
281 | die if $@ && !ref $@; |
276 | |
282 | |
277 | $self->eoconn; |
283 | $self->eoconn; |
278 | |
284 | |
279 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
285 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
280 | |
286 | |
… | |
… | |
375 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
381 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
376 | } else { |
382 | } else { |
377 | $ims < $self->{stat}[9] |
383 | $ims < $self->{stat}[9] |
378 | or $self->err(304, "not modified"); |
384 | or $self->err(304, "not modified"); |
379 | |
385 | |
380 | if ($self->{method} eq "GET") { |
|
|
381 | if (-r "$path/index.html") { |
386 | if (-r "$path/index.html") { |
382 | $self->{path} .= "/index.html"; |
387 | $self->{path} .= "/index.html"; |
383 | $self->handle_file; |
388 | $self->handle_file; |
384 | } else { |
389 | } else { |
385 | $self->handle_dir; |
390 | $self->handle_dir; |
386 | } |
|
|
387 | } |
391 | } |
388 | } |
392 | } |
389 | } elsif (-f _ && -r _) { |
393 | } elsif (-f _ && -r _) { |
390 | -x _ and $self->err(403, "forbidden"); |
394 | -x _ and $self->err(403, "forbidden"); |
391 | $self->handle_file; |
395 | $self->handle_file; |
… | |
… | |
427 | goto ignore; |
431 | goto ignore; |
428 | } |
432 | } |
429 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
433 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
430 | } |
434 | } |
431 | $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"}.")"); |
432 | $self->err(416, "not satisfiable", $hdr); |
438 | $self->err(416, "not satisfiable", $hdr, ""); |
433 | |
439 | |
434 | satisfiable: |
440 | satisfiable: |
435 | # check for segmented downloads |
441 | # check for segmented downloads |
436 | if ($l && $::NO_SEGMENTED) { |
442 | if ($l && $::NO_SEGMENTED) { |
437 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
443 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |