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.44 by root, Fri Sep 14 12:38:18 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
188 $self->{h}{connection} = "close" if $hdr->{Connection} =~ /close/; 189 if (exists $hdr->{Connection}) {
190 if ($hdr->{Connection} =~ /close/) {
191 $self->{h}{connection} = "close"
192 }
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 }
189 202
190 $res .= "Date: $HTTP_NOW\015\012"; 203 $res .= "Date: $HTTP_NOW\015\012";
191 204
192 while (my ($h, $v) = each %$hdr) { 205 while (my ($h, $v) = each %$hdr) {
193 $res .= "$h: $v\015\012" 206 $res .= "$h: $v\015\012"
194 } 207 }
195 $res .= "\015\012"; 208 $res .= "\015\012";
196 209
197 $res .= $content if defined $content and $self->{method} ne "HEAD"; 210 $res .= $content if defined $content and $self->{method} ne "HEAD";
198 211
212 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW).
199 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";
200 214
201 print $accesslog $log if $accesslog; 215 print $accesslog $log if $accesslog;
202 print STDERR $log; 216 print STDERR $log;
203 217
204 $self->{written} += 218 $self->{written} +=
338 352
339 $self->eoconn; 353 $self->eoconn;
340 354
341 die if $@ && !ref $@; 355 die if $@ && !ref $@;
342 356
343 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; 357 last if $self->{h}{connection} =~ /close/;
344 358
345 $httpevent->broadcast; 359 $httpevent->broadcast;
346 360
347 $fh->timeout($::PER_TIMEOUT); 361 $fh->timeout($::PER_TIMEOUT);
348 } 362 }
420 } else { 434 } else {
421 $ims < $self->{stat}[9] 435 $ims < $self->{stat}[9]
422 or $self->err(304, "not modified"); 436 or $self->err(304, "not modified");
423 437
424 if (-r "$path/index.html") { 438 if (-r "$path/index.html") {
425 $self->{path} .= "/index.html"; 439 # replace directory "size" by index.html filesize
440 $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7];
426 $self->handle_file; 441 $self->handle_file;
427 } else { 442 } else {
428 $self->handle_dir; 443 $self->handle_dir;
429 } 444 }
430 } 445 }
442 457
443 $self->response(200, "ok", 458 $self->response(200, "ok",
444 { 459 {
445 "Content-Type" => "text/html", 460 "Content-Type" => "text/html",
446 "Content-Length" => length $idx, 461 "Content-Length" => length $idx,
462 "Last-Modified" => time2str ((stat _)[9]),
447 }, 463 },
448 $idx); 464 $idx);
449} 465}
450 466
451sub handle_file { 467sub handle_file {
481 my $delay = $::NOW + $::PER_TIMEOUT + 15; 497 my $delay = $::NOW + $::PER_TIMEOUT + 15;
482 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 498 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
483 if ($delay <= $::NOW) { 499 if ($delay <= $::NOW) {
484 $self->err_segmented_download; 500 $self->err_segmented_download;
485 } else { 501 } else {
486 $httpevent->broadcast; 502 $httpevent->wait;
487 } 503 }
488 } 504 }
489 } 505 }
490 506
491 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 507 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
508 524
509 my $fudge = $queue->[0]->waiters; 525 my $fudge = $queue->[0]->waiters;
510 $fudge = $fudge ? ($fudge+1)/$fudge : 1; 526 $fudge = $fudge ? ($fudge+1)/$fudge : 1;
511 527
512 $queue->[1] *= $fudge; 528 $queue->[1] *= $fudge;
513 my $transfer = $queue->[0]->guard;
514
515 if ($fudge != 1) {
516 $queue->[1] /= $fudge;
517 $queue->[1] = $queue->[1] * $::wait_factor
518 + ($::NOW - $self->{time}) * (1 - $::wait_factor);
519 }
520 $self->{time} = $::NOW;
521
522 $self->{fh}->writable or return;
523 529
524 my ($fh, $buf, $r); 530 my ($fh, $buf, $r);
525 my $current = $Coro::current; 531 my $current = $Coro::current;
526 open $fh, "<", $self->{path} 532 open $fh, "<", $self->{path}
527 or die "$self->{path}: late open failure ($!)"; 533 or die "$self->{path}: late open failure ($!)";
531 if (0) { 537 if (0) {
532 if ($l) { 538 if ($l) {
533 sysseek $fh, $l, 0; 539 sysseek $fh, $l, 0;
534 } 540 }
535 } 541 }
542
543 my $transfer; # transfer guard
544 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
536 545
537 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
538 if (0) { 559 if (0) {
539 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 560 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
540 or last; 561 or last;
541 } else { 562 } else {
542 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 563 aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h),
543 $buf, 0, sub { 564 $buf, 0, sub {
544 $r = $_[0]; 565 $r = $_[0];
545 Coro::ready($current); 566 Coro::ready($current);
546 }); 567 });
547 &Coro::schedule; 568 &Coro::schedule;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines