… | |
… | |
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 => 200000; |
59 | |
61 | |
60 | my @newcons; |
62 | my @newcons; |
61 | my @pool; |
63 | my @pool; |
62 | |
64 | |
63 | # one "execution thread" |
65 | # one "execution thread" |
… | |
… | |
232 | " \"$self->{h}{referer}\"\n"; |
234 | " \"$self->{h}{referer}\"\n"; |
233 | |
235 | |
234 | print $::accesslog $log if $::accesslog; |
236 | print $::accesslog $log if $::accesslog; |
235 | print STDERR $log; |
237 | print STDERR $log; |
236 | |
238 | |
237 | $self->{written} += |
239 | $tbf_top->request(length $res, 1e6); |
238 | print {$self->{fh}} $res; |
240 | $self->{written} += print {$self->{fh}} $res; |
239 | } |
241 | } |
240 | |
242 | |
241 | sub err { |
243 | sub err { |
242 | my $self = shift; |
244 | my $self = shift; |
243 | my ($code, $msg, $hdr, $content) = @_; |
245 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
457 | or $self->err(304, "not modified"); |
459 | or $self->err(304, "not modified"); |
458 | |
460 | |
459 | if (-r "$path/index.html") { |
461 | if (-r "$path/index.html") { |
460 | # replace directory "size" by index.html filesize |
462 | # replace directory "size" by index.html filesize |
461 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
463 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
462 | $self->handle_file($queue_index); |
464 | $self->handle_file($queue_index, $tbf_top); |
463 | } else { |
465 | } else { |
464 | $self->handle_dir; |
466 | $self->handle_dir; |
465 | } |
467 | } |
466 | } |
468 | } |
467 | } elsif (-f _ && -r _) { |
469 | } elsif (-f _ && -r _) { |
… | |
… | |
476 | $httpevent->wait; |
478 | $httpevent->wait; |
477 | } |
479 | } |
478 | } |
480 | } |
479 | } |
481 | } |
480 | |
482 | |
481 | $self->handle_file($queue_file); |
483 | $self->handle_file($queue_file, $tbf_top); |
482 | } else { |
484 | } else { |
483 | $self->err(404, "not found"); |
485 | $self->err(404, "not found"); |
484 | } |
486 | } |
485 | } |
487 | } |
486 | } |
488 | } |
… | |
… | |
497 | }, |
499 | }, |
498 | $idx); |
500 | $idx); |
499 | } |
501 | } |
500 | |
502 | |
501 | sub handle_file { |
503 | sub handle_file { |
502 | my ($self, $queue) = @_; |
504 | my ($self, $queue, $tbf) = @_; |
503 | my $length = $self->{stat}[7]; |
505 | my $length = $self->{stat}[7]; |
504 | my $hdr = { |
506 | my $hdr = { |
505 | "Last-Modified" => time2str ((stat _)[9]), |
507 | "Last-Modified" => time2str ((stat _)[9]), |
506 | }; |
508 | }; |
507 | |
509 | |
… | |
… | |
598 | Coro::ready($current); |
600 | Coro::ready($current); |
599 | }); |
601 | }); |
600 | &Coro::schedule; |
602 | &Coro::schedule; |
601 | last unless $r; |
603 | last unless $r; |
602 | } |
604 | } |
|
|
605 | |
|
|
606 | $tbf->request(length $buf); |
603 | my $w = syswrite $self->{fh}, $buf |
607 | my $w = syswrite $self->{fh}, $buf |
604 | or last; |
608 | or last; |
605 | $::written += $w; |
609 | $::written += $w; |
606 | $self->{written} += $w; |
610 | $self->{written} += $w; |
607 | $l += $r; |
611 | $l += $r; |