… | |
… | |
70 | package conn; |
70 | package conn; |
71 | |
71 | |
72 | use Socket; |
72 | use Socket; |
73 | use HTTP::Date; |
73 | use HTTP::Date; |
74 | use Convert::Scalar 'weaken'; |
74 | use Convert::Scalar 'weaken'; |
|
|
75 | use Linux::AIO; |
|
|
76 | |
|
|
77 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
|
|
78 | |
|
|
79 | Event->io(fd => Linux::AIO::poll_fileno, |
|
|
80 | poll => 'r', |
|
|
81 | async => 1, |
|
|
82 | cb => \&Linux::AIO::poll_cb ); |
|
|
83 | |
|
|
84 | Event->add_hooks(prepare => sub { |
|
|
85 | &Coro::cede while &Coro::nready; |
|
|
86 | 1e6; |
|
|
87 | }); |
75 | |
88 | |
76 | our %conn; # $conn{ip}{fh} => connobj |
89 | our %conn; # $conn{ip}{fh} => connobj |
77 | our %blocked; |
90 | our %blocked; |
78 | our %mimetype; |
91 | our %mimetype; |
79 | |
92 | |
… | |
… | |
200 | defined $req or |
213 | defined $req or |
201 | $self->err(408, "request timeout"); |
214 | $self->err(408, "request timeout"); |
202 | |
215 | |
203 | my $ip = $self->{remote_addr}; |
216 | my $ip = $self->{remote_addr}; |
204 | |
217 | |
205 | $self->err_blocked($blocked{$ip}); |
|
|
206 | if ($blocked{$ip}) { |
218 | if ($blocked{$ip}) { |
207 | $self->err_blocked($blocked{$ip}) |
219 | $self->err_blocked($blocked{$ip}) |
208 | if $blocked{$ip} > $::NOW; |
220 | if $blocked{$ip} > $::NOW; |
209 | |
221 | |
210 | delete $blocked{$ip}; |
222 | delete $blocked{$ip}; |
… | |
… | |
428 | $hdr->{"Content-Length"} = $length; |
440 | $hdr->{"Content-Length"} = $length; |
429 | |
441 | |
430 | $self->response(@code, $hdr, ""); |
442 | $self->response(@code, $hdr, ""); |
431 | |
443 | |
432 | if ($self->{method} eq "GET") { |
444 | if ($self->{method} eq "GET") { |
433 | my ($fh, $buf); |
445 | my ($fh, $buf, $r); |
|
|
446 | my $current = $Coro::current; |
434 | open $fh, "<", $self->{path} |
447 | open $fh, "<", $self->{path} |
435 | or die "$self->{path}: late open failure ($!)"; |
448 | or die "$self->{path}: late open failure ($!)"; |
436 | |
449 | |
437 | if ($l) { |
|
|
438 | sysseek $fh, $l, 0 |
|
|
439 | or die "$self->{path}: cannot seek to $l ($!)"; |
|
|
440 | } |
|
|
441 | |
|
|
442 | $h -= $l - 1; |
450 | $h -= $l - 1; |
443 | |
451 | |
444 | while ($h > 0) { |
452 | while ($h > 0) { |
445 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
453 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
|
|
454 | $buf, 0, sub { |
|
|
455 | $r = $_[0]; |
|
|
456 | $current->ready; |
|
|
457 | }); |
|
|
458 | &Coro::schedule; |
|
|
459 | last unless $r; |
446 | my $w = $self->{fh}->syswrite($buf) |
460 | my $w = $self->{fh}->syswrite($buf) |
447 | or last; |
461 | or last; |
448 | $::written += $w; |
462 | $::written += $w; |
449 | $self->{written} += $w; |
463 | $self->{written} += $w; |
|
|
464 | $l += $r; |
450 | } |
465 | } |
451 | } |
466 | } |
452 | |
467 | |
453 | close $fh; |
468 | close $fh; |
454 | } |
469 | } |