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.67 by root, Sun May 19 21:00:47 2002 UTC vs.
Revision 1.95 by root, Wed Apr 24 23:49:34 2013 UTC

1use AnyEvent ();
2
1use Coro; 3use Coro;
2use Coro::Semaphore; 4use Coro::Semaphore;
5use Coro::SemaphoreSet;
3use Coro::Event; 6use Coro::EV;
4use Coro::Socket; 7use Coro::Socket;
5use Coro::Signal; 8use Coro::Signal;
9use Coro::AIO ();
6 10
11use Fcntl;
7use HTTP::Date; 12use HTTP::Date;
8use POSIX (); 13use POSIX ();
9 14
10use Compress::Zlib (); 15use Compress::Zlib ();
11 16
12no utf8; 17use common::sense;
13use bytes;
14 18
15# at least on my machine, this thingy serves files 19# at least on my machine, this thingy serves files
16# quite a bit faster than apache, ;) 20# quite a bit faster than apache, ;)
17# and quite a bit slower than thttpd :( 21# and quite a bit slower than thttpd :(
18 22
19$SIG{PIPE} = 'IGNORE'; 23$SIG{PIPE} = 'IGNORE';
20 24
21our $accesslog; 25our $accesslog;
22our $errorlog; 26our $errorlog;
27our @listen_sockets;
23 28
24our $NOW; 29our $NOW;
25our $HTTP_NOW; 30our $HTTP_NOW;
26 31
27Event->timer(interval => 1, hard => 1, cb => sub { 32our $ERROR_LOG;
33our $ACCESS_LOG;
34our $TRANSFER_LOCK = new Coro::SemaphoreSet; # lock to be acquired per ip
35
36our $update_time = EV::periodic 0, 1, undef, sub {
28 $NOW = time; 37 $NOW = time;
29 $HTTP_NOW = time2str $NOW; 38 $HTTP_NOW = time2str $NOW;
30})->now; 39};
40$update_time->invoke;
31 41
32if ($ERROR_LOG) { 42if ($ERROR_LOG) {
33 use IO::Handle; 43 use IO::Handle;
34 open $errorlog, ">>$ERROR_LOG" 44 open $errorlog, ">>$ERROR_LOG"
35 or die "$ERROR_LOG: $!"; 45 or die "$ERROR_LOG: $!";
44} 54}
45 55
46sub slog { 56sub slog {
47 my $level = shift; 57 my $level = shift;
48 my $format = shift; 58 my $format = shift;
59
60 $format = sprintf $format, @_ if @_;
61
49 my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); 62 my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW);
50 printf "$NOW: $format\n", @_; 63 print "$NOW: $format\n";
51 printf $errorlog "$NOW: $format\n", @_ if $errorlog; 64 print $errorlog "$NOW: $format\n", @_ if $errorlog;
52} 65}
53 66
54our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 67our $connections = new Coro::Semaphore $::MAX_CONNECTS || 250;
55our $httpevent = new Coro::Signal; 68our $httpevent = new Coro::Signal;
56 69
57our $queue_file = new transferqueue $MAX_TRANSFERS; 70our $queue_file = new transferqueue $::MAX_TRANSFERS;
58our $queue_index = new transferqueue 10; 71our $queue_index = new transferqueue 10;
59 72
60our $tbf_top = new tbf rate => 200000; 73our $tbf_top = new tbf rate => $::TBF_RATE || 100000;
61 74
62my @newcons; 75my $unused_bytes = 0;
63my @pool; 76my $unused_last = time;
64 77
65# one "execution thread" 78sub unused_bandwidth {
66sub handler { 79 $unused_bytes += $_[0];
67 while () { 80 if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) {
68 if (@newcons) { 81 $unused_last = $NOW;
69 eval { 82 $unused_bytes = 0;
70 conn->new(@{pop @newcons})->handle; 83 $queue_file->force_wake_next
71 }; 84 and slog 1, "forced filetransfer due to unused bandwidth";
72 slog 1, "$@" if $@ && !ref $@;
73
74 $httpevent->broadcast; # only for testing, but doesn't matter much
75
76 $connections->up;
77 } else {
78 last if @pool >= $MAX_POOL;
79 push @pool, $Coro::current;
80 schedule;
81 }
82 } 85 }
83} 86}
84 87
85sub listen_on { 88sub listen_on {
86 my $listen = $_[0]; 89 my $listen = $_[0];
90 # the "main thread" 93 # the "main thread"
91 async { 94 async {
92 slog 1, "accepting connections"; 95 slog 1, "accepting connections";
93 while () { 96 while () {
94 $connections->down; 97 $connections->down;
95 push @newcons, [$listen->accept]; 98 my @conn = $listen->accept;
96 #slog 3, "accepted @$connections ".scalar(@pool); 99 #slog 3, "accepted @$connections ".scalar(@pool);
97 if (@pool) { 100
98 (pop @pool)->ready; 101 async_pool {
99 } else { 102 eval {
100 async \&handler; 103 conn->new (@conn)->handle;
101 } 104 };
105 slog 1, "$@" if $@ && !ref $@;
102 106
107 $httpevent->broadcast; # only for testing, but doesn't matter much
108
109 $connections->up;
110 }
103 } 111 }
104 }; 112 };
105} 113}
106 114
107my $http_port = new Coro::Socket 115my $http_port = new Coro::Socket
108 LocalAddr => $SERVER_HOST, 116 LocalAddr => $::SERVER_HOST,
109 LocalPort => $SERVER_PORT, 117 LocalPort => $::SERVER_PORT,
110 ReuseAddr => 1, 118 ReuseAddr => 1,
111 Listen => 50, 119 Listen => 50,
112 or die "unable to start server"; 120 or die "unable to start server";
113 121
114listen_on $http_port; 122listen_on $http_port;
115 123
116if ($SERVER_PORT2) { 124if ($::SERVER_PORT2) {
117 my $http_port = new Coro::Socket 125 my $http_port = new Coro::Socket
118 LocalAddr => $SERVER_HOST, 126 LocalAddr => $::SERVER_HOST,
119 LocalPort => $SERVER_PORT2, 127 LocalPort => $::SERVER_PORT2,
120 ReuseAddr => 1, 128 ReuseAddr => 1,
121 Listen => 50, 129 Listen => 50,
122 or die "unable to start server"; 130 or die "unable to start server";
123 131
124 listen_on $http_port; 132 listen_on $http_port;
125} 133}
126 134
127package conn; 135package conn;
136
137use common::sense;
128 138
129use Socket; 139use Socket;
130use HTTP::Date; 140use HTTP::Date;
131use Convert::Scalar 'weaken'; 141use Convert::Scalar 'weaken';
132use Linux::AIO; 142use IO::AIO;
143use AnyEvent::AIO;
133 144
134Linux::AIO::min_parallel $::AIO_PARALLEL; 145IO::AIO::min_parallel $::AIO_PARALLEL;
135
136Event->io(fd => Linux::AIO::poll_fileno,
137 poll => 'r', async => 1,
138 cb => \&Linux::AIO::poll_cb);
139 146
140our %conn; # $conn{ip}{self} => connobj 147our %conn; # $conn{ip}{self} => connobj
141our %uri; # $uri{ip}{uri}{self} 148our %uri; # $uri{ip}{uri}{self}
142our %blocked; 149our %blocked;
143our %mimetype; 150our %mimetype;
144 151
145sub read_mimetypes { 152sub read_mimetypes {
146 local *M;
147 if (open M, "<mime_types") { 153 if (open my $fh, "<mime_types") {
148 while (<M>) { 154 while (<$fh>) {
149 if (/^([^#]\S+)\t+(\S+)$/) { 155 if (/^([^#]\S+)\t+(\S+)$/) {
150 $mimetype{lc $1} = $2; 156 $mimetype{lc $1} = $2;
151 } 157 }
152 } 158 }
153 } else { 159 } else {
161 my $class = shift; 167 my $class = shift;
162 my $fh = shift; 168 my $fh = shift;
163 my $peername = shift; 169 my $peername = shift;
164 my $self = bless { fh => $fh }, $class; 170 my $self = bless { fh => $fh }, $class;
165 my (undef, $iaddr) = unpack_sockaddr_in $peername 171 my (undef, $iaddr) = unpack_sockaddr_in $peername
166 or $self->err(500, "unable to decode peername"); 172 or $self->err (500, "unable to decode peername");
167 173
168 $self->{remote_addr} = 174 $self->{remote_addr} =
169 $self->{remote_id} = inet_ntoa $iaddr; 175 $self->{remote_id} = inet_ntoa $iaddr;
170 176
171 $self->{time} = $::NOW; 177 $self->{time} = $::NOW;
172 178
173 weaken ($Coro::current->{conn} = $self); 179 weaken ($Coro::current->{conn} = $self);
174 180
175 $::conns++; 181 ++$::conns;
176 $::maxconns = $::conns if $::conns > $::maxconns; 182 $::maxconns = $::conns if $::conns > $::maxconns;
177 183
178 $self; 184 $self
179} 185}
180 186
181sub DESTROY { 187sub DESTROY {
182 #my $self = shift; 188 my $self = shift;
189
183 $::conns--; 190 --$::conns;
184} 191}
192
193sub prune_cache {
194 my $hash = $_[0];
195
196 for (keys %$hash) {
197 if (ref $hash->{$_} eq HASH::) {
198 prune_cache($hash->{$_});
199 unless (scalar keys %{$hash->{$_}}) {
200 delete $hash->{$_};
201 }
202 }
203 }
204}
205
206sub prune_caches {
207 prune_cache \%conn;
208 prune_cache \%uri;
209
210 for (keys %blocked) {
211 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
212 }
213}
214
215our $PRUNE_WATCHER = AE::timer 60, 60, \&prune_caches;
185 216
186sub slog { 217sub slog {
187 my $self = shift; 218 my $self = shift;
188 main::slog($_[0], "$self->{remote_id}> $_[1]"); 219 main::slog($_[0], "$self->{remote_id}> $_[1]");
189} 220}
219 $hdr->{"Content-Length"} = length $content; 250 $hdr->{"Content-Length"} = length $content;
220 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); 251 $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig);
221 } 252 }
222 253
223 $res .= "Date: $HTTP_NOW\015\012"; 254 $res .= "Date: $HTTP_NOW\015\012";
255 $res .= "Server: $::NAME\015\012";
224 256
225 while (my ($h, $v) = each %$hdr) { 257 while (my ($h, $v) = each %$hdr) {
226 $res .= "$h: $v\015\012" 258 $res .= "$h: $v\015\012"
227 } 259 }
228 $res .= "\015\012"; 260 $res .= "\015\012";
249 $hdr->{"Content-Type"} = "text/plain"; 281 $hdr->{"Content-Type"} = "text/plain";
250 $hdr->{"Content-Length"} = length $content; 282 $hdr->{"Content-Length"} = length $content;
251 } 283 }
252 $hdr->{"Connection"} = "close"; 284 $hdr->{"Connection"} = "close";
253 285
254 $self->response($code, $msg, $hdr, $content); 286 $self->response ($code, $msg, $hdr, $content);
255 287
256 die bless {}, err::; 288 die bless {}, err::
257} 289}
258 290
259sub handle { 291sub handle {
260 my $self = shift; 292 my $self = shift;
261 my $fh = $self->{fh}; 293 my $fh = $self->{fh};
262 294
263 my $host; 295 my $host;
264 296
265 $fh->timeout($::REQ_TIMEOUT); 297 $fh->timeout($::REQ_TIMEOUT);
266 while() { 298 while () {
267 $self->{reqs}++; 299 $self->{reqs}++;
268 300
269 # read request and parse first line 301 # read request and parse first line
270 my $req = $fh->readline("\015\012\015\012"); 302 my $req = $fh->readline("\015\012\015\012");
271 303
380# uri => path mapping 412# uri => path mapping
381sub map_uri { 413sub map_uri {
382 my $self = shift; 414 my $self = shift;
383 my $host = $self->{server_name}; 415 my $host = $self->{server_name};
384 my $uri = $self->{uri}; 416 my $uri = $self->{uri};
417
418 $host =~ /[\/\\]/
419 and $self->err(400, "bad request");
385 420
386 # some massaging, also makes it more secure 421 # some massaging, also makes it more secure
387 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; 422 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge;
388 $uri =~ s%//+%/%g; 423 $uri =~ s%//+%/%g;
389 $uri =~ s%/\.(?=/|$)%%g; 424 $uri =~ s%/\.(?=/|$)%%g;
433 468
434 if ($self->{name} =~ s%^/internal/([^/]+)%%) { 469 if ($self->{name} =~ s%^/internal/([^/]+)%%) {
435 if ($::internal{$1}) { 470 if ($::internal{$1}) {
436 $::internal{$1}->($self); 471 $::internal{$1}->($self);
437 } else { 472 } else {
438 $self->err(404, "not found"); 473 $self->err (404, "not found");
439 } 474 }
440 } else { 475 } else {
441 476
442 stat $path 477 Coro::AIO::aio_stat $path
443 or $self->err(404, "not found"); 478 and $self->err (404, "not found");
444 479
445 $self->{stat} = [stat _]; 480 $self->{stat} = [stat _];
446 481
447 # idiotic netscape sends idiotic headers AGAIN 482 # idiotic netscape sends idiotic headers AGAIN
448 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 483 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
451 if (-d _ && -r _) { 486 if (-d _ && -r _) {
452 # directory 487 # directory
453 if ($path !~ /\/$/) { 488 if ($path !~ /\/$/) {
454 # create a redirect to get the trailing "/" 489 # create a redirect to get the trailing "/"
455 # we don't try to avoid the :80 490 # we don't try to avoid the :80
456 $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); 491 $self->err (301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" });
457 } else { 492 } else {
458 $ims < $self->{stat}[9] 493 $ims < $self->{stat}[9]
459 or $self->err(304, "not modified"); 494 or $self->err (304, "not modified");
460 495
461 if (-r "$path/index.html") { 496 if (-r "$path/index.html") {
462 # replace directory "size" by index.html filesize 497 # replace directory "size" by index.html filesize
463 $self->{stat} = [stat ($self->{path} .= "/index.html")]; 498 $self->{stat} = [stat ($self->{path} .= "/index.html")];
464 $self->handle_file($queue_index, $tbf_top); 499 $self->handle_file ($queue_index, $tbf_top);
465 } else { 500 } else {
466 $self->handle_dir; 501 $self->handle_dir;
467 } 502 }
468 } 503 }
469 } elsif (-f _ && -r _) { 504 } elsif (-f _ && -r _) {
470 -x _ and $self->err(403, "forbidden"); 505 -x _ and $self->err (403, "forbidden");
471 506
472 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { 507 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
473 my $timeout = $::NOW + 10; 508 my $timeout = $::NOW + 10;
474 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { 509 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
475 if ($timeout < $::NOW) { 510 if ($timeout < $::NOW) {
478 $httpevent->wait; 513 $httpevent->wait;
479 } 514 }
480 } 515 }
481 } 516 }
482 517
483 $self->handle_file($queue_file, $tbf_top); 518 $self->handle_file ($queue_file, $tbf_top);
484 } else { 519 } else {
485 $self->err(404, "not found"); 520 $self->err (404, "not found");
486 } 521 }
487 } 522 }
488} 523}
489 524
490sub handle_dir { 525sub handle_dir {
491 my $self = shift; 526 my $self = shift;
492 my $idx = $self->diridx; 527 my $idx = $self->diridx;
493 528
494 $self->response(200, "ok", 529 $self->response (200, "ok",
495 { 530 {
496 "Content-Type" => "text/html", 531 "Content-Type" => "text/html; charset=utf-8",
497 "Content-Length" => length $idx, 532 "Content-Length" => length $idx,
498 "Last-Modified" => time2str ($self->{stat}[9]), 533 "Last-Modified" => time2str ($self->{stat}[9]),
499 }, 534 },
500 $idx); 535 $idx);
501} 536}
502 537
503sub handle_file { 538sub handle_file {
504 my ($self, $queue, $tbf) = @_; 539 my ($self, $queue, $tbf) = @_;
505 my $length = $self->{stat}[7]; 540 my $length = $self->{stat}[7];
506 my $hdr = { 541 my $hdr = {
507 "Last-Modified" => time2str ((stat _)[9]), 542 "Last-Modified" => time2str ((stat _)[9]),
543 "Accept-Ranges" => "bytes",
508 }; 544 };
509 545
510 my @code = (200, "ok"); 546 my @code = (200, "ok");
511 my ($l, $h); 547 my ($l, $h);
512 548
513 if ($self->{h}{range} =~ /^bytes=(.*)$/) { 549 if ($self->{h}{range} =~ /^bytes=(.*)$/i) {
514 for (split /,/, $1) { 550 for (split /,/, $1) {
515 if (/^-(\d+)$/) { 551 if (/^-(\d+)$/) {
516 ($l, $h) = ($length - $1, $length - 1); 552 ($l, $h) = ($length - $1, $length - 1);
517 } elsif (/^(\d+)-(\d*)$/) { 553 } elsif (/^(\d+)-(\d*)$/) {
518 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 554 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
522 } 558 }
523 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; 559 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
524 } 560 }
525 $hdr->{"Content-Range"} = "bytes */$length"; 561 $hdr->{"Content-Range"} = "bytes */$length";
526 $hdr->{"Content-Length"} = $length; 562 $hdr->{"Content-Length"} = $length;
527 $self->err(416, "not satisfiable", $hdr, ""); 563 $self->err (416, "not satisfiable", $hdr, "");
528 564
529satisfiable: 565satisfiable:
530 # check for segmented downloads 566 # check for segmented downloads
531 if ($l && $::NO_SEGMENTED) { 567 if ($l && $::NO_SEGMENTED) {
532 my $timeout = $::NOW + 15; 568 my $timeout = $::NOW + 60;
533 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 569 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
534 if ($timeout <= $::NOW) { 570 if ($timeout <= $::NOW) {
535 $self->block($::BLOCKTIME, "segmented downloads are forbidden");
536 #$self->err_segmented_download; 571 $self->err_segmented_download;
537 } else { 572 } else {
538 $httpevent->wait; 573 $httpevent->wait;
539 } 574 }
540 } 575 }
541 } 576 }
551 586
552 $self->{path} =~ /\.([^.]+)$/; 587 $self->{path} =~ /\.([^.]+)$/;
553 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; 588 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
554 $hdr->{"Content-Length"} = $length; 589 $hdr->{"Content-Length"} = $length;
555 590
556 $self->response(@code, $hdr, ""); 591 $self->response (@code, $hdr, "");
557 592
558 if ($self->{method} eq "GET") { 593 if ($self->{method} eq "GET") {
559 $self->{time} = $::NOW; 594 $self->{time} = $::NOW;
595 $self->{written} = 0;
560 596
561 my $current = $Coro::current; 597 my $fh = Coro::AIO::aio_open $self->{path}, Fcntl::O_RDONLY, 0
562
563 my ($fh, $buf, $r);
564
565 open $fh, "<", $self->{path}
566 or die "$self->{path}: late open failure ($!)"; 598 or die "$self->{path}: late open failure ($!)";
567 599
568 $h -= $l - 1; 600 $h -= $l - 1;
569 601
570 if (0) { # !AIO
571 if ($l) {
572 sysseek $fh, $l, 0;
573 }
574 }
575
576 my $transfer = $queue->start_transfer($h); 602 my $transfer = $queue->start_transfer ($h);
577 my $locked; 603 my $locked;
578 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size 604 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
579 605
580 while ($h > 0) { 606 while ($h > 0) {
607 Coro::cede;
608 my $transfer_lock = $TRANSFER_LOCK->guard ($self->{remote_id});
609
581 unless ($locked) { 610 unless ($locked) {
582 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 611 if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) {
583 $bufsize = $::BUFSIZE; 612 $bufsize = $::BUFSIZE;
584 $self->{time} = $::NOW; 613 $self->{time} = $::NOW;
614 $self->{written} = 0;
585 } 615 }
586 } 616 }
587 617
588 if ($blocked{$self->{remote_id}}) { 618 if ($blocked{$self->{remote_id}}) {
589 $self->{h}{connection} = "close"; 619 $self->{h}{connection} = "close";
590 die bless {}, err::; 620 die bless {}, err::;
591 } 621 }
592 622
593 if (0) { # !AIO 623 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0
594 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
595 or last; 624 or last;
596 } else {
597 aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h),
598 $buf, 0, sub {
599 $r = $_[0];
600 Coro::ready($current);
601 });
602 &Coro::schedule;
603 last unless $r;
604 }
605 625
626 # readahead to work around rijk disk issues
627 IO::AIO::aio_readahead $fh, $l + $bufsize, $bufsize;
628
606 $tbf->request(length $buf); 629 $tbf->request (length $buf);
607 my $w = syswrite $self->{fh}, $buf 630 my $w = $self->{fh}->syswrite ($buf)
608 or last; 631 or last;
609 $::written += $w; 632 $::written += $w;
610 $self->{written} += $w; 633 $self->{written} += $w;
611 $l += $r; 634 $l += $w;
612 } 635 }
613 636
614 close $fh; 637 close $fh;
615 } 638 }
616} 639}
617 640
6181; 6411
642

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines