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.57 by root, Mon Dec 3 05:40:09 2001 UTC vs.
Revision 1.67 by root, Sun May 19 21:00:47 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 => 200000;
57 61
58my @newcons; 62my @newcons;
59my @pool; 63my @pool;
60 64
61# one "execution thread" 65# one "execution thread"
161 my (undef, $iaddr) = unpack_sockaddr_in $peername 165 my (undef, $iaddr) = unpack_sockaddr_in $peername
162 or $self->err(500, "unable to decode peername"); 166 or $self->err(500, "unable to decode peername");
163 167
164 $self->{remote_addr} = 168 $self->{remote_addr} =
165 $self->{remote_id} = inet_ntoa $iaddr; 169 $self->{remote_id} = inet_ntoa $iaddr;
170
166 $self->{time} = $::NOW; 171 $self->{time} = $::NOW;
167 172
168 weaken ($Coro::current->{conn} = $self); 173 weaken ($Coro::current->{conn} = $self);
169 174
170 $::conns++; 175 $::conns++;
184} 189}
185 190
186sub response { 191sub response {
187 my ($self, $code, $msg, $hdr, $content) = @_; 192 my ($self, $code, $msg, $hdr, $content) = @_;
188 my $res = "HTTP/1.1 $code $msg\015\012"; 193 my $res = "HTTP/1.1 $code $msg\015\012";
194 my $GZ = "";
189 195
190 if (exists $hdr->{Connection}) { 196 if (exists $hdr->{Connection}) {
191 if ($hdr->{Connection} =~ /close/) { 197 if ($hdr->{Connection} =~ /close/) {
192 $self->{h}{connection} = "close" 198 $self->{h}{connection} = "close"
193 } 199 }
199 $self->{h}{connection} = "close" 205 $self->{h}{connection} = "close"
200 } 206 }
201 } 207 }
202 } 208 }
203 209
210 if ($self->{method} ne "HEAD"
211 && $self->{h}{"accept-encoding"} =~ /\bgzip\b/
212 && 400 < length $content
213 && $hdr->{"Content-Length"} == length $content
214 && !exists $hdr->{"Content-Encoding"}
215 ) {
216 my $orig = length $content;
217 $hdr->{"Content-Encoding"} = "gzip";
218 $content = Compress::Zlib::memGzip(\$content);
219 $hdr->{"Content-Length"} = length $content;
220 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig);
221 }
222
204 $res .= "Date: $HTTP_NOW\015\012"; 223 $res .= "Date: $HTTP_NOW\015\012";
205 224
206 while (my ($h, $v) = each %$hdr) { 225 while (my ($h, $v) = each %$hdr) {
207 $res .= "$h: $v\015\012" 226 $res .= "$h: $v\015\012"
208 } 227 }
209 $res .= "\015\012"; 228 $res .= "\015\012";
210 229
211 $res .= $content if defined $content and $self->{method} ne "HEAD"; 230 $res .= $content if defined $content and $self->{method} ne "HEAD";
212 231
213 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). 232 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW).
214 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. 233 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ.
215 " \"$self->{h}{referer}\"\n"; 234 " \"$self->{h}{referer}\"\n";
216 235
217 print $accesslog $log if $accesslog; 236 print $::accesslog $log if $::accesslog;
218 print STDERR $log; 237 print STDERR $log;
219 238
220 $self->{written} += 239 $tbf_top->request(length $res, 1e6);
221 print {$self->{fh}} $res; 240 $self->{written} += print {$self->{fh}} $res;
222} 241}
223 242
224sub err { 243sub err {
225 my $self = shift; 244 my $self = shift;
226 my ($code, $msg, $hdr, $content) = @_; 245 my ($code, $msg, $hdr, $content) = @_;
281 my (%hdr, $h, $v); 300 my (%hdr, $h, $v);
282 301
283 $hdr{lc $1} .= ",$2" 302 $hdr{lc $1} .= ",$2"
284 while $req =~ /\G 303 while $req =~ /\G
285 ([^:\000-\040]+): 304 ([^:\000-\040]+):
286 [\008\040]* 305 [\011\040]*
287 ((?: [^\015\012]+ | \015\012[\008\040] )*) 306 ((?: [^\015\012]+ | \015\012[\011\040] )*)
288 \015\012 307 \015\012
289 /gxc; 308 /gxc;
290 309
291 $req =~ /\G\015\012$/ 310 $req =~ /\G\015\012$/
292 or $self->err(400, "bad request"); 311 or $self->err(400, "bad request");
440 or $self->err(304, "not modified"); 459 or $self->err(304, "not modified");
441 460
442 if (-r "$path/index.html") { 461 if (-r "$path/index.html") {
443 # replace directory "size" by index.html filesize 462 # replace directory "size" by index.html filesize
444 $self->{stat} = [stat ($self->{path} .= "/index.html")]; 463 $self->{stat} = [stat ($self->{path} .= "/index.html")];
445 $self->handle_file($queue_index); 464 $self->handle_file($queue_index, $tbf_top);
446 } else { 465 } else {
447 $self->handle_dir; 466 $self->handle_dir;
448 } 467 }
449 } 468 }
450 } elsif (-f _ && -r _) { 469 } elsif (-f _ && -r _) {
451 -x _ and $self->err(403, "forbidden"); 470 -x _ and $self->err(403, "forbidden");
452 471
453 if (%{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { 472 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
454 my $timeout = $::NOW + 10; 473 my $timeout = $::NOW + 10;
455 while (%{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { 474 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
456 if ($timeout < $::NOW) { 475 if ($timeout < $::NOW) {
457 $self->block($::BLOCKTIME, "too many connections"); 476 $self->block($::BLOCKTIME, "too many connections");
458 } else { 477 } else {
459 $httpevent->wait; 478 $httpevent->wait;
460 } 479 }
461 } 480 }
462 } 481 }
463 482
464 $self->handle_file($queue_file); 483 $self->handle_file($queue_file, $tbf_top);
465 } else { 484 } else {
466 $self->err(404, "not found"); 485 $self->err(404, "not found");
467 } 486 }
468 } 487 }
469} 488}
480 }, 499 },
481 $idx); 500 $idx);
482} 501}
483 502
484sub handle_file { 503sub handle_file {
485 my ($self, $queue) = @_; 504 my ($self, $queue, $tbf) = @_;
486 my $length = $self->{stat}[7]; 505 my $length = $self->{stat}[7];
487 my $hdr = { 506 my $hdr = {
488 "Last-Modified" => time2str ((stat _)[9]), 507 "Last-Modified" => time2str ((stat _)[9]),
489 }; 508 };
490 509
509 528
510satisfiable: 529satisfiable:
511 # check for segmented downloads 530 # check for segmented downloads
512 if ($l && $::NO_SEGMENTED) { 531 if ($l && $::NO_SEGMENTED) {
513 my $timeout = $::NOW + 15; 532 my $timeout = $::NOW + 15;
514 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 533 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
515 if ($timeout <= $::NOW) { 534 if ($timeout <= $::NOW) {
516 $self->block($::BLOCKTIME, "segmented downloads are forbidden"); 535 $self->block($::BLOCKTIME, "segmented downloads are forbidden");
517 #$self->err_segmented_download; 536 #$self->err_segmented_download;
518 } else { 537 } else {
519 $httpevent->wait; 538 $httpevent->wait;
566 } 585 }
567 } 586 }
568 587
569 if ($blocked{$self->{remote_id}}) { 588 if ($blocked{$self->{remote_id}}) {
570 $self->{h}{connection} = "close"; 589 $self->{h}{connection} = "close";
571 die bless {}, err:: 590 die bless {}, err::;
572 } 591 }
573 592
574 if (0) { # !AIO 593 if (0) { # !AIO
575 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h 594 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
576 or last; 595 or last;
581 Coro::ready($current); 600 Coro::ready($current);
582 }); 601 });
583 &Coro::schedule; 602 &Coro::schedule;
584 last unless $r; 603 last unless $r;
585 } 604 }
605
606 $tbf->request(length $buf);
586 my $w = syswrite $self->{fh}, $buf 607 my $w = syswrite $self->{fh}, $buf
587 or last; 608 or last;
588 $::written += $w; 609 $::written += $w;
589 $self->{written} += $w; 610 $self->{written} += $w;
590 $l += $r; 611 $l += $r;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines