… | |
… | |
54 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
54 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
55 | our $httpevent = new Coro::Signal; |
55 | our $httpevent = new Coro::Signal; |
56 | |
56 | |
57 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
57 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
58 | our $queue_index = new transferqueue 10; |
58 | our $queue_index = new transferqueue 10; |
|
|
59 | |
|
|
60 | our $tbf_top = new tbf rate => 150000; |
|
|
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" |
… | |
… | |
225 | } |
240 | } |
226 | $res .= "\015\012"; |
241 | $res .= "\015\012"; |
227 | |
242 | |
228 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
243 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
229 | |
244 | |
230 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). |
245 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW). |
231 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. |
246 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. |
232 | " \"$self->{h}{referer}\"\n"; |
247 | " \"$self->{h}{referer}\"\n"; |
233 | |
248 | |
234 | print $accesslog $log if $accesslog; |
249 | print $::accesslog $log if $::accesslog; |
235 | print STDERR $log; |
250 | print STDERR $log; |
236 | |
251 | |
237 | $self->{written} += |
252 | $tbf_top->request(length $res, 1e6); |
238 | print {$self->{fh}} $res; |
253 | $self->{written} += print {$self->{fh}} $res; |
239 | } |
254 | } |
240 | |
255 | |
241 | sub err { |
256 | sub err { |
242 | my $self = shift; |
257 | my $self = shift; |
243 | my ($code, $msg, $hdr, $content) = @_; |
258 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
298 | my (%hdr, $h, $v); |
313 | my (%hdr, $h, $v); |
299 | |
314 | |
300 | $hdr{lc $1} .= ",$2" |
315 | $hdr{lc $1} .= ",$2" |
301 | while $req =~ /\G |
316 | while $req =~ /\G |
302 | ([^:\000-\040]+): |
317 | ([^:\000-\040]+): |
303 | [\010\040]* |
318 | [\011\040]* |
304 | ((?: [^\015\012]+ | \015\012[\010\040] )*) |
319 | ((?: [^\015\012]+ | \015\012[\011\040] )*) |
305 | \015\012 |
320 | \015\012 |
306 | /gxc; |
321 | /gxc; |
307 | |
322 | |
308 | $req =~ /\G\015\012$/ |
323 | $req =~ /\G\015\012$/ |
309 | or $self->err(400, "bad request"); |
324 | or $self->err(400, "bad request"); |
… | |
… | |
457 | or $self->err(304, "not modified"); |
472 | or $self->err(304, "not modified"); |
458 | |
473 | |
459 | if (-r "$path/index.html") { |
474 | if (-r "$path/index.html") { |
460 | # replace directory "size" by index.html filesize |
475 | # replace directory "size" by index.html filesize |
461 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
476 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
462 | $self->handle_file($queue_index); |
477 | $self->handle_file($queue_index, $tbf_top); |
463 | } else { |
478 | } else { |
464 | $self->handle_dir; |
479 | $self->handle_dir; |
465 | } |
480 | } |
466 | } |
481 | } |
467 | } elsif (-f _ && -r _) { |
482 | } elsif (-f _ && -r _) { |
… | |
… | |
476 | $httpevent->wait; |
491 | $httpevent->wait; |
477 | } |
492 | } |
478 | } |
493 | } |
479 | } |
494 | } |
480 | |
495 | |
481 | $self->handle_file($queue_file); |
496 | $self->handle_file($queue_file, $tbf_top); |
482 | } else { |
497 | } else { |
483 | $self->err(404, "not found"); |
498 | $self->err(404, "not found"); |
484 | } |
499 | } |
485 | } |
500 | } |
486 | } |
501 | } |
… | |
… | |
497 | }, |
512 | }, |
498 | $idx); |
513 | $idx); |
499 | } |
514 | } |
500 | |
515 | |
501 | sub handle_file { |
516 | sub handle_file { |
502 | my ($self, $queue) = @_; |
517 | my ($self, $queue, $tbf) = @_; |
503 | my $length = $self->{stat}[7]; |
518 | my $length = $self->{stat}[7]; |
504 | my $hdr = { |
519 | my $hdr = { |
505 | "Last-Modified" => time2str ((stat _)[9]), |
520 | "Last-Modified" => time2str ((stat _)[9]), |
506 | }; |
521 | }; |
507 | |
522 | |
… | |
… | |
598 | Coro::ready($current); |
613 | Coro::ready($current); |
599 | }); |
614 | }); |
600 | &Coro::schedule; |
615 | &Coro::schedule; |
601 | last unless $r; |
616 | last unless $r; |
602 | } |
617 | } |
|
|
618 | |
|
|
619 | $tbf->request(length $buf); |
603 | my $w = syswrite $self->{fh}, $buf |
620 | my $w = syswrite $self->{fh}, $buf |
604 | or last; |
621 | or last; |
605 | $::written += $w; |
622 | $::written += $w; |
606 | $self->{written} += $w; |
623 | $self->{written} += $w; |
607 | $l += $r; |
624 | $l += $r; |