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.72 by root, Tue Sep 10 03:08:03 2002 UTC vs.
Revision 1.81 by root, Sat Dec 2 03:29:29 2006 UTC

1use Coro; 1use Coro;
2use Coro::Semaphore; 2use Coro::Semaphore;
3use Coro::Event; 3use Coro::Event;
4use Coro::Socket; 4use Coro::Socket;
5use Coro::Signal; 5use Coro::Signal;
6use Coro::AIO ();
6 7
7use HTTP::Date; 8use HTTP::Date;
8use POSIX (); 9use POSIX ();
9 10
10use Compress::Zlib (); 11use Compress::Zlib ();
21our $accesslog; 22our $accesslog;
22our $errorlog; 23our $errorlog;
23 24
24our $NOW; 25our $NOW;
25our $HTTP_NOW; 26our $HTTP_NOW;
27
28our $ERROR_LOG;
29our $ACCESS_LOG;
26 30
27Event->timer(interval => 1, hard => 1, cb => sub { 31Event->timer(interval => 1, hard => 1, cb => sub {
28 $NOW = time; 32 $NOW = time;
29 $HTTP_NOW = time2str $NOW; 33 $HTTP_NOW = time2str $NOW;
30})->now; 34})->now;
78# one "execution thread" 82# one "execution thread"
79sub handler { 83sub handler {
80 while () { 84 while () {
81 if (@newcons) { 85 if (@newcons) {
82 eval { 86 eval {
83 conn->new(@{pop @newcons})->handle; 87 conn->new (@{pop @newcons})->handle;
84 }; 88 };
85 slog 1, "$@" if $@ && !ref $@; 89 slog 1, "$@" if $@ && !ref $@;
86 90
87 $httpevent->broadcast; # only for testing, but doesn't matter much 91 $httpevent->broadcast; # only for testing, but doesn't matter much
88 92
118 122
119my $http_port = new Coro::Socket 123my $http_port = new Coro::Socket
120 LocalAddr => $SERVER_HOST, 124 LocalAddr => $SERVER_HOST,
121 LocalPort => $SERVER_PORT, 125 LocalPort => $SERVER_PORT,
122 ReuseAddr => 1, 126 ReuseAddr => 1,
123 Listen => 50, 127 Listen => 50,
124 or die "unable to start server"; 128 or die "unable to start server";
125 129
126listen_on $http_port; 130listen_on $http_port;
127 131
128if ($SERVER_PORT2) { 132if ($SERVER_PORT2) {
129 my $http_port = new Coro::Socket 133 my $http_port = new Coro::Socket
130 LocalAddr => $SERVER_HOST, 134 LocalAddr => $SERVER_HOST,
131 LocalPort => $SERVER_PORT2, 135 LocalPort => $SERVER_PORT2,
132 ReuseAddr => 1, 136 ReuseAddr => 1,
133 Listen => 50, 137 Listen => 50,
134 or die "unable to start server"; 138 or die "unable to start server";
135 139
136 listen_on $http_port; 140 listen_on $http_port;
137} 141}
138 142
139package conn; 143package conn;
144
145use strict;
146use bytes;
140 147
141use Socket; 148use Socket;
142use HTTP::Date; 149use HTTP::Date;
143use Convert::Scalar 'weaken'; 150use Convert::Scalar 'weaken';
144use Linux::AIO; 151use IO::AIO;
145 152
146Linux::AIO::min_parallel $::AIO_PARALLEL; 153IO::AIO::min_parallel $::AIO_PARALLEL;
147 154
148Event->io(fd => Linux::AIO::poll_fileno, 155Event->io (fd => IO::AIO::poll_fileno,
149 poll => 'r', async => 1, 156 poll => 'r', async => 1,
150 cb => \&Linux::AIO::poll_cb); 157 cb => \&IO::AIO::poll_cb);
151 158
152our %conn; # $conn{ip}{self} => connobj 159our %conn; # $conn{ip}{self} => connobj
153our %uri; # $uri{ip}{uri}{self} 160our %uri; # $uri{ip}{uri}{self}
154our %blocked; 161our %blocked;
155our %mimetype; 162our %mimetype;
156 163
157sub read_mimetypes { 164sub read_mimetypes {
158 local *M;
159 if (open M, "<mime_types") { 165 if (open my $fh, "<mime_types") {
160 while (<M>) { 166 while (<$fh>) {
161 if (/^([^#]\S+)\t+(\S+)$/) { 167 if (/^([^#]\S+)\t+(\S+)$/) {
162 $mimetype{lc $1} = $2; 168 $mimetype{lc $1} = $2;
163 } 169 }
164 } 170 }
165 } else { 171 } else {
173 my $class = shift; 179 my $class = shift;
174 my $fh = shift; 180 my $fh = shift;
175 my $peername = shift; 181 my $peername = shift;
176 my $self = bless { fh => $fh }, $class; 182 my $self = bless { fh => $fh }, $class;
177 my (undef, $iaddr) = unpack_sockaddr_in $peername 183 my (undef, $iaddr) = unpack_sockaddr_in $peername
178 or $self->err(500, "unable to decode peername"); 184 or $self->err (500, "unable to decode peername");
179 185
180 $self->{remote_addr} = 186 $self->{remote_addr} =
181 $self->{remote_id} = inet_ntoa $iaddr; 187 $self->{remote_id} = inet_ntoa $iaddr;
182 188
183 $self->{time} = $::NOW; 189 $self->{time} = $::NOW;
185 weaken ($Coro::current->{conn} = $self); 191 weaken ($Coro::current->{conn} = $self);
186 192
187 $::conns++; 193 $::conns++;
188 $::maxconns = $::conns if $::conns > $::maxconns; 194 $::maxconns = $::conns if $::conns > $::maxconns;
189 195
190 $self; 196 $self
191} 197}
192 198
193sub DESTROY { 199sub DESTROY {
194 #my $self = shift; 200 #my $self = shift;
201 close $self->{fh}; # workaround
195 $::conns--; 202 --$::conns;
196} 203}
204
205sub prune_cache {
206 my $hash = $_[0];
207
208 for (keys %$hash) {
209 if (ref $hash->{$_} eq HASH::) {
210 prune_cache($hash->{$_});
211 unless (scalar keys %{$hash->{$_}}) {
212 delete $hash->{$_};
213 }
214 }
215 }
216}
217
218sub prune_caches {
219 prune_cache \%conn;
220 prune_cache \%uri;
221
222 for (keys %blocked) {
223 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
224 }
225}
226
227Event->timer (interval => 60, cb => \&prune_caches);
197 228
198sub slog { 229sub slog {
199 my $self = shift; 230 my $self = shift;
200 main::slog($_[0], "$self->{remote_id}> $_[1]"); 231 main::slog($_[0], "$self->{remote_id}> $_[1]");
201} 232}
262 $hdr->{"Content-Type"} = "text/plain"; 293 $hdr->{"Content-Type"} = "text/plain";
263 $hdr->{"Content-Length"} = length $content; 294 $hdr->{"Content-Length"} = length $content;
264 } 295 }
265 $hdr->{"Connection"} = "close"; 296 $hdr->{"Connection"} = "close";
266 297
267 $self->response($code, $msg, $hdr, $content); 298 $self->response ($code, $msg, $hdr, $content);
268 299
269 die bless {}, err::; 300 die bless {}, err::
270} 301}
271 302
272sub handle { 303sub handle {
273 my $self = shift; 304 my $self = shift;
274 my $fh = $self->{fh}; 305 my $fh = $self->{fh};
275 306
276 my $host; 307 my $host;
277 308
278 $fh->timeout($::REQ_TIMEOUT); 309 $fh->timeout($::REQ_TIMEOUT);
279 while() { 310 while () {
280 $self->{reqs}++; 311 $self->{reqs}++;
281 312
282 # read request and parse first line 313 # read request and parse first line
283 my $req = $fh->readline("\015\012\015\012"); 314 my $req = $fh->readline("\015\012\015\012");
284 315
446 477
447 if ($self->{name} =~ s%^/internal/([^/]+)%%) { 478 if ($self->{name} =~ s%^/internal/([^/]+)%%) {
448 if ($::internal{$1}) { 479 if ($::internal{$1}) {
449 $::internal{$1}->($self); 480 $::internal{$1}->($self);
450 } else { 481 } else {
451 $self->err(404, "not found"); 482 $self->err (404, "not found");
452 } 483 }
453 } else { 484 } else {
454 485
455 stat $path 486 stat $path
456 or $self->err(404, "not found"); 487 or $self->err (404, "not found");
457 488
458 $self->{stat} = [stat _]; 489 $self->{stat} = [stat _];
459 490
460 # idiotic netscape sends idiotic headers AGAIN 491 # idiotic netscape sends idiotic headers AGAIN
461 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 492 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
464 if (-d _ && -r _) { 495 if (-d _ && -r _) {
465 # directory 496 # directory
466 if ($path !~ /\/$/) { 497 if ($path !~ /\/$/) {
467 # create a redirect to get the trailing "/" 498 # create a redirect to get the trailing "/"
468 # we don't try to avoid the :80 499 # we don't try to avoid the :80
469 $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}/" });
470 } else { 501 } else {
471 $ims < $self->{stat}[9] 502 $ims < $self->{stat}[9]
472 or $self->err(304, "not modified"); 503 or $self->err (304, "not modified");
473 504
474 if (-r "$path/index.html") { 505 if (-r "$path/index.html") {
475 # replace directory "size" by index.html filesize 506 # replace directory "size" by index.html filesize
476 $self->{stat} = [stat ($self->{path} .= "/index.html")]; 507 $self->{stat} = [stat ($self->{path} .= "/index.html")];
477 $self->handle_file($queue_index, $tbf_top); 508 $self->handle_file ($queue_index, $tbf_top);
478 } else { 509 } else {
479 $self->handle_dir; 510 $self->handle_dir;
480 } 511 }
481 } 512 }
482 } elsif (-f _ && -r _) { 513 } elsif (-f _ && -r _) {
483 -x _ and $self->err(403, "forbidden"); 514 -x _ and $self->err (403, "forbidden");
484 515
485 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { 516 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
486 my $timeout = $::NOW + 10; 517 my $timeout = $::NOW + 10;
487 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { 518 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
488 if ($timeout < $::NOW) { 519 if ($timeout < $::NOW) {
491 $httpevent->wait; 522 $httpevent->wait;
492 } 523 }
493 } 524 }
494 } 525 }
495 526
496 $self->handle_file($queue_file, $tbf_top); 527 $self->handle_file ($queue_file, $tbf_top);
497 } else { 528 } else {
498 $self->err(404, "not found"); 529 $self->err (404, "not found");
499 } 530 }
500 } 531 }
501} 532}
502 533
503sub handle_dir { 534sub handle_dir {
504 my $self = shift; 535 my $self = shift;
505 my $idx = $self->diridx; 536 my $idx = $self->diridx;
506 537
507 $self->response(200, "ok", 538 $self->response (200, "ok",
508 { 539 {
509 "Content-Type" => "text/html", 540 "Content-Type" => "text/html; charset=utf-8",
510 "Content-Length" => length $idx, 541 "Content-Length" => length $idx,
511 "Last-Modified" => time2str ($self->{stat}[9]), 542 "Last-Modified" => time2str ($self->{stat}[9]),
512 }, 543 },
513 $idx); 544 $idx);
514} 545}
515 546
516sub handle_file { 547sub handle_file {
517 my ($self, $queue, $tbf) = @_; 548 my ($self, $queue, $tbf) = @_;
518 my $length = $self->{stat}[7]; 549 my $length = $self->{stat}[7];
519 my $hdr = { 550 my $hdr = {
520 "Last-Modified" => time2str ((stat _)[9]), 551 "Last-Modified" => time2str ((stat _)[9]),
552 "Accept-Ranges" => "bytes",
521 }; 553 };
522 554
523 my @code = (200, "ok"); 555 my @code = (200, "ok");
524 my ($l, $h); 556 my ($l, $h);
525 557
535 } 567 }
536 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; 568 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
537 } 569 }
538 $hdr->{"Content-Range"} = "bytes */$length"; 570 $hdr->{"Content-Range"} = "bytes */$length";
539 $hdr->{"Content-Length"} = $length; 571 $hdr->{"Content-Length"} = $length;
540 $self->err(416, "not satisfiable", $hdr, ""); 572 $self->err (416, "not satisfiable", $hdr, "");
541 573
542satisfiable: 574satisfiable:
543 # check for segmented downloads 575 # check for segmented downloads
544 if ($l && $::NO_SEGMENTED) { 576 if ($l && $::NO_SEGMENTED) {
545 my $timeout = $::NOW + 15; 577 my $timeout = $::NOW + 15;
546 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 578 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
547 if ($timeout <= $::NOW) { 579 if ($timeout <= $::NOW) {
548 $self->block($::BLOCKTIME, "segmented downloads are forbidden"); 580 $self->block ($::BLOCKTIME, "segmented downloads are forbidden");
549 #$self->err_segmented_download; 581 #$self->err_segmented_download;
550 } else { 582 } else {
551 $httpevent->wait; 583 $httpevent->wait;
552 } 584 }
553 } 585 }
564 596
565 $self->{path} =~ /\.([^.]+)$/; 597 $self->{path} =~ /\.([^.]+)$/;
566 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; 598 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
567 $hdr->{"Content-Length"} = $length; 599 $hdr->{"Content-Length"} = $length;
568 600
569 $self->response(@code, $hdr, ""); 601 $self->response (@code, $hdr, "");
570 602
571 if ($self->{method} eq "GET") { 603 if ($self->{method} eq "GET") {
572 $self->{time} = $::NOW; 604 $self->{time} = $::NOW;
573 $self->{written} = 0; 605 $self->{written} = 0;
574 606
575 my $current = $Coro::current;
576
577 my ($fh, $buf, $r);
578
579 open $fh, "<", $self->{path} 607 open my $fh, "<", $self->{path}
580 or die "$self->{path}: late open failure ($!)"; 608 or die "$self->{path}: late open failure ($!)";
581 609
582 $h -= $l - 1; 610 $h -= $l - 1;
583 611
584 if (0) { # !AIO
585 if ($l) {
586 sysseek $fh, $l, 0;
587 }
588 }
589
590 my $transfer = $queue->start_transfer($h); 612 my $transfer = $queue->start_transfer ($h);
591 my $locked; 613 my $locked;
592 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size 614 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
593 615
594 while ($h > 0) { 616 while ($h > 0) {
595 unless ($locked) { 617 unless ($locked) {
596 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 618 if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) {
597 $bufsize = $::BUFSIZE; 619 $bufsize = $::BUFSIZE;
598 $self->{time} = $::NOW; 620 $self->{time} = $::NOW;
599 $self->{written} = 0; 621 $self->{written} = 0;
600 } 622 }
601 } 623 }
603 if ($blocked{$self->{remote_id}}) { 625 if ($blocked{$self->{remote_id}}) {
604 $self->{h}{connection} = "close"; 626 $self->{h}{connection} = "close";
605 die bless {}, err::; 627 die bless {}, err::;
606 } 628 }
607 629
608 if (0) { # !AIO 630 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0
609 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
610 or last; 631 or last;
611 } else {
612 aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h),
613 $buf, 0, sub {
614 $r = $_[0];
615 Coro::ready($current);
616 });
617 &Coro::schedule;
618 last unless $r;
619 }
620 632
621 $tbf->request(length $buf); 633 $tbf->request (length $buf);
622 my $w = syswrite $self->{fh}, $buf 634 my $w = syswrite $self->{fh}, $buf
623 or last; 635 or last;
624 $::written += $w; 636 $::written += $w;
625 $self->{written} += $w; 637 $self->{written} += $w;
626 $l += $r; 638 $l += $w;
627 } 639 }
628 640
629 close $fh; 641 close $fh;
630 } 642 }
631} 643}
632 644
6331; 6451
646

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines