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 | |
… | |
… | |
212 | } |
224 | } |
213 | |
225 | |
214 | $self->{h} = {}; |
226 | $self->{h} = {}; |
215 | |
227 | |
216 | $fh->timeout($::RES_TIMEOUT); |
228 | $fh->timeout($::RES_TIMEOUT); |
217 | my $ip = $self->{remote_addr}; |
|
|
218 | |
|
|
219 | if ($blocked{$ip}) { |
|
|
220 | $self->err_blocked($blocked{$ip}) |
|
|
221 | if $blocked{$ip} > $::NOW; |
|
|
222 | |
|
|
223 | delete $blocked{$ip}; |
|
|
224 | } |
|
|
225 | |
|
|
226 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
227 | my $delay = 120; |
|
|
228 | while (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
229 | if ($delay <= 0) { |
|
|
230 | $self->slog(2, "blocked ip $ip"); |
|
|
231 | $self->err_blocked; |
|
|
232 | } else { |
|
|
233 | Coro::Event::do_timer(after => 3); |
|
|
234 | $delay -= 3; |
|
|
235 | } |
|
|
236 | } |
|
|
237 | } |
|
|
238 | |
229 | |
239 | $req =~ /^(?:\015\012)? |
230 | $req =~ /^(?:\015\012)? |
240 | (GET|HEAD) \040+ |
231 | (GET|HEAD) \040+ |
241 | ([^\040]+) \040+ |
232 | ([^\040]+) \040+ |
242 | HTTP\/([0-9]+\.[0-9]+) |
233 | HTTP\/([0-9]+\.[0-9]+) |
… | |
… | |
267 | |
258 | |
268 | $self->{h}{$h} = substr $v, 1 |
259 | $self->{h}{$h} = substr $v, 1 |
269 | while ($h, $v) = each %hdr; |
260 | while ($h, $v) = each %hdr; |
270 | } |
261 | } |
271 | |
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 | |
272 | # find out server name and port |
293 | # find out server name and port |
273 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
294 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
274 | $host = $1; |
295 | $host = $1; |
275 | } else { |
296 | } else { |
276 | $host = $self->{h}{host}; |
297 | $host = $self->{h}{host}; |
… | |
… | |
285 | $host = inet_ntoa $host; |
306 | $host = inet_ntoa $host; |
286 | } |
307 | } |
287 | |
308 | |
288 | $self->{server_name} = $host; |
309 | $self->{server_name} = $host; |
289 | |
310 | |
290 | # remote id should be unique per user |
311 | # enter ourselves into various lists |
291 | $self->{remote_id} = $self->{remote_addr}; |
312 | weaken ($conn{$id}{$self*1} = $self); |
292 | |
|
|
293 | if (exists $self->{h}{"client-ip"}) { |
|
|
294 | $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]"; |
|
|
295 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
296 | $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
297 | } |
|
|
298 | |
|
|
299 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
313 | weaken ($uri{$id}{$self->{uri}}{$self*1} = $self); |
300 | |
314 | |
301 | eval { |
315 | eval { |
302 | $self->map_uri; |
316 | $self->map_uri; |
303 | $self->respond; |
317 | $self->respond; |
304 | }; |
318 | }; |
… | |
… | |
413 | $idx); |
427 | $idx); |
414 | } |
428 | } |
415 | |
429 | |
416 | sub handle_file { |
430 | sub handle_file { |
417 | my $self = shift; |
431 | my $self = shift; |
418 | my $length = -s _; |
432 | my $length = $self->{stat}[7]; |
|
|
433 | my $queue = $::transfers[$length >= $::TRANSFER_SMALL]; |
419 | my $hdr = { |
434 | my $hdr = { |
420 | "Last-Modified" => time2str ((stat _)[9]), |
435 | "Last-Modified" => time2str ((stat _)[9]), |
421 | }; |
436 | }; |
422 | |
437 | |
423 | my @code = (200, "ok"); |
438 | my @code = (200, "ok"); |
… | |
… | |
435 | } |
450 | } |
436 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
451 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
437 | } |
452 | } |
438 | $hdr->{"Content-Range"} = "bytes */$length"; |
453 | $hdr->{"Content-Range"} = "bytes */$length"; |
439 | $hdr->{"Content-Length"} = $length; |
454 | $hdr->{"Content-Length"} = $length; |
440 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
|
|
441 | $self->err(416, "not satisfiable", $hdr, ""); |
455 | $self->err(416, "not satisfiable", $hdr, ""); |
442 | |
456 | |
443 | satisfiable: |
457 | satisfiable: |
444 | # check for segmented downloads |
458 | # check for segmented downloads |
445 | if ($l && $::NO_SEGMENTED) { |
459 | if ($l && $::NO_SEGMENTED) { |
446 | my $delay = 180; |
460 | my $delay = $::PER_TIMEOUT + 15; |
447 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
461 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
448 | if ($delay <= 0) { |
462 | if ($delay <= 0) { |
449 | $self->err_segmented_download; |
463 | $self->err_segmented_download; |
450 | } else { |
464 | } else { |
451 | Coro::Event::do_timer(after => 3); $delay -= 3; |
465 | Coro::Event::do_timer(after => 4); $delay -= 4; |
452 | } |
466 | } |
453 | } |
467 | } |
454 | } |
468 | } |
455 | |
469 | |
456 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
470 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
467 | $hdr->{"Content-Length"} = $length; |
481 | $hdr->{"Content-Length"} = $length; |
468 | |
482 | |
469 | $self->response(@code, $hdr, ""); |
483 | $self->response(@code, $hdr, ""); |
470 | |
484 | |
471 | 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 | |
472 | my ($fh, $buf, $r); |
503 | my ($fh, $buf, $r); |
473 | my $current = $Coro::current; |
504 | my $current = $Coro::current; |
474 | open $fh, "<", $self->{path} |
505 | open $fh, "<", $self->{path} |
475 | or die "$self->{path}: late open failure ($!)"; |
506 | or die "$self->{path}: late open failure ($!)"; |
476 | |
507 | |
… | |
… | |
485 | while ($h > 0) { |
516 | while ($h > 0) { |
486 | if (0) { |
517 | if (0) { |
487 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
518 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
488 | or last; |
519 | or last; |
489 | } else { |
520 | } else { |
490 | undef $buf; |
|
|
491 | $aio_requests->down; |
|
|
492 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
521 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
493 | $buf, 0, sub { |
522 | $buf, 0, sub { |
494 | $r = $_[0]; |
523 | $r = $_[0]; |
495 | $current->ready; |
524 | Coro::ready($current); |
496 | }); |
525 | }); |
497 | &Coro::schedule; |
526 | &Coro::schedule; |
498 | $aio_requests->up; |
|
|
499 | last unless $r; |
527 | last unless $r; |
500 | } |
528 | } |
501 | my $w = $self->{fh}->syswrite($buf) |
529 | my $w = syswrite $self->{fh}, $buf |
502 | or last; |
530 | or last; |
503 | $::written += $w; |
531 | $::written += $w; |
504 | $self->{written} += $w; |
532 | $self->{written} += $w; |
505 | $l += $r; |
533 | $l += $r; |
506 | } |
534 | } |
507 | } |
|
|
508 | |
535 | |
509 | close $fh; |
536 | close $fh; |
|
|
537 | } |
510 | } |
538 | } |
511 | |
539 | |
512 | 1; |
540 | 1; |