… | |
… | |
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" |
42 | sub handler { |
45 | sub handler { |
43 | while () { |
46 | while () { |
44 | my $new = pop @newcons; |
|
|
45 | if ($new) { |
47 | if (@newcons) { |
46 | eval { |
48 | eval { |
47 | conn->new(@$new)->handle; |
49 | conn->new(@{pop @newcons})->handle; |
48 | }; |
50 | }; |
49 | slog 1, "$@" if $@ && !ref $@; |
51 | slog 1, "$@" if $@ && !ref $@; |
50 | $connections->up; |
52 | $connections->up; |
51 | } else { |
53 | } else { |
52 | last if @pool >= $MAX_POOL; |
54 | last if @pool >= $MAX_POOL; |
… | |
… | |
54 | schedule; |
56 | schedule; |
55 | } |
57 | } |
56 | } |
58 | } |
57 | } |
59 | } |
58 | |
60 | |
|
|
61 | sub listen_on { |
|
|
62 | my $listen = $_[0]; |
|
|
63 | |
|
|
64 | push @listen_sockets, $listen; |
|
|
65 | |
|
|
66 | # the "main thread" |
|
|
67 | async { |
|
|
68 | slog 1, "accepting connections"; |
|
|
69 | while () { |
|
|
70 | $connections->down; |
|
|
71 | push @newcons, [$listen->accept]; |
|
|
72 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
73 | if (@pool) { |
|
|
74 | (pop @pool)->ready; |
|
|
75 | } else { |
|
|
76 | async \&handler; |
|
|
77 | } |
|
|
78 | |
|
|
79 | } |
|
|
80 | }; |
|
|
81 | } |
|
|
82 | |
59 | my $http_port = new Coro::Socket |
83 | my $http_port = new Coro::Socket |
60 | LocalAddr => $SERVER_HOST, |
84 | LocalAddr => $SERVER_HOST, |
61 | LocalPort => $SERVER_PORT, |
85 | LocalPort => $SERVER_PORT, |
62 | ReuseAddr => 1, |
86 | ReuseAddr => 1, |
63 | Listen => 50, |
87 | Listen => 50, |
64 | or die "unable to start server"; |
88 | or die "unable to start server"; |
65 | |
89 | |
66 | push @listen_sockets, $http_port; |
90 | listen_on $http_port; |
|
|
91 | |
|
|
92 | if ($SERVER_PORT2) { |
|
|
93 | my $http_port = new Coro::Socket |
|
|
94 | LocalAddr => $SERVER_HOST, |
|
|
95 | LocalPort => $SERVER_PORT2, |
|
|
96 | ReuseAddr => 1, |
|
|
97 | Listen => 50, |
|
|
98 | or die "unable to start server"; |
|
|
99 | |
|
|
100 | listen_on $http_port; |
|
|
101 | } |
67 | |
102 | |
68 | our $NOW; |
103 | our $NOW; |
69 | our $HTTP_NOW; |
104 | our $HTTP_NOW; |
70 | |
105 | |
71 | Event->timer(interval => 1, hard => 1, cb => sub { |
106 | Event->timer(interval => 1, hard => 1, cb => sub { |
72 | $NOW = time; |
107 | $NOW = time; |
73 | $HTTP_NOW = time2str $NOW; |
108 | $HTTP_NOW = time2str $NOW; |
74 | }); |
109 | })->now; |
75 | |
|
|
76 | # the "main thread" |
|
|
77 | async { |
|
|
78 | slog 1, "accepting connections"; |
|
|
79 | while () { |
|
|
80 | $connections->down; |
|
|
81 | push @newcons, [$http_port->accept]; |
|
|
82 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
83 | if (@pool) { |
|
|
84 | (pop @pool)->ready; |
|
|
85 | } else { |
|
|
86 | async \&handler; |
|
|
87 | } |
|
|
88 | |
|
|
89 | } |
|
|
90 | }; |
|
|
91 | |
110 | |
92 | package conn; |
111 | package conn; |
93 | |
112 | |
94 | use Socket; |
113 | use Socket; |
95 | use HTTP::Date; |
114 | use HTTP::Date; |
96 | use Convert::Scalar 'weaken'; |
115 | use Convert::Scalar 'weaken'; |
97 | use Linux::AIO; |
116 | use Linux::AIO; |
98 | |
117 | |
99 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
118 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
100 | |
|
|
101 | my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4; |
|
|
102 | |
119 | |
103 | Event->io(fd => Linux::AIO::poll_fileno, |
120 | Event->io(fd => Linux::AIO::poll_fileno, |
104 | poll => 'r', async => 1, |
121 | poll => 'r', async => 1, |
105 | cb => \&Linux::AIO::poll_cb); |
122 | cb => \&Linux::AIO::poll_cb); |
106 | |
123 | |
… | |
… | |
133 | or $self->err(500, "unable to decode peername"); |
150 | or $self->err(500, "unable to decode peername"); |
134 | |
151 | |
135 | $self->{remote_addr} = inet_ntoa $iaddr; |
152 | $self->{remote_addr} = inet_ntoa $iaddr; |
136 | $self->{time} = $::NOW; |
153 | $self->{time} = $::NOW; |
137 | |
154 | |
138 | # enter ourselves into various lists |
|
|
139 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
|
|
140 | |
|
|
141 | $::conns++; |
155 | $::conns++; |
142 | |
156 | |
143 | $self; |
157 | $self; |
144 | } |
158 | } |
145 | |
159 | |
146 | sub DESTROY { |
160 | sub DESTROY { |
147 | my $self = shift; |
161 | my $self = shift; |
148 | |
|
|
149 | $::conns--; |
162 | $::conns--; |
150 | |
|
|
151 | $self->eoconn; |
163 | $self->eoconn; |
152 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
153 | } |
164 | } |
154 | |
165 | |
155 | # end of connection |
166 | # end of connection |
156 | sub eoconn { |
167 | sub eoconn { |
157 | my $self = shift; |
168 | my $self = shift; |
|
|
169 | |
|
|
170 | # clean up hints |
|
|
171 | delete $conn{$self->{remote_id}}{$self*1}; |
158 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
172 | delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; |
159 | } |
173 | } |
160 | |
174 | |
161 | sub slog { |
175 | sub slog { |
162 | my $self = shift; |
176 | my $self = shift; |
163 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
177 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
… | |
… | |
190 | sub err { |
204 | sub err { |
191 | my $self = shift; |
205 | my $self = shift; |
192 | my ($code, $msg, $hdr, $content) = @_; |
206 | my ($code, $msg, $hdr, $content) = @_; |
193 | |
207 | |
194 | unless (defined $content) { |
208 | unless (defined $content) { |
195 | $content = "$code $msg"; |
209 | $content = "$code $msg\n"; |
196 | $hdr->{"Content-Type"} = "text/plain"; |
210 | $hdr->{"Content-Type"} = "text/plain"; |
197 | $hdr->{"Content-Length"} = length $content; |
211 | $hdr->{"Content-Length"} = length $content; |
198 | } |
212 | } |
199 | $hdr->{"Connection"} = "close"; |
213 | $hdr->{"Connection"} = "close"; |
200 | |
214 | |
… | |
… | |
225 | } |
239 | } |
226 | |
240 | |
227 | $self->{h} = {}; |
241 | $self->{h} = {}; |
228 | |
242 | |
229 | $fh->timeout($::RES_TIMEOUT); |
243 | $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 | |
244 | |
252 | $req =~ /^(?:\015\012)? |
245 | $req =~ /^(?:\015\012)? |
253 | (GET|HEAD) \040+ |
246 | (GET|HEAD) \040+ |
254 | ([^\040]+) \040+ |
247 | ([^\040]+) \040+ |
255 | HTTP\/([0-9]+\.[0-9]+) |
248 | HTTP\/([0-9]+\.[0-9]+) |
… | |
… | |
280 | |
273 | |
281 | $self->{h}{$h} = substr $v, 1 |
274 | $self->{h}{$h} = substr $v, 1 |
282 | while ($h, $v) = each %hdr; |
275 | while ($h, $v) = each %hdr; |
283 | } |
276 | } |
284 | |
277 | |
|
|
278 | # remote id should be unique per user |
|
|
279 | my $id = $self->{remote_addr}; |
|
|
280 | |
|
|
281 | if (exists $self->{h}{"client-ip"}) { |
|
|
282 | $id .= "[".$self->{h}{"client-ip"}."]"; |
|
|
283 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
284 | $id .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
285 | } |
|
|
286 | |
|
|
287 | $self->{remote_id} = $id; |
|
|
288 | |
|
|
289 | if ($blocked{$id}) { |
|
|
290 | $self->err_blocked($blocked{$id}) |
|
|
291 | if $blocked{$id} > $::NOW; |
|
|
292 | |
|
|
293 | delete $blocked{$id}; |
|
|
294 | } |
|
|
295 | |
|
|
296 | if (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
297 | my $delay = $::PER_TIMEOUT + 15; |
|
|
298 | while (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
299 | if ($delay <= 0) { |
|
|
300 | $self->slog(2, "blocked ip $id"); |
|
|
301 | $self->err_blocked; |
|
|
302 | } else { |
|
|
303 | Coro::Event::do_timer(after => 4); $delay -= 4; |
|
|
304 | } |
|
|
305 | } |
|
|
306 | } |
|
|
307 | |
285 | # find out server name and port |
308 | # find out server name and port |
286 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
309 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
287 | $host = $1; |
310 | $host = $1; |
288 | } else { |
311 | } else { |
289 | $host = $self->{h}{host}; |
312 | $host = $self->{h}{host}; |
… | |
… | |
298 | $host = inet_ntoa $host; |
321 | $host = inet_ntoa $host; |
299 | } |
322 | } |
300 | |
323 | |
301 | $self->{server_name} = $host; |
324 | $self->{server_name} = $host; |
302 | |
325 | |
303 | # remote id should be unique per user |
326 | # enter ourselves into various lists |
304 | $self->{remote_id} = $self->{remote_addr}; |
327 | 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); |
328 | weaken ($uri{$id}{$self->{uri}}{$self*1} = $self); |
313 | |
329 | |
314 | eval { |
330 | eval { |
315 | $self->map_uri; |
331 | $self->map_uri; |
316 | $self->respond; |
332 | $self->respond; |
317 | }; |
333 | }; |
… | |
… | |
426 | $idx); |
442 | $idx); |
427 | } |
443 | } |
428 | |
444 | |
429 | sub handle_file { |
445 | sub handle_file { |
430 | my $self = shift; |
446 | my $self = shift; |
431 | my $length = -s _; |
447 | my $length = $self->{stat}[7]; |
|
|
448 | my $queue = $::transfers[$length >= $::TRANSFER_SMALL]; |
432 | my $hdr = { |
449 | my $hdr = { |
433 | "Last-Modified" => time2str ((stat _)[9]), |
450 | "Last-Modified" => time2str ((stat _)[9]), |
434 | }; |
451 | }; |
435 | |
452 | |
436 | my @code = (200, "ok"); |
453 | my @code = (200, "ok"); |
… | |
… | |
448 | } |
465 | } |
449 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
466 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
450 | } |
467 | } |
451 | $hdr->{"Content-Range"} = "bytes */$length"; |
468 | $hdr->{"Content-Range"} = "bytes */$length"; |
452 | $hdr->{"Content-Length"} = $length; |
469 | $hdr->{"Content-Length"} = $length; |
453 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
|
|
454 | $self->err(416, "not satisfiable", $hdr, ""); |
470 | $self->err(416, "not satisfiable", $hdr, ""); |
455 | |
471 | |
456 | satisfiable: |
472 | satisfiable: |
457 | # check for segmented downloads |
473 | # check for segmented downloads |
458 | if ($l && $::NO_SEGMENTED) { |
474 | if ($l && $::NO_SEGMENTED) { |
459 | my $delay = 180; |
475 | my $delay = $::PER_TIMEOUT + 15; |
460 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
476 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
461 | if ($delay <= 0) { |
477 | if ($delay <= 0) { |
462 | $self->err_segmented_download; |
478 | $self->err_segmented_download; |
463 | } else { |
479 | } else { |
464 | Coro::Event::do_timer(after => 3); $delay -= 3; |
480 | Coro::Event::do_timer(after => 4); $delay -= 4; |
465 | } |
481 | } |
466 | } |
482 | } |
467 | } |
483 | } |
468 | |
484 | |
469 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
485 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
482 | $self->response(@code, $hdr, ""); |
498 | $self->response(@code, $hdr, ""); |
483 | |
499 | |
484 | if ($self->{method} eq "GET") { |
500 | if ($self->{method} eq "GET") { |
485 | $self->{time} = $::NOW; |
501 | $self->{time} = $::NOW; |
486 | |
502 | |
|
|
503 | my $fudge = $queue->[0]->waiters; |
|
|
504 | $fudge = $fudge ? ($fudge+1)/$fudge : 1; |
|
|
505 | |
|
|
506 | $queue->[1] *= $fudge; |
487 | my $transfer = $::transfers->guard; |
507 | my $transfer = $queue->[0]->guard; |
|
|
508 | |
|
|
509 | if ($fudge != 1) { |
|
|
510 | $queue->[1] /= $fudge; |
|
|
511 | $queue->[1] = $queue->[1] * $::wait_factor |
|
|
512 | + ($::NOW - $self->{time}) * (1 - $::wait_factor); |
|
|
513 | } |
|
|
514 | $self->{time} = $::NOW; |
|
|
515 | |
488 | $self->{fh}->writable or return; |
516 | $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 | |
517 | |
494 | my ($fh, $buf, $r); |
518 | my ($fh, $buf, $r); |
495 | my $current = $Coro::current; |
519 | my $current = $Coro::current; |
496 | open $fh, "<", $self->{path} |
520 | open $fh, "<", $self->{path} |
497 | or die "$self->{path}: late open failure ($!)"; |
521 | or die "$self->{path}: late open failure ($!)"; |
… | |
… | |
507 | while ($h > 0) { |
531 | while ($h > 0) { |
508 | if (0) { |
532 | if (0) { |
509 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
533 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
510 | or last; |
534 | or last; |
511 | } else { |
535 | } else { |
512 | undef $buf; |
|
|
513 | $aio_requests->down; |
|
|
514 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
536 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
515 | $buf, 0, sub { |
537 | $buf, 0, sub { |
516 | $r = $_[0]; |
538 | $r = $_[0]; |
517 | $current->ready; |
539 | Coro::ready($current); |
518 | }); |
540 | }); |
519 | &Coro::schedule; |
541 | &Coro::schedule; |
520 | $aio_requests->up; |
|
|
521 | last unless $r; |
542 | last unless $r; |
522 | } |
543 | } |
523 | my $w = $self->{fh}->syswrite($buf) |
544 | my $w = syswrite $self->{fh}, $buf |
524 | or last; |
545 | or last; |
525 | $::written += $w; |
546 | $::written += $w; |
526 | $self->{written} += $w; |
547 | $self->{written} += $w; |
527 | $l += $r; |
548 | $l += $r; |
528 | } |
549 | } |