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.64 by root, Wed Jan 23 04:49:50 2002 UTC vs.
Revision 1.68 by root, Mon May 20 04:08:03 2002 UTC

54our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 54our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
55our $httpevent = new Coro::Signal; 55our $httpevent = new Coro::Signal;
56 56
57our $queue_file = new transferqueue $MAX_TRANSFERS; 57our $queue_file = new transferqueue $MAX_TRANSFERS;
58our $queue_index = new transferqueue 10; 58our $queue_index = new transferqueue 10;
59
60our $tbf_top = new tbf rate => 200000;
61
62my $unused_bytes = 0;
63my $unused_last = time;
64
65sub unused_bandwidth {
66 $unused_bytes += $_[0];
67 if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) {
68 $unused_last = $NOW;
69 $unused_bytes = 0;
70 $queue_file->force_wake_next;
71 slog 1, "forced filetransfer due to unused bandwidth";
72 }
73}
59 74
60my @newcons; 75my @newcons;
61my @pool; 76my @pool;
62 77
63# one "execution thread" 78# one "execution thread"
225 } 240 }
226 $res .= "\015\012"; 241 $res .= "\015\012";
227 242
228 $res .= $content if defined $content and $self->{method} ne "HEAD"; 243 $res .= $content if defined $content and $self->{method} ne "HEAD";
229 244
230 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). 245 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW).
231 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. 246 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ.
232 " \"$self->{h}{referer}\"\n"; 247 " \"$self->{h}{referer}\"\n";
233 248
234 print $accesslog $log if $accesslog; 249 print $::accesslog $log if $::accesslog;
235 print STDERR $log; 250 print STDERR $log;
236 251
237 $self->{written} += 252 $tbf_top->request(length $res, 1e6);
238 print {$self->{fh}} $res; 253 $self->{written} += print {$self->{fh}} $res;
239} 254}
240 255
241sub err { 256sub err {
242 my $self = shift; 257 my $self = shift;
243 my ($code, $msg, $hdr, $content) = @_; 258 my ($code, $msg, $hdr, $content) = @_;
298 my (%hdr, $h, $v); 313 my (%hdr, $h, $v);
299 314
300 $hdr{lc $1} .= ",$2" 315 $hdr{lc $1} .= ",$2"
301 while $req =~ /\G 316 while $req =~ /\G
302 ([^:\000-\040]+): 317 ([^:\000-\040]+):
303 [\010\040]* 318 [\011\040]*
304 ((?: [^\015\012]+ | \015\012[\010\040] )*) 319 ((?: [^\015\012]+ | \015\012[\011\040] )*)
305 \015\012 320 \015\012
306 /gxc; 321 /gxc;
307 322
308 $req =~ /\G\015\012$/ 323 $req =~ /\G\015\012$/
309 or $self->err(400, "bad request"); 324 or $self->err(400, "bad request");
457 or $self->err(304, "not modified"); 472 or $self->err(304, "not modified");
458 473
459 if (-r "$path/index.html") { 474 if (-r "$path/index.html") {
460 # replace directory "size" by index.html filesize 475 # replace directory "size" by index.html filesize
461 $self->{stat} = [stat ($self->{path} .= "/index.html")]; 476 $self->{stat} = [stat ($self->{path} .= "/index.html")];
462 $self->handle_file($queue_index); 477 $self->handle_file($queue_index, $tbf_top);
463 } else { 478 } else {
464 $self->handle_dir; 479 $self->handle_dir;
465 } 480 }
466 } 481 }
467 } elsif (-f _ && -r _) { 482 } elsif (-f _ && -r _) {
476 $httpevent->wait; 491 $httpevent->wait;
477 } 492 }
478 } 493 }
479 } 494 }
480 495
481 $self->handle_file($queue_file); 496 $self->handle_file($queue_file, $tbf_top);
482 } else { 497 } else {
483 $self->err(404, "not found"); 498 $self->err(404, "not found");
484 } 499 }
485 } 500 }
486} 501}
497 }, 512 },
498 $idx); 513 $idx);
499} 514}
500 515
501sub handle_file { 516sub handle_file {
502 my ($self, $queue) = @_; 517 my ($self, $queue, $tbf) = @_;
503 my $length = $self->{stat}[7]; 518 my $length = $self->{stat}[7];
504 my $hdr = { 519 my $hdr = {
505 "Last-Modified" => time2str ((stat _)[9]), 520 "Last-Modified" => time2str ((stat _)[9]),
506 }; 521 };
507 522
598 Coro::ready($current); 613 Coro::ready($current);
599 }); 614 });
600 &Coro::schedule; 615 &Coro::schedule;
601 last unless $r; 616 last unless $r;
602 } 617 }
618
619 $tbf->request(length $buf);
603 my $w = syswrite $self->{fh}, $buf 620 my $w = syswrite $self->{fh}, $buf
604 or last; 621 or last;
605 $::written += $w; 622 $::written += $w;
606 $self->{written} += $w; 623 $self->{written} += $w;
607 $l += $r; 624 $l += $r;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines