1 | use Coro; |
1 | use Coro; |
2 | use Coro::Semaphore; |
2 | use Coro::Semaphore; |
3 | use Coro::Event; |
3 | use Coro::Event; |
4 | use Coro::Socket; |
4 | use Coro::Socket; |
|
|
5 | |
|
|
6 | use HTTP::Date; |
5 | |
7 | |
6 | no utf8; |
8 | no utf8; |
7 | use bytes; |
9 | use bytes; |
8 | |
10 | |
9 | # at least on my machine, this thingy serves files |
11 | # at least on my machine, this thingy serves files |
… | |
… | |
25 | my $level = shift; |
27 | my $level = shift; |
26 | my $format = shift; |
28 | my $format = shift; |
27 | printf "---: $format\n", @_; |
29 | printf "---: $format\n", @_; |
28 | } |
30 | } |
29 | |
31 | |
30 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
32 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
|
|
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 | ); |
31 | |
40 | |
32 | my @newcons; |
41 | my @newcons; |
33 | my @pool; |
42 | my @pool; |
34 | |
43 | |
35 | # one "execution thread" |
44 | # one "execution thread" |
… | |
… | |
57 | Listen => 50, |
66 | Listen => 50, |
58 | or die "unable to start server"; |
67 | or die "unable to start server"; |
59 | |
68 | |
60 | push @listen_sockets, $http_port; |
69 | push @listen_sockets, $http_port; |
61 | |
70 | |
|
|
71 | our $NOW; |
|
|
72 | our $HTTP_NOW; |
|
|
73 | |
|
|
74 | Event->timer(interval => 1, hard => 1, cb => sub { |
|
|
75 | $NOW = time; |
|
|
76 | $HTTP_NOW = time2str $NOW; |
|
|
77 | })->now; |
|
|
78 | |
62 | # the "main thread" |
79 | # the "main thread" |
63 | async { |
80 | async { |
64 | slog 1, "accepting connections"; |
81 | slog 1, "accepting connections"; |
65 | while () { |
82 | while () { |
66 | $connections->down; |
83 | $connections->down; |
67 | push @newcons, [$http_port->accept]; |
84 | push @newcons, [$http_port->accept]; |
68 | #slog 3, "accepted @$connections ".scalar(@pool); |
85 | #slog 3, "accepted @$connections ".scalar(@pool); |
69 | $::NOW = time; |
|
|
70 | if (@pool) { |
86 | if (@pool) { |
71 | (pop @pool)->ready; |
87 | (pop @pool)->ready; |
72 | } else { |
88 | } else { |
73 | async \&handler; |
89 | async \&handler; |
74 | } |
90 | } |
… | |
… | |
154 | my ($self, $code, $msg, $hdr, $content) = @_; |
170 | my ($self, $code, $msg, $hdr, $content) = @_; |
155 | my $res = "HTTP/1.1 $code $msg\015\012"; |
171 | my $res = "HTTP/1.1 $code $msg\015\012"; |
156 | |
172 | |
157 | $self->{h}{connection} ||= $hdr->{Connection}; |
173 | $self->{h}{connection} ||= $hdr->{Connection}; |
158 | |
174 | |
159 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
175 | $res .= "Date: $HTTP_NOW\015\012"; |
160 | |
176 | |
161 | while (my ($h, $v) = each %$hdr) { |
177 | while (my ($h, $v) = each %$hdr) { |
162 | $res .= "$h: $v\015\012" |
178 | $res .= "$h: $v\015\012" |
163 | } |
179 | } |
164 | $res .= "\015\012"; |
180 | $res .= "\015\012"; |
… | |
… | |
177 | sub err { |
193 | sub err { |
178 | my $self = shift; |
194 | my $self = shift; |
179 | my ($code, $msg, $hdr, $content) = @_; |
195 | my ($code, $msg, $hdr, $content) = @_; |
180 | |
196 | |
181 | unless (defined $content) { |
197 | unless (defined $content) { |
182 | $content = "$code $msg"; |
198 | $content = "$code $msg\n"; |
183 | $hdr->{"Content-Type"} = "text/plain"; |
199 | $hdr->{"Content-Type"} = "text/plain"; |
184 | $hdr->{"Content-Length"} = length $content; |
200 | $hdr->{"Content-Length"} = length $content; |
185 | } |
201 | } |
186 | $hdr->{"Connection"} = "close"; |
202 | $hdr->{"Connection"} = "close"; |
187 | |
203 | |
… | |
… | |
222 | |
238 | |
223 | delete $blocked{$ip}; |
239 | delete $blocked{$ip}; |
224 | } |
240 | } |
225 | |
241 | |
226 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
242 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
243 | my $delay = 120; |
|
|
244 | while (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
245 | if ($delay <= 0) { |
227 | $self->slog(2, "blocked ip $ip"); |
246 | $self->slog(2, "blocked ip $ip"); |
228 | $self->err_blocked; |
247 | $self->err_blocked; |
|
|
248 | } else { |
|
|
249 | Coro::Event::do_timer(after => 3); |
|
|
250 | $delay -= 3; |
|
|
251 | } |
|
|
252 | } |
229 | } |
253 | } |
230 | |
254 | |
231 | $req =~ /^(?:\015\012)? |
255 | $req =~ /^(?:\015\012)? |
232 | (GET|HEAD) \040+ |
256 | (GET|HEAD) \040+ |
233 | ([^\040]+) \040+ |
257 | ([^\040]+) \040+ |
… | |
… | |
405 | $idx); |
429 | $idx); |
406 | } |
430 | } |
407 | |
431 | |
408 | sub handle_file { |
432 | sub handle_file { |
409 | my $self = shift; |
433 | my $self = shift; |
410 | my $length = -s _; |
434 | my $length = $self->{stat}[7]; |
|
|
435 | my $queue = $::transfers[$length >= $::TRANSFER_SMALL]; |
411 | my $hdr = { |
436 | my $hdr = { |
412 | "Last-Modified" => time2str ((stat _)[9]), |
437 | "Last-Modified" => time2str ((stat _)[9]), |
413 | }; |
438 | }; |
414 | |
439 | |
415 | my @code = (200, "ok"); |
440 | my @code = (200, "ok"); |
… | |
… | |
427 | } |
452 | } |
428 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
453 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
429 | } |
454 | } |
430 | $hdr->{"Content-Range"} = "bytes */$length"; |
455 | $hdr->{"Content-Range"} = "bytes */$length"; |
431 | $hdr->{"Content-Length"} = $length; |
456 | $hdr->{"Content-Length"} = $length; |
432 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
|
|
433 | $self->err(416, "not satisfiable", $hdr, ""); |
457 | $self->err(416, "not satisfiable", $hdr, ""); |
434 | |
458 | |
435 | satisfiable: |
459 | satisfiable: |
436 | # check for segmented downloads |
460 | # check for segmented downloads |
437 | if ($l && $::NO_SEGMENTED) { |
461 | if ($l && $::NO_SEGMENTED) { |
… | |
… | |
459 | $hdr->{"Content-Length"} = $length; |
483 | $hdr->{"Content-Length"} = $length; |
460 | |
484 | |
461 | $self->response(@code, $hdr, ""); |
485 | $self->response(@code, $hdr, ""); |
462 | |
486 | |
463 | if ($self->{method} eq "GET") { |
487 | if ($self->{method} eq "GET") { |
|
|
488 | $self->{time} = $::NOW; |
|
|
489 | |
|
|
490 | my $fudge = $queue->[0]->waiters; |
|
|
491 | $fudge = $fudge ? ($fudge+1)/$fudge : 1; |
|
|
492 | |
|
|
493 | $queue->[1] *= $fudge; |
|
|
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 | |
|
|
503 | $self->{fh}->writable or return; |
|
|
504 | |
464 | my ($fh, $buf, $r); |
505 | my ($fh, $buf, $r); |
465 | my $current = $Coro::current; |
506 | my $current = $Coro::current; |
466 | open $fh, "<", $self->{path} |
507 | open $fh, "<", $self->{path} |
467 | or die "$self->{path}: late open failure ($!)"; |
508 | or die "$self->{path}: late open failure ($!)"; |
468 | |
509 | |
… | |
… | |
494 | or last; |
535 | or last; |
495 | $::written += $w; |
536 | $::written += $w; |
496 | $self->{written} += $w; |
537 | $self->{written} += $w; |
497 | $l += $r; |
538 | $l += $r; |
498 | } |
539 | } |
499 | } |
|
|
500 | |
540 | |
501 | close $fh; |
541 | close $fh; |
|
|
542 | } |
502 | } |
543 | } |
503 | |
544 | |
504 | 1; |
545 | 1; |