… | |
… | |
42 | my @pool; |
42 | my @pool; |
43 | |
43 | |
44 | # one "execution thread" |
44 | # one "execution thread" |
45 | sub handler { |
45 | sub handler { |
46 | while () { |
46 | while () { |
47 | my $new = pop @newcons; |
|
|
48 | if ($new) { |
47 | if (@newcons) { |
49 | eval { |
48 | eval { |
50 | conn->new(@$new)->handle; |
49 | conn->new(@{pop @newcons})->handle; |
51 | }; |
50 | }; |
52 | slog 1, "$@" if $@ && !ref $@; |
51 | slog 1, "$@" if $@ && !ref $@; |
53 | $connections->up; |
52 | $connections->up; |
54 | } else { |
53 | } else { |
55 | last if @pool >= $MAX_POOL; |
54 | last if @pool >= $MAX_POOL; |
… | |
… | |
99 | use Convert::Scalar 'weaken'; |
98 | use Convert::Scalar 'weaken'; |
100 | use Linux::AIO; |
99 | use Linux::AIO; |
101 | |
100 | |
102 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
101 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
103 | |
102 | |
104 | my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4; |
|
|
105 | |
|
|
106 | Event->io(fd => Linux::AIO::poll_fileno, |
103 | Event->io(fd => Linux::AIO::poll_fileno, |
107 | poll => 'r', async => 1, |
104 | poll => 'r', async => 1, |
108 | cb => \&Linux::AIO::poll_cb); |
105 | cb => \&Linux::AIO::poll_cb); |
109 | |
106 | |
110 | our %conn; # $conn{ip}{self} => connobj |
107 | our %conn; # $conn{ip}{self} => connobj |
… | |
… | |
136 | or $self->err(500, "unable to decode peername"); |
133 | or $self->err(500, "unable to decode peername"); |
137 | |
134 | |
138 | $self->{remote_addr} = inet_ntoa $iaddr; |
135 | $self->{remote_addr} = inet_ntoa $iaddr; |
139 | $self->{time} = $::NOW; |
136 | $self->{time} = $::NOW; |
140 | |
137 | |
141 | # enter ourselves into various lists |
|
|
142 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
|
|
143 | |
|
|
144 | $::conns++; |
138 | $::conns++; |
145 | |
139 | |
146 | $self; |
140 | $self; |
147 | } |
141 | } |
148 | |
142 | |
149 | sub DESTROY { |
143 | sub DESTROY { |
150 | my $self = shift; |
144 | my $self = shift; |
151 | |
|
|
152 | $::conns--; |
145 | $::conns--; |
153 | |
|
|
154 | $self->eoconn; |
146 | $self->eoconn; |
155 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
156 | } |
147 | } |
157 | |
148 | |
158 | # end of connection |
149 | # end of connection |
159 | sub eoconn { |
150 | sub eoconn { |
160 | my $self = shift; |
151 | my $self = shift; |
|
|
152 | |
|
|
153 | # clean up hints |
|
|
154 | delete $conn{$self->{remote_id}}{$self*1}; |
161 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
155 | delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; |
162 | } |
156 | } |
163 | |
157 | |
164 | sub slog { |
158 | sub slog { |
165 | my $self = shift; |
159 | my $self = shift; |
166 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
160 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
… | |
… | |
228 | } |
222 | } |
229 | |
223 | |
230 | $self->{h} = {}; |
224 | $self->{h} = {}; |
231 | |
225 | |
232 | $fh->timeout($::RES_TIMEOUT); |
226 | $fh->timeout($::RES_TIMEOUT); |
233 | my $ip = $self->{remote_addr}; |
|
|
234 | |
|
|
235 | if ($blocked{$ip}) { |
|
|
236 | $self->err_blocked($blocked{$ip}) |
|
|
237 | if $blocked{$ip} > $::NOW; |
|
|
238 | |
|
|
239 | delete $blocked{$ip}; |
|
|
240 | } |
|
|
241 | |
|
|
242 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
243 | my $delay = 120; |
|
|
244 | while (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
245 | if ($delay <= 0) { |
|
|
246 | $self->slog(2, "blocked ip $ip"); |
|
|
247 | $self->err_blocked; |
|
|
248 | } else { |
|
|
249 | Coro::Event::do_timer(after => 3); |
|
|
250 | $delay -= 3; |
|
|
251 | } |
|
|
252 | } |
|
|
253 | } |
|
|
254 | |
227 | |
255 | $req =~ /^(?:\015\012)? |
228 | $req =~ /^(?:\015\012)? |
256 | (GET|HEAD) \040+ |
229 | (GET|HEAD) \040+ |
257 | ([^\040]+) \040+ |
230 | ([^\040]+) \040+ |
258 | HTTP\/([0-9]+\.[0-9]+) |
231 | HTTP\/([0-9]+\.[0-9]+) |
… | |
… | |
283 | |
256 | |
284 | $self->{h}{$h} = substr $v, 1 |
257 | $self->{h}{$h} = substr $v, 1 |
285 | while ($h, $v) = each %hdr; |
258 | while ($h, $v) = each %hdr; |
286 | } |
259 | } |
287 | |
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 | |
288 | # find out server name and port |
291 | # find out server name and port |
289 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
292 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
290 | $host = $1; |
293 | $host = $1; |
291 | } else { |
294 | } else { |
292 | $host = $self->{h}{host}; |
295 | $host = $self->{h}{host}; |
… | |
… | |
301 | $host = inet_ntoa $host; |
304 | $host = inet_ntoa $host; |
302 | } |
305 | } |
303 | |
306 | |
304 | $self->{server_name} = $host; |
307 | $self->{server_name} = $host; |
305 | |
308 | |
306 | # remote id should be unique per user |
309 | # enter ourselves into various lists |
307 | $self->{remote_id} = $self->{remote_addr}; |
310 | weaken ($conn{$id}{$self*1} = $self); |
308 | |
|
|
309 | if (exists $self->{h}{"client-ip"}) { |
|
|
310 | $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]"; |
|
|
311 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
312 | $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
313 | } |
|
|
314 | |
|
|
315 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
311 | weaken ($uri{$id}{$self->{uri}}{$self*1} = $self); |
316 | |
312 | |
317 | eval { |
313 | eval { |
318 | $self->map_uri; |
314 | $self->map_uri; |
319 | $self->respond; |
315 | $self->respond; |
320 | }; |
316 | }; |
… | |
… | |
457 | $self->err(416, "not satisfiable", $hdr, ""); |
453 | $self->err(416, "not satisfiable", $hdr, ""); |
458 | |
454 | |
459 | satisfiable: |
455 | satisfiable: |
460 | # check for segmented downloads |
456 | # check for segmented downloads |
461 | if ($l && $::NO_SEGMENTED) { |
457 | if ($l && $::NO_SEGMENTED) { |
462 | my $delay = 180; |
458 | my $delay = $::PER_TIMEOUT + 15; |
463 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
459 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
464 | if ($delay <= 0) { |
460 | if ($delay <= 0) { |
465 | $self->err_segmented_download; |
461 | $self->err_segmented_download; |
466 | } else { |
462 | } else { |
467 | Coro::Event::do_timer(after => 3); $delay -= 3; |
463 | Coro::Event::do_timer(after => 4); $delay -= 4; |
468 | } |
464 | } |
469 | } |
465 | } |
470 | } |
466 | } |
471 | |
467 | |
472 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
468 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
518 | while ($h > 0) { |
514 | while ($h > 0) { |
519 | if (0) { |
515 | if (0) { |
520 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
516 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
521 | or last; |
517 | or last; |
522 | } else { |
518 | } else { |
523 | undef $buf; |
|
|
524 | $aio_requests->down; |
|
|
525 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
519 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
526 | $buf, 0, sub { |
520 | $buf, 0, sub { |
527 | $r = $_[0]; |
521 | $r = $_[0]; |
528 | $current->ready; |
522 | Coro::ready($current); |
529 | }); |
523 | }); |
530 | &Coro::schedule; |
524 | &Coro::schedule; |
531 | $aio_requests->up; |
|
|
532 | last unless $r; |
525 | last unless $r; |
533 | } |
526 | } |
534 | my $w = $self->{fh}->syswrite($buf) |
527 | my $w = syswrite $self->{fh}, $buf |
535 | or last; |
528 | or last; |
536 | $::written += $w; |
529 | $::written += $w; |
537 | $self->{written} += $w; |
530 | $self->{written} += $w; |
538 | $l += $r; |
531 | $l += $r; |
539 | } |
532 | } |