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.61 by root, Thu Jan 3 01:20:17 2002 UTC vs.
Revision 1.72 by root, Tue Sep 10 03:08:03 2002 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
50} 52}
51 53
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 slots => $MAX_TRANSFERS, maxsize => 256*1024*1024; 57our $queue_file = new transferqueue $MAX_TRANSFERS;
56our $queue_index = new transferqueue slots => 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
185} 201}
186 202
187sub response { 203sub response {
188 my ($self, $code, $msg, $hdr, $content) = @_; 204 my ($self, $code, $msg, $hdr, $content) = @_;
189 my $res = "HTTP/1.1 $code $msg\015\012"; 205 my $res = "HTTP/1.1 $code $msg\015\012";
206 my $GZ = "";
190 207
191 if (exists $hdr->{Connection}) { 208 if (exists $hdr->{Connection}) {
192 if ($hdr->{Connection} =~ /close/) { 209 if ($hdr->{Connection} =~ /close/) {
193 $self->{h}{connection} = "close" 210 $self->{h}{connection} = "close"
194 } 211 }
200 $self->{h}{connection} = "close" 217 $self->{h}{connection} = "close"
201 } 218 }
202 } 219 }
203 } 220 }
204 221
222 if ($self->{method} ne "HEAD"
223 && $self->{h}{"accept-encoding"} =~ /\bgzip\b/
224 && 400 < length $content
225 && $hdr->{"Content-Length"} == length $content
226 && !exists $hdr->{"Content-Encoding"}
227 ) {
228 my $orig = length $content;
229 $hdr->{"Content-Encoding"} = "gzip";
230 $content = Compress::Zlib::memGzip(\$content);
231 $hdr->{"Content-Length"} = length $content;
232 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig);
233 }
234
205 $res .= "Date: $HTTP_NOW\015\012"; 235 $res .= "Date: $HTTP_NOW\015\012";
236 $res .= "Server: $::NAME\015\012";
206 237
207 while (my ($h, $v) = each %$hdr) { 238 while (my ($h, $v) = each %$hdr) {
208 $res .= "$h: $v\015\012" 239 $res .= "$h: $v\015\012"
209 } 240 }
210 $res .= "\015\012"; 241 $res .= "\015\012";
211 242
212 $res .= $content if defined $content and $self->{method} ne "HEAD"; 243 $res .= $content if defined $content and $self->{method} ne "HEAD";
213 244
214 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).
215 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. 246 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ.
216 " \"$self->{h}{referer}\"\n"; 247 " \"$self->{h}{referer}\"\n";
217 248
218 print $accesslog $log if $accesslog; 249 print $::accesslog $log if $::accesslog;
219 print STDERR $log; 250 print STDERR $log;
220 251
221 $self->{written} += 252 $tbf_top->request(length $res, 1e6);
222 print {$self->{fh}} $res; 253 $self->{written} += print {$self->{fh}} $res;
223} 254}
224 255
225sub err { 256sub err {
226 my $self = shift; 257 my $self = shift;
227 my ($code, $msg, $hdr, $content) = @_; 258 my ($code, $msg, $hdr, $content) = @_;
282 my (%hdr, $h, $v); 313 my (%hdr, $h, $v);
283 314
284 $hdr{lc $1} .= ",$2" 315 $hdr{lc $1} .= ",$2"
285 while $req =~ /\G 316 while $req =~ /\G
286 ([^:\000-\040]+): 317 ([^:\000-\040]+):
287 [\008\040]* 318 [\011\040]*
288 ((?: [^\015\012]+ | \015\012[\008\040] )*) 319 ((?: [^\015\012]+ | \015\012[\011\040] )*)
289 \015\012 320 \015\012
290 /gxc; 321 /gxc;
291 322
292 $req =~ /\G\015\012$/ 323 $req =~ /\G\015\012$/
293 or $self->err(400, "bad request"); 324 or $self->err(400, "bad request");
441 or $self->err(304, "not modified"); 472 or $self->err(304, "not modified");
442 473
443 if (-r "$path/index.html") { 474 if (-r "$path/index.html") {
444 # replace directory "size" by index.html filesize 475 # replace directory "size" by index.html filesize
445 $self->{stat} = [stat ($self->{path} .= "/index.html")]; 476 $self->{stat} = [stat ($self->{path} .= "/index.html")];
446 $self->handle_file($queue_index); 477 $self->handle_file($queue_index, $tbf_top);
447 } else { 478 } else {
448 $self->handle_dir; 479 $self->handle_dir;
449 } 480 }
450 } 481 }
451 } elsif (-f _ && -r _) { 482 } elsif (-f _ && -r _) {
460 $httpevent->wait; 491 $httpevent->wait;
461 } 492 }
462 } 493 }
463 } 494 }
464 495
465 $self->handle_file($queue_file); 496 $self->handle_file($queue_file, $tbf_top);
466 } else { 497 } else {
467 $self->err(404, "not found"); 498 $self->err(404, "not found");
468 } 499 }
469 } 500 }
470} 501}
481 }, 512 },
482 $idx); 513 $idx);
483} 514}
484 515
485sub handle_file { 516sub handle_file {
486 my ($self, $queue) = @_; 517 my ($self, $queue, $tbf) = @_;
487 my $length = $self->{stat}[7]; 518 my $length = $self->{stat}[7];
488 my $hdr = { 519 my $hdr = {
489 "Last-Modified" => time2str ((stat _)[9]), 520 "Last-Modified" => time2str ((stat _)[9]),
490 }; 521 };
491 522
537 568
538 $self->response(@code, $hdr, ""); 569 $self->response(@code, $hdr, "");
539 570
540 if ($self->{method} eq "GET") { 571 if ($self->{method} eq "GET") {
541 $self->{time} = $::NOW; 572 $self->{time} = $::NOW;
573 $self->{written} = 0;
542 574
543 my $current = $Coro::current; 575 my $current = $Coro::current;
544 576
545 my ($fh, $buf, $r); 577 my ($fh, $buf, $r);
546 578
562 while ($h > 0) { 594 while ($h > 0) {
563 unless ($locked) { 595 unless ($locked) {
564 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 596 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) {
565 $bufsize = $::BUFSIZE; 597 $bufsize = $::BUFSIZE;
566 $self->{time} = $::NOW; 598 $self->{time} = $::NOW;
599 $self->{written} = 0;
567 } 600 }
568 } 601 }
569 602
570 if ($blocked{$self->{remote_id}}) { 603 if ($blocked{$self->{remote_id}}) {
571 $self->{h}{connection} = "close"; 604 $self->{h}{connection} = "close";
582 Coro::ready($current); 615 Coro::ready($current);
583 }); 616 });
584 &Coro::schedule; 617 &Coro::schedule;
585 last unless $r; 618 last unless $r;
586 } 619 }
620
621 $tbf->request(length $buf);
587 my $w = syswrite $self->{fh}, $buf 622 my $w = syswrite $self->{fh}, $buf
588 or last; 623 or last;
589 $::written += $w; 624 $::written += $w;
590 $self->{written} += $w; 625 $self->{written} += $w;
591 $l += $r; 626 $l += $r;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines