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