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.81 by root, Sat Dec 2 03:29:29 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;
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 close $self->{fh}; # workaround
202 $::conns--; 202 --$::conns;
203} 203}
204 204
205sub prune_cache { 205sub prune_cache {
206 my $hash = $_[0]; 206 my $hash = $_[0];
207 207
222 for (keys %blocked) { 222 for (keys %blocked) {
223 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; 223 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
224 } 224 }
225} 225}
226 226
227Event->timer(interval => 60, cb => \&prune_caches); 227Event->timer (interval => 60, cb => \&prune_caches);
228 228
229sub slog { 229sub slog {
230 my $self = shift; 230 my $self = shift;
231 main::slog($_[0], "$self->{remote_id}> $_[1]"); 231 main::slog($_[0], "$self->{remote_id}> $_[1]");
232} 232}
293 $hdr->{"Content-Type"} = "text/plain"; 293 $hdr->{"Content-Type"} = "text/plain";
294 $hdr->{"Content-Length"} = length $content; 294 $hdr->{"Content-Length"} = length $content;
295 } 295 }
296 $hdr->{"Connection"} = "close"; 296 $hdr->{"Connection"} = "close";
297 297
298 $self->response($code, $msg, $hdr, $content); 298 $self->response ($code, $msg, $hdr, $content);
299 299
300 die bless {}, err::; 300 die bless {}, err::
301} 301}
302 302
303sub handle { 303sub handle {
304 my $self = shift; 304 my $self = shift;
305 my $fh = $self->{fh}; 305 my $fh = $self->{fh};
306 306
307 my $host; 307 my $host;
308 308
309 $fh->timeout($::REQ_TIMEOUT); 309 $fh->timeout($::REQ_TIMEOUT);
310 while() { 310 while () {
311 $self->{reqs}++; 311 $self->{reqs}++;
312 312
313 # read request and parse first line 313 # read request and parse first line
314 my $req = $fh->readline("\015\012\015\012"); 314 my $req = $fh->readline("\015\012\015\012");
315 315
477 477
478 if ($self->{name} =~ s%^/internal/([^/]+)%%) { 478 if ($self->{name} =~ s%^/internal/([^/]+)%%) {
479 if ($::internal{$1}) { 479 if ($::internal{$1}) {
480 $::internal{$1}->($self); 480 $::internal{$1}->($self);
481 } else { 481 } else {
482 $self->err(404, "not found"); 482 $self->err (404, "not found");
483 } 483 }
484 } else { 484 } else {
485 485
486 stat $path 486 stat $path
487 or $self->err(404, "not found"); 487 or $self->err (404, "not found");
488 488
489 $self->{stat} = [stat _]; 489 $self->{stat} = [stat _];
490 490
491 # idiotic netscape sends idiotic headers AGAIN 491 # idiotic netscape sends idiotic headers AGAIN
492 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 492 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
495 if (-d _ && -r _) { 495 if (-d _ && -r _) {
496 # directory 496 # directory
497 if ($path !~ /\/$/) { 497 if ($path !~ /\/$/) {
498 # create a redirect to get the trailing "/" 498 # create a redirect to get the trailing "/"
499 # we don't try to avoid the :80 499 # we don't try to avoid the :80
500 $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); 500 $self->err (301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" });
501 } else { 501 } else {
502 $ims < $self->{stat}[9] 502 $ims < $self->{stat}[9]
503 or $self->err(304, "not modified"); 503 or $self->err (304, "not modified");
504 504
505 if (-r "$path/index.html") { 505 if (-r "$path/index.html") {
506 # replace directory "size" by index.html filesize 506 # replace directory "size" by index.html filesize
507 $self->{stat} = [stat ($self->{path} .= "/index.html")]; 507 $self->{stat} = [stat ($self->{path} .= "/index.html")];
508 $self->handle_file($queue_index, $tbf_top); 508 $self->handle_file ($queue_index, $tbf_top);
509 } else { 509 } else {
510 $self->handle_dir; 510 $self->handle_dir;
511 } 511 }
512 } 512 }
513 } elsif (-f _ && -r _) { 513 } elsif (-f _ && -r _) {
514 -x _ and $self->err(403, "forbidden"); 514 -x _ and $self->err (403, "forbidden");
515 515
516 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { 516 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
517 my $timeout = $::NOW + 10; 517 my $timeout = $::NOW + 10;
518 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { 518 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
519 if ($timeout < $::NOW) { 519 if ($timeout < $::NOW) {
522 $httpevent->wait; 522 $httpevent->wait;
523 } 523 }
524 } 524 }
525 } 525 }
526 526
527 $self->handle_file($queue_file, $tbf_top); 527 $self->handle_file ($queue_file, $tbf_top);
528 } else { 528 } else {
529 $self->err(404, "not found"); 529 $self->err (404, "not found");
530 } 530 }
531 } 531 }
532} 532}
533 533
534sub handle_dir { 534sub handle_dir {
535 my $self = shift; 535 my $self = shift;
536 my $idx = $self->diridx; 536 my $idx = $self->diridx;
537 537
538 $self->response(200, "ok", 538 $self->response (200, "ok",
539 { 539 {
540 "Content-Type" => "text/html; charset=utf-8", 540 "Content-Type" => "text/html; charset=utf-8",
541 "Content-Length" => length $idx, 541 "Content-Length" => length $idx,
542 "Last-Modified" => time2str ($self->{stat}[9]), 542 "Last-Modified" => time2str ($self->{stat}[9]),
543 }, 543 },
567 } 567 }
568 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; 568 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
569 } 569 }
570 $hdr->{"Content-Range"} = "bytes */$length"; 570 $hdr->{"Content-Range"} = "bytes */$length";
571 $hdr->{"Content-Length"} = $length; 571 $hdr->{"Content-Length"} = $length;
572 $self->err(416, "not satisfiable", $hdr, ""); 572 $self->err (416, "not satisfiable", $hdr, "");
573 573
574satisfiable: 574satisfiable:
575 # check for segmented downloads 575 # check for segmented downloads
576 if ($l && $::NO_SEGMENTED) { 576 if ($l && $::NO_SEGMENTED) {
577 my $timeout = $::NOW + 15; 577 my $timeout = $::NOW + 15;
578 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 578 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
579 if ($timeout <= $::NOW) { 579 if ($timeout <= $::NOW) {
580 $self->block($::BLOCKTIME, "segmented downloads are forbidden"); 580 $self->block ($::BLOCKTIME, "segmented downloads are forbidden");
581 #$self->err_segmented_download; 581 #$self->err_segmented_download;
582 } else { 582 } else {
583 $httpevent->wait; 583 $httpevent->wait;
584 } 584 }
585 } 585 }
596 596
597 $self->{path} =~ /\.([^.]+)$/; 597 $self->{path} =~ /\.([^.]+)$/;
598 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; 598 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
599 $hdr->{"Content-Length"} = $length; 599 $hdr->{"Content-Length"} = $length;
600 600
601 $self->response(@code, $hdr, ""); 601 $self->response (@code, $hdr, "");
602 602
603 if ($self->{method} eq "GET") { 603 if ($self->{method} eq "GET") {
604 $self->{time} = $::NOW; 604 $self->{time} = $::NOW;
605 $self->{written} = 0; 605 $self->{written} = 0;
606 606
607 my $fh;
608
609 open $fh, "<", $self->{path} 607 open my $fh, "<", $self->{path}
610 or die "$self->{path}: late open failure ($!)"; 608 or die "$self->{path}: late open failure ($!)";
611 609
612 $h -= $l - 1; 610 $h -= $l - 1;
613 611
614 if (0) { # !AIO
615 if ($l) {
616 sysseek $fh, $l, 0;
617 }
618 }
619
620 my $transfer = $queue->start_transfer($h); 612 my $transfer = $queue->start_transfer ($h);
621 my $locked; 613 my $locked;
622 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size 614 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
623 615
624 while ($h > 0) { 616 while ($h > 0) {
625 unless ($locked) { 617 unless ($locked) {
626 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 618 if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) {
627 $bufsize = $::BUFSIZE; 619 $bufsize = $::BUFSIZE;
628 $self->{time} = $::NOW; 620 $self->{time} = $::NOW;
629 $self->{written} = 0; 621 $self->{written} = 0;
630 } 622 }
631 } 623 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines