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.33 by root, Wed Aug 29 01:40:44 2001 UTC vs.
Revision 1.35 by root, Thu Aug 30 12:35:28 2001 UTC

5 5
6use HTTP::Date; 6use HTTP::Date;
7 7
8no utf8; 8no utf8;
9use bytes; 9use bytes;
10
11our @wait_time = (); # used to calculcate avg. waiting time
12our $wait_time_length = 25;
13 10
14# at least on my machine, this thingy serves files 11# at least on my machine, this thingy serves files
15# quite a bit faster than apache, ;) 12# quite a bit faster than apache, ;)
16# and quite a bit slower than thttpd :( 13# and quite a bit slower than thttpd :(
17 14
31 my $format = shift; 28 my $format = shift;
32 printf "---: $format\n", @_; 29 printf "---: $format\n", @_;
33} 30}
34 31
35our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 32our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
36our $transfers = new Coro::Semaphore $MAX_TRANSFER || 50; 33
34our $wait_factor = 0.95;
35
36our @transfers = (
37 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1],
38 [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1],
39);
37 40
38my @newcons; 41my @newcons;
39my @pool; 42my @pool;
40 43
41# one "execution thread" 44# one "execution thread"
69our $HTTP_NOW; 72our $HTTP_NOW;
70 73
71Event->timer(interval => 1, hard => 1, cb => sub { 74Event->timer(interval => 1, hard => 1, cb => sub {
72 $NOW = time; 75 $NOW = time;
73 $HTTP_NOW = time2str $NOW; 76 $HTTP_NOW = time2str $NOW;
74}); 77})->now;
75 78
76# the "main thread" 79# the "main thread"
77async { 80async {
78 slog 1, "accepting connections"; 81 slog 1, "accepting connections";
79 while () { 82 while () {
190sub err { 193sub err {
191 my $self = shift; 194 my $self = shift;
192 my ($code, $msg, $hdr, $content) = @_; 195 my ($code, $msg, $hdr, $content) = @_;
193 196
194 unless (defined $content) { 197 unless (defined $content) {
195 $content = "$code $msg"; 198 $content = "$code $msg\n";
196 $hdr->{"Content-Type"} = "text/plain"; 199 $hdr->{"Content-Type"} = "text/plain";
197 $hdr->{"Content-Length"} = length $content; 200 $hdr->{"Content-Length"} = length $content;
198 } 201 }
199 $hdr->{"Connection"} = "close"; 202 $hdr->{"Connection"} = "close";
200 203
426 $idx); 429 $idx);
427} 430}
428 431
429sub handle_file { 432sub handle_file {
430 my $self = shift; 433 my $self = shift;
431 my $length = -s _; 434 my $length = $self->{stat}[7];
435 my $queue = $::transfers[$length >= $::TRANSFER_SMALL];
432 my $hdr = { 436 my $hdr = {
433 "Last-Modified" => time2str ((stat _)[9]), 437 "Last-Modified" => time2str ((stat _)[9]),
434 }; 438 };
435 439
436 my @code = (200, "ok"); 440 my @code = (200, "ok");
448 } 452 }
449 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; 453 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
450 } 454 }
451 $hdr->{"Content-Range"} = "bytes */$length"; 455 $hdr->{"Content-Range"} = "bytes */$length";
452 $hdr->{"Content-Length"} = $length; 456 $hdr->{"Content-Length"} = $length;
453 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
454 $self->err(416, "not satisfiable", $hdr, ""); 457 $self->err(416, "not satisfiable", $hdr, "");
455 458
456satisfiable: 459satisfiable:
457 # check for segmented downloads 460 # check for segmented downloads
458 if ($l && $::NO_SEGMENTED) { 461 if ($l && $::NO_SEGMENTED) {
482 $self->response(@code, $hdr, ""); 485 $self->response(@code, $hdr, "");
483 486
484 if ($self->{method} eq "GET") { 487 if ($self->{method} eq "GET") {
485 $self->{time} = $::NOW; 488 $self->{time} = $::NOW;
486 489
490 my $fudge = $queue->[0]->waiters;
491 $fudge = $fudge ? ($fudge+1)/$fudge : 1;
492
493 $queue->[1] *= $fudge;
487 my $transfer = $::transfers->guard; 494 my $transfer = $queue->[0]->guard;
495
496 if ($fudge != 1) {
497 $queue->[1] /= $fudge;
498 $queue->[1] = $queue->[1] * $::wait_factor
499 + ($::NOW - $self->{time}) * (1 - $::wait_factor);
500 }
501 $self->{time} = $::NOW;
502
488 $self->{fh}->writable or return; 503 $self->{fh}->writable or return;
489
490 push @::wait_time, $::NOW - $self->{time};
491 shift @::wait_time if @wait_time > $wait_time_length;
492 $self->{time} = $::NOW;
493 504
494 my ($fh, $buf, $r); 505 my ($fh, $buf, $r);
495 my $current = $Coro::current; 506 my $current = $Coro::current;
496 open $fh, "<", $self->{path} 507 open $fh, "<", $self->{path}
497 or die "$self->{path}: late open failure ($!)"; 508 or die "$self->{path}: late open failure ($!)";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines