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 | use Coro::Signal; |
5 | use Coro::Signal; |
|
|
6 | use Coro::AIO (); |
6 | |
7 | |
7 | use HTTP::Date; |
8 | use HTTP::Date; |
8 | use POSIX (); |
9 | use POSIX (); |
9 | |
10 | |
10 | use Compress::Zlib (); |
11 | use Compress::Zlib (); |
… | |
… | |
21 | our $accesslog; |
22 | our $accesslog; |
22 | our $errorlog; |
23 | our $errorlog; |
23 | |
24 | |
24 | our $NOW; |
25 | our $NOW; |
25 | our $HTTP_NOW; |
26 | our $HTTP_NOW; |
|
|
27 | |
|
|
28 | our $ERROR_LOG; |
|
|
29 | our $ACCESS_LOG; |
26 | |
30 | |
27 | Event->timer(interval => 1, hard => 1, cb => sub { |
31 | Event->timer(interval => 1, hard => 1, cb => sub { |
28 | $NOW = time; |
32 | $NOW = time; |
29 | $HTTP_NOW = time2str $NOW; |
33 | $HTTP_NOW = time2str $NOW; |
30 | })->now; |
34 | })->now; |
… | |
… | |
55 | our $httpevent = new Coro::Signal; |
59 | our $httpevent = new Coro::Signal; |
56 | |
60 | |
57 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
61 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
58 | our $queue_index = new transferqueue 10; |
62 | our $queue_index = new transferqueue 10; |
59 | |
63 | |
60 | our $tbf_top = new tbf rate => 100000; |
64 | our $tbf_top = new tbf rate => $TBF_RATE || 100000; |
61 | |
65 | |
62 | my $unused_bytes = 0; |
66 | my $unused_bytes = 0; |
63 | my $unused_last = time; |
67 | my $unused_last = time; |
64 | |
68 | |
65 | sub unused_bandwidth { |
69 | sub unused_bandwidth { |
… | |
… | |
78 | # one "execution thread" |
82 | # one "execution thread" |
79 | sub handler { |
83 | sub handler { |
80 | while () { |
84 | while () { |
81 | if (@newcons) { |
85 | if (@newcons) { |
82 | eval { |
86 | eval { |
83 | conn->new(@{pop @newcons})->handle; |
87 | conn->new (@{pop @newcons})->handle; |
84 | }; |
88 | }; |
85 | slog 1, "$@" if $@ && !ref $@; |
89 | slog 1, "$@" if $@ && !ref $@; |
86 | |
90 | |
87 | $httpevent->broadcast; # only for testing, but doesn't matter much |
91 | $httpevent->broadcast; # only for testing, but doesn't matter much |
88 | |
92 | |
… | |
… | |
110 | if (@pool) { |
114 | if (@pool) { |
111 | (pop @pool)->ready; |
115 | (pop @pool)->ready; |
112 | } else { |
116 | } else { |
113 | async \&handler; |
117 | async \&handler; |
114 | } |
118 | } |
115 | |
|
|
116 | } |
119 | } |
117 | }; |
120 | }; |
118 | } |
121 | } |
119 | |
122 | |
120 | my $http_port = new Coro::Socket |
123 | my $http_port = new Coro::Socket |
121 | LocalAddr => $SERVER_HOST, |
124 | LocalAddr => $SERVER_HOST, |
122 | LocalPort => $SERVER_PORT, |
125 | LocalPort => $SERVER_PORT, |
123 | ReuseAddr => 1, |
126 | ReuseAddr => 1, |
124 | Listen => 50, |
127 | Listen => 50, |
125 | or die "unable to start server"; |
128 | or die "unable to start server"; |
126 | |
129 | |
127 | listen_on $http_port; |
130 | listen_on $http_port; |
128 | |
131 | |
129 | if ($SERVER_PORT2) { |
132 | if ($SERVER_PORT2) { |
130 | my $http_port = new Coro::Socket |
133 | my $http_port = new Coro::Socket |
131 | LocalAddr => $SERVER_HOST, |
134 | LocalAddr => $SERVER_HOST, |
132 | LocalPort => $SERVER_PORT2, |
135 | LocalPort => $SERVER_PORT2, |
133 | ReuseAddr => 1, |
136 | ReuseAddr => 1, |
134 | Listen => 50, |
137 | Listen => 50, |
135 | or die "unable to start server"; |
138 | or die "unable to start server"; |
136 | |
139 | |
137 | listen_on $http_port; |
140 | listen_on $http_port; |
138 | } |
141 | } |
139 | |
142 | |
140 | package conn; |
143 | package conn; |
|
|
144 | |
|
|
145 | use strict; |
|
|
146 | use bytes; |
141 | |
147 | |
142 | use Socket; |
148 | use Socket; |
143 | use HTTP::Date; |
149 | use HTTP::Date; |
144 | use Convert::Scalar 'weaken'; |
150 | use Convert::Scalar 'weaken'; |
145 | use Linux::AIO; |
151 | use IO::AIO; |
146 | |
152 | |
147 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
153 | IO::AIO::min_parallel $::AIO_PARALLEL; |
148 | |
154 | |
149 | Event->io(fd => Linux::AIO::poll_fileno, |
155 | Event->io (fd => IO::AIO::poll_fileno, |
150 | poll => 'r', async => 1, |
156 | poll => 'r', async => 1, |
151 | cb => \&Linux::AIO::poll_cb); |
157 | cb => \&IO::AIO::poll_cb); |
152 | |
158 | |
153 | our %conn; # $conn{ip}{self} => connobj |
159 | our %conn; # $conn{ip}{self} => connobj |
154 | our %uri; # $uri{ip}{uri}{self} |
160 | our %uri; # $uri{ip}{uri}{self} |
155 | our %blocked; |
161 | our %blocked; |
156 | our %mimetype; |
162 | our %mimetype; |
157 | |
163 | |
158 | sub read_mimetypes { |
164 | sub read_mimetypes { |
159 | local *M; |
|
|
160 | if (open M, "<mime_types") { |
165 | if (open my $fh, "<mime_types") { |
161 | while (<M>) { |
166 | while (<$fh>) { |
162 | if (/^([^#]\S+)\t+(\S+)$/) { |
167 | if (/^([^#]\S+)\t+(\S+)$/) { |
163 | $mimetype{lc $1} = $2; |
168 | $mimetype{lc $1} = $2; |
164 | } |
169 | } |
165 | } |
170 | } |
166 | } else { |
171 | } else { |
… | |
… | |
174 | my $class = shift; |
179 | my $class = shift; |
175 | my $fh = shift; |
180 | my $fh = shift; |
176 | my $peername = shift; |
181 | my $peername = shift; |
177 | my $self = bless { fh => $fh }, $class; |
182 | my $self = bless { fh => $fh }, $class; |
178 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
183 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
179 | or $self->err(500, "unable to decode peername"); |
184 | or $self->err (500, "unable to decode peername"); |
180 | |
185 | |
181 | $self->{remote_addr} = |
186 | $self->{remote_addr} = |
182 | $self->{remote_id} = inet_ntoa $iaddr; |
187 | $self->{remote_id} = inet_ntoa $iaddr; |
183 | |
188 | |
184 | $self->{time} = $::NOW; |
189 | $self->{time} = $::NOW; |
185 | |
190 | |
186 | weaken ($Coro::current->{conn} = $self); |
191 | weaken ($Coro::current->{conn} = $self); |
187 | |
192 | |
188 | $::conns++; |
193 | ++$::conns; |
189 | $::maxconns = $::conns if $::conns > $::maxconns; |
194 | $::maxconns = $::conns if $::conns > $::maxconns; |
190 | |
195 | |
191 | $self; |
196 | $self |
192 | } |
197 | } |
193 | |
198 | |
194 | sub DESTROY { |
199 | sub DESTROY { |
195 | #my $self = shift; |
200 | my $self = shift; |
|
|
201 | |
|
|
202 | close $self->{fh}; # workaround |
196 | $::conns--; |
203 | --$::conns; |
197 | } |
204 | } |
|
|
205 | |
|
|
206 | sub prune_cache { |
|
|
207 | my $hash = $_[0]; |
|
|
208 | |
|
|
209 | for (keys %$hash) { |
|
|
210 | if (ref $hash->{$_} eq HASH::) { |
|
|
211 | prune_cache($hash->{$_}); |
|
|
212 | unless (scalar keys %{$hash->{$_}}) { |
|
|
213 | delete $hash->{$_}; |
|
|
214 | } |
|
|
215 | } |
|
|
216 | } |
|
|
217 | } |
|
|
218 | |
|
|
219 | sub prune_caches { |
|
|
220 | prune_cache \%conn; |
|
|
221 | prune_cache \%uri; |
|
|
222 | |
|
|
223 | for (keys %blocked) { |
|
|
224 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
|
|
225 | } |
|
|
226 | } |
|
|
227 | |
|
|
228 | Event->timer (interval => 60, cb => \&prune_caches); |
198 | |
229 | |
199 | sub slog { |
230 | sub slog { |
200 | my $self = shift; |
231 | my $self = shift; |
201 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
232 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
202 | } |
233 | } |
… | |
… | |
263 | $hdr->{"Content-Type"} = "text/plain"; |
294 | $hdr->{"Content-Type"} = "text/plain"; |
264 | $hdr->{"Content-Length"} = length $content; |
295 | $hdr->{"Content-Length"} = length $content; |
265 | } |
296 | } |
266 | $hdr->{"Connection"} = "close"; |
297 | $hdr->{"Connection"} = "close"; |
267 | |
298 | |
268 | $self->response($code, $msg, $hdr, $content); |
299 | $self->response ($code, $msg, $hdr, $content); |
269 | |
300 | |
270 | die bless {}, err::; |
301 | die bless {}, err:: |
271 | } |
302 | } |
272 | |
303 | |
273 | sub handle { |
304 | sub handle { |
274 | my $self = shift; |
305 | my $self = shift; |
275 | my $fh = $self->{fh}; |
306 | my $fh = $self->{fh}; |
276 | |
307 | |
277 | my $host; |
308 | my $host; |
278 | |
309 | |
279 | $fh->timeout($::REQ_TIMEOUT); |
310 | $fh->timeout($::REQ_TIMEOUT); |
280 | while() { |
311 | while () { |
281 | $self->{reqs}++; |
312 | $self->{reqs}++; |
282 | |
313 | |
283 | # read request and parse first line |
314 | # read request and parse first line |
284 | my $req = $fh->readline("\015\012\015\012"); |
315 | my $req = $fh->readline("\015\012\015\012"); |
285 | |
316 | |
… | |
… | |
447 | |
478 | |
448 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
479 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
449 | if ($::internal{$1}) { |
480 | if ($::internal{$1}) { |
450 | $::internal{$1}->($self); |
481 | $::internal{$1}->($self); |
451 | } else { |
482 | } else { |
452 | $self->err(404, "not found"); |
483 | $self->err (404, "not found"); |
453 | } |
484 | } |
454 | } else { |
485 | } else { |
455 | |
486 | |
456 | stat $path |
487 | stat $path |
457 | or $self->err(404, "not found"); |
488 | or $self->err (404, "not found"); |
458 | |
489 | |
459 | $self->{stat} = [stat _]; |
490 | $self->{stat} = [stat _]; |
460 | |
491 | |
461 | # idiotic netscape sends idiotic headers AGAIN |
492 | # idiotic netscape sends idiotic headers AGAIN |
462 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
493 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
… | |
… | |
465 | if (-d _ && -r _) { |
496 | if (-d _ && -r _) { |
466 | # directory |
497 | # directory |
467 | if ($path !~ /\/$/) { |
498 | if ($path !~ /\/$/) { |
468 | # create a redirect to get the trailing "/" |
499 | # create a redirect to get the trailing "/" |
469 | # we don't try to avoid the :80 |
500 | # we don't try to avoid the :80 |
470 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
501 | $self->err (301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
471 | } else { |
502 | } else { |
472 | $ims < $self->{stat}[9] |
503 | $ims < $self->{stat}[9] |
473 | or $self->err(304, "not modified"); |
504 | or $self->err (304, "not modified"); |
474 | |
505 | |
475 | if (-r "$path/index.html") { |
506 | if (-r "$path/index.html") { |
476 | # replace directory "size" by index.html filesize |
507 | # replace directory "size" by index.html filesize |
477 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
508 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
478 | $self->handle_file($queue_index, $tbf_top); |
509 | $self->handle_file ($queue_index, $tbf_top); |
479 | } else { |
510 | } else { |
480 | $self->handle_dir; |
511 | $self->handle_dir; |
481 | } |
512 | } |
482 | } |
513 | } |
483 | } elsif (-f _ && -r _) { |
514 | } elsif (-f _ && -r _) { |
484 | -x _ and $self->err(403, "forbidden"); |
515 | -x _ and $self->err (403, "forbidden"); |
485 | |
516 | |
486 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
517 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
487 | my $timeout = $::NOW + 10; |
518 | my $timeout = $::NOW + 10; |
488 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
519 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
489 | if ($timeout < $::NOW) { |
520 | if ($timeout < $::NOW) { |
… | |
… | |
492 | $httpevent->wait; |
523 | $httpevent->wait; |
493 | } |
524 | } |
494 | } |
525 | } |
495 | } |
526 | } |
496 | |
527 | |
497 | $self->handle_file($queue_file, $tbf_top); |
528 | $self->handle_file ($queue_file, $tbf_top); |
498 | } else { |
529 | } else { |
499 | $self->err(404, "not found"); |
530 | $self->err (404, "not found"); |
500 | } |
531 | } |
501 | } |
532 | } |
502 | } |
533 | } |
503 | |
534 | |
504 | sub handle_dir { |
535 | sub handle_dir { |
505 | my $self = shift; |
536 | my $self = shift; |
506 | my $idx = $self->diridx; |
537 | my $idx = $self->diridx; |
507 | |
538 | |
508 | $self->response(200, "ok", |
539 | $self->response (200, "ok", |
509 | { |
540 | { |
510 | "Content-Type" => "text/html", |
541 | "Content-Type" => "text/html; charset=utf-8", |
511 | "Content-Length" => length $idx, |
542 | "Content-Length" => length $idx, |
512 | "Last-Modified" => time2str ($self->{stat}[9]), |
543 | "Last-Modified" => time2str ($self->{stat}[9]), |
513 | }, |
544 | }, |
514 | $idx); |
545 | $idx); |
515 | } |
546 | } |
516 | |
547 | |
517 | sub handle_file { |
548 | sub handle_file { |
518 | my ($self, $queue, $tbf) = @_; |
549 | my ($self, $queue, $tbf) = @_; |
519 | my $length = $self->{stat}[7]; |
550 | my $length = $self->{stat}[7]; |
520 | my $hdr = { |
551 | my $hdr = { |
521 | "Last-Modified" => time2str ((stat _)[9]), |
552 | "Last-Modified" => time2str ((stat _)[9]), |
|
|
553 | "Accept-Ranges" => "bytes", |
522 | }; |
554 | }; |
523 | |
555 | |
524 | my @code = (200, "ok"); |
556 | my @code = (200, "ok"); |
525 | my ($l, $h); |
557 | my ($l, $h); |
526 | |
558 | |
… | |
… | |
536 | } |
568 | } |
537 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
569 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
538 | } |
570 | } |
539 | $hdr->{"Content-Range"} = "bytes */$length"; |
571 | $hdr->{"Content-Range"} = "bytes */$length"; |
540 | $hdr->{"Content-Length"} = $length; |
572 | $hdr->{"Content-Length"} = $length; |
541 | $self->err(416, "not satisfiable", $hdr, ""); |
573 | $self->err (416, "not satisfiable", $hdr, ""); |
542 | |
574 | |
543 | satisfiable: |
575 | satisfiable: |
544 | # check for segmented downloads |
576 | # check for segmented downloads |
545 | if ($l && $::NO_SEGMENTED) { |
577 | if ($l && $::NO_SEGMENTED) { |
546 | my $timeout = $::NOW + 15; |
578 | my $timeout = $::NOW + 15; |
547 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
579 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
548 | if ($timeout <= $::NOW) { |
580 | if ($timeout <= $::NOW) { |
549 | $self->block($::BLOCKTIME, "segmented downloads are forbidden"); |
581 | $self->block ($::BLOCKTIME, "segmented downloads are forbidden"); |
550 | #$self->err_segmented_download; |
582 | #$self->err_segmented_download; |
551 | } else { |
583 | } else { |
552 | $httpevent->wait; |
584 | $httpevent->wait; |
553 | } |
585 | } |
554 | } |
586 | } |
… | |
… | |
565 | |
597 | |
566 | $self->{path} =~ /\.([^.]+)$/; |
598 | $self->{path} =~ /\.([^.]+)$/; |
567 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
599 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
568 | $hdr->{"Content-Length"} = $length; |
600 | $hdr->{"Content-Length"} = $length; |
569 | |
601 | |
570 | $self->response(@code, $hdr, ""); |
602 | $self->response (@code, $hdr, ""); |
571 | |
603 | |
572 | if ($self->{method} eq "GET") { |
604 | if ($self->{method} eq "GET") { |
573 | $self->{time} = $::NOW; |
605 | $self->{time} = $::NOW; |
574 | $self->{written} = 0; |
606 | $self->{written} = 0; |
575 | |
607 | |
576 | my $current = $Coro::current; |
|
|
577 | |
|
|
578 | my ($fh, $buf, $r); |
|
|
579 | |
|
|
580 | open $fh, "<", $self->{path} |
608 | open my $fh, "<", $self->{path} |
581 | or die "$self->{path}: late open failure ($!)"; |
609 | or die "$self->{path}: late open failure ($!)"; |
582 | |
610 | |
583 | $h -= $l - 1; |
611 | $h -= $l - 1; |
584 | |
612 | |
585 | if (0) { # !AIO |
|
|
586 | if ($l) { |
|
|
587 | sysseek $fh, $l, 0; |
|
|
588 | } |
|
|
589 | } |
|
|
590 | |
|
|
591 | my $transfer = $queue->start_transfer($h); |
613 | my $transfer = $queue->start_transfer ($h); |
592 | my $locked; |
614 | my $locked; |
593 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
615 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
594 | |
616 | |
595 | while ($h > 0) { |
617 | while ($h > 0) { |
596 | unless ($locked) { |
618 | unless ($locked) { |
597 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
619 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
598 | $bufsize = $::BUFSIZE; |
620 | $bufsize = $::BUFSIZE; |
599 | $self->{time} = $::NOW; |
621 | $self->{time} = $::NOW; |
600 | $self->{written} = 0; |
622 | $self->{written} = 0; |
601 | } |
623 | } |
602 | } |
624 | } |
… | |
… | |
604 | if ($blocked{$self->{remote_id}}) { |
626 | if ($blocked{$self->{remote_id}}) { |
605 | $self->{h}{connection} = "close"; |
627 | $self->{h}{connection} = "close"; |
606 | die bless {}, err::; |
628 | die bless {}, err::; |
607 | } |
629 | } |
608 | |
630 | |
609 | if (0) { # !AIO |
631 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
610 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
|
|
611 | or last; |
632 | or last; |
612 | } else { |
|
|
613 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
|
|
614 | $buf, 0, sub { |
|
|
615 | $r = $_[0]; |
|
|
616 | Coro::ready($current); |
|
|
617 | }); |
|
|
618 | &Coro::schedule; |
|
|
619 | last unless $r; |
|
|
620 | } |
|
|
621 | |
633 | |
622 | $tbf->request(length $buf); |
634 | $tbf->request (length $buf); |
623 | my $w = syswrite $self->{fh}, $buf |
635 | my $w = syswrite $self->{fh}, $buf |
624 | or last; |
636 | or last; |
625 | $::written += $w; |
637 | $::written += $w; |
626 | $self->{written} += $w; |
638 | $self->{written} += $w; |
627 | $l += $r; |
639 | $l += $w; |
628 | } |
640 | } |
629 | |
641 | |
630 | close $fh; |
642 | close $fh; |
631 | } |
643 | } |
632 | } |
644 | } |
633 | |
645 | |
634 | 1; |
646 | 1 |
|
|
647 | |