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.49 by root, Wed Nov 28 03:39:46 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),
40 [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1], 41 (new Coro::Semaphore $MAX_TRANSFERS_LARGE),
41); 42);
42 43
43my @newcons; 44my @newcons;
44my @pool; 45my @pool;
45 46
152 or $self->err(500, "unable to decode peername"); 153 or $self->err(500, "unable to decode peername");
153 154
154 $self->{remote_addr} = inet_ntoa $iaddr; 155 $self->{remote_addr} = inet_ntoa $iaddr;
155 $self->{time} = $::NOW; 156 $self->{time} = $::NOW;
156 157
158 weaken ($Coro::current->{conn} = $self);
159
157 $::conns++; 160 $::conns++;
158 161
159 $self; 162 $self;
160} 163}
161 164
183 186
184sub response { 187sub response {
185 my ($self, $code, $msg, $hdr, $content) = @_; 188 my ($self, $code, $msg, $hdr, $content) = @_;
186 my $res = "HTTP/1.1 $code $msg\015\012"; 189 my $res = "HTTP/1.1 $code $msg\015\012";
187 190
191 if (exists $hdr->{Connection}) {
192 if ($hdr->{Connection} =~ /close/) {
188 $self->{h}{connection} = "close" 193 $self->{h}{connection} = "close"
189 if exists $hdr->{Connection} # to avoid "empty" header lines due to vivification 194 }
190 and $hdr->{Connection} =~ /close/; 195 } else {
196 if ($self->{version} < 1.1) {
197 if ($self->{h}{connection} =~ /keep-alive/i) {
198 $hdr->{Connection} = "Keep-Alive";
199 } else {
200 $self->{h}{connection} = "close"
201 }
202 }
203 }
191 204
192 $res .= "Date: $HTTP_NOW\015\012"; 205 $res .= "Date: $HTTP_NOW\015\012";
193 206
194 while (my ($h, $v) = each %$hdr) { 207 while (my ($h, $v) = each %$hdr) {
195 $res .= "$h: $v\015\012" 208 $res .= "$h: $v\015\012"
196 } 209 }
197 $res .= "\015\012"; 210 $res .= "\015\012";
198 211
199 $res .= $content if defined $content and $self->{method} ne "HEAD"; 212 $res .= $content if defined $content and $self->{method} ne "HEAD";
200 213
214 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"; 215 " $self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";
202 216
203 print $accesslog $log if $accesslog; 217 print $accesslog $log if $accesslog;
204 print STDERR $log; 218 print STDERR $log;
205 219
206 $self->{written} += 220 $self->{written} +=
340 354
341 $self->eoconn; 355 $self->eoconn;
342 356
343 die if $@ && !ref $@; 357 die if $@ && !ref $@;
344 358
345 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; 359 last if $self->{h}{connection} =~ /close/;
346 360
347 $httpevent->broadcast; 361 $httpevent->broadcast;
348 362
349 $fh->timeout($::PER_TIMEOUT); 363 $fh->timeout($::PER_TIMEOUT);
350 } 364 }
402 416
403sub respond { 417sub respond {
404 my $self = shift; 418 my $self = shift;
405 my $path = $self->{path}; 419 my $path = $self->{path};
406 420
407 stat $path 421 if ($self->{name} =~ s%^/internal/([^/]+)%%) {
408 or $self->err(404, "not found"); 422 if ($::internal{$1}) {
409 423 $::internal{$1}->($self);
410 $self->{stat} = [stat _];
411
412 # idiotic netscape sends idiotic headers AGAIN
413 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
414 ? str2time $1 : 0;
415
416 if (-d _ && -r _) {
417 # directory
418 if ($path !~ /\/$/) {
419 # create a redirect to get the trailing "/"
420 # we don't try to avoid the :80
421 $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" });
422 } else { 424 } else {
423 $ims < $self->{stat}[9] 425 $self->err(404, "not found");
426 }
427 } else {
428
429 stat $path
424 or $self->err(304, "not modified"); 430 or $self->err(404, "not found");
425 431
426 if (-r "$path/index.html") { 432 $self->{stat} = [stat _];
427 # replace directory "size" by index.html filesize 433
428 $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; 434 # idiotic netscape sends idiotic headers AGAIN
429 $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}/" });
430 } 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 {
431 $self->handle_dir; 453 $self->handle_dir;
432 } 454 }
433 } 455 }
434 } elsif (-f _ && -r _) { 456 } elsif (-f _ && -r _) {
435 -x _ and $self->err(403, "forbidden"); 457 -x _ and $self->err(403, "forbidden");
436 $self->handle_file; 458 $self->handle_file;
437 } else { 459 } else {
438 $self->err(404, "not found"); 460 $self->err(404, "not found");
461 }
439 } 462 }
440} 463}
441 464
442sub handle_dir { 465sub handle_dir {
443 my $self = shift; 466 my $self = shift;
445 468
446 $self->response(200, "ok", 469 $self->response(200, "ok",
447 { 470 {
448 "Content-Type" => "text/html", 471 "Content-Type" => "text/html",
449 "Content-Length" => length $idx, 472 "Content-Length" => length $idx,
473 "Last-Modified" => time2str ((stat _)[9]),
450 }, 474 },
451 $idx); 475 $idx);
452} 476}
453 477
454sub handle_file { 478sub handle_file {
484 my $delay = $::NOW + $::PER_TIMEOUT + 15; 508 my $delay = $::NOW + $::PER_TIMEOUT + 15;
485 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 509 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
486 if ($delay <= $::NOW) { 510 if ($delay <= $::NOW) {
487 $self->err_segmented_download; 511 $self->err_segmented_download;
488 } else { 512 } else {
489 $httpevent->broadcast; 513 $httpevent->wait;
490 } 514 }
491 } 515 }
492 } 516 }
493 517
494 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 518 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
507 $self->response(@code, $hdr, ""); 531 $self->response(@code, $hdr, "");
508 532
509 if ($self->{method} eq "GET") { 533 if ($self->{method} eq "GET") {
510 $self->{time} = $::NOW; 534 $self->{time} = $::NOW;
511 535
512 my $fudge = $queue->[0]->waiters; 536 my $current = $Coro::current;
513 $fudge = $fudge ? ($fudge+1)/$fudge : 1;
514
515 $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 537
527 my ($fh, $buf, $r); 538 my ($fh, $buf, $r);
528 my $current = $Coro::current; 539
529 open $fh, "<", $self->{path} 540 open $fh, "<", $self->{path}
530 or die "$self->{path}: late open failure ($!)"; 541 or die "$self->{path}: late open failure ($!)";
531 542
532 $h -= $l - 1; 543 $h -= $l - 1;
533 544
534 if (0) { 545 if (0) { # !AIO
535 if ($l) { 546 if ($l) {
536 sysseek $fh, $l, 0; 547 sysseek $fh, $l, 0;
537 } 548 }
538 } 549 }
550
551 my $transfer; # transfer guard
552 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
553
554 $self->{time} = $::NOW;
539 555
540 while ($h > 0) { 556 while ($h > 0) {
557 unless ($transfer) {
558 if ($transfer ||= $queue->timed_guard($::WAIT_INTERVAL)) {
559 $bufsize = $::BUFSIZE;
560 $self->{time} = $::NOW;
561 }
562 }
563
541 if (0) { 564 if (0) { # !AIO
542 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 565 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
543 or last; 566 or last;
544 } else { 567 } else {
545 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 568 aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h),
546 $buf, 0, sub { 569 $buf, 0, sub {
547 $r = $_[0]; 570 $r = $_[0];
548 Coro::ready($current); 571 Coro::ready($current);
549 }); 572 });
550 &Coro::schedule; 573 &Coro::schedule;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines