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.84 by root, Sat Dec 8 21:01:16 2007 UTC vs.
Revision 1.94 by root, Sat Aug 13 23:42:05 2011 UTC

1use AnyEvent ();
2
1use Coro; 3use Coro;
2use Coro::Semaphore; 4use Coro::Semaphore;
5use Coro::SemaphoreSet;
3use Coro::EV; 6use Coro::EV;
4use Coro::Socket; 7use Coro::Socket;
5use Coro::Signal; 8use Coro::Signal;
6use Coro::AIO (); 9use Coro::AIO ();
7 10
11use Fcntl;
8use HTTP::Date; 12use HTTP::Date;
9use POSIX (); 13use POSIX ();
10 14
11use Compress::Zlib (); 15use Compress::Zlib ();
12 16
13no utf8; 17use common::sense;
14use bytes;
15 18
16# at least on my machine, this thingy serves files 19# at least on my machine, this thingy serves files
17# quite a bit faster than apache, ;) 20# quite a bit faster than apache, ;)
18# and quite a bit slower than thttpd :( 21# and quite a bit slower than thttpd :(
19 22
20$SIG{PIPE} = 'IGNORE'; 23$SIG{PIPE} = 'IGNORE';
21 24
22our $accesslog; 25our $accesslog;
23our $errorlog; 26our $errorlog;
27our @listen_sockets;
24 28
25our $NOW; 29our $NOW;
26our $HTTP_NOW; 30our $HTTP_NOW;
27 31
28our $ERROR_LOG; 32our $ERROR_LOG;
29our $ACCESS_LOG; 33our $ACCESS_LOG;
34our $TRANSFER_LOCK = new Coro::SemaphoreSet; # lock to be acquired per ip
30 35
31our $update_time = EV::periodic 0, 1, undef, sub { 36our $update_time = EV::periodic 0, 1, undef, sub {
32 $NOW = time; 37 $NOW = time;
33 $HTTP_NOW = time2str $NOW; 38 $HTTP_NOW = time2str $NOW;
34}; 39};
49} 54}
50 55
51sub slog { 56sub slog {
52 my $level = shift; 57 my $level = shift;
53 my $format = shift; 58 my $format = shift;
59
60 $format = sprintf $format, @_ if @_;
61
54 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);
55 printf "$NOW: $format\n", @_; 63 print "$NOW: $format\n";
56 printf $errorlog "$NOW: $format\n", @_ if $errorlog; 64 print $errorlog "$NOW: $format\n", @_ if $errorlog;
57} 65}
58 66
59our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 67our $connections = new Coro::Semaphore $::MAX_CONNECTS || 250;
60our $httpevent = new Coro::Signal; 68our $httpevent = new Coro::Signal;
61 69
62our $queue_file = new transferqueue $MAX_TRANSFERS; 70our $queue_file = new transferqueue $::MAX_TRANSFERS;
63our $queue_index = new transferqueue 10; 71our $queue_index = new transferqueue 10;
64 72
65our $tbf_top = new tbf rate => $TBF_RATE || 100000; 73our $tbf_top = new tbf rate => $::TBF_RATE || 100000;
66 74
67my $unused_bytes = 0; 75my $unused_bytes = 0;
68my $unused_last = time; 76my $unused_last = time;
69 77
70sub unused_bandwidth { 78sub unused_bandwidth {
71 $unused_bytes += $_[0]; 79 $unused_bytes += $_[0];
72 if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { 80 if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) {
73 $unused_last = $NOW; 81 $unused_last = $NOW;
74 $unused_bytes = 0; 82 $unused_bytes = 0;
75 $queue_file->force_wake_next; 83 $queue_file->force_wake_next
76 slog 1, "forced filetransfer due to unused bandwidth"; 84 and slog 1, "forced filetransfer due to unused bandwidth";
77 }
78}
79
80my @newcons;
81my @pool;
82
83# one "execution thread"
84sub handler {
85 while () {
86 if (@newcons) {
87 eval {
88 conn->new (@{pop @newcons})->handle;
89 };
90 slog 1, "$@" if $@ && !ref $@;
91
92 $httpevent->broadcast; # only for testing, but doesn't matter much
93
94 $connections->up;
95 } else {
96 last if @pool >= $MAX_POOL;
97 push @pool, $Coro::current;
98 schedule;
99 }
100 } 85 }
101} 86}
102 87
103sub listen_on { 88sub listen_on {
104 my $listen = $_[0]; 89 my $listen = $_[0];
108 # the "main thread" 93 # the "main thread"
109 async { 94 async {
110 slog 1, "accepting connections"; 95 slog 1, "accepting connections";
111 while () { 96 while () {
112 $connections->down; 97 $connections->down;
113 push @newcons, [$listen->accept]; 98 my @conn = $listen->accept;
114 #slog 3, "accepted @$connections ".scalar(@pool); 99 #slog 3, "accepted @$connections ".scalar(@pool);
115 if (@pool) { 100
116 (pop @pool)->ready; 101 async_pool {
117 } else { 102 eval {
118 async \&handler; 103 conn->new (@conn)->handle;
104 };
105 slog 1, "$@" if $@ && !ref $@;
106
107 $httpevent->broadcast; # only for testing, but doesn't matter much
108
109 $connections->up;
119 } 110 }
120 } 111 }
121 }; 112 };
122} 113}
123 114
124my $http_port = new Coro::Socket 115my $http_port = new Coro::Socket
125 LocalAddr => $SERVER_HOST, 116 LocalAddr => $::SERVER_HOST,
126 LocalPort => $SERVER_PORT, 117 LocalPort => $::SERVER_PORT,
127 ReuseAddr => 1, 118 ReuseAddr => 1,
128 Listen => 50, 119 Listen => 50,
129 or die "unable to start server"; 120 or die "unable to start server";
130 121
131listen_on $http_port; 122listen_on $http_port;
132 123
133if ($SERVER_PORT2) { 124if ($::SERVER_PORT2) {
134 my $http_port = new Coro::Socket 125 my $http_port = new Coro::Socket
135 LocalAddr => $SERVER_HOST, 126 LocalAddr => $::SERVER_HOST,
136 LocalPort => $SERVER_PORT2, 127 LocalPort => $::SERVER_PORT2,
137 ReuseAddr => 1, 128 ReuseAddr => 1,
138 Listen => 50, 129 Listen => 50,
139 or die "unable to start server"; 130 or die "unable to start server";
140 131
141 listen_on $http_port; 132 listen_on $http_port;
142} 133}
143 134
144package conn; 135package conn;
145 136
146use strict; 137use common::sense;
147use bytes;
148 138
149use Socket; 139use Socket;
150use HTTP::Date; 140use HTTP::Date;
151use Convert::Scalar 'weaken'; 141use Convert::Scalar 'weaken';
152use IO::AIO; 142use IO::AIO;
143use AnyEvent::AIO;
153 144
154IO::AIO::min_parallel $::AIO_PARALLEL; 145IO::AIO::min_parallel $::AIO_PARALLEL;
155
156our $AIO_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb;
157 146
158our %conn; # $conn{ip}{self} => connobj 147our %conn; # $conn{ip}{self} => connobj
159our %uri; # $uri{ip}{uri}{self} 148our %uri; # $uri{ip}{uri}{self}
160our %blocked; 149our %blocked;
161our %mimetype; 150our %mimetype;
221 for (keys %blocked) { 210 for (keys %blocked) {
222 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; 211 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
223 } 212 }
224} 213}
225 214
226our $PRUNE_WATCHER = EV::timer 60, 60, \&prune_caches; 215our $PRUNE_WATCHER = AE::timer 60, 60, \&prune_caches;
227 216
228sub slog { 217sub slog {
229 my $self = shift; 218 my $self = shift;
230 main::slog($_[0], "$self->{remote_id}> $_[1]"); 219 main::slog($_[0], "$self->{remote_id}> $_[1]");
231} 220}
424sub map_uri { 413sub map_uri {
425 my $self = shift; 414 my $self = shift;
426 my $host = $self->{server_name}; 415 my $host = $self->{server_name};
427 my $uri = $self->{uri}; 416 my $uri = $self->{uri};
428 417
418 $host =~ /[\/\\]/
419 and $self->err(400, "bad request");
420
429 # some massaging, also makes it more secure 421 # some massaging, also makes it more secure
430 $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;
431 $uri =~ s%//+%/%g; 423 $uri =~ s%//+%/%g;
432 $uri =~ s%/\.(?=/|$)%%g; 424 $uri =~ s%/\.(?=/|$)%%g;
433 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; 425 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%;
480 } else { 472 } else {
481 $self->err (404, "not found"); 473 $self->err (404, "not found");
482 } 474 }
483 } else { 475 } else {
484 476
485 stat $path 477 Coro::AIO::aio_stat $path
486 or $self->err (404, "not found"); 478 and $self->err (404, "not found");
487 479
488 $self->{stat} = [stat _]; 480 $self->{stat} = [stat _];
489 481
490 # idiotic netscape sends idiotic headers AGAIN 482 # idiotic netscape sends idiotic headers AGAIN
491 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 483 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
552 }; 544 };
553 545
554 my @code = (200, "ok"); 546 my @code = (200, "ok");
555 my ($l, $h); 547 my ($l, $h);
556 548
557 if ($self->{h}{range} =~ /^bytes=(.*)$/) { 549 if ($self->{h}{range} =~ /^bytes=(.*)$/i) {
558 for (split /,/, $1) { 550 for (split /,/, $1) {
559 if (/^-(\d+)$/) { 551 if (/^-(\d+)$/) {
560 ($l, $h) = ($length - $1, $length - 1); 552 ($l, $h) = ($length - $1, $length - 1);
561 } elsif (/^(\d+)-(\d*)$/) { 553 } elsif (/^(\d+)-(\d*)$/) {
562 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 554 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
601 593
602 if ($self->{method} eq "GET") { 594 if ($self->{method} eq "GET") {
603 $self->{time} = $::NOW; 595 $self->{time} = $::NOW;
604 $self->{written} = 0; 596 $self->{written} = 0;
605 597
606 open my $fh, "<", $self->{path} 598 my $fh = Coro::AIO::aio_open $self->{path}, Fcntl::O_RDONLY, 0
607 or die "$self->{path}: late open failure ($!)"; 599 or die "$self->{path}: late open failure ($!)";
608 600
609 $h -= $l - 1; 601 $h -= $l - 1;
610 602
611 my $transfer = $queue->start_transfer ($h); 603 my $transfer = $queue->start_transfer ($h);
612 my $locked; 604 my $locked;
613 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size 605 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
614 606
615 while ($h > 0) { 607 while ($h > 0) {
608 Coro::cede;
609 my $transfer_lock = $TRANSFER_LOCK->guard ($self->{remote_id});
610
616 unless ($locked) { 611 unless ($locked) {
617 if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { 612 if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) {
618 $bufsize = $::BUFSIZE; 613 $bufsize = $::BUFSIZE;
619 $self->{time} = $::NOW; 614 $self->{time} = $::NOW;
620 $self->{written} = 0; 615 $self->{written} = 0;
628 623
629 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 624 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0
630 or last; 625 or last;
631 626
632 $tbf->request (length $buf); 627 $tbf->request (length $buf);
633 my $w = syswrite $self->{fh}, $buf 628 my $w = $self->{fh}->syswrite ($buf)
634 or last; 629 or last;
635 $::written += $w; 630 $::written += $w;
636 $self->{written} += $w; 631 $self->{written} += $w;
637 $l += $w; 632 $l += $w;
638 } 633 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines