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.52 by root, Fri Nov 30 05:11:23 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;
157
158 weaken ($Coro::current->{conn} = $self);
156 159
157 $::conns++; 160 $::conns++;
158 161
159 $self; 162 $self;
160} 163}
176 $httpevent->broadcast; 179 $httpevent->broadcast;
177} 180}
178 181
179sub slog { 182sub slog {
180 my $self = shift; 183 my $self = shift;
181 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); 184 main::slog($_[0], "$self->{remote_id}> $_[1]");
182} 185}
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
188 $self->{h}{connection} = "close" if $hdr->{Connection} =~ /close/; 191 if (exists $hdr->{Connection}) {
192 if ($hdr->{Connection} =~ /close/) {
193 $self->{h}{connection} = "close"
194 }
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 }
189 204
190 $res .= "Date: $HTTP_NOW\015\012"; 205 $res .= "Date: $HTTP_NOW\015\012";
191 206
192 while (my ($h, $v) = each %$hdr) { 207 while (my ($h, $v) = each %$hdr) {
193 $res .= "$h: $v\015\012" 208 $res .= "$h: $v\015\012"
194 } 209 }
195 $res .= "\015\012"; 210 $res .= "\015\012";
196 211
197 $res .= $content if defined $content and $self->{method} ne "HEAD"; 212 $res .= $content if defined $content and $self->{method} ne "HEAD";
198 213
199 my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; 214 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW).
215 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.
216 " \"$self->{h}{referer}\"\n";
200 217
201 print $accesslog $log if $accesslog; 218 print $accesslog $log if $accesslog;
202 print STDERR $log; 219 print STDERR $log;
203 220
204 $self->{written} += 221 $self->{written} +=
338 355
339 $self->eoconn; 356 $self->eoconn;
340 357
341 die if $@ && !ref $@; 358 die if $@ && !ref $@;
342 359
343 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; 360 last if $self->{h}{connection} =~ /close/;
344 361
345 $httpevent->broadcast; 362 $httpevent->broadcast;
346 363
347 $fh->timeout($::PER_TIMEOUT); 364 $fh->timeout($::PER_TIMEOUT);
348 } 365 }
400 417
401sub respond { 418sub respond {
402 my $self = shift; 419 my $self = shift;
403 my $path = $self->{path}; 420 my $path = $self->{path};
404 421
405 stat $path 422 if ($self->{name} =~ s%^/internal/([^/]+)%%) {
406 or $self->err(404, "not found"); 423 if ($::internal{$1}) {
407 424 $::internal{$1}->($self);
408 $self->{stat} = [stat _];
409
410 # idiotic netscape sends idiotic headers AGAIN
411 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
412 ? str2time $1 : 0;
413
414 if (-d _ && -r _) {
415 # directory
416 if ($path !~ /\/$/) {
417 # create a redirect to get the trailing "/"
418 # we don't try to avoid the :80
419 $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" });
420 } else { 425 } else {
421 $ims < $self->{stat}[9] 426 $self->err(404, "not found");
427 }
428 } else {
429
430 stat $path
422 or $self->err(304, "not modified"); 431 or $self->err(404, "not found");
423 432
424 if (-r "$path/index.html") { 433 $self->{stat} = [stat _];
425 $self->{path} .= "/index.html"; 434
426 $self->handle_file; 435 # idiotic netscape sends idiotic headers AGAIN
436 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
437 ? str2time $1 : 0;
438
439 if (-d _ && -r _) {
440 # directory
441 if ($path !~ /\/$/) {
442 # create a redirect to get the trailing "/"
443 # we don't try to avoid the :80
444 $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" });
427 } else { 445 } else {
446 $ims < $self->{stat}[9]
447 or $self->err(304, "not modified");
448
449 if (-r "$path/index.html") {
450 # replace directory "size" by index.html filesize
451 $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7];
452 $self->handle_file($queue_index);
453 } else {
428 $self->handle_dir; 454 $self->handle_dir;
429 } 455 }
430 } 456 }
431 } elsif (-f _ && -r _) { 457 } elsif (-f _ && -r _) {
432 -x _ and $self->err(403, "forbidden"); 458 -x _ and $self->err(403, "forbidden");
433 $self->handle_file; 459 $self->handle_file(-s _ >= $::TRANSFER_SMALL ? $queue_large : $queue_small);
434 } else { 460 } else {
435 $self->err(404, "not found"); 461 $self->err(404, "not found");
462 }
436 } 463 }
437} 464}
438 465
439sub handle_dir { 466sub handle_dir {
440 my $self = shift; 467 my $self = shift;
442 469
443 $self->response(200, "ok", 470 $self->response(200, "ok",
444 { 471 {
445 "Content-Type" => "text/html", 472 "Content-Type" => "text/html",
446 "Content-Length" => length $idx, 473 "Content-Length" => length $idx,
474 #d# directories change all the time, so X-
475 "X-Last-Modified" => time2str ((stat _)[9]),
447 }, 476 },
448 $idx); 477 $idx);
449} 478}
450 479
451sub handle_file { 480sub handle_file {
452 my $self = shift; 481 my ($self, $queue) = @_;
453 my $length = $self->{stat}[7]; 482 my $length = $self->{stat}[7];
454 my $queue = $::transfers[$length >= $::TRANSFER_SMALL];
455 my $hdr = { 483 my $hdr = {
456 "Last-Modified" => time2str ((stat _)[9]), 484 "Last-Modified" => time2str ((stat _)[9]),
457 }; 485 };
458 486
459 my @code = (200, "ok"); 487 my @code = (200, "ok");
481 my $delay = $::NOW + $::PER_TIMEOUT + 15; 509 my $delay = $::NOW + $::PER_TIMEOUT + 15;
482 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 510 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
483 if ($delay <= $::NOW) { 511 if ($delay <= $::NOW) {
484 $self->err_segmented_download; 512 $self->err_segmented_download;
485 } else { 513 } else {
486 $httpevent->broadcast; 514 $httpevent->wait;
487 } 515 }
488 } 516 }
489 } 517 }
490 518
491 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 519 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
504 $self->response(@code, $hdr, ""); 532 $self->response(@code, $hdr, "");
505 533
506 if ($self->{method} eq "GET") { 534 if ($self->{method} eq "GET") {
507 $self->{time} = $::NOW; 535 $self->{time} = $::NOW;
508 536
509 my $fudge = $queue->[0]->waiters; 537 my $current = $Coro::current;
510 $fudge = $fudge ? ($fudge+1)/$fudge : 1;
511
512 $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 538
524 my ($fh, $buf, $r); 539 my ($fh, $buf, $r);
525 my $current = $Coro::current; 540
526 open $fh, "<", $self->{path} 541 open $fh, "<", $self->{path}
527 or die "$self->{path}: late open failure ($!)"; 542 or die "$self->{path}: late open failure ($!)";
528 543
529 $h -= $l - 1; 544 $h -= $l - 1;
530 545
531 if (0) { 546 if (0) { # !AIO
532 if ($l) { 547 if ($l) {
533 sysseek $fh, $l, 0; 548 sysseek $fh, $l, 0;
534 } 549 }
535 } 550 }
551
552 my $transfer = $queue->start_transfer;
553 my $locked;
554 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
536 555
537 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
538 if (0) { 564 if (0) { # !AIO
539 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 565 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
540 or last; 566 or last;
541 } else { 567 } else {
542 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 568 aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h),
543 $buf, 0, sub { 569 $buf, 0, sub {
544 $r = $_[0]; 570 $r = $_[0];
545 Coro::ready($current); 571 Coro::ready($current);
546 }); 572 });
547 &Coro::schedule; 573 &Coro::schedule;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines