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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines