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" |
36 | sub handler { |
45 | sub handler { |
37 | while () { |
46 | while () { |
38 | my $new = pop @newcons; |
|
|
39 | if ($new) { |
47 | if (@newcons) { |
40 | eval { |
48 | eval { |
41 | conn->new(@$new)->handle; |
49 | conn->new(@{pop @newcons})->handle; |
42 | }; |
50 | }; |
43 | slog 1, "$@" if $@ && !ref $@; |
51 | slog 1, "$@" if $@ && !ref $@; |
44 | $connections->up; |
52 | $connections->up; |
45 | } else { |
53 | } else { |
46 | last if @pool >= $MAX_POOL; |
54 | last if @pool >= $MAX_POOL; |
… | |
… | |
57 | Listen => 50, |
65 | Listen => 50, |
58 | or die "unable to start server"; |
66 | or die "unable to start server"; |
59 | |
67 | |
60 | push @listen_sockets, $http_port; |
68 | push @listen_sockets, $http_port; |
61 | |
69 | |
|
|
70 | our $NOW; |
|
|
71 | our $HTTP_NOW; |
|
|
72 | |
|
|
73 | Event->timer(interval => 1, hard => 1, cb => sub { |
|
|
74 | $NOW = time; |
|
|
75 | $HTTP_NOW = time2str $NOW; |
|
|
76 | })->now; |
|
|
77 | |
62 | # the "main thread" |
78 | # the "main thread" |
63 | async { |
79 | async { |
64 | slog 1, "accepting connections"; |
80 | slog 1, "accepting connections"; |
65 | while () { |
81 | while () { |
66 | $connections->down; |
82 | $connections->down; |
67 | push @newcons, [$http_port->accept]; |
83 | push @newcons, [$http_port->accept]; |
68 | #slog 3, "accepted @$connections ".scalar(@pool); |
84 | #slog 3, "accepted @$connections ".scalar(@pool); |
69 | $::NOW = time; |
|
|
70 | if (@pool) { |
85 | if (@pool) { |
71 | (pop @pool)->ready; |
86 | (pop @pool)->ready; |
72 | } else { |
87 | } else { |
73 | async \&handler; |
88 | async \&handler; |
74 | } |
89 | } |
… | |
… | |
82 | use HTTP::Date; |
97 | use HTTP::Date; |
83 | use Convert::Scalar 'weaken'; |
98 | use Convert::Scalar 'weaken'; |
84 | use Linux::AIO; |
99 | use Linux::AIO; |
85 | |
100 | |
86 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
101 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
87 | |
|
|
88 | my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4; |
|
|
89 | |
102 | |
90 | Event->io(fd => Linux::AIO::poll_fileno, |
103 | Event->io(fd => Linux::AIO::poll_fileno, |
91 | poll => 'r', async => 1, |
104 | poll => 'r', async => 1, |
92 | cb => \&Linux::AIO::poll_cb); |
105 | cb => \&Linux::AIO::poll_cb); |
93 | |
106 | |
… | |
… | |
120 | or $self->err(500, "unable to decode peername"); |
133 | or $self->err(500, "unable to decode peername"); |
121 | |
134 | |
122 | $self->{remote_addr} = inet_ntoa $iaddr; |
135 | $self->{remote_addr} = inet_ntoa $iaddr; |
123 | $self->{time} = $::NOW; |
136 | $self->{time} = $::NOW; |
124 | |
137 | |
125 | # enter ourselves into various lists |
|
|
126 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
|
|
127 | |
|
|
128 | $::conns++; |
138 | $::conns++; |
129 | |
139 | |
130 | $self; |
140 | $self; |
131 | } |
141 | } |
132 | |
142 | |
… | |
… | |
134 | my $self = shift; |
144 | my $self = shift; |
135 | |
145 | |
136 | $::conns--; |
146 | $::conns--; |
137 | |
147 | |
138 | $self->eoconn; |
148 | $self->eoconn; |
139 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
140 | } |
149 | } |
141 | |
150 | |
142 | # end of connection |
151 | # end of connection |
143 | sub eoconn { |
152 | sub eoconn { |
144 | my $self = shift; |
153 | my $self = shift; |
|
|
154 | |
|
|
155 | # clean up hints |
|
|
156 | delete $conn{$self->{remote_id}}{$self*1}; |
145 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
157 | delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; |
146 | } |
158 | } |
147 | |
159 | |
148 | sub slog { |
160 | sub slog { |
149 | my $self = shift; |
161 | my $self = shift; |
150 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
162 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
… | |
… | |
154 | my ($self, $code, $msg, $hdr, $content) = @_; |
166 | my ($self, $code, $msg, $hdr, $content) = @_; |
155 | my $res = "HTTP/1.1 $code $msg\015\012"; |
167 | my $res = "HTTP/1.1 $code $msg\015\012"; |
156 | |
168 | |
157 | $self->{h}{connection} ||= $hdr->{Connection}; |
169 | $self->{h}{connection} ||= $hdr->{Connection}; |
158 | |
170 | |
159 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
171 | $res .= "Date: $HTTP_NOW\015\012"; |
160 | |
172 | |
161 | while (my ($h, $v) = each %$hdr) { |
173 | while (my ($h, $v) = each %$hdr) { |
162 | $res .= "$h: $v\015\012" |
174 | $res .= "$h: $v\015\012" |
163 | } |
175 | } |
164 | $res .= "\015\012"; |
176 | $res .= "\015\012"; |
… | |
… | |
177 | sub err { |
189 | sub err { |
178 | my $self = shift; |
190 | my $self = shift; |
179 | my ($code, $msg, $hdr, $content) = @_; |
191 | my ($code, $msg, $hdr, $content) = @_; |
180 | |
192 | |
181 | unless (defined $content) { |
193 | unless (defined $content) { |
182 | $content = "$code $msg"; |
194 | $content = "$code $msg\n"; |
183 | $hdr->{"Content-Type"} = "text/plain"; |
195 | $hdr->{"Content-Type"} = "text/plain"; |
184 | $hdr->{"Content-Length"} = length $content; |
196 | $hdr->{"Content-Length"} = length $content; |
185 | } |
197 | } |
186 | $hdr->{"Connection"} = "close"; |
198 | $hdr->{"Connection"} = "close"; |
187 | |
199 | |
188 | $self->response($code, $msg, $hdr, $content); |
200 | $self->response($code, $msg, $hdr, $content); |
189 | |
201 | |
190 | die bless {}, err::; |
202 | die bless {}, err::; |
191 | } |
|
|
192 | |
|
|
193 | sub err_blocked { |
|
|
194 | my $self = shift; |
|
|
195 | my $ip = $self->{remote_addr}; |
|
|
196 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
|
|
197 | |
|
|
198 | Coro::Event::do_timer(after => 20*rand); |
|
|
199 | |
|
|
200 | $self->err(401, "too many connections", |
|
|
201 | { |
|
|
202 | "Content-Type" => "text/html", |
|
|
203 | "Retry-After" => $::BLOCKTIME, |
|
|
204 | "Warning" => "Please do NOT retry, you have been blocked", |
|
|
205 | "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", |
|
|
206 | "Connection" => "close", |
|
|
207 | }, |
|
|
208 | <<EOF); |
|
|
209 | <html> |
|
|
210 | <head> |
|
|
211 | <title>Too many connections</title> |
|
|
212 | </head> |
|
|
213 | <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000"> |
|
|
214 | |
|
|
215 | <p>You have been blocked because you opened too many connections. You |
|
|
216 | may retry at</p> |
|
|
217 | |
|
|
218 | <p><blockquote>$time.</blockquote></p> |
|
|
219 | |
|
|
220 | <p>Until then, each new access will renew the block. You might want to have a |
|
|
221 | look at the <a href="http://www.goof.com/pcg/marc/animefaq.html#connectionlimit">FAQ</a>.</p> |
|
|
222 | |
|
|
223 | </body></html> |
|
|
224 | EOF |
|
|
225 | } |
203 | } |
226 | |
204 | |
227 | sub handle { |
205 | sub handle { |
228 | my $self = shift; |
206 | my $self = shift; |
229 | my $fh = $self->{fh}; |
207 | my $fh = $self->{fh}; |
… | |
… | |
246 | } |
224 | } |
247 | |
225 | |
248 | $self->{h} = {}; |
226 | $self->{h} = {}; |
249 | |
227 | |
250 | $fh->timeout($::RES_TIMEOUT); |
228 | $fh->timeout($::RES_TIMEOUT); |
251 | my $ip = $self->{remote_addr}; |
|
|
252 | |
|
|
253 | if ($blocked{$ip}) { |
|
|
254 | $self->err_blocked($blocked{$ip}) |
|
|
255 | if $blocked{$ip} > $::NOW; |
|
|
256 | |
|
|
257 | delete $blocked{$ip}; |
|
|
258 | } |
|
|
259 | |
|
|
260 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
261 | $self->slog(2, "blocked ip $ip"); |
|
|
262 | $self->err_blocked; |
|
|
263 | } |
|
|
264 | |
229 | |
265 | $req =~ /^(?:\015\012)? |
230 | $req =~ /^(?:\015\012)? |
266 | (GET|HEAD) \040+ |
231 | (GET|HEAD) \040+ |
267 | ([^\040]+) \040+ |
232 | ([^\040]+) \040+ |
268 | HTTP\/([0-9]+\.[0-9]+) |
233 | HTTP\/([0-9]+\.[0-9]+) |
… | |
… | |
293 | |
258 | |
294 | $self->{h}{$h} = substr $v, 1 |
259 | $self->{h}{$h} = substr $v, 1 |
295 | while ($h, $v) = each %hdr; |
260 | while ($h, $v) = each %hdr; |
296 | } |
261 | } |
297 | |
262 | |
|
|
263 | # remote id should be unique per user |
|
|
264 | my $id = $self->{remote_addr}; |
|
|
265 | |
|
|
266 | if (exists $self->{h}{"client-ip"}) { |
|
|
267 | $id .= "[".$self->{h}{"client-ip"}."]"; |
|
|
268 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
269 | $id .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
270 | } |
|
|
271 | |
|
|
272 | $self->{remote_id} = $id; |
|
|
273 | |
|
|
274 | if ($blocked{$id}) { |
|
|
275 | $self->err_blocked($blocked{$id}) |
|
|
276 | if $blocked{$id} > $::NOW; |
|
|
277 | |
|
|
278 | delete $blocked{$id}; |
|
|
279 | } |
|
|
280 | |
|
|
281 | if (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
282 | my $delay = $::PER_TIMEOUT + 15; |
|
|
283 | while (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
284 | if ($delay <= 0) { |
|
|
285 | $self->slog(2, "blocked ip $id"); |
|
|
286 | $self->err_blocked; |
|
|
287 | } else { |
|
|
288 | Coro::Event::do_timer(after => 4); $delay -= 4; |
|
|
289 | } |
|
|
290 | } |
|
|
291 | } |
|
|
292 | |
298 | # find out server name and port |
293 | # find out server name and port |
299 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
294 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
300 | $host = $1; |
295 | $host = $1; |
301 | } else { |
296 | } else { |
302 | $host = $self->{h}{host}; |
297 | $host = $self->{h}{host}; |
… | |
… | |
311 | $host = inet_ntoa $host; |
306 | $host = inet_ntoa $host; |
312 | } |
307 | } |
313 | |
308 | |
314 | $self->{server_name} = $host; |
309 | $self->{server_name} = $host; |
315 | |
310 | |
316 | # remote id should be unique per user |
311 | # enter ourselves into various lists |
317 | $self->{remote_id} = $self->{remote_addr}; |
312 | weaken ($conn{$id}{$self*1} = $self); |
318 | |
|
|
319 | if (exists $self->{h}{"client-ip"}) { |
|
|
320 | $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]"; |
|
|
321 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
322 | $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
323 | } |
|
|
324 | |
|
|
325 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
313 | weaken ($uri{$id}{$self->{uri}}{$self*1} = $self); |
326 | |
314 | |
327 | eval { |
315 | eval { |
328 | $self->map_uri; |
316 | $self->map_uri; |
329 | $self->respond; |
317 | $self->respond; |
330 | }; |
318 | }; |
… | |
… | |
439 | $idx); |
427 | $idx); |
440 | } |
428 | } |
441 | |
429 | |
442 | sub handle_file { |
430 | sub handle_file { |
443 | my $self = shift; |
431 | my $self = shift; |
444 | my $length = -s _; |
432 | my $length = $self->{stat}[7]; |
|
|
433 | my $queue = $::transfers[$length >= $::TRANSFER_SMALL]; |
445 | my $hdr = { |
434 | my $hdr = { |
446 | "Last-Modified" => time2str ((stat _)[9]), |
435 | "Last-Modified" => time2str ((stat _)[9]), |
447 | }; |
436 | }; |
448 | |
437 | |
449 | my @code = (200, "ok"); |
438 | my @code = (200, "ok"); |
… | |
… | |
461 | } |
450 | } |
462 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
451 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
463 | } |
452 | } |
464 | $hdr->{"Content-Range"} = "bytes */$length"; |
453 | $hdr->{"Content-Range"} = "bytes */$length"; |
465 | $hdr->{"Content-Length"} = $length; |
454 | $hdr->{"Content-Length"} = $length; |
466 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
|
|
467 | $self->err(416, "not satisfiable", $hdr, ""); |
455 | $self->err(416, "not satisfiable", $hdr, ""); |
468 | |
456 | |
469 | satisfiable: |
457 | satisfiable: |
470 | # check for segmented downloads |
458 | # check for segmented downloads |
471 | if ($l && $::NO_SEGMENTED) { |
459 | if ($l && $::NO_SEGMENTED) { |
472 | my $delay = 60; |
460 | my $delay = $::PER_TIMEOUT + 15; |
473 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
461 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
474 | if ($delay <= 0) { |
462 | if ($delay <= 0) { |
475 | $self->err(400, "segmented downloads are not allowed", |
463 | $self->err_segmented_download; |
476 | { "Content-Type" => "text/html", Connection => "close" }, <<EOF); |
|
|
477 | <html> |
|
|
478 | <head> |
|
|
479 | <title>Segmented downloads are not allowed</title> |
|
|
480 | </head> |
|
|
481 | <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000"> |
|
|
482 | |
|
|
483 | <p>Segmented downloads are not allowed on this server. Please refer to the |
|
|
484 | <a href="http://www.goof.com/pcg/marc/animefaq.html#segmented_downloads">FAQ</a>.</p> |
|
|
485 | |
|
|
486 | </body></html> |
|
|
487 | EOF |
|
|
488 | } else { |
464 | } else { |
489 | Coro::Event::do_timer(after => 3); $delay -= 3; |
465 | Coro::Event::do_timer(after => 4); $delay -= 4; |
490 | } |
466 | } |
491 | } |
467 | } |
492 | } |
468 | } |
493 | |
469 | |
494 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
470 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
505 | $hdr->{"Content-Length"} = $length; |
481 | $hdr->{"Content-Length"} = $length; |
506 | |
482 | |
507 | $self->response(@code, $hdr, ""); |
483 | $self->response(@code, $hdr, ""); |
508 | |
484 | |
509 | if ($self->{method} eq "GET") { |
485 | if ($self->{method} eq "GET") { |
|
|
486 | $self->{time} = $::NOW; |
|
|
487 | |
|
|
488 | my $fudge = $queue->[0]->waiters; |
|
|
489 | $fudge = $fudge ? ($fudge+1)/$fudge : 1; |
|
|
490 | |
|
|
491 | $queue->[1] *= $fudge; |
|
|
492 | my $transfer = $queue->[0]->guard; |
|
|
493 | |
|
|
494 | if ($fudge != 1) { |
|
|
495 | $queue->[1] /= $fudge; |
|
|
496 | $queue->[1] = $queue->[1] * $::wait_factor |
|
|
497 | + ($::NOW - $self->{time}) * (1 - $::wait_factor); |
|
|
498 | } |
|
|
499 | $self->{time} = $::NOW; |
|
|
500 | |
|
|
501 | $self->{fh}->writable or return; |
|
|
502 | |
510 | my ($fh, $buf, $r); |
503 | my ($fh, $buf, $r); |
511 | my $current = $Coro::current; |
504 | my $current = $Coro::current; |
512 | open $fh, "<", $self->{path} |
505 | open $fh, "<", $self->{path} |
513 | or die "$self->{path}: late open failure ($!)"; |
506 | or die "$self->{path}: late open failure ($!)"; |
514 | |
507 | |
… | |
… | |
523 | while ($h > 0) { |
516 | while ($h > 0) { |
524 | if (0) { |
517 | if (0) { |
525 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
518 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
526 | or last; |
519 | or last; |
527 | } else { |
520 | } else { |
528 | undef $buf; |
|
|
529 | $aio_requests->down; |
|
|
530 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
521 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
531 | $buf, 0, sub { |
522 | $buf, 0, sub { |
532 | $r = $_[0]; |
523 | $r = $_[0]; |
533 | $current->ready; |
524 | Coro::ready($current); |
534 | }); |
525 | }); |
535 | &Coro::schedule; |
526 | &Coro::schedule; |
536 | $aio_requests->up; |
|
|
537 | last unless $r; |
527 | last unless $r; |
538 | } |
528 | } |
539 | my $w = $self->{fh}->syswrite($buf) |
529 | my $w = syswrite $self->{fh}, $buf |
540 | or last; |
530 | or last; |
541 | $::written += $w; |
531 | $::written += $w; |
542 | $self->{written} += $w; |
532 | $self->{written} += $w; |
543 | $l += $r; |
533 | $l += $r; |
544 | } |
534 | } |
545 | } |
|
|
546 | |
535 | |
547 | close $fh; |
536 | close $fh; |
|
|
537 | } |
548 | } |
538 | } |
549 | |
539 | |
550 | 1; |
540 | 1; |