… | |
… | |
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 @transfers = ( |
40 | [(new Coro::Semaphore $MAX_TRANSFERS_SMALL), 1], |
40 | (new Coro::Semaphore $MAX_TRANSFERS_SMALL), |
41 | [(new Coro::Semaphore $MAX_TRANSFERS_LARGE), 1], |
41 | (new Coro::Semaphore $MAX_TRANSFERS_LARGE), |
42 | ); |
42 | ); |
43 | |
43 | |
44 | my @newcons; |
44 | my @newcons; |
45 | my @pool; |
45 | my @pool; |
46 | |
46 | |
… | |
… | |
153 | or $self->err(500, "unable to decode peername"); |
153 | or $self->err(500, "unable to decode peername"); |
154 | |
154 | |
155 | $self->{remote_addr} = inet_ntoa $iaddr; |
155 | $self->{remote_addr} = inet_ntoa $iaddr; |
156 | $self->{time} = $::NOW; |
156 | $self->{time} = $::NOW; |
157 | |
157 | |
|
|
158 | weaken ($Coro::current->{conn} = $self); |
|
|
159 | |
158 | $::conns++; |
160 | $::conns++; |
159 | |
161 | |
160 | $self; |
162 | $self; |
161 | } |
163 | } |
162 | |
164 | |
… | |
… | |
414 | |
416 | |
415 | sub respond { |
417 | sub respond { |
416 | my $self = shift; |
418 | my $self = shift; |
417 | my $path = $self->{path}; |
419 | my $path = $self->{path}; |
418 | |
420 | |
419 | stat $path |
421 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
420 | or $self->err(404, "not found"); |
422 | if ($::internal{$1}) { |
421 | |
423 | $::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 { |
424 | } else { |
435 | $ims < $self->{stat}[9] |
425 | $self->err(404, "not found"); |
|
|
426 | } |
|
|
427 | } else { |
|
|
428 | |
|
|
429 | stat $path |
436 | or $self->err(304, "not modified"); |
430 | or $self->err(404, "not found"); |
437 | |
431 | |
438 | if (-r "$path/index.html") { |
432 | $self->{stat} = [stat _]; |
439 | # replace directory "size" by index.html filesize |
433 | |
440 | $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; |
434 | # idiotic netscape sends idiotic headers AGAIN |
441 | $self->handle_file; |
435 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
|
|
436 | ? str2time $1 : 0; |
|
|
437 | |
|
|
438 | if (-d _ && -r _) { |
|
|
439 | # directory |
|
|
440 | if ($path !~ /\/$/) { |
|
|
441 | # create a redirect to get the trailing "/" |
|
|
442 | # we don't try to avoid the :80 |
|
|
443 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
442 | } else { |
444 | } else { |
|
|
445 | $ims < $self->{stat}[9] |
|
|
446 | or $self->err(304, "not modified"); |
|
|
447 | |
|
|
448 | if (-r "$path/index.html") { |
|
|
449 | # replace directory "size" by index.html filesize |
|
|
450 | $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; |
|
|
451 | $self->handle_file; |
|
|
452 | } else { |
443 | $self->handle_dir; |
453 | $self->handle_dir; |
444 | } |
454 | } |
445 | } |
455 | } |
446 | } elsif (-f _ && -r _) { |
456 | } elsif (-f _ && -r _) { |
447 | -x _ and $self->err(403, "forbidden"); |
457 | -x _ and $self->err(403, "forbidden"); |
448 | $self->handle_file; |
458 | $self->handle_file; |
449 | } else { |
459 | } else { |
450 | $self->err(404, "not found"); |
460 | $self->err(404, "not found"); |
|
|
461 | } |
451 | } |
462 | } |
452 | } |
463 | } |
453 | |
464 | |
454 | sub handle_dir { |
465 | sub handle_dir { |
455 | my $self = shift; |
466 | my $self = shift; |
… | |
… | |
520 | $self->response(@code, $hdr, ""); |
531 | $self->response(@code, $hdr, ""); |
521 | |
532 | |
522 | if ($self->{method} eq "GET") { |
533 | if ($self->{method} eq "GET") { |
523 | $self->{time} = $::NOW; |
534 | $self->{time} = $::NOW; |
524 | |
535 | |
525 | my $fudge = $queue->[0]->waiters; |
536 | my $current = $Coro::current; |
526 | $fudge = $fudge ? ($fudge+1)/$fudge : 1; |
|
|
527 | |
|
|
528 | $queue->[1] *= $fudge; |
|
|
529 | |
537 | |
530 | my ($fh, $buf, $r); |
538 | my ($fh, $buf, $r); |
531 | my $current = $Coro::current; |
539 | |
532 | open $fh, "<", $self->{path} |
540 | open $fh, "<", $self->{path} |
533 | or die "$self->{path}: late open failure ($!)"; |
541 | or die "$self->{path}: late open failure ($!)"; |
534 | |
542 | |
535 | $h -= $l - 1; |
543 | $h -= $l - 1; |
536 | |
544 | |
537 | if (0) { |
545 | if (0) { # !AIO |
538 | if ($l) { |
546 | if ($l) { |
539 | sysseek $fh, $l, 0; |
547 | sysseek $fh, $l, 0; |
540 | } |
548 | } |
541 | } |
549 | } |
542 | |
550 | |
543 | my $transfer; # transfer guard |
551 | my $transfer; # transfer guard |
544 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
552 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
545 | |
553 | |
|
|
554 | $self->{time} = $::NOW; |
|
|
555 | |
546 | while ($h > 0) { |
556 | while ($h > 0) { |
547 | unless ($transfer) { |
557 | unless ($transfer) { |
548 | if ($transfer ||= $queue->[0]->timed_guard($::WAIT_INTERVAL)) { |
558 | if ($transfer ||= $queue->timed_guard($::WAIT_INTERVAL)) { |
549 | if ($fudge != 1) { |
|
|
550 | $queue->[1] /= $fudge; |
|
|
551 | $queue->[1] = $queue->[1] * $::wait_factor |
|
|
552 | + ($::NOW - $self->{time}) * (1 - $::wait_factor); |
|
|
553 | } |
|
|
554 | $bufsize = $::BUFSIZE; |
559 | $bufsize = $::BUFSIZE; |
555 | $self->{time} = $::NOW; |
560 | $self->{time} = $::NOW; |
556 | } |
561 | } |
557 | } |
562 | } |
558 | |
563 | |
559 | if (0) { |
564 | if (0) { # !AIO |
560 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
565 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
561 | or last; |
566 | or last; |
562 | } else { |
567 | } else { |
563 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
568 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
564 | $buf, 0, sub { |
569 | $buf, 0, sub { |