… | |
… | |
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" |
42 | sub handler { |
45 | sub handler { |
43 | while () { |
46 | while () { |
44 | my $new = pop @newcons; |
|
|
45 | if ($new) { |
47 | if (@newcons) { |
46 | eval { |
48 | eval { |
47 | conn->new(@$new)->handle; |
49 | conn->new(@{pop @newcons})->handle; |
48 | }; |
50 | }; |
49 | slog 1, "$@" if $@ && !ref $@; |
51 | slog 1, "$@" if $@ && !ref $@; |
50 | $connections->up; |
52 | $connections->up; |
51 | } else { |
53 | } else { |
52 | last if @pool >= $MAX_POOL; |
54 | last if @pool >= $MAX_POOL; |
… | |
… | |
69 | our $HTTP_NOW; |
71 | our $HTTP_NOW; |
70 | |
72 | |
71 | Event->timer(interval => 1, hard => 1, cb => sub { |
73 | Event->timer(interval => 1, hard => 1, cb => sub { |
72 | $NOW = time; |
74 | $NOW = time; |
73 | $HTTP_NOW = time2str $NOW; |
75 | $HTTP_NOW = time2str $NOW; |
74 | }); |
76 | })->now; |
75 | |
77 | |
76 | # the "main thread" |
78 | # the "main thread" |
77 | async { |
79 | async { |
78 | slog 1, "accepting connections"; |
80 | slog 1, "accepting connections"; |
79 | while () { |
81 | while () { |
… | |
… | |
96 | use Convert::Scalar 'weaken'; |
98 | use Convert::Scalar 'weaken'; |
97 | use Linux::AIO; |
99 | use Linux::AIO; |
98 | |
100 | |
99 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
101 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
100 | |
102 | |
101 | my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4; |
|
|
102 | |
|
|
103 | Event->io(fd => Linux::AIO::poll_fileno, |
103 | Event->io(fd => Linux::AIO::poll_fileno, |
104 | poll => 'r', async => 1, |
104 | poll => 'r', async => 1, |
105 | cb => \&Linux::AIO::poll_cb); |
105 | cb => \&Linux::AIO::poll_cb); |
106 | |
106 | |
107 | our %conn; # $conn{ip}{self} => connobj |
107 | our %conn; # $conn{ip}{self} => connobj |
… | |
… | |
133 | or $self->err(500, "unable to decode peername"); |
133 | or $self->err(500, "unable to decode peername"); |
134 | |
134 | |
135 | $self->{remote_addr} = inet_ntoa $iaddr; |
135 | $self->{remote_addr} = inet_ntoa $iaddr; |
136 | $self->{time} = $::NOW; |
136 | $self->{time} = $::NOW; |
137 | |
137 | |
138 | # enter ourselves into various lists |
|
|
139 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
|
|
140 | |
|
|
141 | $::conns++; |
138 | $::conns++; |
142 | |
139 | |
143 | $self; |
140 | $self; |
144 | } |
141 | } |
145 | |
142 | |
146 | sub DESTROY { |
143 | sub DESTROY { |
147 | my $self = shift; |
144 | my $self = shift; |
148 | |
|
|
149 | $::conns--; |
145 | $::conns--; |
150 | |
|
|
151 | $self->eoconn; |
146 | $self->eoconn; |
152 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
153 | } |
147 | } |
154 | |
148 | |
155 | # end of connection |
149 | # end of connection |
156 | sub eoconn { |
150 | sub eoconn { |
157 | my $self = shift; |
151 | my $self = shift; |
|
|
152 | |
|
|
153 | # clean up hints |
|
|
154 | delete $conn{$self->{remote_id}}{$self*1}; |
158 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
155 | delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; |
159 | } |
156 | } |
160 | |
157 | |
161 | sub slog { |
158 | sub slog { |
162 | my $self = shift; |
159 | my $self = shift; |
163 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
160 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
… | |
… | |
190 | sub err { |
187 | sub err { |
191 | my $self = shift; |
188 | my $self = shift; |
192 | my ($code, $msg, $hdr, $content) = @_; |
189 | my ($code, $msg, $hdr, $content) = @_; |
193 | |
190 | |
194 | unless (defined $content) { |
191 | unless (defined $content) { |
195 | $content = "$code $msg"; |
192 | $content = "$code $msg\n"; |
196 | $hdr->{"Content-Type"} = "text/plain"; |
193 | $hdr->{"Content-Type"} = "text/plain"; |
197 | $hdr->{"Content-Length"} = length $content; |
194 | $hdr->{"Content-Length"} = length $content; |
198 | } |
195 | } |
199 | $hdr->{"Connection"} = "close"; |
196 | $hdr->{"Connection"} = "close"; |
200 | |
197 | |
… | |
… | |
225 | } |
222 | } |
226 | |
223 | |
227 | $self->{h} = {}; |
224 | $self->{h} = {}; |
228 | |
225 | |
229 | $fh->timeout($::RES_TIMEOUT); |
226 | $fh->timeout($::RES_TIMEOUT); |
230 | my $ip = $self->{remote_addr}; |
|
|
231 | |
|
|
232 | if ($blocked{$ip}) { |
|
|
233 | $self->err_blocked($blocked{$ip}) |
|
|
234 | if $blocked{$ip} > $::NOW; |
|
|
235 | |
|
|
236 | delete $blocked{$ip}; |
|
|
237 | } |
|
|
238 | |
|
|
239 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
240 | my $delay = 120; |
|
|
241 | while (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
242 | if ($delay <= 0) { |
|
|
243 | $self->slog(2, "blocked ip $ip"); |
|
|
244 | $self->err_blocked; |
|
|
245 | } else { |
|
|
246 | Coro::Event::do_timer(after => 3); |
|
|
247 | $delay -= 3; |
|
|
248 | } |
|
|
249 | } |
|
|
250 | } |
|
|
251 | |
227 | |
252 | $req =~ /^(?:\015\012)? |
228 | $req =~ /^(?:\015\012)? |
253 | (GET|HEAD) \040+ |
229 | (GET|HEAD) \040+ |
254 | ([^\040]+) \040+ |
230 | ([^\040]+) \040+ |
255 | HTTP\/([0-9]+\.[0-9]+) |
231 | HTTP\/([0-9]+\.[0-9]+) |
… | |
… | |
280 | |
256 | |
281 | $self->{h}{$h} = substr $v, 1 |
257 | $self->{h}{$h} = substr $v, 1 |
282 | while ($h, $v) = each %hdr; |
258 | while ($h, $v) = each %hdr; |
283 | } |
259 | } |
284 | |
260 | |
|
|
261 | # remote id should be unique per user |
|
|
262 | my $id = $self->{remote_addr}; |
|
|
263 | |
|
|
264 | if (exists $self->{h}{"client-ip"}) { |
|
|
265 | $id .= "[".$self->{h}{"client-ip"}."]"; |
|
|
266 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
267 | $id .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
268 | } |
|
|
269 | |
|
|
270 | $self->{remote_id} = $id; |
|
|
271 | |
|
|
272 | if ($blocked{$id}) { |
|
|
273 | $self->err_blocked($blocked{$id}) |
|
|
274 | if $blocked{$id} > $::NOW; |
|
|
275 | |
|
|
276 | delete $blocked{$id}; |
|
|
277 | } |
|
|
278 | |
|
|
279 | if (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
280 | my $delay = $::PER_TIMEOUT + 15; |
|
|
281 | while (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
282 | if ($delay <= 0) { |
|
|
283 | $self->slog(2, "blocked ip $id"); |
|
|
284 | $self->err_blocked; |
|
|
285 | } else { |
|
|
286 | Coro::Event::do_timer(after => 4); $delay -= 4; |
|
|
287 | } |
|
|
288 | } |
|
|
289 | } |
|
|
290 | |
285 | # find out server name and port |
291 | # find out server name and port |
286 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
292 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
287 | $host = $1; |
293 | $host = $1; |
288 | } else { |
294 | } else { |
289 | $host = $self->{h}{host}; |
295 | $host = $self->{h}{host}; |
… | |
… | |
298 | $host = inet_ntoa $host; |
304 | $host = inet_ntoa $host; |
299 | } |
305 | } |
300 | |
306 | |
301 | $self->{server_name} = $host; |
307 | $self->{server_name} = $host; |
302 | |
308 | |
303 | # remote id should be unique per user |
309 | # enter ourselves into various lists |
304 | $self->{remote_id} = $self->{remote_addr}; |
310 | weaken ($conn{$id}{$self*1} = $self); |
305 | |
|
|
306 | if (exists $self->{h}{"client-ip"}) { |
|
|
307 | $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]"; |
|
|
308 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
309 | $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
310 | } |
|
|
311 | |
|
|
312 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
311 | weaken ($uri{$id}{$self->{uri}}{$self*1} = $self); |
313 | |
312 | |
314 | eval { |
313 | eval { |
315 | $self->map_uri; |
314 | $self->map_uri; |
316 | $self->respond; |
315 | $self->respond; |
317 | }; |
316 | }; |
… | |
… | |
426 | $idx); |
425 | $idx); |
427 | } |
426 | } |
428 | |
427 | |
429 | sub handle_file { |
428 | sub handle_file { |
430 | my $self = shift; |
429 | my $self = shift; |
431 | my $length = -s _; |
430 | my $length = $self->{stat}[7]; |
|
|
431 | my $queue = $::transfers[$length >= $::TRANSFER_SMALL]; |
432 | my $hdr = { |
432 | my $hdr = { |
433 | "Last-Modified" => time2str ((stat _)[9]), |
433 | "Last-Modified" => time2str ((stat _)[9]), |
434 | }; |
434 | }; |
435 | |
435 | |
436 | my @code = (200, "ok"); |
436 | my @code = (200, "ok"); |
… | |
… | |
448 | } |
448 | } |
449 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
449 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
450 | } |
450 | } |
451 | $hdr->{"Content-Range"} = "bytes */$length"; |
451 | $hdr->{"Content-Range"} = "bytes */$length"; |
452 | $hdr->{"Content-Length"} = $length; |
452 | $hdr->{"Content-Length"} = $length; |
453 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
|
|
454 | $self->err(416, "not satisfiable", $hdr, ""); |
453 | $self->err(416, "not satisfiable", $hdr, ""); |
455 | |
454 | |
456 | satisfiable: |
455 | satisfiable: |
457 | # check for segmented downloads |
456 | # check for segmented downloads |
458 | if ($l && $::NO_SEGMENTED) { |
457 | if ($l && $::NO_SEGMENTED) { |
459 | my $delay = 180; |
458 | my $delay = $::PER_TIMEOUT + 15; |
460 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
459 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
461 | if ($delay <= 0) { |
460 | if ($delay <= 0) { |
462 | $self->err_segmented_download; |
461 | $self->err_segmented_download; |
463 | } else { |
462 | } else { |
464 | Coro::Event::do_timer(after => 3); $delay -= 3; |
463 | Coro::Event::do_timer(after => 4); $delay -= 4; |
465 | } |
464 | } |
466 | } |
465 | } |
467 | } |
466 | } |
468 | |
467 | |
469 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
468 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
482 | $self->response(@code, $hdr, ""); |
481 | $self->response(@code, $hdr, ""); |
483 | |
482 | |
484 | if ($self->{method} eq "GET") { |
483 | if ($self->{method} eq "GET") { |
485 | $self->{time} = $::NOW; |
484 | $self->{time} = $::NOW; |
486 | |
485 | |
|
|
486 | my $fudge = $queue->[0]->waiters; |
|
|
487 | $fudge = $fudge ? ($fudge+1)/$fudge : 1; |
|
|
488 | |
|
|
489 | $queue->[1] *= $fudge; |
487 | my $transfer = $::transfers->guard; |
490 | my $transfer = $queue->[0]->guard; |
|
|
491 | |
|
|
492 | if ($fudge != 1) { |
|
|
493 | $queue->[1] /= $fudge; |
|
|
494 | $queue->[1] = $queue->[1] * $::wait_factor |
|
|
495 | + ($::NOW - $self->{time}) * (1 - $::wait_factor); |
|
|
496 | } |
|
|
497 | $self->{time} = $::NOW; |
|
|
498 | |
488 | $self->{fh}->writable or return; |
499 | $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 | |
500 | |
494 | my ($fh, $buf, $r); |
501 | my ($fh, $buf, $r); |
495 | my $current = $Coro::current; |
502 | my $current = $Coro::current; |
496 | open $fh, "<", $self->{path} |
503 | open $fh, "<", $self->{path} |
497 | or die "$self->{path}: late open failure ($!)"; |
504 | or die "$self->{path}: late open failure ($!)"; |
… | |
… | |
507 | while ($h > 0) { |
514 | while ($h > 0) { |
508 | if (0) { |
515 | if (0) { |
509 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
516 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
510 | or last; |
517 | or last; |
511 | } else { |
518 | } else { |
512 | undef $buf; |
|
|
513 | $aio_requests->down; |
|
|
514 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
519 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
515 | $buf, 0, sub { |
520 | $buf, 0, sub { |
516 | $r = $_[0]; |
521 | $r = $_[0]; |
517 | $current->ready; |
522 | Coro::ready($current); |
518 | }); |
523 | }); |
519 | &Coro::schedule; |
524 | &Coro::schedule; |
520 | $aio_requests->up; |
|
|
521 | last unless $r; |
525 | last unless $r; |
522 | } |
526 | } |
523 | my $w = $self->{fh}->syswrite($buf) |
527 | my $w = syswrite $self->{fh}, $buf |
524 | or last; |
528 | or last; |
525 | $::written += $w; |
529 | $::written += $w; |
526 | $self->{written} += $w; |
530 | $self->{written} += $w; |
527 | $l += $r; |
531 | $l += $r; |
528 | } |
532 | } |