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.61 by root, Thu Jan 3 01:20:17 2002 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
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 => 200000;
57 61
58my @newcons; 62my @newcons;
59my @pool; 63my @pool;
60 64
61# one "execution thread" 65# one "execution thread"
185} 189}
186 190
187sub response { 191sub response {
188 my ($self, $code, $msg, $hdr, $content) = @_; 192 my ($self, $code, $msg, $hdr, $content) = @_;
189 my $res = "HTTP/1.1 $code $msg\015\012"; 193 my $res = "HTTP/1.1 $code $msg\015\012";
194 my $GZ = "";
190 195
191 if (exists $hdr->{Connection}) { 196 if (exists $hdr->{Connection}) {
192 if ($hdr->{Connection} =~ /close/) { 197 if ($hdr->{Connection} =~ /close/) {
193 $self->{h}{connection} = "close" 198 $self->{h}{connection} = "close"
194 } 199 }
200 $self->{h}{connection} = "close" 205 $self->{h}{connection} = "close"
201 } 206 }
202 } 207 }
203 } 208 }
204 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
205 $res .= "Date: $HTTP_NOW\015\012"; 223 $res .= "Date: $HTTP_NOW\015\012";
206 224
207 while (my ($h, $v) = each %$hdr) { 225 while (my ($h, $v) = each %$hdr) {
208 $res .= "$h: $v\015\012" 226 $res .= "$h: $v\015\012"
209 } 227 }
210 $res .= "\015\012"; 228 $res .= "\015\012";
211 229
212 $res .= $content if defined $content and $self->{method} ne "HEAD"; 230 $res .= $content if defined $content and $self->{method} ne "HEAD";
213 231
214 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).
215 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. 233 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ.
216 " \"$self->{h}{referer}\"\n"; 234 " \"$self->{h}{referer}\"\n";
217 235
218 print $accesslog $log if $accesslog; 236 print $::accesslog $log if $::accesslog;
219 print STDERR $log; 237 print STDERR $log;
220 238
221 $self->{written} += 239 $tbf_top->request(length $res, 1e6);
222 print {$self->{fh}} $res; 240 $self->{written} += print {$self->{fh}} $res;
223} 241}
224 242
225sub err { 243sub err {
226 my $self = shift; 244 my $self = shift;
227 my ($code, $msg, $hdr, $content) = @_; 245 my ($code, $msg, $hdr, $content) = @_;
282 my (%hdr, $h, $v); 300 my (%hdr, $h, $v);
283 301
284 $hdr{lc $1} .= ",$2" 302 $hdr{lc $1} .= ",$2"
285 while $req =~ /\G 303 while $req =~ /\G
286 ([^:\000-\040]+): 304 ([^:\000-\040]+):
287 [\008\040]* 305 [\011\040]*
288 ((?: [^\015\012]+ | \015\012[\008\040] )*) 306 ((?: [^\015\012]+ | \015\012[\011\040] )*)
289 \015\012 307 \015\012
290 /gxc; 308 /gxc;
291 309
292 $req =~ /\G\015\012$/ 310 $req =~ /\G\015\012$/
293 or $self->err(400, "bad request"); 311 or $self->err(400, "bad request");
441 or $self->err(304, "not modified"); 459 or $self->err(304, "not modified");
442 460
443 if (-r "$path/index.html") { 461 if (-r "$path/index.html") {
444 # replace directory "size" by index.html filesize 462 # replace directory "size" by index.html filesize
445 $self->{stat} = [stat ($self->{path} .= "/index.html")]; 463 $self->{stat} = [stat ($self->{path} .= "/index.html")];
446 $self->handle_file($queue_index); 464 $self->handle_file($queue_index, $tbf_top);
447 } else { 465 } else {
448 $self->handle_dir; 466 $self->handle_dir;
449 } 467 }
450 } 468 }
451 } elsif (-f _ && -r _) { 469 } elsif (-f _ && -r _) {
460 $httpevent->wait; 478 $httpevent->wait;
461 } 479 }
462 } 480 }
463 } 481 }
464 482
465 $self->handle_file($queue_file); 483 $self->handle_file($queue_file, $tbf_top);
466 } else { 484 } else {
467 $self->err(404, "not found"); 485 $self->err(404, "not found");
468 } 486 }
469 } 487 }
470} 488}
481 }, 499 },
482 $idx); 500 $idx);
483} 501}
484 502
485sub handle_file { 503sub handle_file {
486 my ($self, $queue) = @_; 504 my ($self, $queue, $tbf) = @_;
487 my $length = $self->{stat}[7]; 505 my $length = $self->{stat}[7];
488 my $hdr = { 506 my $hdr = {
489 "Last-Modified" => time2str ((stat _)[9]), 507 "Last-Modified" => time2str ((stat _)[9]),
490 }; 508 };
491 509
582 Coro::ready($current); 600 Coro::ready($current);
583 }); 601 });
584 &Coro::schedule; 602 &Coro::schedule;
585 last unless $r; 603 last unless $r;
586 } 604 }
605
606 $tbf->request(length $buf);
587 my $w = syswrite $self->{fh}, $buf 607 my $w = syswrite $self->{fh}, $buf
588 or last; 608 or last;
589 $::written += $w; 609 $::written += $w;
590 $self->{written} += $w; 610 $self->{written} += $w;
591 $l += $r; 611 $l += $r;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines