… | |
… | |
50 | } |
50 | } |
51 | |
51 | |
52 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
52 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
53 | our $httpevent = new Coro::Signal; |
53 | our $httpevent = new Coro::Signal; |
54 | |
54 | |
55 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
55 | our $queue_file = new transferqueue slots => $MAX_TRANSFERS, maxsize => 250_000_000; |
56 | our $queue_index = new transferqueue 10; |
56 | our $queue_index = new transferqueue slots => 10; |
|
|
57 | |
|
|
58 | our $requests; |
57 | |
59 | |
58 | my @newcons; |
60 | my @newcons; |
59 | my @pool; |
61 | my @pool; |
60 | |
62 | |
61 | # one "execution thread" |
63 | # one "execution thread" |
… | |
… | |
161 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
163 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
162 | or $self->err(500, "unable to decode peername"); |
164 | or $self->err(500, "unable to decode peername"); |
163 | |
165 | |
164 | $self->{remote_addr} = |
166 | $self->{remote_addr} = |
165 | $self->{remote_id} = inet_ntoa $iaddr; |
167 | $self->{remote_id} = inet_ntoa $iaddr; |
|
|
168 | |
166 | $self->{time} = $::NOW; |
169 | $self->{time} = $::NOW; |
167 | |
170 | |
168 | weaken ($Coro::current->{conn} = $self); |
171 | weaken ($Coro::current->{conn} = $self); |
169 | |
172 | |
170 | $::conns++; |
173 | $::conns++; |
… | |
… | |
292 | or $self->err(400, "bad request"); |
295 | or $self->err(400, "bad request"); |
293 | |
296 | |
294 | $self->{h}{$h} = substr $v, 1 |
297 | $self->{h}{$h} = substr $v, 1 |
295 | while ($h, $v) = each %hdr; |
298 | while ($h, $v) = each %hdr; |
296 | } |
299 | } |
|
|
300 | |
|
|
301 | $requests++; |
297 | |
302 | |
298 | # remote id should be unique per user |
303 | # remote id should be unique per user |
299 | my $id = $self->{remote_addr}; |
304 | my $id = $self->{remote_addr}; |
300 | |
305 | |
301 | if (exists $self->{h}{"client-ip"}) { |
306 | if (exists $self->{h}{"client-ip"}) { |
… | |
… | |
448 | } |
453 | } |
449 | } |
454 | } |
450 | } elsif (-f _ && -r _) { |
455 | } elsif (-f _ && -r _) { |
451 | -x _ and $self->err(403, "forbidden"); |
456 | -x _ and $self->err(403, "forbidden"); |
452 | |
457 | |
453 | if (%{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
458 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
454 | my $timeout = $::NOW + 10; |
459 | my $timeout = $::NOW + 10; |
455 | while (%{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
460 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
456 | if ($timeout < $::NOW) { |
461 | if ($timeout < $::NOW) { |
457 | $self->block($::BLOCKTIME, "too many connections"); |
462 | $self->block($::BLOCKTIME, "too many connections"); |
458 | } else { |
463 | } else { |
459 | $httpevent->wait; |
464 | $httpevent->wait; |
460 | } |
465 | } |
… | |
… | |
509 | |
514 | |
510 | satisfiable: |
515 | satisfiable: |
511 | # check for segmented downloads |
516 | # check for segmented downloads |
512 | if ($l && $::NO_SEGMENTED) { |
517 | if ($l && $::NO_SEGMENTED) { |
513 | my $timeout = $::NOW + 15; |
518 | my $timeout = $::NOW + 15; |
514 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
519 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
515 | if ($timeout <= $::NOW) { |
520 | if ($timeout <= $::NOW) { |
516 | $self->block($::BLOCKTIME, "segmented downloads are forbidden"); |
521 | $self->block($::BLOCKTIME, "segmented downloads are forbidden"); |
517 | #$self->err_segmented_download; |
522 | #$self->err_segmented_download; |
518 | } else { |
523 | } else { |
519 | $httpevent->wait; |
524 | $httpevent->wait; |
… | |
… | |
566 | } |
571 | } |
567 | } |
572 | } |
568 | |
573 | |
569 | if ($blocked{$self->{remote_id}}) { |
574 | if ($blocked{$self->{remote_id}}) { |
570 | $self->{h}{connection} = "close"; |
575 | $self->{h}{connection} = "close"; |
571 | die bless {}, err:: |
576 | die bless {}, err::; |
572 | } |
577 | } |
573 | |
578 | |
574 | if (0) { # !AIO |
579 | if (0) { # !AIO |
575 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
580 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
576 | or last; |
581 | or last; |