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