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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines