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; |
… | |
… | |
54 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
58 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
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; |
|
|
63 | |
|
|
64 | our $tbf_top = new tbf rate => $TBF_RATE || 100000; |
|
|
65 | |
|
|
66 | my $unused_bytes = 0; |
|
|
67 | my $unused_last = time; |
|
|
68 | |
|
|
69 | sub unused_bandwidth { |
|
|
70 | $unused_bytes += $_[0]; |
|
|
71 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
|
|
72 | $unused_last = $NOW; |
|
|
73 | $unused_bytes = 0; |
|
|
74 | $queue_file->force_wake_next; |
|
|
75 | slog 1, "forced filetransfer due to unused bandwidth"; |
|
|
76 | } |
|
|
77 | } |
59 | |
78 | |
60 | my @newcons; |
79 | my @newcons; |
61 | my @pool; |
80 | my @pool; |
62 | |
81 | |
63 | # one "execution thread" |
82 | # one "execution thread" |
… | |
… | |
95 | if (@pool) { |
114 | if (@pool) { |
96 | (pop @pool)->ready; |
115 | (pop @pool)->ready; |
97 | } else { |
116 | } else { |
98 | async \&handler; |
117 | async \&handler; |
99 | } |
118 | } |
100 | |
|
|
101 | } |
119 | } |
102 | }; |
120 | }; |
103 | } |
121 | } |
104 | |
122 | |
105 | my $http_port = new Coro::Socket |
123 | my $http_port = new Coro::Socket |
… | |
… | |
122 | listen_on $http_port; |
140 | listen_on $http_port; |
123 | } |
141 | } |
124 | |
142 | |
125 | package conn; |
143 | package conn; |
126 | |
144 | |
|
|
145 | use strict; |
|
|
146 | use bytes; |
|
|
147 | |
127 | use Socket; |
148 | use Socket; |
128 | use HTTP::Date; |
149 | use HTTP::Date; |
129 | use Convert::Scalar 'weaken'; |
150 | use Convert::Scalar 'weaken'; |
130 | use Linux::AIO; |
151 | use IO::AIO; |
131 | |
152 | |
132 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
153 | IO::AIO::min_parallel $::AIO_PARALLEL; |
133 | |
154 | |
134 | Event->io(fd => Linux::AIO::poll_fileno, |
155 | Event->io (fd => IO::AIO::poll_fileno, |
135 | poll => 'r', async => 1, |
156 | poll => 'r', async => 1, |
136 | cb => \&Linux::AIO::poll_cb); |
157 | cb => \&IO::AIO::poll_cb); |
137 | |
158 | |
138 | our %conn; # $conn{ip}{self} => connobj |
159 | our %conn; # $conn{ip}{self} => connobj |
139 | our %uri; # $uri{ip}{uri}{self} |
160 | our %uri; # $uri{ip}{uri}{self} |
140 | our %blocked; |
161 | our %blocked; |
141 | our %mimetype; |
162 | our %mimetype; |
… | |
… | |
178 | |
199 | |
179 | sub DESTROY { |
200 | sub DESTROY { |
180 | #my $self = shift; |
201 | #my $self = shift; |
181 | $::conns--; |
202 | $::conns--; |
182 | } |
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); |
183 | |
228 | |
184 | sub slog { |
229 | sub slog { |
185 | my $self = shift; |
230 | my $self = shift; |
186 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
231 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
187 | } |
232 | } |
… | |
… | |
217 | $hdr->{"Content-Length"} = length $content; |
262 | $hdr->{"Content-Length"} = length $content; |
218 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
263 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
219 | } |
264 | } |
220 | |
265 | |
221 | $res .= "Date: $HTTP_NOW\015\012"; |
266 | $res .= "Date: $HTTP_NOW\015\012"; |
|
|
267 | $res .= "Server: $::NAME\015\012"; |
222 | |
268 | |
223 | while (my ($h, $v) = each %$hdr) { |
269 | while (my ($h, $v) = each %$hdr) { |
224 | $res .= "$h: $v\015\012" |
270 | $res .= "$h: $v\015\012" |
225 | } |
271 | } |
226 | $res .= "\015\012"; |
272 | $res .= "\015\012"; |
… | |
… | |
232 | " \"$self->{h}{referer}\"\n"; |
278 | " \"$self->{h}{referer}\"\n"; |
233 | |
279 | |
234 | print $::accesslog $log if $::accesslog; |
280 | print $::accesslog $log if $::accesslog; |
235 | print STDERR $log; |
281 | print STDERR $log; |
236 | |
282 | |
237 | $self->{written} += |
283 | $tbf_top->request(length $res, 1e6); |
238 | print {$self->{fh}} $res; |
284 | $self->{written} += print {$self->{fh}} $res; |
239 | } |
285 | } |
240 | |
286 | |
241 | sub err { |
287 | sub err { |
242 | my $self = shift; |
288 | my $self = shift; |
243 | my ($code, $msg, $hdr, $content) = @_; |
289 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
298 | my (%hdr, $h, $v); |
344 | my (%hdr, $h, $v); |
299 | |
345 | |
300 | $hdr{lc $1} .= ",$2" |
346 | $hdr{lc $1} .= ",$2" |
301 | while $req =~ /\G |
347 | while $req =~ /\G |
302 | ([^:\000-\040]+): |
348 | ([^:\000-\040]+): |
303 | [\010\040]* |
349 | [\011\040]* |
304 | ((?: [^\015\012]+ | \015\012[\010\040] )*) |
350 | ((?: [^\015\012]+ | \015\012[\011\040] )*) |
305 | \015\012 |
351 | \015\012 |
306 | /gxc; |
352 | /gxc; |
307 | |
353 | |
308 | $req =~ /\G\015\012$/ |
354 | $req =~ /\G\015\012$/ |
309 | or $self->err(400, "bad request"); |
355 | or $self->err(400, "bad request"); |
… | |
… | |
457 | or $self->err(304, "not modified"); |
503 | or $self->err(304, "not modified"); |
458 | |
504 | |
459 | if (-r "$path/index.html") { |
505 | if (-r "$path/index.html") { |
460 | # replace directory "size" by index.html filesize |
506 | # replace directory "size" by index.html filesize |
461 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
507 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
462 | $self->handle_file($queue_index); |
508 | $self->handle_file($queue_index, $tbf_top); |
463 | } else { |
509 | } else { |
464 | $self->handle_dir; |
510 | $self->handle_dir; |
465 | } |
511 | } |
466 | } |
512 | } |
467 | } elsif (-f _ && -r _) { |
513 | } elsif (-f _ && -r _) { |
… | |
… | |
476 | $httpevent->wait; |
522 | $httpevent->wait; |
477 | } |
523 | } |
478 | } |
524 | } |
479 | } |
525 | } |
480 | |
526 | |
481 | $self->handle_file($queue_file); |
527 | $self->handle_file($queue_file, $tbf_top); |
482 | } else { |
528 | } else { |
483 | $self->err(404, "not found"); |
529 | $self->err(404, "not found"); |
484 | } |
530 | } |
485 | } |
531 | } |
486 | } |
532 | } |
… | |
… | |
489 | my $self = shift; |
535 | my $self = shift; |
490 | my $idx = $self->diridx; |
536 | my $idx = $self->diridx; |
491 | |
537 | |
492 | $self->response(200, "ok", |
538 | $self->response(200, "ok", |
493 | { |
539 | { |
494 | "Content-Type" => "text/html", |
540 | "Content-Type" => "text/html; charset=utf-8", |
495 | "Content-Length" => length $idx, |
541 | "Content-Length" => length $idx, |
496 | "Last-Modified" => time2str ($self->{stat}[9]), |
542 | "Last-Modified" => time2str ($self->{stat}[9]), |
497 | }, |
543 | }, |
498 | $idx); |
544 | $idx); |
499 | } |
545 | } |
500 | |
546 | |
501 | sub handle_file { |
547 | sub handle_file { |
502 | my ($self, $queue) = @_; |
548 | my ($self, $queue, $tbf) = @_; |
503 | my $length = $self->{stat}[7]; |
549 | my $length = $self->{stat}[7]; |
504 | my $hdr = { |
550 | my $hdr = { |
505 | "Last-Modified" => time2str ((stat _)[9]), |
551 | "Last-Modified" => time2str ((stat _)[9]), |
|
|
552 | "Accept-Ranges" => "bytes", |
506 | }; |
553 | }; |
507 | |
554 | |
508 | my @code = (200, "ok"); |
555 | my @code = (200, "ok"); |
509 | my ($l, $h); |
556 | my ($l, $h); |
510 | |
557 | |
… | |
… | |
553 | |
600 | |
554 | $self->response(@code, $hdr, ""); |
601 | $self->response(@code, $hdr, ""); |
555 | |
602 | |
556 | if ($self->{method} eq "GET") { |
603 | if ($self->{method} eq "GET") { |
557 | $self->{time} = $::NOW; |
604 | $self->{time} = $::NOW; |
|
|
605 | $self->{written} = 0; |
558 | |
606 | |
559 | my $current = $Coro::current; |
607 | my $fh; |
560 | |
|
|
561 | my ($fh, $buf, $r); |
|
|
562 | |
608 | |
563 | open $fh, "<", $self->{path} |
609 | open $fh, "<", $self->{path} |
564 | or die "$self->{path}: late open failure ($!)"; |
610 | or die "$self->{path}: late open failure ($!)"; |
565 | |
611 | |
566 | $h -= $l - 1; |
612 | $h -= $l - 1; |
… | |
… | |
578 | while ($h > 0) { |
624 | while ($h > 0) { |
579 | unless ($locked) { |
625 | unless ($locked) { |
580 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
626 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
581 | $bufsize = $::BUFSIZE; |
627 | $bufsize = $::BUFSIZE; |
582 | $self->{time} = $::NOW; |
628 | $self->{time} = $::NOW; |
|
|
629 | $self->{written} = 0; |
583 | } |
630 | } |
584 | } |
631 | } |
585 | |
632 | |
586 | if ($blocked{$self->{remote_id}}) { |
633 | if ($blocked{$self->{remote_id}}) { |
587 | $self->{h}{connection} = "close"; |
634 | $self->{h}{connection} = "close"; |
588 | die bless {}, err::; |
635 | die bless {}, err::; |
589 | } |
636 | } |
590 | |
637 | |
591 | if (0) { # !AIO |
638 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
592 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
|
|
593 | or last; |
639 | or last; |
594 | } else { |
640 | |
595 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
641 | $tbf->request (length $buf); |
596 | $buf, 0, sub { |
|
|
597 | $r = $_[0]; |
|
|
598 | Coro::ready($current); |
|
|
599 | }); |
|
|
600 | &Coro::schedule; |
|
|
601 | last unless $r; |
|
|
602 | } |
|
|
603 | my $w = syswrite $self->{fh}, $buf |
642 | my $w = syswrite $self->{fh}, $buf |
604 | or last; |
643 | or last; |
605 | $::written += $w; |
644 | $::written += $w; |
606 | $self->{written} += $w; |
645 | $self->{written} += $w; |
607 | $l += $r; |
646 | $l += $w; |
608 | } |
647 | } |
609 | |
648 | |
610 | close $fh; |
649 | close $fh; |
611 | } |
650 | } |
612 | } |
651 | } |
613 | |
652 | |
614 | 1; |
653 | 1 |
|
|
654 | |