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.59 by root, Wed Dec 5 02:02:23 2001 UTC vs.
Revision 1.71 by root, Mon Jul 29 21:41:54 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
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 => 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"
161 my (undef, $iaddr) = unpack_sockaddr_in $peername 178 my (undef, $iaddr) = unpack_sockaddr_in $peername
162 or $self->err(500, "unable to decode peername"); 179 or $self->err(500, "unable to decode peername");
163 180
164 $self->{remote_addr} = 181 $self->{remote_addr} =
165 $self->{remote_id} = inet_ntoa $iaddr; 182 $self->{remote_id} = inet_ntoa $iaddr;
183
166 $self->{time} = $::NOW; 184 $self->{time} = $::NOW;
167 185
168 weaken ($Coro::current->{conn} = $self); 186 weaken ($Coro::current->{conn} = $self);
169 187
170 $::conns++; 188 $::conns++;
184} 202}
185 203
186sub response { 204sub response {
187 my ($self, $code, $msg, $hdr, $content) = @_; 205 my ($self, $code, $msg, $hdr, $content) = @_;
188 my $res = "HTTP/1.1 $code $msg\015\012"; 206 my $res = "HTTP/1.1 $code $msg\015\012";
207 my $GZ = "";
189 208
190 if (exists $hdr->{Connection}) { 209 if (exists $hdr->{Connection}) {
191 if ($hdr->{Connection} =~ /close/) { 210 if ($hdr->{Connection} =~ /close/) {
192 $self->{h}{connection} = "close" 211 $self->{h}{connection} = "close"
193 } 212 }
199 $self->{h}{connection} = "close" 218 $self->{h}{connection} = "close"
200 } 219 }
201 } 220 }
202 } 221 }
203 222
223 if ($self->{method} ne "HEAD"
224 && $self->{h}{"accept-encoding"} =~ /\bgzip\b/
225 && 400 < length $content
226 && $hdr->{"Content-Length"} == length $content
227 && !exists $hdr->{"Content-Encoding"}
228 ) {
229 my $orig = length $content;
230 $hdr->{"Content-Encoding"} = "gzip";
231 $content = Compress::Zlib::memGzip(\$content);
232 $hdr->{"Content-Length"} = length $content;
233 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig);
234 }
235
204 $res .= "Date: $HTTP_NOW\015\012"; 236 $res .= "Date: $HTTP_NOW\015\012";
237 $res .= "Server: $::NAME\015\012";
205 238
206 while (my ($h, $v) = each %$hdr) { 239 while (my ($h, $v) = each %$hdr) {
207 $res .= "$h: $v\015\012" 240 $res .= "$h: $v\015\012"
208 } 241 }
209 $res .= "\015\012"; 242 $res .= "\015\012";
210 243
211 $res .= $content if defined $content and $self->{method} ne "HEAD"; 244 $res .= $content if defined $content and $self->{method} ne "HEAD";
212 245
213 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). 246 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW).
214 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. 247 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ.
215 " \"$self->{h}{referer}\"\n"; 248 " \"$self->{h}{referer}\"\n";
216 249
217 print $accesslog $log if $accesslog; 250 print $::accesslog $log if $::accesslog;
218 print STDERR $log; 251 print STDERR $log;
219 252
220 $self->{written} += 253 $tbf_top->request(length $res, 1e6);
221 print {$self->{fh}} $res; 254 $self->{written} += print {$self->{fh}} $res;
222} 255}
223 256
224sub err { 257sub err {
225 my $self = shift; 258 my $self = shift;
226 my ($code, $msg, $hdr, $content) = @_; 259 my ($code, $msg, $hdr, $content) = @_;
281 my (%hdr, $h, $v); 314 my (%hdr, $h, $v);
282 315
283 $hdr{lc $1} .= ",$2" 316 $hdr{lc $1} .= ",$2"
284 while $req =~ /\G 317 while $req =~ /\G
285 ([^:\000-\040]+): 318 ([^:\000-\040]+):
286 [\008\040]* 319 [\011\040]*
287 ((?: [^\015\012]+ | \015\012[\008\040] )*) 320 ((?: [^\015\012]+ | \015\012[\011\040] )*)
288 \015\012 321 \015\012
289 /gxc; 322 /gxc;
290 323
291 $req =~ /\G\015\012$/ 324 $req =~ /\G\015\012$/
292 or $self->err(400, "bad request"); 325 or $self->err(400, "bad request");
440 or $self->err(304, "not modified"); 473 or $self->err(304, "not modified");
441 474
442 if (-r "$path/index.html") { 475 if (-r "$path/index.html") {
443 # replace directory "size" by index.html filesize 476 # replace directory "size" by index.html filesize
444 $self->{stat} = [stat ($self->{path} .= "/index.html")]; 477 $self->{stat} = [stat ($self->{path} .= "/index.html")];
445 $self->handle_file($queue_index); 478 $self->handle_file($queue_index, $tbf_top);
446 } else { 479 } else {
447 $self->handle_dir; 480 $self->handle_dir;
448 } 481 }
449 } 482 }
450 } elsif (-f _ && -r _) { 483 } elsif (-f _ && -r _) {
459 $httpevent->wait; 492 $httpevent->wait;
460 } 493 }
461 } 494 }
462 } 495 }
463 496
464 $self->handle_file($queue_file); 497 $self->handle_file($queue_file, $tbf_top);
465 } else { 498 } else {
466 $self->err(404, "not found"); 499 $self->err(404, "not found");
467 } 500 }
468 } 501 }
469} 502}
480 }, 513 },
481 $idx); 514 $idx);
482} 515}
483 516
484sub handle_file { 517sub handle_file {
485 my ($self, $queue) = @_; 518 my ($self, $queue, $tbf) = @_;
486 my $length = $self->{stat}[7]; 519 my $length = $self->{stat}[7];
487 my $hdr = { 520 my $hdr = {
488 "Last-Modified" => time2str ((stat _)[9]), 521 "Last-Modified" => time2str ((stat _)[9]),
489 }; 522 };
490 523
536 569
537 $self->response(@code, $hdr, ""); 570 $self->response(@code, $hdr, "");
538 571
539 if ($self->{method} eq "GET") { 572 if ($self->{method} eq "GET") {
540 $self->{time} = $::NOW; 573 $self->{time} = $::NOW;
574 $self->{written} = 0;
541 575
542 my $current = $Coro::current; 576 my $current = $Coro::current;
543 577
544 my ($fh, $buf, $r); 578 my ($fh, $buf, $r);
545 579
561 while ($h > 0) { 595 while ($h > 0) {
562 unless ($locked) { 596 unless ($locked) {
563 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 597 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) {
564 $bufsize = $::BUFSIZE; 598 $bufsize = $::BUFSIZE;
565 $self->{time} = $::NOW; 599 $self->{time} = $::NOW;
600 $self->{written} = 0;
566 } 601 }
567 } 602 }
568 603
569 if ($blocked{$self->{remote_id}}) { 604 if ($blocked{$self->{remote_id}}) {
570 $self->{h}{connection} = "close"; 605 $self->{h}{connection} = "close";
581 Coro::ready($current); 616 Coro::ready($current);
582 }); 617 });
583 &Coro::schedule; 618 &Coro::schedule;
584 last unless $r; 619 last unless $r;
585 } 620 }
621
622 $tbf->request(length $buf);
586 my $w = syswrite $self->{fh}, $buf 623 my $w = syswrite $self->{fh}, $buf
587 or last; 624 or last;
588 $::written += $w; 625 $::written += $w;
589 $self->{written} += $w; 626 $self->{written} += $w;
590 $l += $r; 627 $l += $r;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines