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.80 by root, Fri Dec 1 04:18:32 2006 UTC vs.
Revision 1.82 by root, Sat Dec 2 03:31:42 2006 UTC

82# one "execution thread" 82# one "execution thread"
83sub handler { 83sub handler {
84 while () { 84 while () {
85 if (@newcons) { 85 if (@newcons) {
86 eval { 86 eval {
87 conn->new(@{pop @newcons})->handle; 87 conn->new (@{pop @newcons})->handle;
88 }; 88 };
89 slog 1, "$@" if $@ && !ref $@; 89 slog 1, "$@" if $@ && !ref $@;
90 90
91 $httpevent->broadcast; # only for testing, but doesn't matter much 91 $httpevent->broadcast; # only for testing, but doesn't matter much
92 92
122 122
123my $http_port = new Coro::Socket 123my $http_port = new Coro::Socket
124 LocalAddr => $SERVER_HOST, 124 LocalAddr => $SERVER_HOST,
125 LocalPort => $SERVER_PORT, 125 LocalPort => $SERVER_PORT,
126 ReuseAddr => 1, 126 ReuseAddr => 1,
127 Listen => 50, 127 Listen => 50,
128 or die "unable to start server"; 128 or die "unable to start server";
129 129
130listen_on $http_port; 130listen_on $http_port;
131 131
132if ($SERVER_PORT2) { 132if ($SERVER_PORT2) {
133 my $http_port = new Coro::Socket 133 my $http_port = new Coro::Socket
134 LocalAddr => $SERVER_HOST, 134 LocalAddr => $SERVER_HOST,
135 LocalPort => $SERVER_PORT2, 135 LocalPort => $SERVER_PORT2,
136 ReuseAddr => 1, 136 ReuseAddr => 1,
137 Listen => 50, 137 Listen => 50,
138 or die "unable to start server"; 138 or die "unable to start server";
139 139
140 listen_on $http_port; 140 listen_on $http_port;
141} 141}
142 142
160our %uri; # $uri{ip}{uri}{self} 160our %uri; # $uri{ip}{uri}{self}
161our %blocked; 161our %blocked;
162our %mimetype; 162our %mimetype;
163 163
164sub read_mimetypes { 164sub read_mimetypes {
165 local *M;
166 if (open M, "<mime_types") { 165 if (open my $fh, "<mime_types") {
167 while (<M>) { 166 while (<$fh>) {
168 if (/^([^#]\S+)\t+(\S+)$/) { 167 if (/^([^#]\S+)\t+(\S+)$/) {
169 $mimetype{lc $1} = $2; 168 $mimetype{lc $1} = $2;
170 } 169 }
171 } 170 }
172 } else { 171 } else {
180 my $class = shift; 179 my $class = shift;
181 my $fh = shift; 180 my $fh = shift;
182 my $peername = shift; 181 my $peername = shift;
183 my $self = bless { fh => $fh }, $class; 182 my $self = bless { fh => $fh }, $class;
184 my (undef, $iaddr) = unpack_sockaddr_in $peername 183 my (undef, $iaddr) = unpack_sockaddr_in $peername
185 or $self->err(500, "unable to decode peername"); 184 or $self->err (500, "unable to decode peername");
186 185
187 $self->{remote_addr} = 186 $self->{remote_addr} =
188 $self->{remote_id} = inet_ntoa $iaddr; 187 $self->{remote_id} = inet_ntoa $iaddr;
189 188
190 $self->{time} = $::NOW; 189 $self->{time} = $::NOW;
191 190
192 weaken ($Coro::current->{conn} = $self); 191 weaken ($Coro::current->{conn} = $self);
193 192
194 $::conns++; 193 ++$::conns;
195 $::maxconns = $::conns if $::conns > $::maxconns; 194 $::maxconns = $::conns if $::conns > $::maxconns;
196 195
197 $self; 196 $self
198} 197}
199 198
200sub DESTROY { 199sub DESTROY {
201 #my $self = shift; 200 my $self = shift;
201
202 close $self->{fh}; # workaround
202 $::conns--; 203 --$::conns;
203} 204}
204 205
205sub prune_cache { 206sub prune_cache {
206 my $hash = $_[0]; 207 my $hash = $_[0];
207 208
222 for (keys %blocked) { 223 for (keys %blocked) {
223 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; 224 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
224 } 225 }
225} 226}
226 227
227Event->timer(interval => 60, cb => \&prune_caches); 228Event->timer (interval => 60, cb => \&prune_caches);
228 229
229sub slog { 230sub slog {
230 my $self = shift; 231 my $self = shift;
231 main::slog($_[0], "$self->{remote_id}> $_[1]"); 232 main::slog($_[0], "$self->{remote_id}> $_[1]");
232} 233}
293 $hdr->{"Content-Type"} = "text/plain"; 294 $hdr->{"Content-Type"} = "text/plain";
294 $hdr->{"Content-Length"} = length $content; 295 $hdr->{"Content-Length"} = length $content;
295 } 296 }
296 $hdr->{"Connection"} = "close"; 297 $hdr->{"Connection"} = "close";
297 298
298 $self->response($code, $msg, $hdr, $content); 299 $self->response ($code, $msg, $hdr, $content);
299 300
300 die bless {}, err::; 301 die bless {}, err::
301} 302}
302 303
303sub handle { 304sub handle {
304 my $self = shift; 305 my $self = shift;
305 my $fh = $self->{fh}; 306 my $fh = $self->{fh};
306 307
307 my $host; 308 my $host;
308 309
309 $fh->timeout($::REQ_TIMEOUT); 310 $fh->timeout($::REQ_TIMEOUT);
310 while() { 311 while () {
311 $self->{reqs}++; 312 $self->{reqs}++;
312 313
313 # read request and parse first line 314 # read request and parse first line
314 my $req = $fh->readline("\015\012\015\012"); 315 my $req = $fh->readline("\015\012\015\012");
315 316
477 478
478 if ($self->{name} =~ s%^/internal/([^/]+)%%) { 479 if ($self->{name} =~ s%^/internal/([^/]+)%%) {
479 if ($::internal{$1}) { 480 if ($::internal{$1}) {
480 $::internal{$1}->($self); 481 $::internal{$1}->($self);
481 } else { 482 } else {
482 $self->err(404, "not found"); 483 $self->err (404, "not found");
483 } 484 }
484 } else { 485 } else {
485 486
486 stat $path 487 stat $path
487 or $self->err(404, "not found"); 488 or $self->err (404, "not found");
488 489
489 $self->{stat} = [stat _]; 490 $self->{stat} = [stat _];
490 491
491 # idiotic netscape sends idiotic headers AGAIN 492 # idiotic netscape sends idiotic headers AGAIN
492 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 493 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
495 if (-d _ && -r _) { 496 if (-d _ && -r _) {
496 # directory 497 # directory
497 if ($path !~ /\/$/) { 498 if ($path !~ /\/$/) {
498 # create a redirect to get the trailing "/" 499 # create a redirect to get the trailing "/"
499 # we don't try to avoid the :80 500 # we don't try to avoid the :80
500 $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); 501 $self->err (301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" });
501 } else { 502 } else {
502 $ims < $self->{stat}[9] 503 $ims < $self->{stat}[9]
503 or $self->err(304, "not modified"); 504 or $self->err (304, "not modified");
504 505
505 if (-r "$path/index.html") { 506 if (-r "$path/index.html") {
506 # replace directory "size" by index.html filesize 507 # replace directory "size" by index.html filesize
507 $self->{stat} = [stat ($self->{path} .= "/index.html")]; 508 $self->{stat} = [stat ($self->{path} .= "/index.html")];
508 $self->handle_file($queue_index, $tbf_top); 509 $self->handle_file ($queue_index, $tbf_top);
509 } else { 510 } else {
510 $self->handle_dir; 511 $self->handle_dir;
511 } 512 }
512 } 513 }
513 } elsif (-f _ && -r _) { 514 } elsif (-f _ && -r _) {
514 -x _ and $self->err(403, "forbidden"); 515 -x _ and $self->err (403, "forbidden");
515 516
516 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { 517 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
517 my $timeout = $::NOW + 10; 518 my $timeout = $::NOW + 10;
518 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { 519 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
519 if ($timeout < $::NOW) { 520 if ($timeout < $::NOW) {
522 $httpevent->wait; 523 $httpevent->wait;
523 } 524 }
524 } 525 }
525 } 526 }
526 527
527 $self->handle_file($queue_file, $tbf_top); 528 $self->handle_file ($queue_file, $tbf_top);
528 } else { 529 } else {
529 $self->err(404, "not found"); 530 $self->err (404, "not found");
530 } 531 }
531 } 532 }
532} 533}
533 534
534sub handle_dir { 535sub handle_dir {
535 my $self = shift; 536 my $self = shift;
536 my $idx = $self->diridx; 537 my $idx = $self->diridx;
537 538
538 $self->response(200, "ok", 539 $self->response (200, "ok",
539 { 540 {
540 "Content-Type" => "text/html; charset=utf-8", 541 "Content-Type" => "text/html; charset=utf-8",
541 "Content-Length" => length $idx, 542 "Content-Length" => length $idx,
542 "Last-Modified" => time2str ($self->{stat}[9]), 543 "Last-Modified" => time2str ($self->{stat}[9]),
543 }, 544 },
567 } 568 }
568 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; 569 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
569 } 570 }
570 $hdr->{"Content-Range"} = "bytes */$length"; 571 $hdr->{"Content-Range"} = "bytes */$length";
571 $hdr->{"Content-Length"} = $length; 572 $hdr->{"Content-Length"} = $length;
572 $self->err(416, "not satisfiable", $hdr, ""); 573 $self->err (416, "not satisfiable", $hdr, "");
573 574
574satisfiable: 575satisfiable:
575 # check for segmented downloads 576 # check for segmented downloads
576 if ($l && $::NO_SEGMENTED) { 577 if ($l && $::NO_SEGMENTED) {
577 my $timeout = $::NOW + 15; 578 my $timeout = $::NOW + 15;
578 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 579 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
579 if ($timeout <= $::NOW) { 580 if ($timeout <= $::NOW) {
580 $self->block($::BLOCKTIME, "segmented downloads are forbidden"); 581 $self->block ($::BLOCKTIME, "segmented downloads are forbidden");
581 #$self->err_segmented_download; 582 #$self->err_segmented_download;
582 } else { 583 } else {
583 $httpevent->wait; 584 $httpevent->wait;
584 } 585 }
585 } 586 }
596 597
597 $self->{path} =~ /\.([^.]+)$/; 598 $self->{path} =~ /\.([^.]+)$/;
598 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; 599 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
599 $hdr->{"Content-Length"} = $length; 600 $hdr->{"Content-Length"} = $length;
600 601
601 $self->response(@code, $hdr, ""); 602 $self->response (@code, $hdr, "");
602 603
603 if ($self->{method} eq "GET") { 604 if ($self->{method} eq "GET") {
604 $self->{time} = $::NOW; 605 $self->{time} = $::NOW;
605 $self->{written} = 0; 606 $self->{written} = 0;
606 607
607 my $fh;
608
609 open $fh, "<", $self->{path} 608 open my $fh, "<", $self->{path}
610 or die "$self->{path}: late open failure ($!)"; 609 or die "$self->{path}: late open failure ($!)";
611 610
612 $h -= $l - 1; 611 $h -= $l - 1;
613 612
614 if (0) { # !AIO
615 if ($l) {
616 sysseek $fh, $l, 0;
617 }
618 }
619
620 my $transfer = $queue->start_transfer($h); 613 my $transfer = $queue->start_transfer ($h);
621 my $locked; 614 my $locked;
622 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size 615 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
623 616
624 while ($h > 0) { 617 while ($h > 0) {
625 unless ($locked) { 618 unless ($locked) {
626 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 619 if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) {
627 $bufsize = $::BUFSIZE; 620 $bufsize = $::BUFSIZE;
628 $self->{time} = $::NOW; 621 $self->{time} = $::NOW;
629 $self->{written} = 0; 622 $self->{written} = 0;
630 } 623 }
631 } 624 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines