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 | |
5 | |
|
|
6 | use HTTP::Date; |
|
|
7 | |
6 | no utf8; |
8 | no utf8; |
7 | use bytes; |
9 | use bytes; |
|
|
10 | |
|
|
11 | our @wait_time = (0); # used to calculcate avg. waiting time |
8 | |
12 | |
9 | # at least on my machine, this thingy serves files |
13 | # at least on my machine, this thingy serves files |
10 | # quite a bit faster than apache, ;) |
14 | # quite a bit faster than apache, ;) |
11 | # and quite a bit slower than thttpd :( |
15 | # and quite a bit slower than thttpd :( |
12 | |
16 | |
… | |
… | |
25 | my $level = shift; |
29 | my $level = shift; |
26 | my $format = shift; |
30 | my $format = shift; |
27 | printf "---: $format\n", @_; |
31 | printf "---: $format\n", @_; |
28 | } |
32 | } |
29 | |
33 | |
30 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
34 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
|
|
35 | our $transfers = new Coro::Semaphore $MAX_TRANSFER || 50; |
31 | |
36 | |
32 | my @newcons; |
37 | my @newcons; |
33 | my @pool; |
38 | my @pool; |
34 | |
39 | |
35 | # one "execution thread" |
40 | # one "execution thread" |
… | |
… | |
57 | Listen => 50, |
62 | Listen => 50, |
58 | or die "unable to start server"; |
63 | or die "unable to start server"; |
59 | |
64 | |
60 | push @listen_sockets, $http_port; |
65 | push @listen_sockets, $http_port; |
61 | |
66 | |
|
|
67 | our $NOW; |
|
|
68 | our $HTTP_NOW; |
|
|
69 | |
|
|
70 | Event->timer(interval => 1, hard => 1, cb => sub { |
|
|
71 | $NOW = time; |
|
|
72 | $HTTP_NOW = time2str $NOW; |
|
|
73 | }); |
|
|
74 | |
62 | # the "main thread" |
75 | # the "main thread" |
63 | async { |
76 | async { |
64 | slog 1, "accepting connections"; |
77 | slog 1, "accepting connections"; |
65 | while () { |
78 | while () { |
66 | $connections->down; |
79 | $connections->down; |
67 | push @newcons, [$http_port->accept]; |
80 | push @newcons, [$http_port->accept]; |
68 | #slog 3, "accepted @$connections ".scalar(@pool); |
81 | #slog 3, "accepted @$connections ".scalar(@pool); |
69 | $::NOW = time; |
|
|
70 | if (@pool) { |
82 | if (@pool) { |
71 | (pop @pool)->ready; |
83 | (pop @pool)->ready; |
72 | } else { |
84 | } else { |
73 | async \&handler; |
85 | async \&handler; |
74 | } |
86 | } |
… | |
… | |
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"; |
… | |
… | |
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}; |
… | |
… | |
256 | |
234 | |
257 | delete $blocked{$ip}; |
235 | delete $blocked{$ip}; |
258 | } |
236 | } |
259 | |
237 | |
260 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
238 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
239 | my $delay = 120; |
|
|
240 | while (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
241 | if ($delay <= 0) { |
261 | $self->slog(2, "blocked ip $ip"); |
242 | $self->slog(2, "blocked ip $ip"); |
262 | $self->err_blocked; |
243 | $self->err_blocked; |
|
|
244 | } else { |
|
|
245 | Coro::Event::do_timer(after => 3); |
|
|
246 | $delay -= 3; |
|
|
247 | } |
|
|
248 | } |
263 | } |
249 | } |
264 | |
250 | |
265 | $req =~ /^(?:\015\012)? |
251 | $req =~ /^(?:\015\012)? |
266 | (GET|HEAD) \040+ |
252 | (GET|HEAD) \040+ |
267 | ([^\040]+) \040+ |
253 | ([^\040]+) \040+ |
… | |
… | |
467 | $self->err(416, "not satisfiable", $hdr, ""); |
453 | $self->err(416, "not satisfiable", $hdr, ""); |
468 | |
454 | |
469 | satisfiable: |
455 | satisfiable: |
470 | # check for segmented downloads |
456 | # check for segmented downloads |
471 | if ($l && $::NO_SEGMENTED) { |
457 | if ($l && $::NO_SEGMENTED) { |
472 | my $delay = 60; |
458 | my $delay = 180; |
473 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
459 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
474 | if ($delay <= 0) { |
460 | if ($delay <= 0) { |
475 | $self->err(400, "segmented downloads are not allowed", |
461 | $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 { |
462 | } else { |
489 | Coro::Event::do_timer(after => 3); $delay -= 3; |
463 | Coro::Event::do_timer(after => 3); $delay -= 3; |
490 | } |
464 | } |
491 | } |
465 | } |
492 | } |
466 | } |
… | |
… | |
505 | $hdr->{"Content-Length"} = $length; |
479 | $hdr->{"Content-Length"} = $length; |
506 | |
480 | |
507 | $self->response(@code, $hdr, ""); |
481 | $self->response(@code, $hdr, ""); |
508 | |
482 | |
509 | if ($self->{method} eq "GET") { |
483 | if ($self->{method} eq "GET") { |
|
|
484 | $self->{time} = $::NOW; |
|
|
485 | |
|
|
486 | my $transfer = $::transfers->guard; |
|
|
487 | $self->{fh}->writable or return; |
|
|
488 | |
|
|
489 | push @::wait_time, $::NOW - $self->{time}; |
|
|
490 | shift @::wait_time if @wait_time > 25; |
|
|
491 | $self->{time} = $::NOW; |
|
|
492 | |
510 | my ($fh, $buf, $r); |
493 | my ($fh, $buf, $r); |
511 | my $current = $Coro::current; |
494 | my $current = $Coro::current; |
512 | open $fh, "<", $self->{path} |
495 | open $fh, "<", $self->{path} |
513 | or die "$self->{path}: late open failure ($!)"; |
496 | or die "$self->{path}: late open failure ($!)"; |
514 | |
497 | |
… | |
… | |
540 | or last; |
523 | or last; |
541 | $::written += $w; |
524 | $::written += $w; |
542 | $self->{written} += $w; |
525 | $self->{written} += $w; |
543 | $l += $r; |
526 | $l += $r; |
544 | } |
527 | } |
545 | } |
|
|
546 | |
528 | |
547 | close $fh; |
529 | close $fh; |
|
|
530 | } |
548 | } |
531 | } |
549 | |
532 | |
550 | 1; |
533 | 1; |