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 | |
… | |
… | |
596 | |
602 | |
597 | if ($self->{method} eq "GET") { |
603 | if ($self->{method} eq "GET") { |
598 | $self->{time} = $::NOW; |
604 | $self->{time} = $::NOW; |
599 | $self->{written} = 0; |
605 | $self->{written} = 0; |
600 | |
606 | |
601 | my $current = $Coro::current; |
607 | my $fh; |
602 | |
|
|
603 | my ($fh, $buf, $r); |
|
|
604 | |
608 | |
605 | open $fh, "<", $self->{path} |
609 | open $fh, "<", $self->{path} |
606 | or die "$self->{path}: late open failure ($!)"; |
610 | or die "$self->{path}: late open failure ($!)"; |
607 | |
611 | |
608 | $h -= $l - 1; |
612 | $h -= $l - 1; |
… | |
… | |
629 | if ($blocked{$self->{remote_id}}) { |
633 | if ($blocked{$self->{remote_id}}) { |
630 | $self->{h}{connection} = "close"; |
634 | $self->{h}{connection} = "close"; |
631 | die bless {}, err::; |
635 | die bless {}, err::; |
632 | } |
636 | } |
633 | |
637 | |
634 | if (0) { # !AIO |
638 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
635 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
|
|
636 | or last; |
639 | or last; |
637 | } else { |
|
|
638 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
|
|
639 | $buf, 0, sub { |
|
|
640 | $r = $_[0]; |
|
|
641 | Coro::ready($current); |
|
|
642 | }); |
|
|
643 | &Coro::schedule; |
|
|
644 | last unless $r; |
|
|
645 | } |
|
|
646 | |
640 | |
647 | $tbf->request(length $buf); |
641 | $tbf->request (length $buf); |
648 | my $w = syswrite $self->{fh}, $buf |
642 | my $w = syswrite $self->{fh}, $buf |
649 | or last; |
643 | or last; |
650 | $::written += $w; |
644 | $::written += $w; |
651 | $self->{written} += $w; |
645 | $self->{written} += $w; |
652 | $l += $r; |
646 | $l += $w; |
653 | } |
647 | } |
654 | |
648 | |
655 | close $fh; |
649 | close $fh; |
656 | } |
650 | } |
657 | } |
651 | } |
658 | |
652 | |
659 | 1; |
653 | 1 |
|
|
654 | |