… | |
… | |
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 | |
6 | |
7 | use HTTP::Date; |
7 | use HTTP::Date; |
|
|
8 | use POSIX (); |
8 | |
9 | |
9 | no utf8; |
10 | no utf8; |
10 | use bytes; |
11 | use bytes; |
11 | |
12 | |
12 | # at least on my machine, this thingy serves files |
13 | # at least on my machine, this thingy serves files |
… | |
… | |
33 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
34 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
34 | our $httpevent = new Coro::Signal; |
35 | our $httpevent = new Coro::Signal; |
35 | |
36 | |
36 | our $wait_factor = 0.95; |
37 | our $wait_factor = 0.95; |
37 | |
38 | |
38 | our @transfers = ( |
39 | our $queue_small = new transferqueue $MAX_TRANSFERS_SMALL; |
39 | [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], |
40 | our $queue_large = new transferqueue $MAX_TRANSFERS_LARGE; |
40 | [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1], |
41 | our $queue_index = new transferqueue 5; |
41 | ); |
|
|
42 | |
42 | |
43 | my @newcons; |
43 | my @newcons; |
44 | my @pool; |
44 | my @pool; |
45 | |
45 | |
46 | # one "execution thread" |
46 | # one "execution thread" |
… | |
… | |
149 | my $peername = shift; |
149 | my $peername = shift; |
150 | my $self = bless { fh => $fh }, $class; |
150 | my $self = bless { fh => $fh }, $class; |
151 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
151 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
152 | or $self->err(500, "unable to decode peername"); |
152 | or $self->err(500, "unable to decode peername"); |
153 | |
153 | |
|
|
154 | $self->{remote_addr} = |
154 | $self->{remote_addr} = inet_ntoa $iaddr; |
155 | $self->{remote_id} = inet_ntoa $iaddr; |
155 | $self->{time} = $::NOW; |
156 | $self->{time} = $::NOW; |
|
|
157 | |
|
|
158 | weaken ($Coro::current->{conn} = $self); |
156 | |
159 | |
157 | $::conns++; |
160 | $::conns++; |
158 | |
161 | |
159 | $self; |
162 | $self; |
160 | } |
163 | } |
… | |
… | |
176 | $httpevent->broadcast; |
179 | $httpevent->broadcast; |
177 | } |
180 | } |
178 | |
181 | |
179 | sub slog { |
182 | sub slog { |
180 | my $self = shift; |
183 | my $self = shift; |
181 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
184 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
182 | } |
185 | } |
183 | |
186 | |
184 | sub response { |
187 | sub response { |
185 | my ($self, $code, $msg, $hdr, $content) = @_; |
188 | my ($self, $code, $msg, $hdr, $content) = @_; |
186 | my $res = "HTTP/1.1 $code $msg\015\012"; |
189 | my $res = "HTTP/1.1 $code $msg\015\012"; |
… | |
… | |
206 | } |
209 | } |
207 | $res .= "\015\012"; |
210 | $res .= "\015\012"; |
208 | |
211 | |
209 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
212 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
210 | |
213 | |
211 | my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
214 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). |
|
|
215 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. |
|
|
216 | " \"$self->{h}{referer}\"\n"; |
212 | |
217 | |
213 | print $accesslog $log if $accesslog; |
218 | print $accesslog $log if $accesslog; |
214 | print STDERR $log; |
219 | print STDERR $log; |
215 | |
220 | |
216 | $self->{written} += |
221 | $self->{written} += |
… | |
… | |
412 | |
417 | |
413 | sub respond { |
418 | sub respond { |
414 | my $self = shift; |
419 | my $self = shift; |
415 | my $path = $self->{path}; |
420 | my $path = $self->{path}; |
416 | |
421 | |
417 | stat $path |
422 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
418 | or $self->err(404, "not found"); |
423 | if ($::internal{$1}) { |
419 | |
424 | $::internal{$1}->($self); |
420 | $self->{stat} = [stat _]; |
|
|
421 | |
|
|
422 | # idiotic netscape sends idiotic headers AGAIN |
|
|
423 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
|
|
424 | ? str2time $1 : 0; |
|
|
425 | |
|
|
426 | if (-d _ && -r _) { |
|
|
427 | # directory |
|
|
428 | if ($path !~ /\/$/) { |
|
|
429 | # create a redirect to get the trailing "/" |
|
|
430 | # we don't try to avoid the :80 |
|
|
431 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
|
|
432 | } else { |
425 | } else { |
433 | $ims < $self->{stat}[9] |
426 | $self->err(404, "not found"); |
|
|
427 | } |
|
|
428 | } else { |
|
|
429 | |
|
|
430 | stat $path |
434 | or $self->err(304, "not modified"); |
431 | or $self->err(404, "not found"); |
435 | |
432 | |
436 | if (-r "$path/index.html") { |
433 | $self->{stat} = [stat _]; |
437 | # replace directory "size" by index.html filesize |
434 | |
438 | $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; |
435 | # idiotic netscape sends idiotic headers AGAIN |
439 | $self->handle_file; |
436 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
|
|
437 | ? str2time $1 : 0; |
|
|
438 | |
|
|
439 | if (-d _ && -r _) { |
|
|
440 | # directory |
|
|
441 | if ($path !~ /\/$/) { |
|
|
442 | # create a redirect to get the trailing "/" |
|
|
443 | # we don't try to avoid the :80 |
|
|
444 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
440 | } else { |
445 | } else { |
|
|
446 | $ims < $self->{stat}[9] |
|
|
447 | or $self->err(304, "not modified"); |
|
|
448 | |
|
|
449 | if (-r "$path/index.html") { |
|
|
450 | # replace directory "size" by index.html filesize |
|
|
451 | $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; |
|
|
452 | $self->handle_file($queue_index); |
|
|
453 | } else { |
441 | $self->handle_dir; |
454 | $self->handle_dir; |
442 | } |
455 | } |
443 | } |
456 | } |
444 | } elsif (-f _ && -r _) { |
457 | } elsif (-f _ && -r _) { |
445 | -x _ and $self->err(403, "forbidden"); |
458 | -x _ and $self->err(403, "forbidden"); |
446 | $self->handle_file; |
459 | $self->handle_file(-s _ >= $::TRANSFER_SMALL ? $queue_large : $queue_small); |
447 | } else { |
460 | } else { |
448 | $self->err(404, "not found"); |
461 | $self->err(404, "not found"); |
|
|
462 | } |
449 | } |
463 | } |
450 | } |
464 | } |
451 | |
465 | |
452 | sub handle_dir { |
466 | sub handle_dir { |
453 | my $self = shift; |
467 | my $self = shift; |
… | |
… | |
455 | |
469 | |
456 | $self->response(200, "ok", |
470 | $self->response(200, "ok", |
457 | { |
471 | { |
458 | "Content-Type" => "text/html", |
472 | "Content-Type" => "text/html", |
459 | "Content-Length" => length $idx, |
473 | "Content-Length" => length $idx, |
|
|
474 | #d# directories change all the time, so X- |
|
|
475 | "X-Last-Modified" => time2str ((stat _)[9]), |
460 | }, |
476 | }, |
461 | $idx); |
477 | $idx); |
462 | } |
478 | } |
463 | |
479 | |
464 | sub handle_file { |
480 | sub handle_file { |
465 | my $self = shift; |
481 | my ($self, $queue) = @_; |
466 | my $length = $self->{stat}[7]; |
482 | my $length = $self->{stat}[7]; |
467 | my $queue = $::transfers[$length >= $::TRANSFER_SMALL]; |
|
|
468 | my $hdr = { |
483 | my $hdr = { |
469 | "Last-Modified" => time2str ((stat _)[9]), |
484 | "Last-Modified" => time2str ((stat _)[9]), |
470 | }; |
485 | }; |
471 | |
486 | |
472 | my @code = (200, "ok"); |
487 | my @code = (200, "ok"); |
… | |
… | |
517 | $self->response(@code, $hdr, ""); |
532 | $self->response(@code, $hdr, ""); |
518 | |
533 | |
519 | if ($self->{method} eq "GET") { |
534 | if ($self->{method} eq "GET") { |
520 | $self->{time} = $::NOW; |
535 | $self->{time} = $::NOW; |
521 | |
536 | |
522 | my $fudge = $queue->[0]->waiters; |
537 | my $current = $Coro::current; |
523 | $fudge = $fudge ? ($fudge+1)/$fudge : 1; |
|
|
524 | |
|
|
525 | $queue->[1] *= $fudge; |
|
|
526 | my $transfer = $queue->[0]->guard; |
|
|
527 | |
|
|
528 | if ($fudge != 1) { |
|
|
529 | $queue->[1] /= $fudge; |
|
|
530 | $queue->[1] = $queue->[1] * $::wait_factor |
|
|
531 | + ($::NOW - $self->{time}) * (1 - $::wait_factor); |
|
|
532 | } |
|
|
533 | $self->{time} = $::NOW; |
|
|
534 | |
|
|
535 | $self->{fh}->writable or return; |
|
|
536 | |
538 | |
537 | my ($fh, $buf, $r); |
539 | my ($fh, $buf, $r); |
538 | my $current = $Coro::current; |
540 | |
539 | open $fh, "<", $self->{path} |
541 | open $fh, "<", $self->{path} |
540 | or die "$self->{path}: late open failure ($!)"; |
542 | or die "$self->{path}: late open failure ($!)"; |
541 | |
543 | |
542 | $h -= $l - 1; |
544 | $h -= $l - 1; |
543 | |
545 | |
544 | if (0) { |
546 | if (0) { # !AIO |
545 | if ($l) { |
547 | if ($l) { |
546 | sysseek $fh, $l, 0; |
548 | sysseek $fh, $l, 0; |
547 | } |
549 | } |
548 | } |
550 | } |
|
|
551 | |
|
|
552 | my $transfer = $queue->start_transfer; |
|
|
553 | my $locked; |
|
|
554 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
549 | |
555 | |
550 | while ($h > 0) { |
556 | while ($h > 0) { |
|
|
557 | unless ($locked) { |
|
|
558 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
|
|
559 | $bufsize = $::BUFSIZE; |
|
|
560 | $self->{time} = $::NOW; |
|
|
561 | } |
|
|
562 | } |
|
|
563 | |
551 | if (0) { |
564 | if (0) { # !AIO |
552 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
565 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
553 | or last; |
566 | or last; |
554 | } else { |
567 | } else { |
555 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
568 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
556 | $buf, 0, sub { |
569 | $buf, 0, sub { |
557 | $r = $_[0]; |
570 | $r = $_[0]; |
558 | Coro::ready($current); |
571 | Coro::ready($current); |
559 | }); |
572 | }); |
560 | &Coro::schedule; |
573 | &Coro::schedule; |