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

Comparing Coro/myhttpd/httpd.pl (file contents):
Revision 1.47 by root, Tue Nov 20 01:56:21 2001 UTC vs.
Revision 1.51 by root, Thu Nov 29 01:50:41 2001 UTC

35our $httpevent = new Coro::Signal; 35our $httpevent = new Coro::Signal;
36 36
37our $wait_factor = 0.95; 37our $wait_factor = 0.95;
38 38
39our @transfers = ( 39our @transfers = (
40 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], 40 (new transferqueue $MAX_TRANSFERS_SMALL),
41 [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1], 41 (new transferqueue $MAX_TRANSFERS_LARGE),
42); 42);
43 43
44my @newcons; 44my @newcons;
45my @pool; 45my @pool;
46 46
150 my $peername = shift; 150 my $peername = shift;
151 my $self = bless { fh => $fh }, $class; 151 my $self = bless { fh => $fh }, $class;
152 my (undef, $iaddr) = unpack_sockaddr_in $peername 152 my (undef, $iaddr) = unpack_sockaddr_in $peername
153 or $self->err(500, "unable to decode peername"); 153 or $self->err(500, "unable to decode peername");
154 154
155 $self->{remote_addr} =
155 $self->{remote_addr} = inet_ntoa $iaddr; 156 $self->{remote_id} = inet_ntoa $iaddr;
156 $self->{time} = $::NOW; 157 $self->{time} = $::NOW;
158
159 weaken ($Coro::current->{conn} = $self);
157 160
158 $::conns++; 161 $::conns++;
159 162
160 $self; 163 $self;
161} 164}
177 $httpevent->broadcast; 180 $httpevent->broadcast;
178} 181}
179 182
180sub slog { 183sub 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
185sub response { 188sub 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
415sub respond { 419sub 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;
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;
449 } else { 461 } else {
450 $self->err(404, "not found"); 462 $self->err(404, "not found");
463 }
451 } 464 }
452} 465}
453 466
454sub handle_dir { 467sub 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
467sub handle_file { 481sub handle_file {
520 $self->response(@code, $hdr, ""); 534 $self->response(@code, $hdr, "");
521 535
522 if ($self->{method} eq "GET") { 536 if ($self->{method} eq "GET") {
523 $self->{time} = $::NOW; 537 $self->{time} = $::NOW;
524 538
525 my $fudge = $queue->[0]->waiters; 539 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 540
540 my ($fh, $buf, $r); 541 my ($fh, $buf, $r);
541 my $current = $Coro::current; 542
542 open $fh, "<", $self->{path} 543 open $fh, "<", $self->{path}
543 or die "$self->{path}: late open failure ($!)"; 544 or die "$self->{path}: late open failure ($!)";
544 545
545 $h -= $l - 1; 546 $h -= $l - 1;
546 547
547 if (0) { 548 if (0) { # !AIO
548 if ($l) { 549 if ($l) {
549 sysseek $fh, $l, 0; 550 sysseek $fh, $l, 0;
550 } 551 }
551 } 552 }
553
554 my $transfer = $queue->start_transfer;
555 my $locked;
556 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
552 557
553 while ($h > 0) { 558 while ($h > 0) {
559 unless ($locked) {
560 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) {
561 $bufsize = $::BUFSIZE;
562 $self->{time} = $::NOW;
563 }
564 }
565
554 if (0) { 566 if (0) { # !AIO
555 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 567 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
556 or last; 568 or last;
557 } else { 569 } else {
558 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 570 aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h),
559 $buf, 0, sub { 571 $buf, 0, sub {
560 $r = $_[0]; 572 $r = $_[0];
561 Coro::ready($current); 573 Coro::ready($current);
562 }); 574 });
563 &Coro::schedule; 575 &Coro::schedule;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines