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.45 by root, Sun Nov 11 03:32:19 2001 UTC vs.
Revision 1.51 by root, Thu Nov 29 01:50:41 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 transferqueue $MAX_TRANSFERS_SMALL),
40 [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1], 41 (new transferqueue $MAX_TRANSFERS_LARGE),
41); 42);
42 43
43my @newcons; 44my @newcons;
44my @pool; 45my @pool;
45 46
149 my $peername = shift; 150 my $peername = shift;
150 my $self = bless { fh => $fh }, $class; 151 my $self = bless { fh => $fh }, $class;
151 my (undef, $iaddr) = unpack_sockaddr_in $peername 152 my (undef, $iaddr) = unpack_sockaddr_in $peername
152 or $self->err(500, "unable to decode peername"); 153 or $self->err(500, "unable to decode peername");
153 154
155 $self->{remote_addr} =
154 $self->{remote_addr} = inet_ntoa $iaddr; 156 $self->{remote_id} = inet_ntoa $iaddr;
155 $self->{time} = $::NOW; 157 $self->{time} = $::NOW;
158
159 weaken ($Coro::current->{conn} = $self);
156 160
157 $::conns++; 161 $::conns++;
158 162
159 $self; 163 $self;
160} 164}
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";
187 191
192 if (exists $hdr->{Connection}) {
193 if ($hdr->{Connection} =~ /close/) {
188 $self->{h}{connection} = "close" 194 $self->{h}{connection} = "close"
189 if exists $hdr->{Connection} # to avoid "empty" header lines due to vivification 195 }
190 and $hdr->{Connection} =~ /close/; 196 } else {
197 if ($self->{version} < 1.1) {
198 if ($self->{h}{connection} =~ /keep-alive/i) {
199 $hdr->{Connection} = "Keep-Alive";
200 } else {
201 $self->{h}{connection} = "close"
202 }
203 }
204 }
191 205
192 $res .= "Date: $HTTP_NOW\015\012"; 206 $res .= "Date: $HTTP_NOW\015\012";
193 207
194 while (my ($h, $v) = each %$hdr) { 208 while (my ($h, $v) = each %$hdr) {
195 $res .= "$h: $v\015\012" 209 $res .= "$h: $v\015\012"
196 } 210 }
197 $res .= "\015\012"; 211 $res .= "\015\012";
198 212
199 $res .= $content if defined $content and $self->{method} ne "HEAD"; 213 $res .= $content if defined $content and $self->{method} ne "HEAD";
200 214
201 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";
202 218
203 print $accesslog $log if $accesslog; 219 print $accesslog $log if $accesslog;
204 print STDERR $log; 220 print STDERR $log;
205 221
206 $self->{written} += 222 $self->{written} +=
340 356
341 $self->eoconn; 357 $self->eoconn;
342 358
343 die if $@ && !ref $@; 359 die if $@ && !ref $@;
344 360
345 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; 361 last if $self->{h}{connection} =~ /close/;
346 362
347 $httpevent->broadcast; 363 $httpevent->broadcast;
348 364
349 $fh->timeout($::PER_TIMEOUT); 365 $fh->timeout($::PER_TIMEOUT);
350 } 366 }
402 418
403sub respond { 419sub respond {
404 my $self = shift; 420 my $self = shift;
405 my $path = $self->{path}; 421 my $path = $self->{path};
406 422
407 stat $path 423 if ($self->{name} =~ s%^/internal/([^/]+)%%) {
408 or $self->err(404, "not found"); 424 if ($::internal{$1}) {
409 425 $::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 { 426 } else {
423 $ims < $self->{stat}[9] 427 $self->err(404, "not found");
428 }
429 } else {
430
431 stat $path
424 or $self->err(304, "not modified"); 432 or $self->err(404, "not found");
425 433
426 if (-r "$path/index.html") { 434 $self->{stat} = [stat _];
427 # replace directory "size" by index.html filesize 435
428 $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; 436 # idiotic netscape sends idiotic headers AGAIN
429 $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}/" });
430 } 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 {
431 $self->handle_dir; 455 $self->handle_dir;
432 } 456 }
433 } 457 }
434 } elsif (-f _ && -r _) { 458 } elsif (-f _ && -r _) {
435 -x _ and $self->err(403, "forbidden"); 459 -x _ and $self->err(403, "forbidden");
436 $self->handle_file; 460 $self->handle_file;
437 } else { 461 } else {
438 $self->err(404, "not found"); 462 $self->err(404, "not found");
463 }
439 } 464 }
440} 465}
441 466
442sub handle_dir { 467sub handle_dir {
443 my $self = shift; 468 my $self = shift;
445 470
446 $self->response(200, "ok", 471 $self->response(200, "ok",
447 { 472 {
448 "Content-Type" => "text/html", 473 "Content-Type" => "text/html",
449 "Content-Length" => length $idx, 474 "Content-Length" => length $idx,
475 #d# directories change all the time, so X-
476 "X-Last-Modified" => time2str ((stat _)[9]),
450 }, 477 },
451 $idx); 478 $idx);
452} 479}
453 480
454sub handle_file { 481sub handle_file {
484 my $delay = $::NOW + $::PER_TIMEOUT + 15; 511 my $delay = $::NOW + $::PER_TIMEOUT + 15;
485 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 512 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
486 if ($delay <= $::NOW) { 513 if ($delay <= $::NOW) {
487 $self->err_segmented_download; 514 $self->err_segmented_download;
488 } else { 515 } else {
489 $httpevent->broadcast; 516 $httpevent->wait;
490 } 517 }
491 } 518 }
492 } 519 }
493 520
494 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 521 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
507 $self->response(@code, $hdr, ""); 534 $self->response(@code, $hdr, "");
508 535
509 if ($self->{method} eq "GET") { 536 if ($self->{method} eq "GET") {
510 $self->{time} = $::NOW; 537 $self->{time} = $::NOW;
511 538
512 my $fudge = $queue->[0]->waiters; 539 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 540
527 my ($fh, $buf, $r); 541 my ($fh, $buf, $r);
528 my $current = $Coro::current; 542
529 open $fh, "<", $self->{path} 543 open $fh, "<", $self->{path}
530 or die "$self->{path}: late open failure ($!)"; 544 or die "$self->{path}: late open failure ($!)";
531 545
532 $h -= $l - 1; 546 $h -= $l - 1;
533 547
534 if (0) { 548 if (0) { # !AIO
535 if ($l) { 549 if ($l) {
536 sysseek $fh, $l, 0; 550 sysseek $fh, $l, 0;
537 } 551 }
538 } 552 }
553
554 my $transfer = $queue->start_transfer;
555 my $locked;
556 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
539 557
540 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
541 if (0) { 566 if (0) { # !AIO
542 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 567 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
543 or last; 568 or last;
544 } else { 569 } else {
545 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 570 aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h),
546 $buf, 0, sub { 571 $buf, 0, sub {
547 $r = $_[0]; 572 $r = $_[0];
548 Coro::ready($current); 573 Coro::ready($current);
549 }); 574 });
550 &Coro::schedule; 575 &Coro::schedule;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines