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.46 by root, Sat Nov 17 04:15:23 2001 UTC vs.
Revision 1.54 by root, Fri Nov 30 06:03:48 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
33our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 34our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
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 $queue_small = new transferqueue $MAX_TRANSFERS_SMALL;
39 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], 40our $queue_large = new transferqueue $MAX_TRANSFERS_LARGE;
40 [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1], 41our $queue_index = new transferqueue 5;
41);
42 42
43my @newcons; 43my @newcons;
44my @pool; 44my @pool;
45 45
46# one "execution thread" 46# one "execution thread"
149 my $peername = shift; 149 my $peername = shift;
150 my $self = bless { fh => $fh }, $class; 150 my $self = bless { fh => $fh }, $class;
151 my (undef, $iaddr) = unpack_sockaddr_in $peername 151 my (undef, $iaddr) = unpack_sockaddr_in $peername
152 or $self->err(500, "unable to decode peername"); 152 or $self->err(500, "unable to decode peername");
153 153
154 $self->{remote_addr} =
154 $self->{remote_addr} = inet_ntoa $iaddr; 155 $self->{remote_id} = inet_ntoa $iaddr;
155 $self->{time} = $::NOW; 156 $self->{time} = $::NOW;
156 157
158 weaken ($Coro::current->{conn} = $self);
159
157 $::conns++; 160 $::conns++;
161 $::maxconns = $::conns if $::conns > $::maxconns;
158 162
159 $self; 163 $self;
160} 164}
161 165
162sub DESTROY { 166sub DESTROY {
176 $httpevent->broadcast; 180 $httpevent->broadcast;
177} 181}
178 182
179sub slog { 183sub slog {
180 my $self = shift; 184 my $self = shift;
181 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); 185 main::slog($_[0], "$self->{remote_id}> $_[1]");
182} 186}
183 187
184sub response { 188sub response {
185 my ($self, $code, $msg, $hdr, $content) = @_; 189 my ($self, $code, $msg, $hdr, $content) = @_;
186 my $res = "HTTP/1.1 $code $msg\015\012"; 190 my $res = "HTTP/1.1 $code $msg\015\012";
206 } 210 }
207 $res .= "\015\012"; 211 $res .= "\015\012";
208 212
209 $res .= $content if defined $content and $self->{method} ne "HEAD"; 213 $res .= $content if defined $content and $self->{method} ne "HEAD";
210 214
211 my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; 215 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW).
216 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.
217 " \"$self->{h}{referer}\"\n";
212 218
213 print $accesslog $log if $accesslog; 219 print $accesslog $log if $accesslog;
214 print STDERR $log; 220 print STDERR $log;
215 221
216 $self->{written} += 222 $self->{written} +=
412 418
413sub respond { 419sub respond {
414 my $self = shift; 420 my $self = shift;
415 my $path = $self->{path}; 421 my $path = $self->{path};
416 422
417 stat $path 423 if ($self->{name} =~ s%^/internal/([^/]+)%%) {
418 or $self->err(404, "not found"); 424 if ($::internal{$1}) {
419 425 $::internal{$1}->($self);
420 $self->{stat} = [stat _];
421
422 # idiotic netscape sends idiotic headers AGAIN
423 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
424 ? str2time $1 : 0;
425
426 if (-d _ && -r _) {
427 # directory
428 if ($path !~ /\/$/) {
429 # create a redirect to get the trailing "/"
430 # we don't try to avoid the :80
431 $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" });
432 } else { 426 } else {
433 $ims < $self->{stat}[9] 427 $self->err(404, "not found");
428 }
429 } else {
430
431 stat $path
434 or $self->err(304, "not modified"); 432 or $self->err(404, "not found");
435 433
436 if (-r "$path/index.html") { 434 $self->{stat} = [stat _];
437 # replace directory "size" by index.html filesize 435
438 $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; 436 # idiotic netscape sends idiotic headers AGAIN
439 $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}/" });
440 } 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($queue_index);
454 } else {
441 $self->handle_dir; 455 $self->handle_dir;
442 } 456 }
443 } 457 }
444 } elsif (-f _ && -r _) { 458 } elsif (-f _ && -r _) {
445 -x _ and $self->err(403, "forbidden"); 459 -x _ and $self->err(403, "forbidden");
446 $self->handle_file; 460 $self->handle_file(-s _ >= $::TRANSFER_SMALL ? $queue_large : $queue_small);
447 } else { 461 } else {
448 $self->err(404, "not found"); 462 $self->err(404, "not found");
463 }
449 } 464 }
450} 465}
451 466
452sub handle_dir { 467sub handle_dir {
453 my $self = shift; 468 my $self = shift;
455 470
456 $self->response(200, "ok", 471 $self->response(200, "ok",
457 { 472 {
458 "Content-Type" => "text/html", 473 "Content-Type" => "text/html",
459 "Content-Length" => length $idx, 474 "Content-Length" => length $idx,
475 "Last-Modified" => time2str ((stat _)[9]),
460 }, 476 },
461 $idx); 477 $idx);
462} 478}
463 479
464sub handle_file { 480sub handle_file {
465 my $self = shift; 481 my ($self, $queue) = @_;
466 my $length = $self->{stat}[7]; 482 my $length = $self->{stat}[7];
467 my $queue = $::transfers[$length >= $::TRANSFER_SMALL];
468 my $hdr = { 483 my $hdr = {
469 "Last-Modified" => time2str ((stat _)[9]), 484 "Last-Modified" => time2str ((stat _)[9]),
470 }; 485 };
471 486
472 my @code = (200, "ok"); 487 my @code = (200, "ok");
517 $self->response(@code, $hdr, ""); 532 $self->response(@code, $hdr, "");
518 533
519 if ($self->{method} eq "GET") { 534 if ($self->{method} eq "GET") {
520 $self->{time} = $::NOW; 535 $self->{time} = $::NOW;
521 536
522 my $fudge = $queue->[0]->waiters; 537 my $current = $Coro::current;
523 $fudge = $fudge ? ($fudge+1)/$fudge : 1;
524
525 $queue->[1] *= $fudge;
526 my $transfer = $queue->[0]->guard;
527
528 if ($fudge != 1) {
529 $queue->[1] /= $fudge;
530 $queue->[1] = $queue->[1] * $::wait_factor
531 + ($::NOW - $self->{time}) * (1 - $::wait_factor);
532 }
533 $self->{time} = $::NOW;
534
535 $self->{fh}->writable or return;
536 538
537 my ($fh, $buf, $r); 539 my ($fh, $buf, $r);
538 my $current = $Coro::current; 540
539 open $fh, "<", $self->{path} 541 open $fh, "<", $self->{path}
540 or die "$self->{path}: late open failure ($!)"; 542 or die "$self->{path}: late open failure ($!)";
541 543
542 $h -= $l - 1; 544 $h -= $l - 1;
543 545
544 if (0) { 546 if (0) { # !AIO
545 if ($l) { 547 if ($l) {
546 sysseek $fh, $l, 0; 548 sysseek $fh, $l, 0;
547 } 549 }
548 } 550 }
551
552 my $transfer = $queue->start_transfer;
553 my $locked;
554 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
549 555
550 while ($h > 0) { 556 while ($h > 0) {
557 unless ($locked) {
558 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) {
559 $bufsize = $::BUFSIZE;
560 $self->{time} = $::NOW;
561 }
562 }
563
551 if (0) { 564 if (0) { # !AIO
552 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 565 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
553 or last; 566 or last;
554 } else { 567 } else {
555 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 568 aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h),
556 $buf, 0, sub { 569 $buf, 0, sub {
557 $r = $_[0]; 570 $r = $_[0];
558 Coro::ready($current); 571 Coro::ready($current);
559 }); 572 });
560 &Coro::schedule; 573 &Coro::schedule;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines