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; |
… | |
… | |
55 | our $httpevent = new Coro::Signal; |
59 | our $httpevent = new Coro::Signal; |
56 | |
60 | |
57 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
61 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
58 | our $queue_index = new transferqueue 10; |
62 | our $queue_index = new transferqueue 10; |
59 | |
63 | |
60 | our $tbf_top = new tbf rate => 100000; |
64 | our $tbf_top = new tbf rate => $TBF_RATE || 100000; |
61 | |
65 | |
62 | my $unused_bytes = 0; |
66 | my $unused_bytes = 0; |
63 | my $unused_last = time; |
67 | my $unused_last = time; |
64 | |
68 | |
65 | sub unused_bandwidth { |
69 | sub unused_bandwidth { |
… | |
… | |
110 | if (@pool) { |
114 | if (@pool) { |
111 | (pop @pool)->ready; |
115 | (pop @pool)->ready; |
112 | } else { |
116 | } else { |
113 | async \&handler; |
117 | async \&handler; |
114 | } |
118 | } |
115 | |
|
|
116 | } |
119 | } |
117 | }; |
120 | }; |
118 | } |
121 | } |
119 | |
122 | |
120 | my $http_port = new Coro::Socket |
123 | my $http_port = new Coro::Socket |
… | |
… | |
137 | listen_on $http_port; |
140 | listen_on $http_port; |
138 | } |
141 | } |
139 | |
142 | |
140 | package conn; |
143 | package conn; |
141 | |
144 | |
|
|
145 | use strict; |
|
|
146 | use bytes; |
|
|
147 | |
142 | use Socket; |
148 | use Socket; |
143 | use HTTP::Date; |
149 | use HTTP::Date; |
144 | use Convert::Scalar 'weaken'; |
150 | use Convert::Scalar 'weaken'; |
145 | use Linux::AIO; |
151 | use IO::AIO; |
146 | |
152 | |
147 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
153 | IO::AIO::min_parallel $::AIO_PARALLEL; |
148 | |
154 | |
149 | Event->io(fd => Linux::AIO::poll_fileno, |
155 | Event->io (fd => IO::AIO::poll_fileno, |
150 | poll => 'r', async => 1, |
156 | poll => 'r', async => 1, |
151 | cb => \&Linux::AIO::poll_cb); |
157 | cb => \&IO::AIO::poll_cb); |
152 | |
158 | |
153 | our %conn; # $conn{ip}{self} => connobj |
159 | our %conn; # $conn{ip}{self} => connobj |
154 | our %uri; # $uri{ip}{uri}{self} |
160 | our %uri; # $uri{ip}{uri}{self} |
155 | our %blocked; |
161 | our %blocked; |
156 | our %mimetype; |
162 | our %mimetype; |
… | |
… | |
193 | |
199 | |
194 | sub DESTROY { |
200 | sub DESTROY { |
195 | #my $self = shift; |
201 | #my $self = shift; |
196 | $::conns--; |
202 | $::conns--; |
197 | } |
203 | } |
|
|
204 | |
|
|
205 | sub prune_cache { |
|
|
206 | my $hash = $_[0]; |
|
|
207 | |
|
|
208 | for (keys %$hash) { |
|
|
209 | if (ref $hash->{$_} eq HASH::) { |
|
|
210 | prune_cache($hash->{$_}); |
|
|
211 | unless (scalar keys %{$hash->{$_}}) { |
|
|
212 | delete $hash->{$_}; |
|
|
213 | } |
|
|
214 | } |
|
|
215 | } |
|
|
216 | } |
|
|
217 | |
|
|
218 | sub prune_caches { |
|
|
219 | prune_cache \%conn; |
|
|
220 | prune_cache \%uri; |
|
|
221 | |
|
|
222 | for (keys %blocked) { |
|
|
223 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
|
|
224 | } |
|
|
225 | } |
|
|
226 | |
|
|
227 | Event->timer(interval => 60, cb => \&prune_caches); |
198 | |
228 | |
199 | sub slog { |
229 | sub slog { |
200 | my $self = shift; |
230 | my $self = shift; |
201 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
231 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
202 | } |
232 | } |
… | |
… | |
232 | $hdr->{"Content-Length"} = length $content; |
262 | $hdr->{"Content-Length"} = length $content; |
233 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
263 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
234 | } |
264 | } |
235 | |
265 | |
236 | $res .= "Date: $HTTP_NOW\015\012"; |
266 | $res .= "Date: $HTTP_NOW\015\012"; |
|
|
267 | $res .= "Server: $::NAME\015\012"; |
237 | |
268 | |
238 | while (my ($h, $v) = each %$hdr) { |
269 | while (my ($h, $v) = each %$hdr) { |
239 | $res .= "$h: $v\015\012" |
270 | $res .= "$h: $v\015\012" |
240 | } |
271 | } |
241 | $res .= "\015\012"; |
272 | $res .= "\015\012"; |
… | |
… | |
504 | my $self = shift; |
535 | my $self = shift; |
505 | my $idx = $self->diridx; |
536 | my $idx = $self->diridx; |
506 | |
537 | |
507 | $self->response(200, "ok", |
538 | $self->response(200, "ok", |
508 | { |
539 | { |
509 | "Content-Type" => "text/html", |
540 | "Content-Type" => "text/html; charset=utf-8", |
510 | "Content-Length" => length $idx, |
541 | "Content-Length" => length $idx, |
511 | "Last-Modified" => time2str ($self->{stat}[9]), |
542 | "Last-Modified" => time2str ($self->{stat}[9]), |
512 | }, |
543 | }, |
513 | $idx); |
544 | $idx); |
514 | } |
545 | } |
515 | |
546 | |
516 | sub handle_file { |
547 | sub handle_file { |
517 | my ($self, $queue, $tbf) = @_; |
548 | my ($self, $queue, $tbf) = @_; |
518 | my $length = $self->{stat}[7]; |
549 | my $length = $self->{stat}[7]; |
519 | my $hdr = { |
550 | my $hdr = { |
520 | "Last-Modified" => time2str ((stat _)[9]), |
551 | "Last-Modified" => time2str ((stat _)[9]), |
|
|
552 | "Accept-Ranges" => "bytes", |
521 | }; |
553 | }; |
522 | |
554 | |
523 | my @code = (200, "ok"); |
555 | my @code = (200, "ok"); |
524 | my ($l, $h); |
556 | my ($l, $h); |
525 | |
557 | |
… | |
… | |
568 | |
600 | |
569 | $self->response(@code, $hdr, ""); |
601 | $self->response(@code, $hdr, ""); |
570 | |
602 | |
571 | if ($self->{method} eq "GET") { |
603 | if ($self->{method} eq "GET") { |
572 | $self->{time} = $::NOW; |
604 | $self->{time} = $::NOW; |
|
|
605 | $self->{written} = 0; |
573 | |
606 | |
574 | my $current = $Coro::current; |
607 | my $fh; |
575 | |
|
|
576 | my ($fh, $buf, $r); |
|
|
577 | |
608 | |
578 | open $fh, "<", $self->{path} |
609 | open $fh, "<", $self->{path} |
579 | or die "$self->{path}: late open failure ($!)"; |
610 | or die "$self->{path}: late open failure ($!)"; |
580 | |
611 | |
581 | $h -= $l - 1; |
612 | $h -= $l - 1; |
… | |
… | |
593 | while ($h > 0) { |
624 | while ($h > 0) { |
594 | unless ($locked) { |
625 | unless ($locked) { |
595 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
626 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
596 | $bufsize = $::BUFSIZE; |
627 | $bufsize = $::BUFSIZE; |
597 | $self->{time} = $::NOW; |
628 | $self->{time} = $::NOW; |
|
|
629 | $self->{written} = 0; |
598 | } |
630 | } |
599 | } |
631 | } |
600 | |
632 | |
601 | if ($blocked{$self->{remote_id}}) { |
633 | if ($blocked{$self->{remote_id}}) { |
602 | $self->{h}{connection} = "close"; |
634 | $self->{h}{connection} = "close"; |
603 | die bless {}, err::; |
635 | die bless {}, err::; |
604 | } |
636 | } |
605 | |
637 | |
606 | if (0) { # !AIO |
638 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
607 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
|
|
608 | or last; |
639 | or last; |
609 | } else { |
|
|
610 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
|
|
611 | $buf, 0, sub { |
|
|
612 | $r = $_[0]; |
|
|
613 | Coro::ready($current); |
|
|
614 | }); |
|
|
615 | &Coro::schedule; |
|
|
616 | last unless $r; |
|
|
617 | } |
|
|
618 | |
640 | |
619 | $tbf->request(length $buf); |
641 | $tbf->request (length $buf); |
620 | my $w = syswrite $self->{fh}, $buf |
642 | my $w = syswrite $self->{fh}, $buf |
621 | or last; |
643 | or last; |
622 | $::written += $w; |
644 | $::written += $w; |
623 | $self->{written} += $w; |
645 | $self->{written} += $w; |
624 | $l += $r; |
646 | $l += $w; |
625 | } |
647 | } |
626 | |
648 | |
627 | close $fh; |
649 | close $fh; |
628 | } |
650 | } |
629 | } |
651 | } |
630 | |
652 | |
631 | 1; |
653 | 1 |
|
|
654 | |