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.64 by root, Wed Jan 23 04:49:50 2002 UTC vs.
Revision 1.72 by root, Tue Sep 10 03: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 => $TBF_RATE || 100000;
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"
95 if (@pool) { 110 if (@pool) {
96 (pop @pool)->ready; 111 (pop @pool)->ready;
97 } else { 112 } else {
98 async \&handler; 113 async \&handler;
99 } 114 }
100
101 } 115 }
102 }; 116 };
103} 117}
104 118
105my $http_port = new Coro::Socket 119my $http_port = new Coro::Socket
217 $hdr->{"Content-Length"} = length $content; 231 $hdr->{"Content-Length"} = length $content;
218 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); 232 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig);
219 } 233 }
220 234
221 $res .= "Date: $HTTP_NOW\015\012"; 235 $res .= "Date: $HTTP_NOW\015\012";
236 $res .= "Server: $::NAME\015\012";
222 237
223 while (my ($h, $v) = each %$hdr) { 238 while (my ($h, $v) = each %$hdr) {
224 $res .= "$h: $v\015\012" 239 $res .= "$h: $v\015\012"
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
553 568
554 $self->response(@code, $hdr, ""); 569 $self->response(@code, $hdr, "");
555 570
556 if ($self->{method} eq "GET") { 571 if ($self->{method} eq "GET") {
557 $self->{time} = $::NOW; 572 $self->{time} = $::NOW;
573 $self->{written} = 0;
558 574
559 my $current = $Coro::current; 575 my $current = $Coro::current;
560 576
561 my ($fh, $buf, $r); 577 my ($fh, $buf, $r);
562 578
578 while ($h > 0) { 594 while ($h > 0) {
579 unless ($locked) { 595 unless ($locked) {
580 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 596 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) {
581 $bufsize = $::BUFSIZE; 597 $bufsize = $::BUFSIZE;
582 $self->{time} = $::NOW; 598 $self->{time} = $::NOW;
599 $self->{written} = 0;
583 } 600 }
584 } 601 }
585 602
586 if ($blocked{$self->{remote_id}}) { 603 if ($blocked{$self->{remote_id}}) {
587 $self->{h}{connection} = "close"; 604 $self->{h}{connection} = "close";
598 Coro::ready($current); 615 Coro::ready($current);
599 }); 616 });
600 &Coro::schedule; 617 &Coro::schedule;
601 last unless $r; 618 last unless $r;
602 } 619 }
620
621 $tbf->request(length $buf);
603 my $w = syswrite $self->{fh}, $buf 622 my $w = syswrite $self->{fh}, $buf
604 or last; 623 or last;
605 $::written += $w; 624 $::written += $w;
606 $self->{written} += $w; 625 $self->{written} += $w;
607 $l += $r; 626 $l += $r;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines