… | |
… | |
22 | our $accesslog; |
22 | our $accesslog; |
23 | our $errorlog; |
23 | our $errorlog; |
24 | |
24 | |
25 | our $NOW; |
25 | our $NOW; |
26 | our $HTTP_NOW; |
26 | our $HTTP_NOW; |
|
|
27 | |
|
|
28 | our $ERROR_LOG; |
|
|
29 | our $ACCESS_LOG; |
27 | |
30 | |
28 | Event->timer(interval => 1, hard => 1, cb => sub { |
31 | Event->timer(interval => 1, hard => 1, cb => sub { |
29 | $NOW = time; |
32 | $NOW = time; |
30 | $HTTP_NOW = time2str $NOW; |
33 | $HTTP_NOW = time2str $NOW; |
31 | })->now; |
34 | })->now; |
… | |
… | |
79 | # one "execution thread" |
82 | # one "execution thread" |
80 | sub handler { |
83 | sub handler { |
81 | while () { |
84 | while () { |
82 | if (@newcons) { |
85 | if (@newcons) { |
83 | eval { |
86 | eval { |
84 | conn->new(@{pop @newcons})->handle; |
87 | conn->new (@{pop @newcons})->handle; |
85 | }; |
88 | }; |
86 | slog 1, "$@" if $@ && !ref $@; |
89 | slog 1, "$@" if $@ && !ref $@; |
87 | |
90 | |
88 | $httpevent->broadcast; # only for testing, but doesn't matter much |
91 | $httpevent->broadcast; # only for testing, but doesn't matter much |
89 | |
92 | |
… | |
… | |
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 IO::AIO; |
151 | use IO::AIO; |
… | |
… | |
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 | |
196 | $::conns--; |
202 | --$::conns; |
197 | } |
203 | } |
198 | |
204 | |
199 | sub prune_cache { |
205 | sub prune_cache { |
200 | my $hash = $_[0]; |
206 | my $hash = $_[0]; |
201 | |
207 | |
202 | for (keys %$hash) { |
208 | for (keys %$hash) { |
203 | if (ref $hash->{$_} eq HASH::) { |
209 | if (ref $hash->{$_} eq HASH::) { |
204 | prune_cache($hash->{$_}); |
210 | prune_cache($hash->{$_}); |
205 | unless (scalar keys %{$hash->{$_}}) { |
211 | unless (scalar keys %{$hash->{$_}}) { |
206 | delete $hash->{$_}; |
212 | delete $hash->{$_}; |
207 | $d2++; |
|
|
208 | } |
213 | } |
209 | } |
214 | } |
210 | } |
215 | } |
211 | } |
216 | } |
212 | |
217 | |
… | |
… | |
217 | for (keys %blocked) { |
222 | for (keys %blocked) { |
218 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
223 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
219 | } |
224 | } |
220 | } |
225 | } |
221 | |
226 | |
222 | Event->timer(interval => 60, cb => \&prune_caches); |
227 | Event->timer (interval => 60, cb => \&prune_caches); |
223 | |
228 | |
224 | sub slog { |
229 | sub slog { |
225 | my $self = shift; |
230 | my $self = shift; |
226 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
231 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
227 | } |
232 | } |
… | |
… | |
288 | $hdr->{"Content-Type"} = "text/plain"; |
293 | $hdr->{"Content-Type"} = "text/plain"; |
289 | $hdr->{"Content-Length"} = length $content; |
294 | $hdr->{"Content-Length"} = length $content; |
290 | } |
295 | } |
291 | $hdr->{"Connection"} = "close"; |
296 | $hdr->{"Connection"} = "close"; |
292 | |
297 | |
293 | $self->response($code, $msg, $hdr, $content); |
298 | $self->response ($code, $msg, $hdr, $content); |
294 | |
299 | |
295 | die bless {}, err::; |
300 | die bless {}, err:: |
296 | } |
301 | } |
297 | |
302 | |
298 | sub handle { |
303 | sub handle { |
299 | my $self = shift; |
304 | my $self = shift; |
300 | my $fh = $self->{fh}; |
305 | my $fh = $self->{fh}; |
301 | |
306 | |
302 | my $host; |
307 | my $host; |
303 | |
308 | |
304 | $fh->timeout($::REQ_TIMEOUT); |
309 | $fh->timeout($::REQ_TIMEOUT); |
305 | while() { |
310 | while () { |
306 | $self->{reqs}++; |
311 | $self->{reqs}++; |
307 | |
312 | |
308 | # read request and parse first line |
313 | # read request and parse first line |
309 | my $req = $fh->readline("\015\012\015\012"); |
314 | my $req = $fh->readline("\015\012\015\012"); |
310 | |
315 | |
… | |
… | |
472 | |
477 | |
473 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
478 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
474 | if ($::internal{$1}) { |
479 | if ($::internal{$1}) { |
475 | $::internal{$1}->($self); |
480 | $::internal{$1}->($self); |
476 | } else { |
481 | } else { |
477 | $self->err(404, "not found"); |
482 | $self->err (404, "not found"); |
478 | } |
483 | } |
479 | } else { |
484 | } else { |
480 | |
485 | |
481 | stat $path |
486 | stat $path |
482 | or $self->err(404, "not found"); |
487 | or $self->err (404, "not found"); |
483 | |
488 | |
484 | $self->{stat} = [stat _]; |
489 | $self->{stat} = [stat _]; |
485 | |
490 | |
486 | # idiotic netscape sends idiotic headers AGAIN |
491 | # idiotic netscape sends idiotic headers AGAIN |
487 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
492 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
… | |
… | |
490 | if (-d _ && -r _) { |
495 | if (-d _ && -r _) { |
491 | # directory |
496 | # directory |
492 | if ($path !~ /\/$/) { |
497 | if ($path !~ /\/$/) { |
493 | # create a redirect to get the trailing "/" |
498 | # create a redirect to get the trailing "/" |
494 | # we don't try to avoid the :80 |
499 | # we don't try to avoid the :80 |
495 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
500 | $self->err (301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
496 | } else { |
501 | } else { |
497 | $ims < $self->{stat}[9] |
502 | $ims < $self->{stat}[9] |
498 | or $self->err(304, "not modified"); |
503 | or $self->err (304, "not modified"); |
499 | |
504 | |
500 | if (-r "$path/index.html") { |
505 | if (-r "$path/index.html") { |
501 | # replace directory "size" by index.html filesize |
506 | # replace directory "size" by index.html filesize |
502 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
507 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
503 | $self->handle_file($queue_index, $tbf_top); |
508 | $self->handle_file ($queue_index, $tbf_top); |
504 | } else { |
509 | } else { |
505 | $self->handle_dir; |
510 | $self->handle_dir; |
506 | } |
511 | } |
507 | } |
512 | } |
508 | } elsif (-f _ && -r _) { |
513 | } elsif (-f _ && -r _) { |
509 | -x _ and $self->err(403, "forbidden"); |
514 | -x _ and $self->err (403, "forbidden"); |
510 | |
515 | |
511 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
516 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
512 | my $timeout = $::NOW + 10; |
517 | my $timeout = $::NOW + 10; |
513 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
518 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
514 | if ($timeout < $::NOW) { |
519 | if ($timeout < $::NOW) { |
… | |
… | |
517 | $httpevent->wait; |
522 | $httpevent->wait; |
518 | } |
523 | } |
519 | } |
524 | } |
520 | } |
525 | } |
521 | |
526 | |
522 | $self->handle_file($queue_file, $tbf_top); |
527 | $self->handle_file ($queue_file, $tbf_top); |
523 | } else { |
528 | } else { |
524 | $self->err(404, "not found"); |
529 | $self->err (404, "not found"); |
525 | } |
530 | } |
526 | } |
531 | } |
527 | } |
532 | } |
528 | |
533 | |
529 | sub handle_dir { |
534 | sub handle_dir { |
530 | my $self = shift; |
535 | my $self = shift; |
531 | my $idx = $self->diridx; |
536 | my $idx = $self->diridx; |
532 | |
537 | |
533 | $self->response(200, "ok", |
538 | $self->response (200, "ok", |
534 | { |
539 | { |
535 | "Content-Type" => "text/html; charset=utf-8", |
540 | "Content-Type" => "text/html; charset=utf-8", |
536 | "Content-Length" => length $idx, |
541 | "Content-Length" => length $idx, |
537 | "Last-Modified" => time2str ($self->{stat}[9]), |
542 | "Last-Modified" => time2str ($self->{stat}[9]), |
538 | }, |
543 | }, |
… | |
… | |
562 | } |
567 | } |
563 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
568 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
564 | } |
569 | } |
565 | $hdr->{"Content-Range"} = "bytes */$length"; |
570 | $hdr->{"Content-Range"} = "bytes */$length"; |
566 | $hdr->{"Content-Length"} = $length; |
571 | $hdr->{"Content-Length"} = $length; |
567 | $self->err(416, "not satisfiable", $hdr, ""); |
572 | $self->err (416, "not satisfiable", $hdr, ""); |
568 | |
573 | |
569 | satisfiable: |
574 | satisfiable: |
570 | # check for segmented downloads |
575 | # check for segmented downloads |
571 | if ($l && $::NO_SEGMENTED) { |
576 | if ($l && $::NO_SEGMENTED) { |
572 | my $timeout = $::NOW + 15; |
577 | my $timeout = $::NOW + 15; |
573 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
578 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
574 | if ($timeout <= $::NOW) { |
579 | if ($timeout <= $::NOW) { |
575 | $self->block($::BLOCKTIME, "segmented downloads are forbidden"); |
580 | $self->block ($::BLOCKTIME, "segmented downloads are forbidden"); |
576 | #$self->err_segmented_download; |
581 | #$self->err_segmented_download; |
577 | } else { |
582 | } else { |
578 | $httpevent->wait; |
583 | $httpevent->wait; |
579 | } |
584 | } |
580 | } |
585 | } |
… | |
… | |
591 | |
596 | |
592 | $self->{path} =~ /\.([^.]+)$/; |
597 | $self->{path} =~ /\.([^.]+)$/; |
593 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
598 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
594 | $hdr->{"Content-Length"} = $length; |
599 | $hdr->{"Content-Length"} = $length; |
595 | |
600 | |
596 | $self->response(@code, $hdr, ""); |
601 | $self->response (@code, $hdr, ""); |
597 | |
602 | |
598 | if ($self->{method} eq "GET") { |
603 | if ($self->{method} eq "GET") { |
599 | $self->{time} = $::NOW; |
604 | $self->{time} = $::NOW; |
600 | $self->{written} = 0; |
605 | $self->{written} = 0; |
601 | |
606 | |
602 | my $current = $Coro::current; |
|
|
603 | |
|
|
604 | my ($fh, $buf, $r); |
|
|
605 | |
|
|
606 | open $fh, "<", $self->{path} |
607 | open my $fh, "<", $self->{path} |
607 | or die "$self->{path}: late open failure ($!)"; |
608 | or die "$self->{path}: late open failure ($!)"; |
608 | |
609 | |
609 | $h -= $l - 1; |
610 | $h -= $l - 1; |
610 | |
611 | |
611 | if (0) { # !AIO |
|
|
612 | if ($l) { |
|
|
613 | sysseek $fh, $l, 0; |
|
|
614 | } |
|
|
615 | } |
|
|
616 | |
|
|
617 | my $transfer = $queue->start_transfer($h); |
612 | my $transfer = $queue->start_transfer ($h); |
618 | my $locked; |
613 | my $locked; |
619 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
614 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
620 | |
615 | |
621 | while ($h > 0) { |
616 | while ($h > 0) { |
622 | unless ($locked) { |
617 | unless ($locked) { |
623 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
618 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
624 | $bufsize = $::BUFSIZE; |
619 | $bufsize = $::BUFSIZE; |
625 | $self->{time} = $::NOW; |
620 | $self->{time} = $::NOW; |
626 | $self->{written} = 0; |
621 | $self->{written} = 0; |
627 | } |
622 | } |
628 | } |
623 | } |
… | |
… | |
630 | if ($blocked{$self->{remote_id}}) { |
625 | if ($blocked{$self->{remote_id}}) { |
631 | $self->{h}{connection} = "close"; |
626 | $self->{h}{connection} = "close"; |
632 | die bless {}, err::; |
627 | die bless {}, err::; |
633 | } |
628 | } |
634 | |
629 | |
635 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), $buf, 0 |
630 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
636 | or last; |
631 | or last; |
637 | |
632 | |
638 | $tbf->request (length $buf); |
633 | $tbf->request (length $buf); |
639 | my $w = syswrite $self->{fh}, $buf |
634 | my $w = syswrite $self->{fh}, $buf |
640 | or last; |
635 | or last; |
641 | $::written += $w; |
636 | $::written += $w; |
642 | $self->{written} += $w; |
637 | $self->{written} += $w; |
643 | $l += $r; |
638 | $l += $w; |
644 | } |
639 | } |
645 | |
640 | |
646 | close $fh; |
641 | close $fh; |
647 | } |
642 | } |
648 | } |
643 | } |