… | |
… | |
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 | Event->add_hooks(prepare => sub { |
|
|
84 | &Coro::cede; |
|
|
85 | }); |
|
|
86 | |
|
|
87 | our %conn; # $conn{ip}{fh} => connobj |
83 | our %conn; # $conn{ip}{self} => connobj |
|
|
84 | our %uri; # $uri{ip}{uri}{self} |
88 | our %blocked; |
85 | our %blocked; |
89 | our %mimetype; |
86 | our %mimetype; |
90 | |
87 | |
91 | sub read_mimetypes { |
88 | sub read_mimetypes { |
92 | local *M; |
89 | local *M; |
… | |
… | |
125 | sub DESTROY { |
122 | sub DESTROY { |
126 | my $self = shift; |
123 | my $self = shift; |
127 | |
124 | |
128 | $::conns--; |
125 | $::conns--; |
129 | |
126 | |
|
|
127 | $self->eoconn; |
130 | delete $conn{$self->{remote_addr}}{$self*1}; |
128 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
129 | } |
|
|
130 | |
|
|
131 | # end of connection |
|
|
132 | sub eoconn { |
|
|
133 | my $self = shift; |
131 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
134 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
132 | } |
135 | } |
133 | |
136 | |
134 | sub slog { |
137 | sub slog { |
135 | my $self = shift; |
138 | my $self = shift; |
… | |
… | |
148 | } |
151 | } |
149 | $res .= "\015\012"; |
152 | $res .= "\015\012"; |
150 | |
153 | |
151 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
154 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
152 | |
155 | |
153 | 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"; |
154 | |
157 | |
155 | $self->{written} += |
158 | $self->{written} += |
156 | print {$self->{fh}} $res; |
159 | print {$self->{fh}} $res; |
157 | } |
160 | } |
158 | |
161 | |
… | |
… | |
175 | sub err_blocked { |
178 | sub err_blocked { |
176 | my $self = shift; |
179 | my $self = shift; |
177 | my $ip = $self->{remote_addr}; |
180 | my $ip = $self->{remote_addr}; |
178 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
181 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
179 | |
182 | |
180 | Coro::Event::do_timer(after => 15); |
183 | Coro::Event::do_timer(after => 20*rand); |
181 | |
184 | |
182 | $self->err(401, "too many connections", |
185 | $self->err(401, "too many connections", |
183 | { |
186 | { |
184 | "Content-Type" => "text/html", |
187 | "Content-Type" => "text/html", |
185 | "Retry-After" => $::BLOCKTIME |
188 | "Retry-After" => $::BLOCKTIME, |
|
|
189 | "Warning" => "Please do NOT retry, you have been blocked", |
|
|
190 | "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", |
186 | }, |
191 | }, |
187 | <<EOF); |
192 | <<EOF); |
188 | <html><p> |
193 | <html><p> |
189 | You have been blocked because you opened too many connections. You |
194 | You have been blocked because you opened too many connections. You |
190 | may retry at</p> |
195 | may retry at</p> |
… | |
… | |
242 | |
247 | |
243 | $self->{method} = $1; |
248 | $self->{method} = $1; |
244 | $self->{uri} = $2; |
249 | $self->{uri} = $2; |
245 | $self->{version} = $3; |
250 | $self->{version} = $3; |
246 | |
251 | |
247 | $3 eq "1.0" or $3 eq "1.1" |
252 | $3 =~ /^1\./ |
248 | or $self->err(506, "http protocol version $3 not supported"); |
253 | or $self->err(506, "http protocol version $3 not supported"); |
249 | |
254 | |
250 | # parse headers |
255 | # parse headers |
251 | { |
256 | { |
252 | my (%hdr, $h, $v); |
257 | my (%hdr, $h, $v); |
… | |
… | |
268 | |
273 | |
269 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
274 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
270 | |
275 | |
271 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
276 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
272 | |
277 | |
|
|
278 | eval { |
273 | $self->map_uri; |
279 | $self->map_uri; |
274 | $self->respond; |
280 | $self->respond; |
|
|
281 | }; |
|
|
282 | |
|
|
283 | $self->eoconn; |
|
|
284 | |
|
|
285 | die if $@ && !ref $@; |
275 | |
286 | |
276 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
287 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
277 | |
288 | |
278 | $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
289 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
279 | $fh->timeout($::PER_TIMEOUT); |
290 | $fh->timeout($::PER_TIMEOUT); |
280 | } |
291 | } |
281 | } |
292 | } |
282 | |
293 | |
283 | # uri => path mapping |
294 | # uri => path mapping |
… | |
… | |
372 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
383 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
373 | } else { |
384 | } else { |
374 | $ims < $self->{stat}[9] |
385 | $ims < $self->{stat}[9] |
375 | or $self->err(304, "not modified"); |
386 | or $self->err(304, "not modified"); |
376 | |
387 | |
377 | if ($self->{method} eq "GET") { |
|
|
378 | if (-r "$path/index.html") { |
388 | if (-r "$path/index.html") { |
379 | $self->{path} .= "/index.html"; |
389 | $self->{path} .= "/index.html"; |
380 | $self->handle_file; |
390 | $self->handle_file; |
381 | } else { |
391 | } else { |
382 | $self->handle_dir; |
392 | $self->handle_dir; |
383 | } |
|
|
384 | } |
393 | } |
385 | } |
394 | } |
386 | } elsif (-f _ && -r _) { |
395 | } elsif (-f _ && -r _) { |
387 | -x _ and $self->err(403, "forbidden"); |
396 | -x _ and $self->err(403, "forbidden"); |
388 | $self->handle_file; |
397 | $self->handle_file; |
… | |
… | |
421 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
430 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
422 | } else { |
431 | } else { |
423 | ($l, $h) = (0, $length - 1); |
432 | ($l, $h) = (0, $length - 1); |
424 | goto ignore; |
433 | goto ignore; |
425 | } |
434 | } |
426 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
435 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
427 | } |
436 | } |
428 | $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"}.")"); |
429 | $self->err(416, "not satisfiable", $hdr); |
440 | $self->err(416, "not satisfiable", $hdr, ""); |
430 | |
441 | |
431 | satisfiable: |
442 | satisfiable: |
432 | # check for segmented downloads |
443 | # check for segmented downloads |
433 | if ($l && $::NO_SEGMENTED) { |
444 | if ($l && $::NO_SEGMENTED) { |
434 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
445 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
435 | Coro::Event::do_timer(after => 15); |
|
|
436 | |
|
|
437 | $self->err(400, "segmented downloads are not allowed"); |
446 | $self->err(400, "segmented downloads are not allowed"); |
438 | } |
447 | } |
439 | } |
448 | } |
440 | |
449 | |
441 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
450 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
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; |
469 | }); |
488 | }); |
470 | &Coro::schedule; |
489 | &Coro::schedule; |
471 | last unless $r; |
490 | last unless $r; |
|
|
491 | } |
472 | my $w = $self->{fh}->syswrite($buf) |
492 | my $w = $self->{fh}->syswrite($buf) |
473 | or last; |
493 | or last; |
474 | $::written += $w; |
494 | $::written += $w; |
475 | $self->{written} += $w; |
495 | $self->{written} += $w; |
476 | $l += $r; |
496 | $l += $r; |