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.80 by root, Fri Dec 1 04:18:32 2006 UTC vs.
Revision 1.87 by root, Wed Nov 19 11:41:06 2008 UTC

1use Coro; 1use Coro;
2use Coro::Semaphore; 2use Coro::Semaphore;
3use Coro::Event; 3use Coro::EV;
4use Coro::Socket; 4use Coro::Socket;
5use Coro::Signal; 5use Coro::Signal;
6use Coro::AIO (); 6use Coro::AIO ();
7 7
8use HTTP::Date; 8use HTTP::Date;
26our $HTTP_NOW; 26our $HTTP_NOW;
27 27
28our $ERROR_LOG; 28our $ERROR_LOG;
29our $ACCESS_LOG; 29our $ACCESS_LOG;
30 30
31Event->timer(interval => 1, hard => 1, cb => sub { 31our $update_time = EV::periodic 0, 1, undef, sub {
32 $NOW = time; 32 $NOW = time;
33 $HTTP_NOW = time2str $NOW; 33 $HTTP_NOW = time2str $NOW;
34})->now; 34};
35$update_time->invoke;
35 36
36if ($ERROR_LOG) { 37if ($ERROR_LOG) {
37 use IO::Handle; 38 use IO::Handle;
38 open $errorlog, ">>$ERROR_LOG" 39 open $errorlog, ">>$ERROR_LOG"
39 or die "$ERROR_LOG: $!"; 40 or die "$ERROR_LOG: $!";
82# one "execution thread" 83# one "execution thread"
83sub handler { 84sub handler {
84 while () { 85 while () {
85 if (@newcons) { 86 if (@newcons) {
86 eval { 87 eval {
87 conn->new(@{pop @newcons})->handle; 88 conn->new (@{pop @newcons})->handle;
88 }; 89 };
89 slog 1, "$@" if $@ && !ref $@; 90 slog 1, "$@" if $@ && !ref $@;
90 91
91 $httpevent->broadcast; # only for testing, but doesn't matter much 92 $httpevent->broadcast; # only for testing, but doesn't matter much
92 93
122 123
123my $http_port = new Coro::Socket 124my $http_port = new Coro::Socket
124 LocalAddr => $SERVER_HOST, 125 LocalAddr => $SERVER_HOST,
125 LocalPort => $SERVER_PORT, 126 LocalPort => $SERVER_PORT,
126 ReuseAddr => 1, 127 ReuseAddr => 1,
127 Listen => 50, 128 Listen => 50,
128 or die "unable to start server"; 129 or die "unable to start server";
129 130
130listen_on $http_port; 131listen_on $http_port;
131 132
132if ($SERVER_PORT2) { 133if ($SERVER_PORT2) {
133 my $http_port = new Coro::Socket 134 my $http_port = new Coro::Socket
134 LocalAddr => $SERVER_HOST, 135 LocalAddr => $SERVER_HOST,
135 LocalPort => $SERVER_PORT2, 136 LocalPort => $SERVER_PORT2,
136 ReuseAddr => 1, 137 ReuseAddr => 1,
137 Listen => 50, 138 Listen => 50,
138 or die "unable to start server"; 139 or die "unable to start server";
139 140
140 listen_on $http_port; 141 listen_on $http_port;
141} 142}
142 143
150use Convert::Scalar 'weaken'; 151use Convert::Scalar 'weaken';
151use IO::AIO; 152use IO::AIO;
152 153
153IO::AIO::min_parallel $::AIO_PARALLEL; 154IO::AIO::min_parallel $::AIO_PARALLEL;
154 155
155Event->io (fd => IO::AIO::poll_fileno, 156our $AIO_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb;
156 poll => 'r', async => 1,
157 cb => \&IO::AIO::poll_cb);
158 157
159our %conn; # $conn{ip}{self} => connobj 158our %conn; # $conn{ip}{self} => connobj
160our %uri; # $uri{ip}{uri}{self} 159our %uri; # $uri{ip}{uri}{self}
161our %blocked; 160our %blocked;
162our %mimetype; 161our %mimetype;
163 162
164sub read_mimetypes { 163sub read_mimetypes {
165 local *M;
166 if (open M, "<mime_types") { 164 if (open my $fh, "<mime_types") {
167 while (<M>) { 165 while (<$fh>) {
168 if (/^([^#]\S+)\t+(\S+)$/) { 166 if (/^([^#]\S+)\t+(\S+)$/) {
169 $mimetype{lc $1} = $2; 167 $mimetype{lc $1} = $2;
170 } 168 }
171 } 169 }
172 } else { 170 } else {
180 my $class = shift; 178 my $class = shift;
181 my $fh = shift; 179 my $fh = shift;
182 my $peername = shift; 180 my $peername = shift;
183 my $self = bless { fh => $fh }, $class; 181 my $self = bless { fh => $fh }, $class;
184 my (undef, $iaddr) = unpack_sockaddr_in $peername 182 my (undef, $iaddr) = unpack_sockaddr_in $peername
185 or $self->err(500, "unable to decode peername"); 183 or $self->err (500, "unable to decode peername");
186 184
187 $self->{remote_addr} = 185 $self->{remote_addr} =
188 $self->{remote_id} = inet_ntoa $iaddr; 186 $self->{remote_id} = inet_ntoa $iaddr;
189 187
190 $self->{time} = $::NOW; 188 $self->{time} = $::NOW;
191 189
192 weaken ($Coro::current->{conn} = $self); 190 weaken ($Coro::current->{conn} = $self);
193 191
194 $::conns++; 192 ++$::conns;
195 $::maxconns = $::conns if $::conns > $::maxconns; 193 $::maxconns = $::conns if $::conns > $::maxconns;
196 194
197 $self; 195 $self
198} 196}
199 197
200sub DESTROY { 198sub DESTROY {
201 #my $self = shift; 199 my $self = shift;
200
202 $::conns--; 201 --$::conns;
203} 202}
204 203
205sub prune_cache { 204sub prune_cache {
206 my $hash = $_[0]; 205 my $hash = $_[0];
207 206
222 for (keys %blocked) { 221 for (keys %blocked) {
223 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; 222 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
224 } 223 }
225} 224}
226 225
227Event->timer(interval => 60, cb => \&prune_caches); 226our $PRUNE_WATCHER = EV::timer 60, 60, \&prune_caches;
228 227
229sub slog { 228sub slog {
230 my $self = shift; 229 my $self = shift;
231 main::slog($_[0], "$self->{remote_id}> $_[1]"); 230 main::slog($_[0], "$self->{remote_id}> $_[1]");
232} 231}
293 $hdr->{"Content-Type"} = "text/plain"; 292 $hdr->{"Content-Type"} = "text/plain";
294 $hdr->{"Content-Length"} = length $content; 293 $hdr->{"Content-Length"} = length $content;
295 } 294 }
296 $hdr->{"Connection"} = "close"; 295 $hdr->{"Connection"} = "close";
297 296
298 $self->response($code, $msg, $hdr, $content); 297 $self->response ($code, $msg, $hdr, $content);
299 298
300 die bless {}, err::; 299 die bless {}, err::
301} 300}
302 301
303sub handle { 302sub handle {
304 my $self = shift; 303 my $self = shift;
305 my $fh = $self->{fh}; 304 my $fh = $self->{fh};
306 305
307 my $host; 306 my $host;
308 307
309 $fh->timeout($::REQ_TIMEOUT); 308 $fh->timeout($::REQ_TIMEOUT);
310 while() { 309 while () {
311 $self->{reqs}++; 310 $self->{reqs}++;
312 311
313 # read request and parse first line 312 # read request and parse first line
314 my $req = $fh->readline("\015\012\015\012"); 313 my $req = $fh->readline("\015\012\015\012");
315 314
424# uri => path mapping 423# uri => path mapping
425sub map_uri { 424sub map_uri {
426 my $self = shift; 425 my $self = shift;
427 my $host = $self->{server_name}; 426 my $host = $self->{server_name};
428 my $uri = $self->{uri}; 427 my $uri = $self->{uri};
428
429 $host =~ /[\/\\]/
430 and $self->err(400, "bad request");
429 431
430 # some massaging, also makes it more secure 432 # some massaging, also makes it more secure
431 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; 433 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge;
432 $uri =~ s%//+%/%g; 434 $uri =~ s%//+%/%g;
433 $uri =~ s%/\.(?=/|$)%%g; 435 $uri =~ s%/\.(?=/|$)%%g;
477 479
478 if ($self->{name} =~ s%^/internal/([^/]+)%%) { 480 if ($self->{name} =~ s%^/internal/([^/]+)%%) {
479 if ($::internal{$1}) { 481 if ($::internal{$1}) {
480 $::internal{$1}->($self); 482 $::internal{$1}->($self);
481 } else { 483 } else {
482 $self->err(404, "not found"); 484 $self->err (404, "not found");
483 } 485 }
484 } else { 486 } else {
485 487
486 stat $path 488 stat $path
487 or $self->err(404, "not found"); 489 or $self->err (404, "not found");
488 490
489 $self->{stat} = [stat _]; 491 $self->{stat} = [stat _];
490 492
491 # idiotic netscape sends idiotic headers AGAIN 493 # idiotic netscape sends idiotic headers AGAIN
492 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 494 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
495 if (-d _ && -r _) { 497 if (-d _ && -r _) {
496 # directory 498 # directory
497 if ($path !~ /\/$/) { 499 if ($path !~ /\/$/) {
498 # create a redirect to get the trailing "/" 500 # create a redirect to get the trailing "/"
499 # we don't try to avoid the :80 501 # we don't try to avoid the :80
500 $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); 502 $self->err (301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" });
501 } else { 503 } else {
502 $ims < $self->{stat}[9] 504 $ims < $self->{stat}[9]
503 or $self->err(304, "not modified"); 505 or $self->err (304, "not modified");
504 506
505 if (-r "$path/index.html") { 507 if (-r "$path/index.html") {
506 # replace directory "size" by index.html filesize 508 # replace directory "size" by index.html filesize
507 $self->{stat} = [stat ($self->{path} .= "/index.html")]; 509 $self->{stat} = [stat ($self->{path} .= "/index.html")];
508 $self->handle_file($queue_index, $tbf_top); 510 $self->handle_file ($queue_index, $tbf_top);
509 } else { 511 } else {
510 $self->handle_dir; 512 $self->handle_dir;
511 } 513 }
512 } 514 }
513 } elsif (-f _ && -r _) { 515 } elsif (-f _ && -r _) {
514 -x _ and $self->err(403, "forbidden"); 516 -x _ and $self->err (403, "forbidden");
515 517
516 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { 518 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
517 my $timeout = $::NOW + 10; 519 my $timeout = $::NOW + 10;
518 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { 520 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
519 if ($timeout < $::NOW) { 521 if ($timeout < $::NOW) {
522 $httpevent->wait; 524 $httpevent->wait;
523 } 525 }
524 } 526 }
525 } 527 }
526 528
527 $self->handle_file($queue_file, $tbf_top); 529 $self->handle_file ($queue_file, $tbf_top);
528 } else { 530 } else {
529 $self->err(404, "not found"); 531 $self->err (404, "not found");
530 } 532 }
531 } 533 }
532} 534}
533 535
534sub handle_dir { 536sub handle_dir {
535 my $self = shift; 537 my $self = shift;
536 my $idx = $self->diridx; 538 my $idx = $self->diridx;
537 539
538 $self->response(200, "ok", 540 $self->response (200, "ok",
539 { 541 {
540 "Content-Type" => "text/html; charset=utf-8", 542 "Content-Type" => "text/html; charset=utf-8",
541 "Content-Length" => length $idx, 543 "Content-Length" => length $idx,
542 "Last-Modified" => time2str ($self->{stat}[9]), 544 "Last-Modified" => time2str ($self->{stat}[9]),
543 }, 545 },
553 }; 555 };
554 556
555 my @code = (200, "ok"); 557 my @code = (200, "ok");
556 my ($l, $h); 558 my ($l, $h);
557 559
558 if ($self->{h}{range} =~ /^bytes=(.*)$/) { 560 if ($self->{h}{range} =~ /^bytes=(.*)$/i) {
559 for (split /,/, $1) { 561 for (split /,/, $1) {
560 if (/^-(\d+)$/) { 562 if (/^-(\d+)$/) {
561 ($l, $h) = ($length - $1, $length - 1); 563 ($l, $h) = ($length - $1, $length - 1);
562 } elsif (/^(\d+)-(\d*)$/) { 564 } elsif (/^(\d+)-(\d*)$/) {
563 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 565 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
567 } 569 }
568 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; 570 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
569 } 571 }
570 $hdr->{"Content-Range"} = "bytes */$length"; 572 $hdr->{"Content-Range"} = "bytes */$length";
571 $hdr->{"Content-Length"} = $length; 573 $hdr->{"Content-Length"} = $length;
572 $self->err(416, "not satisfiable", $hdr, ""); 574 $self->err (416, "not satisfiable", $hdr, "");
573 575
574satisfiable: 576satisfiable:
575 # check for segmented downloads 577 # check for segmented downloads
576 if ($l && $::NO_SEGMENTED) { 578 if ($l && $::NO_SEGMENTED) {
577 my $timeout = $::NOW + 15; 579 my $timeout = $::NOW + 15;
578 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 580 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
579 if ($timeout <= $::NOW) { 581 if ($timeout <= $::NOW) {
580 $self->block($::BLOCKTIME, "segmented downloads are forbidden"); 582 $self->block ($::BLOCKTIME, "segmented downloads are forbidden");
581 #$self->err_segmented_download; 583 #$self->err_segmented_download;
582 } else { 584 } else {
583 $httpevent->wait; 585 $httpevent->wait;
584 } 586 }
585 } 587 }
596 598
597 $self->{path} =~ /\.([^.]+)$/; 599 $self->{path} =~ /\.([^.]+)$/;
598 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; 600 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
599 $hdr->{"Content-Length"} = $length; 601 $hdr->{"Content-Length"} = $length;
600 602
601 $self->response(@code, $hdr, ""); 603 $self->response (@code, $hdr, "");
602 604
603 if ($self->{method} eq "GET") { 605 if ($self->{method} eq "GET") {
604 $self->{time} = $::NOW; 606 $self->{time} = $::NOW;
605 $self->{written} = 0; 607 $self->{written} = 0;
606 608
607 my $fh;
608
609 open $fh, "<", $self->{path} 609 open my $fh, "<", $self->{path}
610 or die "$self->{path}: late open failure ($!)"; 610 or die "$self->{path}: late open failure ($!)";
611 611
612 $h -= $l - 1; 612 $h -= $l - 1;
613 613
614 if (0) { # !AIO
615 if ($l) {
616 sysseek $fh, $l, 0;
617 }
618 }
619
620 my $transfer = $queue->start_transfer($h); 614 my $transfer = $queue->start_transfer ($h);
621 my $locked; 615 my $locked;
622 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size 616 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
623 617
624 while ($h > 0) { 618 while ($h > 0) {
625 unless ($locked) { 619 unless ($locked) {
626 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 620 if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) {
627 $bufsize = $::BUFSIZE; 621 $bufsize = $::BUFSIZE;
628 $self->{time} = $::NOW; 622 $self->{time} = $::NOW;
629 $self->{written} = 0; 623 $self->{written} = 0;
630 } 624 }
631 } 625 }
637 631
638 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 632 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0
639 or last; 633 or last;
640 634
641 $tbf->request (length $buf); 635 $tbf->request (length $buf);
642 my $w = syswrite $self->{fh}, $buf 636 my $w = $self->{fh}->syswrite ($buf)
643 or last; 637 or last;
644 $::written += $w; 638 $::written += $w;
645 $self->{written} += $w; 639 $self->{written} += $w;
646 $l += $w; 640 $l += $w;
647 } 641 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines