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 |
… | |
… | |
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 $MAX_TRANSFERS; |
58 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
56 | our $queue_index = new transferqueue 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; |
… | |
… | |
161 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
178 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
162 | or $self->err(500, "unable to decode peername"); |
179 | or $self->err(500, "unable to decode peername"); |
163 | |
180 | |
164 | $self->{remote_addr} = |
181 | $self->{remote_addr} = |
165 | $self->{remote_id} = inet_ntoa $iaddr; |
182 | $self->{remote_id} = inet_ntoa $iaddr; |
|
|
183 | |
166 | $self->{time} = $::NOW; |
184 | $self->{time} = $::NOW; |
167 | |
185 | |
168 | weaken ($Coro::current->{conn} = $self); |
186 | weaken ($Coro::current->{conn} = $self); |
169 | |
187 | |
170 | $::conns++; |
188 | $::conns++; |
… | |
… | |
176 | sub DESTROY { |
194 | sub DESTROY { |
177 | #my $self = shift; |
195 | #my $self = shift; |
178 | $::conns--; |
196 | $::conns--; |
179 | } |
197 | } |
180 | |
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 | |
181 | sub slog { |
224 | sub slog { |
182 | my $self = shift; |
225 | my $self = shift; |
183 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
226 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
184 | } |
227 | } |
185 | |
228 | |
186 | sub response { |
229 | sub response { |
187 | my ($self, $code, $msg, $hdr, $content) = @_; |
230 | my ($self, $code, $msg, $hdr, $content) = @_; |
188 | my $res = "HTTP/1.1 $code $msg\015\012"; |
231 | my $res = "HTTP/1.1 $code $msg\015\012"; |
|
|
232 | my $GZ = ""; |
189 | |
233 | |
190 | if (exists $hdr->{Connection}) { |
234 | if (exists $hdr->{Connection}) { |
191 | if ($hdr->{Connection} =~ /close/) { |
235 | if ($hdr->{Connection} =~ /close/) { |
192 | $self->{h}{connection} = "close" |
236 | $self->{h}{connection} = "close" |
193 | } |
237 | } |
… | |
… | |
199 | $self->{h}{connection} = "close" |
243 | $self->{h}{connection} = "close" |
200 | } |
244 | } |
201 | } |
245 | } |
202 | } |
246 | } |
203 | |
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 | |
204 | $res .= "Date: $HTTP_NOW\015\012"; |
261 | $res .= "Date: $HTTP_NOW\015\012"; |
|
|
262 | $res .= "Server: $::NAME\015\012"; |
205 | |
263 | |
206 | while (my ($h, $v) = each %$hdr) { |
264 | while (my ($h, $v) = each %$hdr) { |
207 | $res .= "$h: $v\015\012" |
265 | $res .= "$h: $v\015\012" |
208 | } |
266 | } |
209 | $res .= "\015\012"; |
267 | $res .= "\015\012"; |
210 | |
268 | |
211 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
269 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
212 | |
270 | |
213 | 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). |
214 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. |
272 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. |
215 | " \"$self->{h}{referer}\"\n"; |
273 | " \"$self->{h}{referer}\"\n"; |
216 | |
274 | |
217 | print $accesslog $log if $accesslog; |
275 | print $::accesslog $log if $::accesslog; |
218 | print STDERR $log; |
276 | print STDERR $log; |
219 | |
277 | |
220 | $self->{written} += |
278 | $tbf_top->request(length $res, 1e6); |
221 | print {$self->{fh}} $res; |
279 | $self->{written} += print {$self->{fh}} $res; |
222 | } |
280 | } |
223 | |
281 | |
224 | sub err { |
282 | sub err { |
225 | my $self = shift; |
283 | my $self = shift; |
226 | my ($code, $msg, $hdr, $content) = @_; |
284 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
281 | my (%hdr, $h, $v); |
339 | my (%hdr, $h, $v); |
282 | |
340 | |
283 | $hdr{lc $1} .= ",$2" |
341 | $hdr{lc $1} .= ",$2" |
284 | while $req =~ /\G |
342 | while $req =~ /\G |
285 | ([^:\000-\040]+): |
343 | ([^:\000-\040]+): |
286 | [\008\040]* |
344 | [\011\040]* |
287 | ((?: [^\015\012]+ | \015\012[\008\040] )*) |
345 | ((?: [^\015\012]+ | \015\012[\011\040] )*) |
288 | \015\012 |
346 | \015\012 |
289 | /gxc; |
347 | /gxc; |
290 | |
348 | |
291 | $req =~ /\G\015\012$/ |
349 | $req =~ /\G\015\012$/ |
292 | or $self->err(400, "bad request"); |
350 | or $self->err(400, "bad request"); |
… | |
… | |
440 | or $self->err(304, "not modified"); |
498 | or $self->err(304, "not modified"); |
441 | |
499 | |
442 | if (-r "$path/index.html") { |
500 | if (-r "$path/index.html") { |
443 | # replace directory "size" by index.html filesize |
501 | # replace directory "size" by index.html filesize |
444 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
502 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
445 | $self->handle_file($queue_index); |
503 | $self->handle_file($queue_index, $tbf_top); |
446 | } else { |
504 | } else { |
447 | $self->handle_dir; |
505 | $self->handle_dir; |
448 | } |
506 | } |
449 | } |
507 | } |
450 | } elsif (-f _ && -r _) { |
508 | } elsif (-f _ && -r _) { |
451 | -x _ and $self->err(403, "forbidden"); |
509 | -x _ and $self->err(403, "forbidden"); |
452 | |
510 | |
453 | if (%{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
511 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
454 | my $timeout = $::NOW + 10; |
512 | my $timeout = $::NOW + 10; |
455 | while (%{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
513 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
456 | if ($timeout < $::NOW) { |
514 | if ($timeout < $::NOW) { |
457 | $self->block($::BLOCKTIME, "too many connections"); |
515 | $self->block($::BLOCKTIME, "too many connections"); |
458 | } else { |
516 | } else { |
459 | $httpevent->wait; |
517 | $httpevent->wait; |
460 | } |
518 | } |
461 | } |
519 | } |
462 | } |
520 | } |
463 | |
521 | |
464 | $self->handle_file($queue_file); |
522 | $self->handle_file($queue_file, $tbf_top); |
465 | } else { |
523 | } else { |
466 | $self->err(404, "not found"); |
524 | $self->err(404, "not found"); |
467 | } |
525 | } |
468 | } |
526 | } |
469 | } |
527 | } |
… | |
… | |
472 | my $self = shift; |
530 | my $self = shift; |
473 | my $idx = $self->diridx; |
531 | my $idx = $self->diridx; |
474 | |
532 | |
475 | $self->response(200, "ok", |
533 | $self->response(200, "ok", |
476 | { |
534 | { |
477 | "Content-Type" => "text/html", |
535 | "Content-Type" => "text/html; charset=utf-8", |
478 | "Content-Length" => length $idx, |
536 | "Content-Length" => length $idx, |
479 | "Last-Modified" => time2str ($self->{stat}[9]), |
537 | "Last-Modified" => time2str ($self->{stat}[9]), |
480 | }, |
538 | }, |
481 | $idx); |
539 | $idx); |
482 | } |
540 | } |
483 | |
541 | |
484 | sub handle_file { |
542 | sub handle_file { |
485 | my ($self, $queue) = @_; |
543 | my ($self, $queue, $tbf) = @_; |
486 | my $length = $self->{stat}[7]; |
544 | my $length = $self->{stat}[7]; |
487 | my $hdr = { |
545 | my $hdr = { |
488 | "Last-Modified" => time2str ((stat _)[9]), |
546 | "Last-Modified" => time2str ((stat _)[9]), |
|
|
547 | "Accept-Ranges" => "bytes", |
489 | }; |
548 | }; |
490 | |
549 | |
491 | my @code = (200, "ok"); |
550 | my @code = (200, "ok"); |
492 | my ($l, $h); |
551 | my ($l, $h); |
493 | |
552 | |
… | |
… | |
509 | |
568 | |
510 | satisfiable: |
569 | satisfiable: |
511 | # check for segmented downloads |
570 | # check for segmented downloads |
512 | if ($l && $::NO_SEGMENTED) { |
571 | if ($l && $::NO_SEGMENTED) { |
513 | my $timeout = $::NOW + 15; |
572 | my $timeout = $::NOW + 15; |
514 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
573 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
515 | if ($timeout <= $::NOW) { |
574 | if ($timeout <= $::NOW) { |
516 | $self->block($::BLOCKTIME, "segmented downloads are forbidden"); |
575 | $self->block($::BLOCKTIME, "segmented downloads are forbidden"); |
517 | #$self->err_segmented_download; |
576 | #$self->err_segmented_download; |
518 | } else { |
577 | } else { |
519 | $httpevent->wait; |
578 | $httpevent->wait; |
… | |
… | |
536 | |
595 | |
537 | $self->response(@code, $hdr, ""); |
596 | $self->response(@code, $hdr, ""); |
538 | |
597 | |
539 | if ($self->{method} eq "GET") { |
598 | if ($self->{method} eq "GET") { |
540 | $self->{time} = $::NOW; |
599 | $self->{time} = $::NOW; |
|
|
600 | $self->{written} = 0; |
541 | |
601 | |
542 | my $current = $Coro::current; |
602 | my $current = $Coro::current; |
543 | |
603 | |
544 | my ($fh, $buf, $r); |
604 | my ($fh, $buf, $r); |
545 | |
605 | |
… | |
… | |
561 | while ($h > 0) { |
621 | while ($h > 0) { |
562 | unless ($locked) { |
622 | unless ($locked) { |
563 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
623 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
564 | $bufsize = $::BUFSIZE; |
624 | $bufsize = $::BUFSIZE; |
565 | $self->{time} = $::NOW; |
625 | $self->{time} = $::NOW; |
|
|
626 | $self->{written} = 0; |
566 | } |
627 | } |
567 | } |
628 | } |
568 | |
629 | |
569 | if ($blocked{$self->{remote_id}}) { |
630 | if ($blocked{$self->{remote_id}}) { |
570 | $self->{h}{connection} = "close"; |
631 | $self->{h}{connection} = "close"; |
571 | die bless {}, err:: |
632 | die bless {}, err::; |
572 | } |
633 | } |
573 | |
634 | |
574 | if (0) { # !AIO |
635 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), $buf, 0 |
575 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
|
|
576 | or last; |
636 | or last; |
577 | } else { |
637 | |
578 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
638 | $tbf->request (length $buf); |
579 | $buf, 0, sub { |
|
|
580 | $r = $_[0]; |
|
|
581 | Coro::ready($current); |
|
|
582 | }); |
|
|
583 | &Coro::schedule; |
|
|
584 | last unless $r; |
|
|
585 | } |
|
|
586 | my $w = syswrite $self->{fh}, $buf |
639 | my $w = syswrite $self->{fh}, $buf |
587 | or last; |
640 | or last; |
588 | $::written += $w; |
641 | $::written += $w; |
589 | $self->{written} += $w; |
642 | $self->{written} += $w; |
590 | $l += $r; |
643 | $l += $r; |
… | |
… | |
592 | |
645 | |
593 | close $fh; |
646 | close $fh; |
594 | } |
647 | } |
595 | } |
648 | } |
596 | |
649 | |
597 | 1; |
650 | 1 |
|
|
651 | |