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