… | |
… | |
121 | sub DESTROY { |
121 | sub DESTROY { |
122 | my $self = shift; |
122 | my $self = shift; |
123 | |
123 | |
124 | $::conns--; |
124 | $::conns--; |
125 | |
125 | |
|
|
126 | $self->eoconn; |
126 | delete $conn{$self->{remote_addr}}{$self*1}; |
127 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
128 | } |
|
|
129 | |
|
|
130 | # end of connection |
|
|
131 | sub eoconn { |
127 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
132 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
128 | } |
133 | } |
129 | |
134 | |
130 | sub slog { |
135 | sub slog { |
131 | my $self = shift; |
136 | my $self = shift; |
… | |
… | |
171 | sub err_blocked { |
176 | sub err_blocked { |
172 | my $self = shift; |
177 | my $self = shift; |
173 | my $ip = $self->{remote_addr}; |
178 | my $ip = $self->{remote_addr}; |
174 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
179 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
175 | |
180 | |
176 | Coro::Event::do_timer(after => 15); |
181 | Coro::Event::do_timer(after => 20*rand); |
177 | |
182 | |
178 | $self->err(401, "too many connections", |
183 | $self->err(401, "too many connections", |
179 | { |
184 | { |
180 | "Content-Type" => "text/html", |
185 | "Content-Type" => "text/html", |
181 | "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\"", |
182 | }, |
189 | }, |
183 | <<EOF); |
190 | <<EOF); |
184 | <html><p> |
191 | <html><p> |
185 | You have been blocked because you opened too many connections. You |
192 | You have been blocked because you opened too many connections. You |
186 | may retry at</p> |
193 | may retry at</p> |
… | |
… | |
238 | |
245 | |
239 | $self->{method} = $1; |
246 | $self->{method} = $1; |
240 | $self->{uri} = $2; |
247 | $self->{uri} = $2; |
241 | $self->{version} = $3; |
248 | $self->{version} = $3; |
242 | |
249 | |
243 | $3 eq "1.0" or $3 eq "1.1" |
250 | $3 =~ /^1\./ |
244 | or $self->err(506, "http protocol version $3 not supported"); |
251 | or $self->err(506, "http protocol version $3 not supported"); |
245 | |
252 | |
246 | # parse headers |
253 | # parse headers |
247 | { |
254 | { |
248 | my (%hdr, $h, $v); |
255 | my (%hdr, $h, $v); |
… | |
… | |
267 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
274 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
268 | |
275 | |
269 | $self->map_uri; |
276 | $self->map_uri; |
270 | $self->respond; |
277 | $self->respond; |
271 | |
278 | |
|
|
279 | $self->eoconn; |
|
|
280 | |
272 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
281 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
273 | |
282 | |
274 | $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
283 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
275 | $fh->timeout($::PER_TIMEOUT); |
284 | $fh->timeout($::PER_TIMEOUT); |
276 | } |
285 | } |
277 | } |
286 | } |
278 | |
287 | |
279 | # uri => path mapping |
288 | # uri => path mapping |
… | |
… | |
420 | goto ignore; |
429 | goto ignore; |
421 | } |
430 | } |
422 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
431 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
423 | } |
432 | } |
424 | $hdr->{"Content-Range"} = "bytes */$length"; |
433 | $hdr->{"Content-Range"} = "bytes */$length"; |
|
|
434 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
425 | $self->err(416, "not satisfiable", $hdr); |
435 | $self->err(416, "not satisfiable", $hdr); |
426 | |
436 | |
427 | satisfiable: |
437 | satisfiable: |
428 | # check for segmented downloads |
438 | # check for segmented downloads |
429 | if ($l && $::NO_SEGMENTED) { |
439 | if ($l && $::NO_SEGMENTED) { |
… | |
… | |
455 | open $fh, "<", $self->{path} |
465 | open $fh, "<", $self->{path} |
456 | or die "$self->{path}: late open failure ($!)"; |
466 | or die "$self->{path}: late open failure ($!)"; |
457 | |
467 | |
458 | $h -= $l - 1; |
468 | $h -= $l - 1; |
459 | |
469 | |
|
|
470 | if (0) { |
|
|
471 | if ($l) { |
|
|
472 | sysseek $fh, $l, 0; |
|
|
473 | } |
|
|
474 | } |
|
|
475 | |
460 | while ($h > 0) { |
476 | while ($h > 0) { |
|
|
477 | if (0) { |
|
|
478 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
|
|
479 | or last; |
|
|
480 | } else { |
461 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
481 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
462 | $buf, 0, sub { |
482 | $buf, 0, sub { |
463 | $r = $_[0]; |
483 | $r = $_[0]; |
464 | $current->ready; |
484 | $current->ready; |
465 | }); |
485 | }); |
466 | &Coro::schedule; |
486 | &Coro::schedule; |
467 | last unless $r; |
487 | last unless $r; |
|
|
488 | } |
468 | my $w = $self->{fh}->syswrite($buf) |
489 | my $w = $self->{fh}->syswrite($buf) |
469 | or last; |
490 | or last; |
470 | $::written += $w; |
491 | $::written += $w; |
471 | $self->{written} += $w; |
492 | $self->{written} += $w; |
472 | $l += $r; |
493 | $l += $r; |