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.66 by root, Mon Feb 25 03:21:09 2002 UTC vs.
Revision 1.77 by root, Sat Sep 17 20:21:11 2005 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
125package conn; 139package conn;
126 140
127use Socket; 141use Socket;
128use HTTP::Date; 142use HTTP::Date;
129use Convert::Scalar 'weaken'; 143use Convert::Scalar 'weaken';
130use Linux::AIO; 144use IO::AIO;
131 145
132Linux::AIO::min_parallel $::AIO_PARALLEL; 146IO::AIO::min_parallel $::AIO_PARALLEL;
133 147
134Event->io(fd => Linux::AIO::poll_fileno, 148Event->io(fd => IO::AIO::poll_fileno,
135 poll => 'r', async => 1, 149 poll => 'r', async => 1,
136 cb => \&Linux::AIO::poll_cb); 150 cb => \&IO::AIO::poll_cb);
137 151
138our %conn; # $conn{ip}{self} => connobj 152our %conn; # $conn{ip}{self} => connobj
139our %uri; # $uri{ip}{uri}{self} 153our %uri; # $uri{ip}{uri}{self}
140our %blocked; 154our %blocked;
141our %mimetype; 155our %mimetype;
178 192
179sub DESTROY { 193sub DESTROY {
180 #my $self = shift; 194 #my $self = shift;
181 $::conns--; 195 $::conns--;
182} 196}
197
198sub prune_cache {
199 my $hash = $_[0];
200
201 for (keys %$hash) {
202 if (ref $hash->{$_} eq HASH::) {
203 prune_cache($hash->{$_});
204 unless (scalar keys %{$hash->{$_}}) {
205 delete $hash->{$_};
206 $d2++;
207 }
208 }
209 }
210}
211
212sub prune_caches {
213 prune_cache \%conn;
214 prune_cache \%uri;
215
216 for (keys %blocked) {
217 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
218 }
219}
220
221Event->timer(interval => 60, cb => \&prune_caches);
183 222
184sub slog { 223sub slog {
185 my $self = shift; 224 my $self = shift;
186 main::slog($_[0], "$self->{remote_id}> $_[1]"); 225 main::slog($_[0], "$self->{remote_id}> $_[1]");
187} 226}
217 $hdr->{"Content-Length"} = length $content; 256 $hdr->{"Content-Length"} = length $content;
218 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); 257 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig);
219 } 258 }
220 259
221 $res .= "Date: $HTTP_NOW\015\012"; 260 $res .= "Date: $HTTP_NOW\015\012";
261 $res .= "Server: $::NAME\015\012";
222 262
223 while (my ($h, $v) = each %$hdr) { 263 while (my ($h, $v) = each %$hdr) {
224 $res .= "$h: $v\015\012" 264 $res .= "$h: $v\015\012"
225 } 265 }
226 $res .= "\015\012"; 266 $res .= "\015\012";
232 " \"$self->{h}{referer}\"\n"; 272 " \"$self->{h}{referer}\"\n";
233 273
234 print $::accesslog $log if $::accesslog; 274 print $::accesslog $log if $::accesslog;
235 print STDERR $log; 275 print STDERR $log;
236 276
237 $self->{written} += 277 $tbf_top->request(length $res, 1e6);
238 print {$self->{fh}} $res; 278 $self->{written} += print {$self->{fh}} $res;
239} 279}
240 280
241sub err { 281sub err {
242 my $self = shift; 282 my $self = shift;
243 my ($code, $msg, $hdr, $content) = @_; 283 my ($code, $msg, $hdr, $content) = @_;
457 or $self->err(304, "not modified"); 497 or $self->err(304, "not modified");
458 498
459 if (-r "$path/index.html") { 499 if (-r "$path/index.html") {
460 # replace directory "size" by index.html filesize 500 # replace directory "size" by index.html filesize
461 $self->{stat} = [stat ($self->{path} .= "/index.html")]; 501 $self->{stat} = [stat ($self->{path} .= "/index.html")];
462 $self->handle_file($queue_index); 502 $self->handle_file($queue_index, $tbf_top);
463 } else { 503 } else {
464 $self->handle_dir; 504 $self->handle_dir;
465 } 505 }
466 } 506 }
467 } elsif (-f _ && -r _) { 507 } elsif (-f _ && -r _) {
476 $httpevent->wait; 516 $httpevent->wait;
477 } 517 }
478 } 518 }
479 } 519 }
480 520
481 $self->handle_file($queue_file); 521 $self->handle_file($queue_file, $tbf_top);
482 } else { 522 } else {
483 $self->err(404, "not found"); 523 $self->err(404, "not found");
484 } 524 }
485 } 525 }
486} 526}
489 my $self = shift; 529 my $self = shift;
490 my $idx = $self->diridx; 530 my $idx = $self->diridx;
491 531
492 $self->response(200, "ok", 532 $self->response(200, "ok",
493 { 533 {
494 "Content-Type" => "text/html", 534 "Content-Type" => "text/html; charset=utf-8",
495 "Content-Length" => length $idx, 535 "Content-Length" => length $idx,
496 "Last-Modified" => time2str ($self->{stat}[9]), 536 "Last-Modified" => time2str ($self->{stat}[9]),
497 }, 537 },
498 $idx); 538 $idx);
499} 539}
500 540
501sub handle_file { 541sub handle_file {
502 my ($self, $queue) = @_; 542 my ($self, $queue, $tbf) = @_;
503 my $length = $self->{stat}[7]; 543 my $length = $self->{stat}[7];
504 my $hdr = { 544 my $hdr = {
505 "Last-Modified" => time2str ((stat _)[9]), 545 "Last-Modified" => time2str ((stat _)[9]),
546 "Accept-Ranges" => "bytes",
506 }; 547 };
507 548
508 my @code = (200, "ok"); 549 my @code = (200, "ok");
509 my ($l, $h); 550 my ($l, $h);
510 551
553 594
554 $self->response(@code, $hdr, ""); 595 $self->response(@code, $hdr, "");
555 596
556 if ($self->{method} eq "GET") { 597 if ($self->{method} eq "GET") {
557 $self->{time} = $::NOW; 598 $self->{time} = $::NOW;
599 $self->{written} = 0;
558 600
559 my $current = $Coro::current; 601 my $current = $Coro::current;
560 602
561 my ($fh, $buf, $r); 603 my ($fh, $buf, $r);
562 604
578 while ($h > 0) { 620 while ($h > 0) {
579 unless ($locked) { 621 unless ($locked) {
580 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 622 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) {
581 $bufsize = $::BUFSIZE; 623 $bufsize = $::BUFSIZE;
582 $self->{time} = $::NOW; 624 $self->{time} = $::NOW;
625 $self->{written} = 0;
583 } 626 }
584 } 627 }
585 628
586 if ($blocked{$self->{remote_id}}) { 629 if ($blocked{$self->{remote_id}}) {
587 $self->{h}{connection} = "close"; 630 $self->{h}{connection} = "close";
598 Coro::ready($current); 641 Coro::ready($current);
599 }); 642 });
600 &Coro::schedule; 643 &Coro::schedule;
601 last unless $r; 644 last unless $r;
602 } 645 }
646
647 $tbf->request(length $buf);
603 my $w = syswrite $self->{fh}, $buf 648 my $w = syswrite $self->{fh}, $buf
604 or last; 649 or last;
605 $::written += $w; 650 $::written += $w;
606 $self->{written} += $w; 651 $self->{written} += $w;
607 $l += $r; 652 $l += $r;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines