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 (); |
|
|
10 | |
|
|
11 | use Compress::Zlib (); |
9 | |
12 | |
10 | no utf8; |
13 | no utf8; |
11 | use bytes; |
14 | use bytes; |
12 | |
15 | |
13 | # at least on my machine, this thingy serves files |
16 | # at least on my machine, this thingy serves files |
… | |
… | |
50 | } |
53 | } |
51 | |
54 | |
52 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
55 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
53 | our $httpevent = new Coro::Signal; |
56 | our $httpevent = new Coro::Signal; |
54 | |
57 | |
55 | our $queue_file = new transferqueue slots => $MAX_TRANSFERS, maxsize => 256*1024*1024; |
58 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
56 | our $queue_index = new transferqueue slots => 10; |
59 | our $queue_index = new transferqueue 10; |
|
|
60 | |
|
|
61 | our $tbf_top = new tbf rate => $TBF_RATE || 100000; |
|
|
62 | |
|
|
63 | my $unused_bytes = 0; |
|
|
64 | my $unused_last = time; |
|
|
65 | |
|
|
66 | sub unused_bandwidth { |
|
|
67 | $unused_bytes += $_[0]; |
|
|
68 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
|
|
69 | $unused_last = $NOW; |
|
|
70 | $unused_bytes = 0; |
|
|
71 | $queue_file->force_wake_next; |
|
|
72 | slog 1, "forced filetransfer due to unused bandwidth"; |
|
|
73 | } |
|
|
74 | } |
57 | |
75 | |
58 | my @newcons; |
76 | my @newcons; |
59 | my @pool; |
77 | my @pool; |
60 | |
78 | |
61 | # one "execution thread" |
79 | # one "execution thread" |
… | |
… | |
93 | if (@pool) { |
111 | if (@pool) { |
94 | (pop @pool)->ready; |
112 | (pop @pool)->ready; |
95 | } else { |
113 | } else { |
96 | async \&handler; |
114 | async \&handler; |
97 | } |
115 | } |
98 | |
|
|
99 | } |
116 | } |
100 | }; |
117 | }; |
101 | } |
118 | } |
102 | |
119 | |
103 | my $http_port = new Coro::Socket |
120 | my $http_port = new Coro::Socket |
… | |
… | |
123 | package conn; |
140 | package conn; |
124 | |
141 | |
125 | use Socket; |
142 | use Socket; |
126 | use HTTP::Date; |
143 | use HTTP::Date; |
127 | use Convert::Scalar 'weaken'; |
144 | use Convert::Scalar 'weaken'; |
128 | use Linux::AIO; |
145 | use IO::AIO; |
129 | |
146 | |
130 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
147 | IO::AIO::min_parallel $::AIO_PARALLEL; |
131 | |
148 | |
132 | Event->io(fd => Linux::AIO::poll_fileno, |
149 | Event->io (fd => IO::AIO::poll_fileno, |
133 | poll => 'r', async => 1, |
150 | poll => 'r', async => 1, |
134 | cb => \&Linux::AIO::poll_cb); |
151 | cb => \&IO::AIO::poll_cb); |
135 | |
152 | |
136 | our %conn; # $conn{ip}{self} => connobj |
153 | our %conn; # $conn{ip}{self} => connobj |
137 | our %uri; # $uri{ip}{uri}{self} |
154 | our %uri; # $uri{ip}{uri}{self} |
138 | our %blocked; |
155 | our %blocked; |
139 | our %mimetype; |
156 | our %mimetype; |
… | |
… | |
177 | sub DESTROY { |
194 | sub DESTROY { |
178 | #my $self = shift; |
195 | #my $self = shift; |
179 | $::conns--; |
196 | $::conns--; |
180 | } |
197 | } |
181 | |
198 | |
|
|
199 | sub prune_cache { |
|
|
200 | my $hash = $_[0]; |
|
|
201 | |
|
|
202 | for (keys %$hash) { |
|
|
203 | if (ref $hash->{$_} eq HASH::) { |
|
|
204 | prune_cache($hash->{$_}); |
|
|
205 | unless (scalar keys %{$hash->{$_}}) { |
|
|
206 | delete $hash->{$_}; |
|
|
207 | $d2++; |
|
|
208 | } |
|
|
209 | } |
|
|
210 | } |
|
|
211 | } |
|
|
212 | |
|
|
213 | sub prune_caches { |
|
|
214 | prune_cache \%conn; |
|
|
215 | prune_cache \%uri; |
|
|
216 | |
|
|
217 | for (keys %blocked) { |
|
|
218 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
|
|
219 | } |
|
|
220 | } |
|
|
221 | |
|
|
222 | Event->timer(interval => 60, cb => \&prune_caches); |
|
|
223 | |
182 | sub slog { |
224 | sub slog { |
183 | my $self = shift; |
225 | my $self = shift; |
184 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
226 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
185 | } |
227 | } |
186 | |
228 | |
187 | sub response { |
229 | sub response { |
188 | my ($self, $code, $msg, $hdr, $content) = @_; |
230 | my ($self, $code, $msg, $hdr, $content) = @_; |
189 | my $res = "HTTP/1.1 $code $msg\015\012"; |
231 | my $res = "HTTP/1.1 $code $msg\015\012"; |
|
|
232 | my $GZ = ""; |
190 | |
233 | |
191 | if (exists $hdr->{Connection}) { |
234 | if (exists $hdr->{Connection}) { |
192 | if ($hdr->{Connection} =~ /close/) { |
235 | if ($hdr->{Connection} =~ /close/) { |
193 | $self->{h}{connection} = "close" |
236 | $self->{h}{connection} = "close" |
194 | } |
237 | } |
… | |
… | |
200 | $self->{h}{connection} = "close" |
243 | $self->{h}{connection} = "close" |
201 | } |
244 | } |
202 | } |
245 | } |
203 | } |
246 | } |
204 | |
247 | |
|
|
248 | if ($self->{method} ne "HEAD" |
|
|
249 | && $self->{h}{"accept-encoding"} =~ /\bgzip\b/ |
|
|
250 | && 400 < length $content |
|
|
251 | && $hdr->{"Content-Length"} == length $content |
|
|
252 | && !exists $hdr->{"Content-Encoding"} |
|
|
253 | ) { |
|
|
254 | my $orig = length $content; |
|
|
255 | $hdr->{"Content-Encoding"} = "gzip"; |
|
|
256 | $content = Compress::Zlib::memGzip(\$content); |
|
|
257 | $hdr->{"Content-Length"} = length $content; |
|
|
258 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
|
|
259 | } |
|
|
260 | |
205 | $res .= "Date: $HTTP_NOW\015\012"; |
261 | $res .= "Date: $HTTP_NOW\015\012"; |
|
|
262 | $res .= "Server: $::NAME\015\012"; |
206 | |
263 | |
207 | while (my ($h, $v) = each %$hdr) { |
264 | while (my ($h, $v) = each %$hdr) { |
208 | $res .= "$h: $v\015\012" |
265 | $res .= "$h: $v\015\012" |
209 | } |
266 | } |
210 | $res .= "\015\012"; |
267 | $res .= "\015\012"; |
211 | |
268 | |
212 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
269 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
213 | |
270 | |
214 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). |
271 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW). |
215 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. |
272 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. |
216 | " \"$self->{h}{referer}\"\n"; |
273 | " \"$self->{h}{referer}\"\n"; |
217 | |
274 | |
218 | print $accesslog $log if $accesslog; |
275 | print $::accesslog $log if $::accesslog; |
219 | print STDERR $log; |
276 | print STDERR $log; |
220 | |
277 | |
221 | $self->{written} += |
278 | $tbf_top->request(length $res, 1e6); |
222 | print {$self->{fh}} $res; |
279 | $self->{written} += print {$self->{fh}} $res; |
223 | } |
280 | } |
224 | |
281 | |
225 | sub err { |
282 | sub err { |
226 | my $self = shift; |
283 | my $self = shift; |
227 | my ($code, $msg, $hdr, $content) = @_; |
284 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
282 | my (%hdr, $h, $v); |
339 | my (%hdr, $h, $v); |
283 | |
340 | |
284 | $hdr{lc $1} .= ",$2" |
341 | $hdr{lc $1} .= ",$2" |
285 | while $req =~ /\G |
342 | while $req =~ /\G |
286 | ([^:\000-\040]+): |
343 | ([^:\000-\040]+): |
287 | [\008\040]* |
344 | [\011\040]* |
288 | ((?: [^\015\012]+ | \015\012[\008\040] )*) |
345 | ((?: [^\015\012]+ | \015\012[\011\040] )*) |
289 | \015\012 |
346 | \015\012 |
290 | /gxc; |
347 | /gxc; |
291 | |
348 | |
292 | $req =~ /\G\015\012$/ |
349 | $req =~ /\G\015\012$/ |
293 | or $self->err(400, "bad request"); |
350 | or $self->err(400, "bad request"); |
… | |
… | |
441 | or $self->err(304, "not modified"); |
498 | or $self->err(304, "not modified"); |
442 | |
499 | |
443 | if (-r "$path/index.html") { |
500 | if (-r "$path/index.html") { |
444 | # replace directory "size" by index.html filesize |
501 | # replace directory "size" by index.html filesize |
445 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
502 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
446 | $self->handle_file($queue_index); |
503 | $self->handle_file($queue_index, $tbf_top); |
447 | } else { |
504 | } else { |
448 | $self->handle_dir; |
505 | $self->handle_dir; |
449 | } |
506 | } |
450 | } |
507 | } |
451 | } elsif (-f _ && -r _) { |
508 | } elsif (-f _ && -r _) { |
… | |
… | |
460 | $httpevent->wait; |
517 | $httpevent->wait; |
461 | } |
518 | } |
462 | } |
519 | } |
463 | } |
520 | } |
464 | |
521 | |
465 | $self->handle_file($queue_file); |
522 | $self->handle_file($queue_file, $tbf_top); |
466 | } else { |
523 | } else { |
467 | $self->err(404, "not found"); |
524 | $self->err(404, "not found"); |
468 | } |
525 | } |
469 | } |
526 | } |
470 | } |
527 | } |
… | |
… | |
473 | my $self = shift; |
530 | my $self = shift; |
474 | my $idx = $self->diridx; |
531 | my $idx = $self->diridx; |
475 | |
532 | |
476 | $self->response(200, "ok", |
533 | $self->response(200, "ok", |
477 | { |
534 | { |
478 | "Content-Type" => "text/html", |
535 | "Content-Type" => "text/html; charset=utf-8", |
479 | "Content-Length" => length $idx, |
536 | "Content-Length" => length $idx, |
480 | "Last-Modified" => time2str ($self->{stat}[9]), |
537 | "Last-Modified" => time2str ($self->{stat}[9]), |
481 | }, |
538 | }, |
482 | $idx); |
539 | $idx); |
483 | } |
540 | } |
484 | |
541 | |
485 | sub handle_file { |
542 | sub handle_file { |
486 | my ($self, $queue) = @_; |
543 | my ($self, $queue, $tbf) = @_; |
487 | my $length = $self->{stat}[7]; |
544 | my $length = $self->{stat}[7]; |
488 | my $hdr = { |
545 | my $hdr = { |
489 | "Last-Modified" => time2str ((stat _)[9]), |
546 | "Last-Modified" => time2str ((stat _)[9]), |
|
|
547 | "Accept-Ranges" => "bytes", |
490 | }; |
548 | }; |
491 | |
549 | |
492 | my @code = (200, "ok"); |
550 | my @code = (200, "ok"); |
493 | my ($l, $h); |
551 | my ($l, $h); |
494 | |
552 | |
… | |
… | |
537 | |
595 | |
538 | $self->response(@code, $hdr, ""); |
596 | $self->response(@code, $hdr, ""); |
539 | |
597 | |
540 | if ($self->{method} eq "GET") { |
598 | if ($self->{method} eq "GET") { |
541 | $self->{time} = $::NOW; |
599 | $self->{time} = $::NOW; |
|
|
600 | $self->{written} = 0; |
542 | |
601 | |
543 | my $current = $Coro::current; |
602 | my $current = $Coro::current; |
544 | |
603 | |
545 | my ($fh, $buf, $r); |
604 | my ($fh, $buf, $r); |
546 | |
605 | |
… | |
… | |
562 | while ($h > 0) { |
621 | while ($h > 0) { |
563 | unless ($locked) { |
622 | unless ($locked) { |
564 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
623 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
565 | $bufsize = $::BUFSIZE; |
624 | $bufsize = $::BUFSIZE; |
566 | $self->{time} = $::NOW; |
625 | $self->{time} = $::NOW; |
|
|
626 | $self->{written} = 0; |
567 | } |
627 | } |
568 | } |
628 | } |
569 | |
629 | |
570 | if ($blocked{$self->{remote_id}}) { |
630 | if ($blocked{$self->{remote_id}}) { |
571 | $self->{h}{connection} = "close"; |
631 | $self->{h}{connection} = "close"; |
572 | die bless {}, err::; |
632 | die bless {}, err::; |
573 | } |
633 | } |
574 | |
634 | |
575 | if (0) { # !AIO |
635 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), $buf, 0 |
576 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
|
|
577 | or last; |
636 | or last; |
578 | } else { |
637 | |
579 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
638 | $tbf->request (length $buf); |
580 | $buf, 0, sub { |
|
|
581 | $r = $_[0]; |
|
|
582 | Coro::ready($current); |
|
|
583 | }); |
|
|
584 | &Coro::schedule; |
|
|
585 | last unless $r; |
|
|
586 | } |
|
|
587 | my $w = syswrite $self->{fh}, $buf |
639 | my $w = syswrite $self->{fh}, $buf |
588 | or last; |
640 | or last; |
589 | $::written += $w; |
641 | $::written += $w; |
590 | $self->{written} += $w; |
642 | $self->{written} += $w; |
591 | $l += $r; |
643 | $l += $r; |
… | |
… | |
593 | |
645 | |
594 | close $fh; |
646 | close $fh; |
595 | } |
647 | } |
596 | } |
648 | } |
597 | |
649 | |
598 | 1; |
650 | 1 |
|
|
651 | |