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