… | |
… | |
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 | my $scheduler = Event->idle( |
83 | Event->add_hooks(prepare => sub { |
83 | max => 0, min => 0, prio => 4, parked => 1, |
84 | &Coro::cede; |
84 | cb => \&Coro::schedule); |
85 | }); |
|
|
86 | |
85 | |
87 | our %conn; # $conn{ip}{fh} => connobj |
86 | our %conn; # $conn{ip}{fh} => connobj |
88 | our %blocked; |
87 | our %blocked; |
89 | our %mimetype; |
88 | our %mimetype; |
90 | |
89 | |
… | |
… | |
125 | sub DESTROY { |
124 | sub DESTROY { |
126 | my $self = shift; |
125 | my $self = shift; |
127 | |
126 | |
128 | $::conns--; |
127 | $::conns--; |
129 | |
128 | |
|
|
129 | $self->eoconn; |
130 | delete $conn{$self->{remote_addr}}{$self*1}; |
130 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
131 | } |
|
|
132 | |
|
|
133 | # end of connection |
|
|
134 | sub eoconn { |
131 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
135 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
132 | } |
136 | } |
133 | |
137 | |
134 | sub slog { |
138 | sub slog { |
135 | my $self = shift; |
139 | my $self = shift; |
… | |
… | |
175 | sub err_blocked { |
179 | sub err_blocked { |
176 | my $self = shift; |
180 | my $self = shift; |
177 | my $ip = $self->{remote_addr}; |
181 | my $ip = $self->{remote_addr}; |
178 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
182 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
179 | |
183 | |
180 | Coro::Event::do_timer(after => 15); |
184 | Coro::Event::do_timer(after => 20*rand); |
181 | |
185 | |
182 | $self->err(401, "too many connections", |
186 | $self->err(401, "too many connections", |
183 | { |
187 | { |
184 | "Content-Type" => "text/html", |
188 | "Content-Type" => "text/html", |
185 | "Retry-After" => $::BLOCKTIME |
189 | "Retry-After" => $::BLOCKTIME, |
|
|
190 | "Warning" => "Please do NOT retry, you have been blocked", |
|
|
191 | "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", |
186 | }, |
192 | }, |
187 | <<EOF); |
193 | <<EOF); |
188 | <html><p> |
194 | <html><p> |
189 | You have been blocked because you opened too many connections. You |
195 | You have been blocked because you opened too many connections. You |
190 | may retry at</p> |
196 | may retry at</p> |
… | |
… | |
242 | |
248 | |
243 | $self->{method} = $1; |
249 | $self->{method} = $1; |
244 | $self->{uri} = $2; |
250 | $self->{uri} = $2; |
245 | $self->{version} = $3; |
251 | $self->{version} = $3; |
246 | |
252 | |
247 | $3 eq "1.0" or $3 eq "1.1" |
253 | $3 =~ /^1\./ |
248 | or $self->err(506, "http protocol version $3 not supported"); |
254 | or $self->err(506, "http protocol version $3 not supported"); |
249 | |
255 | |
250 | # parse headers |
256 | # parse headers |
251 | { |
257 | { |
252 | my (%hdr, $h, $v); |
258 | my (%hdr, $h, $v); |
… | |
… | |
271 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
277 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
272 | |
278 | |
273 | $self->map_uri; |
279 | $self->map_uri; |
274 | $self->respond; |
280 | $self->respond; |
275 | |
281 | |
|
|
282 | $self->eoconn; |
|
|
283 | |
276 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
284 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
277 | |
285 | |
278 | $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
286 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
279 | $fh->timeout($::PER_TIMEOUT); |
287 | $fh->timeout($::PER_TIMEOUT); |
280 | } |
288 | } |
281 | } |
289 | } |
282 | |
290 | |
283 | # uri => path mapping |
291 | # uri => path mapping |
… | |
… | |
424 | goto ignore; |
432 | goto ignore; |
425 | } |
433 | } |
426 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
434 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
427 | } |
435 | } |
428 | $hdr->{"Content-Range"} = "bytes */$length"; |
436 | $hdr->{"Content-Range"} = "bytes */$length"; |
|
|
437 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
429 | $self->err(416, "not satisfiable", $hdr); |
438 | $self->err(416, "not satisfiable", $hdr); |
430 | |
439 | |
431 | satisfiable: |
440 | satisfiable: |
432 | # check for segmented downloads |
441 | # check for segmented downloads |
433 | if ($l && $::NO_SEGMENTED) { |
442 | if ($l && $::NO_SEGMENTED) { |
… | |
… | |
459 | open $fh, "<", $self->{path} |
468 | open $fh, "<", $self->{path} |
460 | or die "$self->{path}: late open failure ($!)"; |
469 | or die "$self->{path}: late open failure ($!)"; |
461 | |
470 | |
462 | $h -= $l - 1; |
471 | $h -= $l - 1; |
463 | |
472 | |
|
|
473 | if (0) { |
|
|
474 | if ($l) { |
|
|
475 | sysseek $fh, $l, 0; |
|
|
476 | } |
|
|
477 | } |
|
|
478 | |
464 | while ($h > 0) { |
479 | while ($h > 0) { |
|
|
480 | if (0) { |
|
|
481 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
|
|
482 | or last; |
|
|
483 | } else { |
465 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
484 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
466 | $buf, 0, sub { |
485 | $buf, 0, sub { |
467 | $r = $_[0]; |
486 | $r = $_[0]; |
468 | $current->ready; |
487 | $current->ready; |
|
|
488 | $scheduler->now; |
469 | }); |
489 | }); |
470 | &Coro::schedule; |
490 | &Coro::schedule; |
471 | last unless $r; |
491 | last unless $r; |
|
|
492 | } |
472 | my $w = $self->{fh}->syswrite($buf) |
493 | my $w = $self->{fh}->syswrite($buf) |
473 | or last; |
494 | or last; |
474 | $::written += $w; |
495 | $::written += $w; |
475 | $self->{written} += $w; |
496 | $self->{written} += $w; |
476 | $l += $r; |
497 | $l += $r; |