… | |
… | |
5 | |
5 | |
6 | use HTTP::Date; |
6 | use HTTP::Date; |
7 | |
7 | |
8 | no utf8; |
8 | no utf8; |
9 | use bytes; |
9 | use bytes; |
10 | |
|
|
11 | our @wait_time = (); # used to calculcate avg. waiting time |
|
|
12 | our $wait_time_length = 25; |
|
|
13 | |
10 | |
14 | # at least on my machine, this thingy serves files |
11 | # at least on my machine, this thingy serves files |
15 | # quite a bit faster than apache, ;) |
12 | # quite a bit faster than apache, ;) |
16 | # and quite a bit slower than thttpd :( |
13 | # and quite a bit slower than thttpd :( |
17 | |
14 | |
… | |
… | |
31 | my $format = shift; |
28 | my $format = shift; |
32 | printf "---: $format\n", @_; |
29 | printf "---: $format\n", @_; |
33 | } |
30 | } |
34 | |
31 | |
35 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
32 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
36 | our $transfers = new Coro::Semaphore $MAX_TRANSFER || 50; |
33 | |
|
|
34 | our $wait_factor = 0.95; |
|
|
35 | |
|
|
36 | our @transfers = ( |
|
|
37 | [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], |
|
|
38 | [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1], |
|
|
39 | ); |
37 | |
40 | |
38 | my @newcons; |
41 | my @newcons; |
39 | my @pool; |
42 | my @pool; |
40 | |
43 | |
41 | # one "execution thread" |
44 | # one "execution thread" |
… | |
… | |
69 | our $HTTP_NOW; |
72 | our $HTTP_NOW; |
70 | |
73 | |
71 | Event->timer(interval => 1, hard => 1, cb => sub { |
74 | Event->timer(interval => 1, hard => 1, cb => sub { |
72 | $NOW = time; |
75 | $NOW = time; |
73 | $HTTP_NOW = time2str $NOW; |
76 | $HTTP_NOW = time2str $NOW; |
74 | }); |
77 | })->now; |
75 | |
78 | |
76 | # the "main thread" |
79 | # the "main thread" |
77 | async { |
80 | async { |
78 | slog 1, "accepting connections"; |
81 | slog 1, "accepting connections"; |
79 | while () { |
82 | while () { |
… | |
… | |
190 | sub err { |
193 | sub err { |
191 | my $self = shift; |
194 | my $self = shift; |
192 | my ($code, $msg, $hdr, $content) = @_; |
195 | my ($code, $msg, $hdr, $content) = @_; |
193 | |
196 | |
194 | unless (defined $content) { |
197 | unless (defined $content) { |
195 | $content = "$code $msg"; |
198 | $content = "$code $msg\n"; |
196 | $hdr->{"Content-Type"} = "text/plain"; |
199 | $hdr->{"Content-Type"} = "text/plain"; |
197 | $hdr->{"Content-Length"} = length $content; |
200 | $hdr->{"Content-Length"} = length $content; |
198 | } |
201 | } |
199 | $hdr->{"Connection"} = "close"; |
202 | $hdr->{"Connection"} = "close"; |
200 | |
203 | |
… | |
… | |
426 | $idx); |
429 | $idx); |
427 | } |
430 | } |
428 | |
431 | |
429 | sub handle_file { |
432 | sub handle_file { |
430 | my $self = shift; |
433 | my $self = shift; |
431 | my $length = -s _; |
434 | my $length = $self->{stat}[7]; |
|
|
435 | my $queue = $::transfers[$length >= $::TRANSFER_SMALL]; |
432 | my $hdr = { |
436 | my $hdr = { |
433 | "Last-Modified" => time2str ((stat _)[9]), |
437 | "Last-Modified" => time2str ((stat _)[9]), |
434 | }; |
438 | }; |
435 | |
439 | |
436 | my @code = (200, "ok"); |
440 | my @code = (200, "ok"); |
… | |
… | |
448 | } |
452 | } |
449 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
453 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
450 | } |
454 | } |
451 | $hdr->{"Content-Range"} = "bytes */$length"; |
455 | $hdr->{"Content-Range"} = "bytes */$length"; |
452 | $hdr->{"Content-Length"} = $length; |
456 | $hdr->{"Content-Length"} = $length; |
453 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
|
|
454 | $self->err(416, "not satisfiable", $hdr, ""); |
457 | $self->err(416, "not satisfiable", $hdr, ""); |
455 | |
458 | |
456 | satisfiable: |
459 | satisfiable: |
457 | # check for segmented downloads |
460 | # check for segmented downloads |
458 | if ($l && $::NO_SEGMENTED) { |
461 | if ($l && $::NO_SEGMENTED) { |
… | |
… | |
482 | $self->response(@code, $hdr, ""); |
485 | $self->response(@code, $hdr, ""); |
483 | |
486 | |
484 | if ($self->{method} eq "GET") { |
487 | if ($self->{method} eq "GET") { |
485 | $self->{time} = $::NOW; |
488 | $self->{time} = $::NOW; |
486 | |
489 | |
|
|
490 | my $fudge = $queue->[0]->waiters; |
|
|
491 | $fudge = $fudge ? ($fudge+1)/$fudge : 1; |
|
|
492 | |
|
|
493 | $queue->[1] *= $fudge; |
487 | my $transfer = $::transfers->guard; |
494 | my $transfer = $queue->[0]->guard; |
|
|
495 | |
|
|
496 | if ($fudge != 1) { |
|
|
497 | $queue->[1] /= $fudge; |
|
|
498 | $queue->[1] = $queue->[1] * $::wait_factor |
|
|
499 | + ($::NOW - $self->{time}) * (1 - $::wait_factor); |
|
|
500 | } |
|
|
501 | $self->{time} = $::NOW; |
|
|
502 | |
488 | $self->{fh}->writable or return; |
503 | $self->{fh}->writable or return; |
489 | |
|
|
490 | push @::wait_time, $::NOW - $self->{time}; |
|
|
491 | shift @::wait_time if @wait_time > $wait_time_length; |
|
|
492 | $self->{time} = $::NOW; |
|
|
493 | |
504 | |
494 | my ($fh, $buf, $r); |
505 | my ($fh, $buf, $r); |
495 | my $current = $Coro::current; |
506 | my $current = $Coro::current; |
496 | open $fh, "<", $self->{path} |
507 | open $fh, "<", $self->{path} |
497 | or die "$self->{path}: late open failure ($!)"; |
508 | or die "$self->{path}: late open failure ($!)"; |