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.56 by root, Mon Dec 3 04:57:22 2001 UTC vs.
Revision 1.77 by root, Sat Sep 17 20:21:11 2005 UTC

4use Coro::Socket; 4use Coro::Socket;
5use Coro::Signal; 5use Coro::Signal;
6 6
7use HTTP::Date; 7use HTTP::Date;
8use POSIX (); 8use POSIX ();
9
10use Compress::Zlib ();
9 11
10no utf8; 12no utf8;
11use bytes; 13use bytes;
12 14
13# at least on my machine, this thingy serves files 15# at least on my machine, this thingy serves files
52our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 54our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
53our $httpevent = new Coro::Signal; 55our $httpevent = new Coro::Signal;
54 56
55our $queue_file = new transferqueue $MAX_TRANSFERS; 57our $queue_file = new transferqueue $MAX_TRANSFERS;
56our $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}
57 74
58my @newcons; 75my @newcons;
59my @pool; 76my @pool;
60 77
61# one "execution thread" 78# one "execution thread"
93 if (@pool) { 110 if (@pool) {
94 (pop @pool)->ready; 111 (pop @pool)->ready;
95 } else { 112 } else {
96 async \&handler; 113 async \&handler;
97 } 114 }
98
99 } 115 }
100 }; 116 };
101} 117}
102 118
103my $http_port = new Coro::Socket 119my $http_port = new Coro::Socket
123package conn; 139package conn;
124 140
125use Socket; 141use Socket;
126use HTTP::Date; 142use HTTP::Date;
127use Convert::Scalar 'weaken'; 143use Convert::Scalar 'weaken';
128use Linux::AIO; 144use IO::AIO;
129 145
130Linux::AIO::min_parallel $::AIO_PARALLEL; 146IO::AIO::min_parallel $::AIO_PARALLEL;
131 147
132Event->io(fd => Linux::AIO::poll_fileno, 148Event->io(fd => IO::AIO::poll_fileno,
133 poll => 'r', async => 1, 149 poll => 'r', async => 1,
134 cb => \&Linux::AIO::poll_cb); 150 cb => \&IO::AIO::poll_cb);
135 151
136our %conn; # $conn{ip}{self} => connobj 152our %conn; # $conn{ip}{self} => connobj
137our %uri; # $uri{ip}{uri}{self} 153our %uri; # $uri{ip}{uri}{self}
138our %blocked; 154our %blocked;
139our %mimetype; 155our %mimetype;
161 my (undef, $iaddr) = unpack_sockaddr_in $peername 177 my (undef, $iaddr) = unpack_sockaddr_in $peername
162 or $self->err(500, "unable to decode peername"); 178 or $self->err(500, "unable to decode peername");
163 179
164 $self->{remote_addr} = 180 $self->{remote_addr} =
165 $self->{remote_id} = inet_ntoa $iaddr; 181 $self->{remote_id} = inet_ntoa $iaddr;
182
166 $self->{time} = $::NOW; 183 $self->{time} = $::NOW;
167 184
168 weaken ($Coro::current->{conn} = $self); 185 weaken ($Coro::current->{conn} = $self);
169 186
170 $::conns++; 187 $::conns++;
176sub DESTROY { 193sub DESTROY {
177 #my $self = shift; 194 #my $self = shift;
178 $::conns--; 195 $::conns--;
179} 196}
180 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);
222
181sub slog { 223sub slog {
182 my $self = shift; 224 my $self = shift;
183 main::slog($_[0], "$self->{remote_id}> $_[1]"); 225 main::slog($_[0], "$self->{remote_id}> $_[1]");
184} 226}
185 227
186sub response { 228sub response {
187 my ($self, $code, $msg, $hdr, $content) = @_; 229 my ($self, $code, $msg, $hdr, $content) = @_;
188 my $res = "HTTP/1.1 $code $msg\015\012"; 230 my $res = "HTTP/1.1 $code $msg\015\012";
231 my $GZ = "";
189 232
190 if (exists $hdr->{Connection}) { 233 if (exists $hdr->{Connection}) {
191 if ($hdr->{Connection} =~ /close/) { 234 if ($hdr->{Connection} =~ /close/) {
192 $self->{h}{connection} = "close" 235 $self->{h}{connection} = "close"
193 } 236 }
199 $self->{h}{connection} = "close" 242 $self->{h}{connection} = "close"
200 } 243 }
201 } 244 }
202 } 245 }
203 246
247 if ($self->{method} ne "HEAD"
248 && $self->{h}{"accept-encoding"} =~ /\bgzip\b/
249 && 400 < length $content
250 && $hdr->{"Content-Length"} == length $content
251 && !exists $hdr->{"Content-Encoding"}
252 ) {
253 my $orig = length $content;
254 $hdr->{"Content-Encoding"} = "gzip";
255 $content = Compress::Zlib::memGzip(\$content);
256 $hdr->{"Content-Length"} = length $content;
257 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig);
258 }
259
204 $res .= "Date: $HTTP_NOW\015\012"; 260 $res .= "Date: $HTTP_NOW\015\012";
261 $res .= "Server: $::NAME\015\012";
205 262
206 while (my ($h, $v) = each %$hdr) { 263 while (my ($h, $v) = each %$hdr) {
207 $res .= "$h: $v\015\012" 264 $res .= "$h: $v\015\012"
208 } 265 }
209 $res .= "\015\012"; 266 $res .= "\015\012";
210 267
211 $res .= $content if defined $content and $self->{method} ne "HEAD"; 268 $res .= $content if defined $content and $self->{method} ne "HEAD";
212 269
213 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). 270 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW).
214 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. 271 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ.
215 " \"$self->{h}{referer}\"\n"; 272 " \"$self->{h}{referer}\"\n";
216 273
217 print $accesslog $log if $accesslog; 274 print $::accesslog $log if $::accesslog;
218 print STDERR $log; 275 print STDERR $log;
219 276
220 $self->{written} += 277 $tbf_top->request(length $res, 1e6);
221 print {$self->{fh}} $res; 278 $self->{written} += print {$self->{fh}} $res;
222} 279}
223 280
224sub err { 281sub err {
225 my $self = shift; 282 my $self = shift;
226 my ($code, $msg, $hdr, $content) = @_; 283 my ($code, $msg, $hdr, $content) = @_;
281 my (%hdr, $h, $v); 338 my (%hdr, $h, $v);
282 339
283 $hdr{lc $1} .= ",$2" 340 $hdr{lc $1} .= ",$2"
284 while $req =~ /\G 341 while $req =~ /\G
285 ([^:\000-\040]+): 342 ([^:\000-\040]+):
286 [\008\040]* 343 [\011\040]*
287 ((?: [^\015\012]+ | \015\012[\008\040] )*) 344 ((?: [^\015\012]+ | \015\012[\011\040] )*)
288 \015\012 345 \015\012
289 /gxc; 346 /gxc;
290 347
291 $req =~ /\G\015\012$/ 348 $req =~ /\G\015\012$/
292 or $self->err(400, "bad request"); 349 or $self->err(400, "bad request");
440 or $self->err(304, "not modified"); 497 or $self->err(304, "not modified");
441 498
442 if (-r "$path/index.html") { 499 if (-r "$path/index.html") {
443 # replace directory "size" by index.html filesize 500 # replace directory "size" by index.html filesize
444 $self->{stat} = [stat ($self->{path} .= "/index.html")]; 501 $self->{stat} = [stat ($self->{path} .= "/index.html")];
445 $self->handle_file($queue_index); 502 $self->handle_file($queue_index, $tbf_top);
446 } else { 503 } else {
447 $self->handle_dir; 504 $self->handle_dir;
448 } 505 }
449 } 506 }
450 } elsif (-f _ && -r _) { 507 } elsif (-f _ && -r _) {
451 -x _ and $self->err(403, "forbidden"); 508 -x _ and $self->err(403, "forbidden");
452 509
453 if (%{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { 510 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
454 my $timeout = $::NOW + 10; 511 my $timeout = $::NOW + 10;
455 while (%{$conn{$self->{remote_id}}} >= $::MAX_TRANSFERS_IP) { 512 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
456 if ($timeout < $::NOW) { 513 if ($timeout < $::NOW) {
457 $self->block($::BLOCKTIME, "too many connections"); 514 $self->block($::BLOCKTIME, "too many connections");
458 } else { 515 } else {
459 $httpevent->wait; 516 $httpevent->wait;
460 } 517 }
461 } 518 }
462 } 519 }
463 520
464 $self->handle_file($queue_file); 521 $self->handle_file($queue_file, $tbf_top);
465 } else { 522 } else {
466 $self->err(404, "not found"); 523 $self->err(404, "not found");
467 } 524 }
468 } 525 }
469} 526}
472 my $self = shift; 529 my $self = shift;
473 my $idx = $self->diridx; 530 my $idx = $self->diridx;
474 531
475 $self->response(200, "ok", 532 $self->response(200, "ok",
476 { 533 {
477 "Content-Type" => "text/html", 534 "Content-Type" => "text/html; charset=utf-8",
478 "Content-Length" => length $idx, 535 "Content-Length" => length $idx,
479 "Last-Modified" => time2str ($self->{stat}[9]), 536 "Last-Modified" => time2str ($self->{stat}[9]),
480 }, 537 },
481 $idx); 538 $idx);
482} 539}
483 540
484sub handle_file { 541sub handle_file {
485 my ($self, $queue) = @_; 542 my ($self, $queue, $tbf) = @_;
486 my $length = $self->{stat}[7]; 543 my $length = $self->{stat}[7];
487 my $hdr = { 544 my $hdr = {
488 "Last-Modified" => time2str ((stat _)[9]), 545 "Last-Modified" => time2str ((stat _)[9]),
546 "Accept-Ranges" => "bytes",
489 }; 547 };
490 548
491 my @code = (200, "ok"); 549 my @code = (200, "ok");
492 my ($l, $h); 550 my ($l, $h);
493 551
509 567
510satisfiable: 568satisfiable:
511 # check for segmented downloads 569 # check for segmented downloads
512 if ($l && $::NO_SEGMENTED) { 570 if ($l && $::NO_SEGMENTED) {
513 my $timeout = $::NOW + 15; 571 my $timeout = $::NOW + 15;
514 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 572 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
515 if ($timeout <= $::NOW) { 573 if ($timeout <= $::NOW) {
516 $self->block($::BLOCKTIME, "segmented downloads are forbidden"); 574 $self->block($::BLOCKTIME, "segmented downloads are forbidden");
517 #$self->err_segmented_download; 575 #$self->err_segmented_download;
518 } else { 576 } else {
519 $httpevent->wait; 577 $httpevent->wait;
536 594
537 $self->response(@code, $hdr, ""); 595 $self->response(@code, $hdr, "");
538 596
539 if ($self->{method} eq "GET") { 597 if ($self->{method} eq "GET") {
540 $self->{time} = $::NOW; 598 $self->{time} = $::NOW;
599 $self->{written} = 0;
541 600
542 my $current = $Coro::current; 601 my $current = $Coro::current;
543 602
544 my ($fh, $buf, $r); 603 my ($fh, $buf, $r);
545 604
561 while ($h > 0) { 620 while ($h > 0) {
562 unless ($locked) { 621 unless ($locked) {
563 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 622 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) {
564 $bufsize = $::BUFSIZE; 623 $bufsize = $::BUFSIZE;
565 $self->{time} = $::NOW; 624 $self->{time} = $::NOW;
625 $self->{written} = 0;
566 } 626 }
567 } 627 }
568 628
569 if ($blocked{$self->{remote_id}}) { 629 if ($blocked{$self->{remote_id}}) {
570 $self->{h}{connection} = "close"; 630 $self->{h}{connection} = "close";
571 die bless {}, err:: 631 die bless {}, err::;
572 } 632 }
573 633
574 if (0) { # !AIO 634 if (0) { # !AIO
575 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h 635 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
576 or last; 636 or last;
581 Coro::ready($current); 641 Coro::ready($current);
582 }); 642 });
583 &Coro::schedule; 643 &Coro::schedule;
584 last unless $r; 644 last unless $r;
585 } 645 }
646
647 $tbf->request(length $buf);
586 my $w = syswrite $self->{fh}, $buf 648 my $w = syswrite $self->{fh}, $buf
587 or last; 649 or last;
588 $::written += $w; 650 $::written += $w;
589 $self->{written} += $w; 651 $self->{written} += $w;
590 $l += $r; 652 $l += $r;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines