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