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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines