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.67 by root, Sun May 19 21:00:47 2002 UTC vs.
Revision 1.77 by root, Sat Sep 17 20:21:11 2005 UTC

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 59
60our $tbf_top = new tbf rate => 200000; 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}
61 74
62my @newcons; 75my @newcons;
63my @pool; 76my @pool;
64 77
65# one "execution thread" 78# one "execution thread"
97 if (@pool) { 110 if (@pool) {
98 (pop @pool)->ready; 111 (pop @pool)->ready;
99 } else { 112 } else {
100 async \&handler; 113 async \&handler;
101 } 114 }
102
103 } 115 }
104 }; 116 };
105} 117}
106 118
107my $http_port = new Coro::Socket 119my $http_port = new Coro::Socket
127package conn; 139package conn;
128 140
129use Socket; 141use Socket;
130use HTTP::Date; 142use HTTP::Date;
131use Convert::Scalar 'weaken'; 143use Convert::Scalar 'weaken';
132use Linux::AIO; 144use IO::AIO;
133 145
134Linux::AIO::min_parallel $::AIO_PARALLEL; 146IO::AIO::min_parallel $::AIO_PARALLEL;
135 147
136Event->io(fd => Linux::AIO::poll_fileno, 148Event->io(fd => IO::AIO::poll_fileno,
137 poll => 'r', async => 1, 149 poll => 'r', async => 1,
138 cb => \&Linux::AIO::poll_cb); 150 cb => \&IO::AIO::poll_cb);
139 151
140our %conn; # $conn{ip}{self} => connobj 152our %conn; # $conn{ip}{self} => connobj
141our %uri; # $uri{ip}{uri}{self} 153our %uri; # $uri{ip}{uri}{self}
142our %blocked; 154our %blocked;
143our %mimetype; 155our %mimetype;
180 192
181sub DESTROY { 193sub DESTROY {
182 #my $self = shift; 194 #my $self = shift;
183 $::conns--; 195 $::conns--;
184} 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);
185 222
186sub slog { 223sub slog {
187 my $self = shift; 224 my $self = shift;
188 main::slog($_[0], "$self->{remote_id}> $_[1]"); 225 main::slog($_[0], "$self->{remote_id}> $_[1]");
189} 226}
219 $hdr->{"Content-Length"} = length $content; 256 $hdr->{"Content-Length"} = length $content;
220 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); 257 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig);
221 } 258 }
222 259
223 $res .= "Date: $HTTP_NOW\015\012"; 260 $res .= "Date: $HTTP_NOW\015\012";
261 $res .= "Server: $::NAME\015\012";
224 262
225 while (my ($h, $v) = each %$hdr) { 263 while (my ($h, $v) = each %$hdr) {
226 $res .= "$h: $v\015\012" 264 $res .= "$h: $v\015\012"
227 } 265 }
228 $res .= "\015\012"; 266 $res .= "\015\012";
491 my $self = shift; 529 my $self = shift;
492 my $idx = $self->diridx; 530 my $idx = $self->diridx;
493 531
494 $self->response(200, "ok", 532 $self->response(200, "ok",
495 { 533 {
496 "Content-Type" => "text/html", 534 "Content-Type" => "text/html; charset=utf-8",
497 "Content-Length" => length $idx, 535 "Content-Length" => length $idx,
498 "Last-Modified" => time2str ($self->{stat}[9]), 536 "Last-Modified" => time2str ($self->{stat}[9]),
499 }, 537 },
500 $idx); 538 $idx);
501} 539}
502 540
503sub handle_file { 541sub handle_file {
504 my ($self, $queue, $tbf) = @_; 542 my ($self, $queue, $tbf) = @_;
505 my $length = $self->{stat}[7]; 543 my $length = $self->{stat}[7];
506 my $hdr = { 544 my $hdr = {
507 "Last-Modified" => time2str ((stat _)[9]), 545 "Last-Modified" => time2str ((stat _)[9]),
546 "Accept-Ranges" => "bytes",
508 }; 547 };
509 548
510 my @code = (200, "ok"); 549 my @code = (200, "ok");
511 my ($l, $h); 550 my ($l, $h);
512 551
555 594
556 $self->response(@code, $hdr, ""); 595 $self->response(@code, $hdr, "");
557 596
558 if ($self->{method} eq "GET") { 597 if ($self->{method} eq "GET") {
559 $self->{time} = $::NOW; 598 $self->{time} = $::NOW;
599 $self->{written} = 0;
560 600
561 my $current = $Coro::current; 601 my $current = $Coro::current;
562 602
563 my ($fh, $buf, $r); 603 my ($fh, $buf, $r);
564 604
580 while ($h > 0) { 620 while ($h > 0) {
581 unless ($locked) { 621 unless ($locked) {
582 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 622 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) {
583 $bufsize = $::BUFSIZE; 623 $bufsize = $::BUFSIZE;
584 $self->{time} = $::NOW; 624 $self->{time} = $::NOW;
625 $self->{written} = 0;
585 } 626 }
586 } 627 }
587 628
588 if ($blocked{$self->{remote_id}}) { 629 if ($blocked{$self->{remote_id}}) {
589 $self->{h}{connection} = "close"; 630 $self->{h}{connection} = "close";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines