… | |
… | |
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 | |
… | |
… | |
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); |
… | |
… | |
264 | |
271 | |
265 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
272 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
266 | |
273 | |
267 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
274 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
268 | |
275 | |
|
|
276 | eval { |
269 | $self->map_uri; |
277 | $self->map_uri; |
270 | $self->respond; |
278 | $self->respond; |
|
|
279 | }; |
|
|
280 | |
|
|
281 | die if $@ && !ref $@; |
|
|
282 | |
|
|
283 | $self->eoconn; |
271 | |
284 | |
272 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
285 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
273 | |
286 | |
274 | $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
287 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
275 | $fh->timeout($::PER_TIMEOUT); |
288 | $fh->timeout($::PER_TIMEOUT); |
276 | } |
289 | } |
277 | } |
290 | } |
278 | |
291 | |
279 | # uri => path mapping |
292 | # uri => path mapping |
… | |
… | |
420 | goto ignore; |
433 | goto ignore; |
421 | } |
434 | } |
422 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
435 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
423 | } |
436 | } |
424 | $hdr->{"Content-Range"} = "bytes */$length"; |
437 | $hdr->{"Content-Range"} = "bytes */$length"; |
|
|
438 | $hdr->{"Content-Length"} = $length; |
|
|
439 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
425 | $self->err(416, "not satisfiable", $hdr); |
440 | $self->err(416, "not satisfiable", $hdr, ""); |
426 | |
441 | |
427 | satisfiable: |
442 | satisfiable: |
428 | # check for segmented downloads |
443 | # check for segmented downloads |
429 | if ($l && $::NO_SEGMENTED) { |
444 | if ($l && $::NO_SEGMENTED) { |
430 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
445 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
… | |
… | |
455 | open $fh, "<", $self->{path} |
470 | open $fh, "<", $self->{path} |
456 | or die "$self->{path}: late open failure ($!)"; |
471 | or die "$self->{path}: late open failure ($!)"; |
457 | |
472 | |
458 | $h -= $l - 1; |
473 | $h -= $l - 1; |
459 | |
474 | |
|
|
475 | if (0) { |
|
|
476 | if ($l) { |
|
|
477 | sysseek $fh, $l, 0; |
|
|
478 | } |
|
|
479 | } |
|
|
480 | |
460 | while ($h > 0) { |
481 | while ($h > 0) { |
|
|
482 | if (0) { |
|
|
483 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
|
|
484 | or last; |
|
|
485 | } else { |
461 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
486 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
462 | $buf, 0, sub { |
487 | $buf, 0, sub { |
463 | $r = $_[0]; |
488 | $r = $_[0]; |
464 | $current->ready; |
489 | $current->ready; |
465 | }); |
490 | }); |
466 | &Coro::schedule; |
491 | &Coro::schedule; |
467 | last unless $r; |
492 | last unless $r; |
|
|
493 | } |
468 | my $w = $self->{fh}->syswrite($buf) |
494 | my $w = $self->{fh}->syswrite($buf) |
469 | or last; |
495 | or last; |
470 | $::written += $w; |
496 | $::written += $w; |
471 | $self->{written} += $w; |
497 | $self->{written} += $w; |
472 | $l += $r; |
498 | $l += $r; |