1 | use Coro; |
1 | use Coro; |
2 | use Coro::Semaphore; |
2 | use Coro::Semaphore; |
3 | use Coro::Event; |
3 | use Coro::Event; |
4 | use Coro::Socket; |
4 | use Coro::Socket; |
5 | use Coro::Signal; |
5 | use Coro::Signal; |
|
|
6 | use Coro::AIO (); |
6 | |
7 | |
7 | use HTTP::Date; |
8 | use HTTP::Date; |
8 | use POSIX (); |
9 | use POSIX (); |
9 | |
10 | |
10 | use Compress::Zlib (); |
11 | use Compress::Zlib (); |
… | |
… | |
21 | our $accesslog; |
22 | our $accesslog; |
22 | our $errorlog; |
23 | our $errorlog; |
23 | |
24 | |
24 | our $NOW; |
25 | our $NOW; |
25 | our $HTTP_NOW; |
26 | our $HTTP_NOW; |
|
|
27 | |
|
|
28 | our $ERROR_LOG; |
|
|
29 | our $ACCESS_LOG; |
26 | |
30 | |
27 | Event->timer(interval => 1, hard => 1, cb => sub { |
31 | Event->timer(interval => 1, hard => 1, cb => sub { |
28 | $NOW = time; |
32 | $NOW = time; |
29 | $HTTP_NOW = time2str $NOW; |
33 | $HTTP_NOW = time2str $NOW; |
30 | })->now; |
34 | })->now; |
… | |
… | |
136 | listen_on $http_port; |
140 | listen_on $http_port; |
137 | } |
141 | } |
138 | |
142 | |
139 | package conn; |
143 | package conn; |
140 | |
144 | |
|
|
145 | use strict; |
|
|
146 | use bytes; |
|
|
147 | |
141 | use Socket; |
148 | use Socket; |
142 | use HTTP::Date; |
149 | use HTTP::Date; |
143 | use Convert::Scalar 'weaken'; |
150 | use Convert::Scalar 'weaken'; |
144 | use Linux::AIO; |
151 | use IO::AIO; |
145 | |
152 | |
146 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
153 | IO::AIO::min_parallel $::AIO_PARALLEL; |
147 | |
154 | |
148 | Event->io(fd => Linux::AIO::poll_fileno, |
155 | Event->io (fd => IO::AIO::poll_fileno, |
149 | poll => 'r', async => 1, |
156 | poll => 'r', async => 1, |
150 | cb => \&Linux::AIO::poll_cb); |
157 | cb => \&IO::AIO::poll_cb); |
151 | |
158 | |
152 | our %conn; # $conn{ip}{self} => connobj |
159 | our %conn; # $conn{ip}{self} => connobj |
153 | our %uri; # $uri{ip}{uri}{self} |
160 | our %uri; # $uri{ip}{uri}{self} |
154 | our %blocked; |
161 | our %blocked; |
155 | our %mimetype; |
162 | our %mimetype; |
… | |
… | |
201 | for (keys %$hash) { |
208 | for (keys %$hash) { |
202 | if (ref $hash->{$_} eq HASH::) { |
209 | if (ref $hash->{$_} eq HASH::) { |
203 | prune_cache($hash->{$_}); |
210 | prune_cache($hash->{$_}); |
204 | unless (scalar keys %{$hash->{$_}}) { |
211 | unless (scalar keys %{$hash->{$_}}) { |
205 | delete $hash->{$_}; |
212 | delete $hash->{$_}; |
206 | $d2++; |
|
|
207 | } |
213 | } |
208 | } |
214 | } |
209 | } |
215 | } |
210 | } |
216 | } |
211 | |
217 | |
… | |
… | |
529 | my $self = shift; |
535 | my $self = shift; |
530 | my $idx = $self->diridx; |
536 | my $idx = $self->diridx; |
531 | |
537 | |
532 | $self->response(200, "ok", |
538 | $self->response(200, "ok", |
533 | { |
539 | { |
534 | "Content-Type" => "text/html", |
540 | "Content-Type" => "text/html; charset=utf-8", |
535 | "Content-Length" => length $idx, |
541 | "Content-Length" => length $idx, |
536 | "Last-Modified" => time2str ($self->{stat}[9]), |
542 | "Last-Modified" => time2str ($self->{stat}[9]), |
537 | }, |
543 | }, |
538 | $idx); |
544 | $idx); |
539 | } |
545 | } |
540 | |
546 | |
541 | sub handle_file { |
547 | sub handle_file { |
542 | my ($self, $queue, $tbf) = @_; |
548 | my ($self, $queue, $tbf) = @_; |
543 | my $length = $self->{stat}[7]; |
549 | my $length = $self->{stat}[7]; |
544 | my $hdr = { |
550 | my $hdr = { |
545 | "Last-Modified" => time2str ((stat _)[9]), |
551 | "Last-Modified" => time2str ((stat _)[9]), |
|
|
552 | "Accept-Ranges" => "bytes", |
546 | }; |
553 | }; |
547 | |
554 | |
548 | my @code = (200, "ok"); |
555 | my @code = (200, "ok"); |
549 | my ($l, $h); |
556 | my ($l, $h); |
550 | |
557 | |
… | |
… | |
595 | |
602 | |
596 | if ($self->{method} eq "GET") { |
603 | if ($self->{method} eq "GET") { |
597 | $self->{time} = $::NOW; |
604 | $self->{time} = $::NOW; |
598 | $self->{written} = 0; |
605 | $self->{written} = 0; |
599 | |
606 | |
600 | my $current = $Coro::current; |
607 | my $fh; |
601 | |
|
|
602 | my ($fh, $buf, $r); |
|
|
603 | |
608 | |
604 | open $fh, "<", $self->{path} |
609 | open $fh, "<", $self->{path} |
605 | or die "$self->{path}: late open failure ($!)"; |
610 | or die "$self->{path}: late open failure ($!)"; |
606 | |
611 | |
607 | $h -= $l - 1; |
612 | $h -= $l - 1; |
… | |
… | |
628 | if ($blocked{$self->{remote_id}}) { |
633 | if ($blocked{$self->{remote_id}}) { |
629 | $self->{h}{connection} = "close"; |
634 | $self->{h}{connection} = "close"; |
630 | die bless {}, err::; |
635 | die bless {}, err::; |
631 | } |
636 | } |
632 | |
637 | |
633 | if (0) { # !AIO |
638 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
634 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
|
|
635 | or last; |
639 | or last; |
636 | } else { |
|
|
637 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
|
|
638 | $buf, 0, sub { |
|
|
639 | $r = $_[0]; |
|
|
640 | Coro::ready($current); |
|
|
641 | }); |
|
|
642 | &Coro::schedule; |
|
|
643 | last unless $r; |
|
|
644 | } |
|
|
645 | |
640 | |
646 | $tbf->request(length $buf); |
641 | $tbf->request (length $buf); |
647 | my $w = syswrite $self->{fh}, $buf |
642 | my $w = syswrite $self->{fh}, $buf |
648 | or last; |
643 | or last; |
649 | $::written += $w; |
644 | $::written += $w; |
650 | $self->{written} += $w; |
645 | $self->{written} += $w; |
651 | $l += $r; |
646 | $l += $w; |
652 | } |
647 | } |
653 | |
648 | |
654 | close $fh; |
649 | close $fh; |
655 | } |
650 | } |
656 | } |
651 | } |
657 | |
652 | |
658 | 1; |
653 | 1 |
|
|
654 | |