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.63 by root, Wed Jan 23 03:07:05 2002 UTC vs.
Revision 1.80 by root, Fri Dec 1 04:18:32 2006 UTC

1use Coro; 1use Coro;
2use Coro::Semaphore; 2use Coro::Semaphore;
3use Coro::Event; 3use Coro::Event;
4use Coro::Socket; 4use Coro::Socket;
5use Coro::Signal; 5use Coro::Signal;
6use Coro::AIO ();
6 7
7use HTTP::Date; 8use HTTP::Date;
8use POSIX (); 9use POSIX ();
9 10
10use Compress::Zlib (); 11use Compress::Zlib ();
21our $accesslog; 22our $accesslog;
22our $errorlog; 23our $errorlog;
23 24
24our $NOW; 25our $NOW;
25our $HTTP_NOW; 26our $HTTP_NOW;
27
28our $ERROR_LOG;
29our $ACCESS_LOG;
26 30
27Event->timer(interval => 1, hard => 1, cb => sub { 31Event->timer(interval => 1, hard => 1, cb => sub {
28 $NOW = time; 32 $NOW = time;
29 $HTTP_NOW = time2str $NOW; 33 $HTTP_NOW = time2str $NOW;
30})->now; 34})->now;
54our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 58our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
55our $httpevent = new Coro::Signal; 59our $httpevent = new Coro::Signal;
56 60
57our $queue_file = new transferqueue $MAX_TRANSFERS; 61our $queue_file = new transferqueue $MAX_TRANSFERS;
58our $queue_index = new transferqueue 10; 62our $queue_index = new transferqueue 10;
63
64our $tbf_top = new tbf rate => $TBF_RATE || 100000;
65
66my $unused_bytes = 0;
67my $unused_last = time;
68
69sub unused_bandwidth {
70 $unused_bytes += $_[0];
71 if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) {
72 $unused_last = $NOW;
73 $unused_bytes = 0;
74 $queue_file->force_wake_next;
75 slog 1, "forced filetransfer due to unused bandwidth";
76 }
77}
59 78
60my @newcons; 79my @newcons;
61my @pool; 80my @pool;
62 81
63# one "execution thread" 82# one "execution thread"
95 if (@pool) { 114 if (@pool) {
96 (pop @pool)->ready; 115 (pop @pool)->ready;
97 } else { 116 } else {
98 async \&handler; 117 async \&handler;
99 } 118 }
100
101 } 119 }
102 }; 120 };
103} 121}
104 122
105my $http_port = new Coro::Socket 123my $http_port = new Coro::Socket
122 listen_on $http_port; 140 listen_on $http_port;
123} 141}
124 142
125package conn; 143package conn;
126 144
145use strict;
146use bytes;
147
127use Socket; 148use Socket;
128use HTTP::Date; 149use HTTP::Date;
129use Convert::Scalar 'weaken'; 150use Convert::Scalar 'weaken';
130use Linux::AIO; 151use IO::AIO;
131 152
132Linux::AIO::min_parallel $::AIO_PARALLEL; 153IO::AIO::min_parallel $::AIO_PARALLEL;
133 154
134Event->io(fd => Linux::AIO::poll_fileno, 155Event->io (fd => IO::AIO::poll_fileno,
135 poll => 'r', async => 1, 156 poll => 'r', async => 1,
136 cb => \&Linux::AIO::poll_cb); 157 cb => \&IO::AIO::poll_cb);
137 158
138our %conn; # $conn{ip}{self} => connobj 159our %conn; # $conn{ip}{self} => connobj
139our %uri; # $uri{ip}{uri}{self} 160our %uri; # $uri{ip}{uri}{self}
140our %blocked; 161our %blocked;
141our %mimetype; 162our %mimetype;
179sub DESTROY { 200sub DESTROY {
180 #my $self = shift; 201 #my $self = shift;
181 $::conns--; 202 $::conns--;
182} 203}
183 204
205sub prune_cache {
206 my $hash = $_[0];
207
208 for (keys %$hash) {
209 if (ref $hash->{$_} eq HASH::) {
210 prune_cache($hash->{$_});
211 unless (scalar keys %{$hash->{$_}}) {
212 delete $hash->{$_};
213 }
214 }
215 }
216}
217
218sub prune_caches {
219 prune_cache \%conn;
220 prune_cache \%uri;
221
222 for (keys %blocked) {
223 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
224 }
225}
226
227Event->timer(interval => 60, cb => \&prune_caches);
228
184sub slog { 229sub slog {
185 my $self = shift; 230 my $self = shift;
186 main::slog($_[0], "$self->{remote_id}> $_[1]"); 231 main::slog($_[0], "$self->{remote_id}> $_[1]");
187} 232}
188 233
205 } 250 }
206 } 251 }
207 252
208 if ($self->{method} ne "HEAD" 253 if ($self->{method} ne "HEAD"
209 && $self->{h}{"accept-encoding"} =~ /\bgzip\b/ 254 && $self->{h}{"accept-encoding"} =~ /\bgzip\b/
255 && 400 < length $content
210 && $hdr->{"Content-Length"} == length $content 256 && $hdr->{"Content-Length"} == length $content
211 && !exists $hdr->{"Content-Encoding"} 257 && !exists $hdr->{"Content-Encoding"}
212 ) { 258 ) {
213 my $orig = length $content; 259 my $orig = length $content;
214 $hdr->{"Content-Encoding"} = "gzip"; 260 $hdr->{"Content-Encoding"} = "gzip";
216 $hdr->{"Content-Length"} = length $content; 262 $hdr->{"Content-Length"} = length $content;
217 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); 263 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig);
218 } 264 }
219 265
220 $res .= "Date: $HTTP_NOW\015\012"; 266 $res .= "Date: $HTTP_NOW\015\012";
267 $res .= "Server: $::NAME\015\012";
221 268
222 while (my ($h, $v) = each %$hdr) { 269 while (my ($h, $v) = each %$hdr) {
223 $res .= "$h: $v\015\012" 270 $res .= "$h: $v\015\012"
224 } 271 }
225 $res .= "\015\012"; 272 $res .= "\015\012";
226 273
227 $res .= $content if defined $content and $self->{method} ne "HEAD"; 274 $res .= $content if defined $content and $self->{method} ne "HEAD";
228 275
229 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). 276 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW).
230 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. 277 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ.
231 " \"$self->{h}{referer}\"\n"; 278 " \"$self->{h}{referer}\"\n";
232 279
233 print $accesslog $log if $accesslog; 280 print $::accesslog $log if $::accesslog;
234 print STDERR $log; 281 print STDERR $log;
235 282
236 $self->{written} += 283 $tbf_top->request(length $res, 1e6);
237 print {$self->{fh}} $res; 284 $self->{written} += print {$self->{fh}} $res;
238} 285}
239 286
240sub err { 287sub err {
241 my $self = shift; 288 my $self = shift;
242 my ($code, $msg, $hdr, $content) = @_; 289 my ($code, $msg, $hdr, $content) = @_;
297 my (%hdr, $h, $v); 344 my (%hdr, $h, $v);
298 345
299 $hdr{lc $1} .= ",$2" 346 $hdr{lc $1} .= ",$2"
300 while $req =~ /\G 347 while $req =~ /\G
301 ([^:\000-\040]+): 348 ([^:\000-\040]+):
302 [\010\040]* 349 [\011\040]*
303 ((?: [^\015\012]+ | \015\012[\010\040] )*) 350 ((?: [^\015\012]+ | \015\012[\011\040] )*)
304 \015\012 351 \015\012
305 /gxc; 352 /gxc;
306 353
307 $req =~ /\G\015\012$/ 354 $req =~ /\G\015\012$/
308 or $self->err(400, "bad request"); 355 or $self->err(400, "bad request");
456 or $self->err(304, "not modified"); 503 or $self->err(304, "not modified");
457 504
458 if (-r "$path/index.html") { 505 if (-r "$path/index.html") {
459 # replace directory "size" by index.html filesize 506 # replace directory "size" by index.html filesize
460 $self->{stat} = [stat ($self->{path} .= "/index.html")]; 507 $self->{stat} = [stat ($self->{path} .= "/index.html")];
461 $self->handle_file($queue_index); 508 $self->handle_file($queue_index, $tbf_top);
462 } else { 509 } else {
463 $self->handle_dir; 510 $self->handle_dir;
464 } 511 }
465 } 512 }
466 } elsif (-f _ && -r _) { 513 } elsif (-f _ && -r _) {
475 $httpevent->wait; 522 $httpevent->wait;
476 } 523 }
477 } 524 }
478 } 525 }
479 526
480 $self->handle_file($queue_file); 527 $self->handle_file($queue_file, $tbf_top);
481 } else { 528 } else {
482 $self->err(404, "not found"); 529 $self->err(404, "not found");
483 } 530 }
484 } 531 }
485} 532}
488 my $self = shift; 535 my $self = shift;
489 my $idx = $self->diridx; 536 my $idx = $self->diridx;
490 537
491 $self->response(200, "ok", 538 $self->response(200, "ok",
492 { 539 {
493 "Content-Type" => "text/html", 540 "Content-Type" => "text/html; charset=utf-8",
494 "Content-Length" => length $idx, 541 "Content-Length" => length $idx,
495 "Last-Modified" => time2str ($self->{stat}[9]), 542 "Last-Modified" => time2str ($self->{stat}[9]),
496 }, 543 },
497 $idx); 544 $idx);
498} 545}
499 546
500sub handle_file { 547sub handle_file {
501 my ($self, $queue) = @_; 548 my ($self, $queue, $tbf) = @_;
502 my $length = $self->{stat}[7]; 549 my $length = $self->{stat}[7];
503 my $hdr = { 550 my $hdr = {
504 "Last-Modified" => time2str ((stat _)[9]), 551 "Last-Modified" => time2str ((stat _)[9]),
552 "Accept-Ranges" => "bytes",
505 }; 553 };
506 554
507 my @code = (200, "ok"); 555 my @code = (200, "ok");
508 my ($l, $h); 556 my ($l, $h);
509 557
552 600
553 $self->response(@code, $hdr, ""); 601 $self->response(@code, $hdr, "");
554 602
555 if ($self->{method} eq "GET") { 603 if ($self->{method} eq "GET") {
556 $self->{time} = $::NOW; 604 $self->{time} = $::NOW;
605 $self->{written} = 0;
557 606
558 my $current = $Coro::current; 607 my $fh;
559
560 my ($fh, $buf, $r);
561 608
562 open $fh, "<", $self->{path} 609 open $fh, "<", $self->{path}
563 or die "$self->{path}: late open failure ($!)"; 610 or die "$self->{path}: late open failure ($!)";
564 611
565 $h -= $l - 1; 612 $h -= $l - 1;
577 while ($h > 0) { 624 while ($h > 0) {
578 unless ($locked) { 625 unless ($locked) {
579 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 626 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) {
580 $bufsize = $::BUFSIZE; 627 $bufsize = $::BUFSIZE;
581 $self->{time} = $::NOW; 628 $self->{time} = $::NOW;
629 $self->{written} = 0;
582 } 630 }
583 } 631 }
584 632
585 if ($blocked{$self->{remote_id}}) { 633 if ($blocked{$self->{remote_id}}) {
586 $self->{h}{connection} = "close"; 634 $self->{h}{connection} = "close";
587 die bless {}, err::; 635 die bless {}, err::;
588 } 636 }
589 637
590 if (0) { # !AIO 638 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0
591 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
592 or last; 639 or last;
593 } else { 640
594 aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), 641 $tbf->request (length $buf);
595 $buf, 0, sub {
596 $r = $_[0];
597 Coro::ready($current);
598 });
599 &Coro::schedule;
600 last unless $r;
601 }
602 my $w = syswrite $self->{fh}, $buf 642 my $w = syswrite $self->{fh}, $buf
603 or last; 643 or last;
604 $::written += $w; 644 $::written += $w;
605 $self->{written} += $w; 645 $self->{written} += $w;
606 $l += $r; 646 $l += $w;
607 } 647 }
608 648
609 close $fh; 649 close $fh;
610 } 650 }
611} 651}
612 652
6131; 6531
654

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines