… | |
… | |
4 | use Coro::Socket; |
4 | use Coro::Socket; |
5 | use Coro::Signal; |
5 | use Coro::Signal; |
6 | |
6 | |
7 | use HTTP::Date; |
7 | use HTTP::Date; |
8 | use POSIX (); |
8 | use POSIX (); |
|
|
9 | |
|
|
10 | use Compress::Zlib (); |
9 | |
11 | |
10 | no utf8; |
12 | no utf8; |
11 | use bytes; |
13 | use bytes; |
12 | |
14 | |
13 | # at least on my machine, this thingy serves files |
15 | # at least on my machine, this thingy serves files |
… | |
… | |
50 | } |
52 | } |
51 | |
53 | |
52 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
54 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
53 | our $httpevent = new Coro::Signal; |
55 | our $httpevent = new Coro::Signal; |
54 | |
56 | |
55 | our $queue_file = new transferqueue slots => $MAX_TRANSFERS, maxsize => 250_000_000; |
57 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
56 | our $queue_index = new transferqueue slots => 10; |
58 | our $queue_index = new transferqueue 10; |
57 | |
59 | |
58 | our $requests; |
60 | our $tbf_top = new tbf rate => $TBF_RATE || 100000; |
|
|
61 | |
|
|
62 | my $unused_bytes = 0; |
|
|
63 | my $unused_last = time; |
|
|
64 | |
|
|
65 | sub unused_bandwidth { |
|
|
66 | $unused_bytes += $_[0]; |
|
|
67 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
|
|
68 | $unused_last = $NOW; |
|
|
69 | $unused_bytes = 0; |
|
|
70 | $queue_file->force_wake_next; |
|
|
71 | slog 1, "forced filetransfer due to unused bandwidth"; |
|
|
72 | } |
|
|
73 | } |
59 | |
74 | |
60 | my @newcons; |
75 | my @newcons; |
61 | my @pool; |
76 | my @pool; |
62 | |
77 | |
63 | # one "execution thread" |
78 | # one "execution thread" |
… | |
… | |
95 | if (@pool) { |
110 | if (@pool) { |
96 | (pop @pool)->ready; |
111 | (pop @pool)->ready; |
97 | } else { |
112 | } else { |
98 | async \&handler; |
113 | async \&handler; |
99 | } |
114 | } |
100 | |
|
|
101 | } |
115 | } |
102 | }; |
116 | }; |
103 | } |
117 | } |
104 | |
118 | |
105 | my $http_port = new Coro::Socket |
119 | my $http_port = new Coro::Socket |
… | |
… | |
179 | sub DESTROY { |
193 | sub DESTROY { |
180 | #my $self = shift; |
194 | #my $self = shift; |
181 | $::conns--; |
195 | $::conns--; |
182 | } |
196 | } |
183 | |
197 | |
|
|
198 | sub prune_cache { |
|
|
199 | my $hash = $_[0]; |
|
|
200 | |
|
|
201 | for (keys %$hash) { |
|
|
202 | if (ref $hash->{$_} eq HASH::) { |
|
|
203 | prune_cache($hash->{$_}); |
|
|
204 | unless (scalar keys %{$hash->{$_}}) { |
|
|
205 | delete $hash->{$_}; |
|
|
206 | $d2++; |
|
|
207 | } |
|
|
208 | } |
|
|
209 | } |
|
|
210 | } |
|
|
211 | |
|
|
212 | sub prune_caches { |
|
|
213 | prune_cache \%conn; |
|
|
214 | prune_cache \%uri; |
|
|
215 | |
|
|
216 | for (keys %blocked) { |
|
|
217 | delete $blocked{$_} if $blocked{$_}[0] > $::NOW; |
|
|
218 | } |
|
|
219 | } |
|
|
220 | |
|
|
221 | Event->timer(interval => 60, cb => \&prune_caches); |
|
|
222 | |
184 | sub slog { |
223 | sub slog { |
185 | my $self = shift; |
224 | my $self = shift; |
186 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
225 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
187 | } |
226 | } |
188 | |
227 | |
189 | sub response { |
228 | sub response { |
190 | my ($self, $code, $msg, $hdr, $content) = @_; |
229 | my ($self, $code, $msg, $hdr, $content) = @_; |
191 | my $res = "HTTP/1.1 $code $msg\015\012"; |
230 | my $res = "HTTP/1.1 $code $msg\015\012"; |
|
|
231 | my $GZ = ""; |
192 | |
232 | |
193 | if (exists $hdr->{Connection}) { |
233 | if (exists $hdr->{Connection}) { |
194 | if ($hdr->{Connection} =~ /close/) { |
234 | if ($hdr->{Connection} =~ /close/) { |
195 | $self->{h}{connection} = "close" |
235 | $self->{h}{connection} = "close" |
196 | } |
236 | } |
… | |
… | |
202 | $self->{h}{connection} = "close" |
242 | $self->{h}{connection} = "close" |
203 | } |
243 | } |
204 | } |
244 | } |
205 | } |
245 | } |
206 | |
246 | |
|
|
247 | if ($self->{method} ne "HEAD" |
|
|
248 | && $self->{h}{"accept-encoding"} =~ /\bgzip\b/ |
|
|
249 | && 400 < length $content |
|
|
250 | && $hdr->{"Content-Length"} == length $content |
|
|
251 | && !exists $hdr->{"Content-Encoding"} |
|
|
252 | ) { |
|
|
253 | my $orig = length $content; |
|
|
254 | $hdr->{"Content-Encoding"} = "gzip"; |
|
|
255 | $content = Compress::Zlib::memGzip(\$content); |
|
|
256 | $hdr->{"Content-Length"} = length $content; |
|
|
257 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
|
|
258 | } |
|
|
259 | |
207 | $res .= "Date: $HTTP_NOW\015\012"; |
260 | $res .= "Date: $HTTP_NOW\015\012"; |
|
|
261 | $res .= "Server: $::NAME\015\012"; |
208 | |
262 | |
209 | while (my ($h, $v) = each %$hdr) { |
263 | while (my ($h, $v) = each %$hdr) { |
210 | $res .= "$h: $v\015\012" |
264 | $res .= "$h: $v\015\012" |
211 | } |
265 | } |
212 | $res .= "\015\012"; |
266 | $res .= "\015\012"; |
213 | |
267 | |
214 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
268 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
215 | |
269 | |
216 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). |
270 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW). |
217 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. |
271 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. |
218 | " \"$self->{h}{referer}\"\n"; |
272 | " \"$self->{h}{referer}\"\n"; |
219 | |
273 | |
220 | print $accesslog $log if $accesslog; |
274 | print $::accesslog $log if $::accesslog; |
221 | print STDERR $log; |
275 | print STDERR $log; |
222 | |
276 | |
223 | $self->{written} += |
277 | $tbf_top->request(length $res, 1e6); |
224 | print {$self->{fh}} $res; |
278 | $self->{written} += print {$self->{fh}} $res; |
225 | } |
279 | } |
226 | |
280 | |
227 | sub err { |
281 | sub err { |
228 | my $self = shift; |
282 | my $self = shift; |
229 | my ($code, $msg, $hdr, $content) = @_; |
283 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
284 | my (%hdr, $h, $v); |
338 | my (%hdr, $h, $v); |
285 | |
339 | |
286 | $hdr{lc $1} .= ",$2" |
340 | $hdr{lc $1} .= ",$2" |
287 | while $req =~ /\G |
341 | while $req =~ /\G |
288 | ([^:\000-\040]+): |
342 | ([^:\000-\040]+): |
289 | [\008\040]* |
343 | [\011\040]* |
290 | ((?: [^\015\012]+ | \015\012[\008\040] )*) |
344 | ((?: [^\015\012]+ | \015\012[\011\040] )*) |
291 | \015\012 |
345 | \015\012 |
292 | /gxc; |
346 | /gxc; |
293 | |
347 | |
294 | $req =~ /\G\015\012$/ |
348 | $req =~ /\G\015\012$/ |
295 | or $self->err(400, "bad request"); |
349 | or $self->err(400, "bad request"); |
296 | |
350 | |
297 | $self->{h}{$h} = substr $v, 1 |
351 | $self->{h}{$h} = substr $v, 1 |
298 | while ($h, $v) = each %hdr; |
352 | while ($h, $v) = each %hdr; |
299 | } |
353 | } |
300 | |
|
|
301 | $requests++; |
|
|
302 | |
354 | |
303 | # remote id should be unique per user |
355 | # remote id should be unique per user |
304 | my $id = $self->{remote_addr}; |
356 | my $id = $self->{remote_addr}; |
305 | |
357 | |
306 | if (exists $self->{h}{"client-ip"}) { |
358 | if (exists $self->{h}{"client-ip"}) { |
… | |
… | |
445 | or $self->err(304, "not modified"); |
497 | or $self->err(304, "not modified"); |
446 | |
498 | |
447 | if (-r "$path/index.html") { |
499 | if (-r "$path/index.html") { |
448 | # replace directory "size" by index.html filesize |
500 | # replace directory "size" by index.html filesize |
449 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
501 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
450 | $self->handle_file($queue_index); |
502 | $self->handle_file($queue_index, $tbf_top); |
451 | } else { |
503 | } else { |
452 | $self->handle_dir; |
504 | $self->handle_dir; |
453 | } |
505 | } |
454 | } |
506 | } |
455 | } elsif (-f _ && -r _) { |
507 | } elsif (-f _ && -r _) { |
… | |
… | |
464 | $httpevent->wait; |
516 | $httpevent->wait; |
465 | } |
517 | } |
466 | } |
518 | } |
467 | } |
519 | } |
468 | |
520 | |
469 | $self->handle_file($queue_file); |
521 | $self->handle_file($queue_file, $tbf_top); |
470 | } else { |
522 | } else { |
471 | $self->err(404, "not found"); |
523 | $self->err(404, "not found"); |
472 | } |
524 | } |
473 | } |
525 | } |
474 | } |
526 | } |
… | |
… | |
485 | }, |
537 | }, |
486 | $idx); |
538 | $idx); |
487 | } |
539 | } |
488 | |
540 | |
489 | sub handle_file { |
541 | sub handle_file { |
490 | my ($self, $queue) = @_; |
542 | my ($self, $queue, $tbf) = @_; |
491 | my $length = $self->{stat}[7]; |
543 | my $length = $self->{stat}[7]; |
492 | my $hdr = { |
544 | my $hdr = { |
493 | "Last-Modified" => time2str ((stat _)[9]), |
545 | "Last-Modified" => time2str ((stat _)[9]), |
494 | }; |
546 | }; |
495 | |
547 | |
… | |
… | |
541 | |
593 | |
542 | $self->response(@code, $hdr, ""); |
594 | $self->response(@code, $hdr, ""); |
543 | |
595 | |
544 | if ($self->{method} eq "GET") { |
596 | if ($self->{method} eq "GET") { |
545 | $self->{time} = $::NOW; |
597 | $self->{time} = $::NOW; |
|
|
598 | $self->{written} = 0; |
546 | |
599 | |
547 | my $current = $Coro::current; |
600 | my $current = $Coro::current; |
548 | |
601 | |
549 | my ($fh, $buf, $r); |
602 | my ($fh, $buf, $r); |
550 | |
603 | |
… | |
… | |
566 | while ($h > 0) { |
619 | while ($h > 0) { |
567 | unless ($locked) { |
620 | unless ($locked) { |
568 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
621 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
569 | $bufsize = $::BUFSIZE; |
622 | $bufsize = $::BUFSIZE; |
570 | $self->{time} = $::NOW; |
623 | $self->{time} = $::NOW; |
|
|
624 | $self->{written} = 0; |
571 | } |
625 | } |
572 | } |
626 | } |
573 | |
627 | |
574 | if ($blocked{$self->{remote_id}}) { |
628 | if ($blocked{$self->{remote_id}}) { |
575 | $self->{h}{connection} = "close"; |
629 | $self->{h}{connection} = "close"; |
… | |
… | |
586 | Coro::ready($current); |
640 | Coro::ready($current); |
587 | }); |
641 | }); |
588 | &Coro::schedule; |
642 | &Coro::schedule; |
589 | last unless $r; |
643 | last unless $r; |
590 | } |
644 | } |
|
|
645 | |
|
|
646 | $tbf->request(length $buf); |
591 | my $w = syswrite $self->{fh}, $buf |
647 | my $w = syswrite $self->{fh}, $buf |
592 | or last; |
648 | or last; |
593 | $::written += $w; |
649 | $::written += $w; |
594 | $self->{written} += $w; |
650 | $self->{written} += $w; |
595 | $l += $r; |
651 | $l += $r; |