ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/httpd.pl
(Generate patch)

Comparing cvsroot/Coro/myhttpd/httpd.pl (file contents):
Revision 1.45 by root, Sun Nov 11 03:32:19 2001 UTC vs.
Revision 1.48 by root, Tue Nov 27 03:38:08 2001 UTC

3use Coro::Event; 3use Coro::Event;
4use Coro::Socket; 4use Coro::Socket;
5use Coro::Signal; 5use Coro::Signal;
6 6
7use HTTP::Date; 7use HTTP::Date;
8use POSIX ();
8 9
9no utf8; 10no utf8;
10use bytes; 11use bytes;
11 12
12# at least on my machine, this thingy serves files 13# at least on my machine, this thingy serves files
34our $httpevent = new Coro::Signal; 35our $httpevent = new Coro::Signal;
35 36
36our $wait_factor = 0.95; 37our $wait_factor = 0.95;
37 38
38our @transfers = ( 39our @transfers = (
39 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], 40 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL), 1],
40 [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1], 41 [(new Coro::Semaphore $MAX_TRANSFERS_LARGE), 1],
41); 42);
42 43
43my @newcons; 44my @newcons;
44my @pool; 45my @pool;
45 46
183 184
184sub response { 185sub response {
185 my ($self, $code, $msg, $hdr, $content) = @_; 186 my ($self, $code, $msg, $hdr, $content) = @_;
186 my $res = "HTTP/1.1 $code $msg\015\012"; 187 my $res = "HTTP/1.1 $code $msg\015\012";
187 188
189 if (exists $hdr->{Connection}) {
190 if ($hdr->{Connection} =~ /close/) {
188 $self->{h}{connection} = "close" 191 $self->{h}{connection} = "close"
189 if exists $hdr->{Connection} # to avoid "empty" header lines due to vivification 192 }
190 and $hdr->{Connection} =~ /close/; 193 } else {
194 if ($self->{version} < 1.1) {
195 if ($self->{h}{connection} =~ /keep-alive/i) {
196 $hdr->{Connection} = "Keep-Alive";
197 } else {
198 $self->{h}{connection} = "close"
199 }
200 }
201 }
191 202
192 $res .= "Date: $HTTP_NOW\015\012"; 203 $res .= "Date: $HTTP_NOW\015\012";
193 204
194 while (my ($h, $v) = each %$hdr) { 205 while (my ($h, $v) = each %$hdr) {
195 $res .= "$h: $v\015\012" 206 $res .= "$h: $v\015\012"
196 } 207 }
197 $res .= "\015\012"; 208 $res .= "\015\012";
198 209
199 $res .= $content if defined $content and $self->{method} ne "HEAD"; 210 $res .= $content if defined $content and $self->{method} ne "HEAD";
200 211
212 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW).
201 my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; 213 " $self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";
202 214
203 print $accesslog $log if $accesslog; 215 print $accesslog $log if $accesslog;
204 print STDERR $log; 216 print STDERR $log;
205 217
206 $self->{written} += 218 $self->{written} +=
340 352
341 $self->eoconn; 353 $self->eoconn;
342 354
343 die if $@ && !ref $@; 355 die if $@ && !ref $@;
344 356
345 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; 357 last if $self->{h}{connection} =~ /close/;
346 358
347 $httpevent->broadcast; 359 $httpevent->broadcast;
348 360
349 $fh->timeout($::PER_TIMEOUT); 361 $fh->timeout($::PER_TIMEOUT);
350 } 362 }
445 457
446 $self->response(200, "ok", 458 $self->response(200, "ok",
447 { 459 {
448 "Content-Type" => "text/html", 460 "Content-Type" => "text/html",
449 "Content-Length" => length $idx, 461 "Content-Length" => length $idx,
462 "Last-Modified" => time2str ((stat _)[9]),
450 }, 463 },
451 $idx); 464 $idx);
452} 465}
453 466
454sub handle_file { 467sub handle_file {
484 my $delay = $::NOW + $::PER_TIMEOUT + 15; 497 my $delay = $::NOW + $::PER_TIMEOUT + 15;
485 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 498 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
486 if ($delay <= $::NOW) { 499 if ($delay <= $::NOW) {
487 $self->err_segmented_download; 500 $self->err_segmented_download;
488 } else { 501 } else {
489 $httpevent->broadcast; 502 $httpevent->wait;
490 } 503 }
491 } 504 }
492 } 505 }
493 506
494 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 507 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
511 524
512 my $fudge = $queue->[0]->waiters; 525 my $fudge = $queue->[0]->waiters;
513 $fudge = $fudge ? ($fudge+1)/$fudge : 1; 526 $fudge = $fudge ? ($fudge+1)/$fudge : 1;
514 527
515 $queue->[1] *= $fudge; 528 $queue->[1] *= $fudge;
516 my $transfer = $queue->[0]->guard;
517
518 if ($fudge != 1) {
519 $queue->[1] /= $fudge;
520 $queue->[1] = $queue->[1] * $::wait_factor
521 + ($::NOW - $self->{time}) * (1 - $::wait_factor);
522 }
523 $self->{time} = $::NOW;
524
525 $self->{fh}->writable or return;
526 529
527 my ($fh, $buf, $r); 530 my ($fh, $buf, $r);
528 my $current = $Coro::current; 531 my $current = $Coro::current;
529 open $fh, "<", $self->{path} 532 open $fh, "<", $self->{path}
530 or die "$self->{path}: late open failure ($!)"; 533 or die "$self->{path}: late open failure ($!)";
534 if (0) { 537 if (0) {
535 if ($l) { 538 if ($l) {
536 sysseek $fh, $l, 0; 539 sysseek $fh, $l, 0;
537 } 540 }
538 } 541 }
542
543 my $transfer; # transfer guard
544 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
539 545
540 while ($h > 0) { 546 while ($h > 0) {
547 unless ($transfer) {
548 if ($transfer ||= $queue->[0]->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;
555 $self->{time} = $::NOW;
556 }
557 }
558
541 if (0) { 559 if (0) {
542 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 560 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
543 or last; 561 or last;
544 } else { 562 } else {
545 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 563 aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h),
546 $buf, 0, sub { 564 $buf, 0, sub {
547 $r = $_[0]; 565 $r = $_[0];
548 Coro::ready($current); 566 Coro::ready($current);
549 }); 567 });
550 &Coro::schedule; 568 &Coro::schedule;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines