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; |
… | |
… | |
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 | |
… | |
… | |
118 | |
122 | |
119 | my $http_port = new Coro::Socket |
123 | my $http_port = new Coro::Socket |
120 | LocalAddr => $SERVER_HOST, |
124 | LocalAddr => $SERVER_HOST, |
121 | LocalPort => $SERVER_PORT, |
125 | LocalPort => $SERVER_PORT, |
122 | ReuseAddr => 1, |
126 | ReuseAddr => 1, |
123 | Listen => 50, |
127 | Listen => 50, |
124 | or die "unable to start server"; |
128 | or die "unable to start server"; |
125 | |
129 | |
126 | listen_on $http_port; |
130 | listen_on $http_port; |
127 | |
131 | |
128 | if ($SERVER_PORT2) { |
132 | if ($SERVER_PORT2) { |
129 | my $http_port = new Coro::Socket |
133 | my $http_port = new Coro::Socket |
130 | LocalAddr => $SERVER_HOST, |
134 | LocalAddr => $SERVER_HOST, |
131 | LocalPort => $SERVER_PORT2, |
135 | LocalPort => $SERVER_PORT2, |
132 | ReuseAddr => 1, |
136 | ReuseAddr => 1, |
133 | Listen => 50, |
137 | Listen => 50, |
134 | or die "unable to start server"; |
138 | or die "unable to start server"; |
135 | |
139 | |
136 | listen_on $http_port; |
140 | listen_on $http_port; |
137 | } |
141 | } |
138 | |
142 | |
139 | package conn; |
143 | package conn; |
|
|
144 | |
|
|
145 | use strict; |
|
|
146 | use bytes; |
140 | |
147 | |
141 | use Socket; |
148 | use Socket; |
142 | use HTTP::Date; |
149 | use HTTP::Date; |
143 | use Convert::Scalar 'weaken'; |
150 | use Convert::Scalar 'weaken'; |
144 | use IO::AIO; |
151 | use IO::AIO; |
145 | |
152 | |
146 | IO::AIO::min_parallel $::AIO_PARALLEL; |
153 | IO::AIO::min_parallel $::AIO_PARALLEL; |
147 | |
154 | |
148 | Event->io(fd => IO::AIO::poll_fileno, |
155 | Event->io (fd => IO::AIO::poll_fileno, |
149 | poll => 'r', async => 1, |
156 | poll => 'r', async => 1, |
150 | cb => \&IO::AIO::poll_cb); |
157 | cb => \&IO::AIO::poll_cb); |
151 | |
158 | |
152 | our %conn; # $conn{ip}{self} => connobj |
159 | our %conn; # $conn{ip}{self} => connobj |
153 | our %uri; # $uri{ip}{uri}{self} |
160 | our %uri; # $uri{ip}{uri}{self} |
154 | our %blocked; |
161 | our %blocked; |
155 | our %mimetype; |
162 | our %mimetype; |
156 | |
163 | |
157 | sub read_mimetypes { |
164 | sub read_mimetypes { |
158 | local *M; |
|
|
159 | if (open M, "<mime_types") { |
165 | if (open my $fh, "<mime_types") { |
160 | while (<M>) { |
166 | while (<$fh>) { |
161 | if (/^([^#]\S+)\t+(\S+)$/) { |
167 | if (/^([^#]\S+)\t+(\S+)$/) { |
162 | $mimetype{lc $1} = $2; |
168 | $mimetype{lc $1} = $2; |
163 | } |
169 | } |
164 | } |
170 | } |
165 | } else { |
171 | } else { |
… | |
… | |
173 | my $class = shift; |
179 | my $class = shift; |
174 | my $fh = shift; |
180 | my $fh = shift; |
175 | my $peername = shift; |
181 | my $peername = shift; |
176 | my $self = bless { fh => $fh }, $class; |
182 | my $self = bless { fh => $fh }, $class; |
177 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
183 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
178 | or $self->err(500, "unable to decode peername"); |
184 | or $self->err (500, "unable to decode peername"); |
179 | |
185 | |
180 | $self->{remote_addr} = |
186 | $self->{remote_addr} = |
181 | $self->{remote_id} = inet_ntoa $iaddr; |
187 | $self->{remote_id} = inet_ntoa $iaddr; |
182 | |
188 | |
183 | $self->{time} = $::NOW; |
189 | $self->{time} = $::NOW; |
184 | |
190 | |
185 | weaken ($Coro::current->{conn} = $self); |
191 | weaken ($Coro::current->{conn} = $self); |
186 | |
192 | |
187 | $::conns++; |
193 | ++$::conns; |
188 | $::maxconns = $::conns if $::conns > $::maxconns; |
194 | $::maxconns = $::conns if $::conns > $::maxconns; |
189 | |
195 | |
190 | $self; |
196 | $self |
191 | } |
197 | } |
192 | |
198 | |
193 | sub DESTROY { |
199 | sub DESTROY { |
194 | #my $self = shift; |
200 | my $self = shift; |
|
|
201 | |
|
|
202 | close $self->{fh}; # workaround |
195 | $::conns--; |
203 | --$::conns; |
196 | } |
204 | } |
197 | |
205 | |
198 | sub prune_cache { |
206 | sub prune_cache { |
199 | my $hash = $_[0]; |
207 | my $hash = $_[0]; |
200 | |
208 | |
201 | for (keys %$hash) { |
209 | for (keys %$hash) { |
202 | if (ref $hash->{$_} eq HASH::) { |
210 | if (ref $hash->{$_} eq HASH::) { |
203 | prune_cache($hash->{$_}); |
211 | prune_cache($hash->{$_}); |
204 | unless (scalar keys %{$hash->{$_}}) { |
212 | unless (scalar keys %{$hash->{$_}}) { |
205 | delete $hash->{$_}; |
213 | delete $hash->{$_}; |
206 | $d2++; |
|
|
207 | } |
214 | } |
208 | } |
215 | } |
209 | } |
216 | } |
210 | } |
217 | } |
211 | |
218 | |
… | |
… | |
216 | for (keys %blocked) { |
223 | for (keys %blocked) { |
217 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
224 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
218 | } |
225 | } |
219 | } |
226 | } |
220 | |
227 | |
221 | Event->timer(interval => 60, cb => \&prune_caches); |
228 | Event->timer (interval => 60, cb => \&prune_caches); |
222 | |
229 | |
223 | sub slog { |
230 | sub slog { |
224 | my $self = shift; |
231 | my $self = shift; |
225 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
232 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
226 | } |
233 | } |
… | |
… | |
287 | $hdr->{"Content-Type"} = "text/plain"; |
294 | $hdr->{"Content-Type"} = "text/plain"; |
288 | $hdr->{"Content-Length"} = length $content; |
295 | $hdr->{"Content-Length"} = length $content; |
289 | } |
296 | } |
290 | $hdr->{"Connection"} = "close"; |
297 | $hdr->{"Connection"} = "close"; |
291 | |
298 | |
292 | $self->response($code, $msg, $hdr, $content); |
299 | $self->response ($code, $msg, $hdr, $content); |
293 | |
300 | |
294 | die bless {}, err::; |
301 | die bless {}, err:: |
295 | } |
302 | } |
296 | |
303 | |
297 | sub handle { |
304 | sub handle { |
298 | my $self = shift; |
305 | my $self = shift; |
299 | my $fh = $self->{fh}; |
306 | my $fh = $self->{fh}; |
300 | |
307 | |
301 | my $host; |
308 | my $host; |
302 | |
309 | |
303 | $fh->timeout($::REQ_TIMEOUT); |
310 | $fh->timeout($::REQ_TIMEOUT); |
304 | while() { |
311 | while () { |
305 | $self->{reqs}++; |
312 | $self->{reqs}++; |
306 | |
313 | |
307 | # read request and parse first line |
314 | # read request and parse first line |
308 | my $req = $fh->readline("\015\012\015\012"); |
315 | my $req = $fh->readline("\015\012\015\012"); |
309 | |
316 | |
… | |
… | |
471 | |
478 | |
472 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
479 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
473 | if ($::internal{$1}) { |
480 | if ($::internal{$1}) { |
474 | $::internal{$1}->($self); |
481 | $::internal{$1}->($self); |
475 | } else { |
482 | } else { |
476 | $self->err(404, "not found"); |
483 | $self->err (404, "not found"); |
477 | } |
484 | } |
478 | } else { |
485 | } else { |
479 | |
486 | |
480 | stat $path |
487 | stat $path |
481 | or $self->err(404, "not found"); |
488 | or $self->err (404, "not found"); |
482 | |
489 | |
483 | $self->{stat} = [stat _]; |
490 | $self->{stat} = [stat _]; |
484 | |
491 | |
485 | # idiotic netscape sends idiotic headers AGAIN |
492 | # idiotic netscape sends idiotic headers AGAIN |
486 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
493 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
… | |
… | |
489 | if (-d _ && -r _) { |
496 | if (-d _ && -r _) { |
490 | # directory |
497 | # directory |
491 | if ($path !~ /\/$/) { |
498 | if ($path !~ /\/$/) { |
492 | # create a redirect to get the trailing "/" |
499 | # create a redirect to get the trailing "/" |
493 | # we don't try to avoid the :80 |
500 | # we don't try to avoid the :80 |
494 | $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}/" }); |
495 | } else { |
502 | } else { |
496 | $ims < $self->{stat}[9] |
503 | $ims < $self->{stat}[9] |
497 | or $self->err(304, "not modified"); |
504 | or $self->err (304, "not modified"); |
498 | |
505 | |
499 | if (-r "$path/index.html") { |
506 | if (-r "$path/index.html") { |
500 | # replace directory "size" by index.html filesize |
507 | # replace directory "size" by index.html filesize |
501 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
508 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
502 | $self->handle_file($queue_index, $tbf_top); |
509 | $self->handle_file ($queue_index, $tbf_top); |
503 | } else { |
510 | } else { |
504 | $self->handle_dir; |
511 | $self->handle_dir; |
505 | } |
512 | } |
506 | } |
513 | } |
507 | } elsif (-f _ && -r _) { |
514 | } elsif (-f _ && -r _) { |
508 | -x _ and $self->err(403, "forbidden"); |
515 | -x _ and $self->err (403, "forbidden"); |
509 | |
516 | |
510 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
517 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
511 | my $timeout = $::NOW + 10; |
518 | my $timeout = $::NOW + 10; |
512 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
519 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
513 | if ($timeout < $::NOW) { |
520 | if ($timeout < $::NOW) { |
… | |
… | |
516 | $httpevent->wait; |
523 | $httpevent->wait; |
517 | } |
524 | } |
518 | } |
525 | } |
519 | } |
526 | } |
520 | |
527 | |
521 | $self->handle_file($queue_file, $tbf_top); |
528 | $self->handle_file ($queue_file, $tbf_top); |
522 | } else { |
529 | } else { |
523 | $self->err(404, "not found"); |
530 | $self->err (404, "not found"); |
524 | } |
531 | } |
525 | } |
532 | } |
526 | } |
533 | } |
527 | |
534 | |
528 | sub handle_dir { |
535 | sub handle_dir { |
529 | my $self = shift; |
536 | my $self = shift; |
530 | my $idx = $self->diridx; |
537 | my $idx = $self->diridx; |
531 | |
538 | |
532 | $self->response(200, "ok", |
539 | $self->response (200, "ok", |
533 | { |
540 | { |
534 | "Content-Type" => "text/html; charset=utf-8", |
541 | "Content-Type" => "text/html; charset=utf-8", |
535 | "Content-Length" => length $idx, |
542 | "Content-Length" => length $idx, |
536 | "Last-Modified" => time2str ($self->{stat}[9]), |
543 | "Last-Modified" => time2str ($self->{stat}[9]), |
537 | }, |
544 | }, |
… | |
… | |
561 | } |
568 | } |
562 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
569 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
563 | } |
570 | } |
564 | $hdr->{"Content-Range"} = "bytes */$length"; |
571 | $hdr->{"Content-Range"} = "bytes */$length"; |
565 | $hdr->{"Content-Length"} = $length; |
572 | $hdr->{"Content-Length"} = $length; |
566 | $self->err(416, "not satisfiable", $hdr, ""); |
573 | $self->err (416, "not satisfiable", $hdr, ""); |
567 | |
574 | |
568 | satisfiable: |
575 | satisfiable: |
569 | # check for segmented downloads |
576 | # check for segmented downloads |
570 | if ($l && $::NO_SEGMENTED) { |
577 | if ($l && $::NO_SEGMENTED) { |
571 | my $timeout = $::NOW + 15; |
578 | my $timeout = $::NOW + 15; |
572 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
579 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
573 | if ($timeout <= $::NOW) { |
580 | if ($timeout <= $::NOW) { |
574 | $self->block($::BLOCKTIME, "segmented downloads are forbidden"); |
581 | $self->block ($::BLOCKTIME, "segmented downloads are forbidden"); |
575 | #$self->err_segmented_download; |
582 | #$self->err_segmented_download; |
576 | } else { |
583 | } else { |
577 | $httpevent->wait; |
584 | $httpevent->wait; |
578 | } |
585 | } |
579 | } |
586 | } |
… | |
… | |
590 | |
597 | |
591 | $self->{path} =~ /\.([^.]+)$/; |
598 | $self->{path} =~ /\.([^.]+)$/; |
592 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
599 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
593 | $hdr->{"Content-Length"} = $length; |
600 | $hdr->{"Content-Length"} = $length; |
594 | |
601 | |
595 | $self->response(@code, $hdr, ""); |
602 | $self->response (@code, $hdr, ""); |
596 | |
603 | |
597 | if ($self->{method} eq "GET") { |
604 | if ($self->{method} eq "GET") { |
598 | $self->{time} = $::NOW; |
605 | $self->{time} = $::NOW; |
599 | $self->{written} = 0; |
606 | $self->{written} = 0; |
600 | |
607 | |
601 | my $current = $Coro::current; |
|
|
602 | |
|
|
603 | my ($fh, $buf, $r); |
|
|
604 | |
|
|
605 | open $fh, "<", $self->{path} |
608 | open my $fh, "<", $self->{path} |
606 | or die "$self->{path}: late open failure ($!)"; |
609 | or die "$self->{path}: late open failure ($!)"; |
607 | |
610 | |
608 | $h -= $l - 1; |
611 | $h -= $l - 1; |
609 | |
612 | |
610 | if (0) { # !AIO |
|
|
611 | if ($l) { |
|
|
612 | sysseek $fh, $l, 0; |
|
|
613 | } |
|
|
614 | } |
|
|
615 | |
|
|
616 | my $transfer = $queue->start_transfer($h); |
613 | my $transfer = $queue->start_transfer ($h); |
617 | my $locked; |
614 | my $locked; |
618 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
615 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
619 | |
616 | |
620 | while ($h > 0) { |
617 | while ($h > 0) { |
621 | unless ($locked) { |
618 | unless ($locked) { |
622 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
619 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
623 | $bufsize = $::BUFSIZE; |
620 | $bufsize = $::BUFSIZE; |
624 | $self->{time} = $::NOW; |
621 | $self->{time} = $::NOW; |
625 | $self->{written} = 0; |
622 | $self->{written} = 0; |
626 | } |
623 | } |
627 | } |
624 | } |
… | |
… | |
629 | if ($blocked{$self->{remote_id}}) { |
626 | if ($blocked{$self->{remote_id}}) { |
630 | $self->{h}{connection} = "close"; |
627 | $self->{h}{connection} = "close"; |
631 | die bless {}, err::; |
628 | die bless {}, err::; |
632 | } |
629 | } |
633 | |
630 | |
634 | if (0) { # !AIO |
631 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
635 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
|
|
636 | or last; |
632 | or last; |
637 | } else { |
|
|
638 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
|
|
639 | $buf, 0, sub { |
|
|
640 | $r = $_[0]; |
|
|
641 | Coro::ready($current); |
|
|
642 | }); |
|
|
643 | &Coro::schedule; |
|
|
644 | last unless $r; |
|
|
645 | } |
|
|
646 | |
633 | |
647 | $tbf->request(length $buf); |
634 | $tbf->request (length $buf); |
648 | my $w = syswrite $self->{fh}, $buf |
635 | my $w = syswrite $self->{fh}, $buf |
649 | or last; |
636 | or last; |
650 | $::written += $w; |
637 | $::written += $w; |
651 | $self->{written} += $w; |
638 | $self->{written} += $w; |
652 | $l += $r; |
639 | $l += $w; |
653 | } |
640 | } |
654 | |
641 | |
655 | close $fh; |
642 | close $fh; |
656 | } |
643 | } |
657 | } |
644 | } |
658 | |
645 | |
659 | 1; |
646 | 1 |
|
|
647 | |