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.34 by root, Thu Aug 30 02:58:17 2001 UTC vs.
Revision 1.43 by root, Wed Sep 12 20:29:43 2001 UTC

32our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 32our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
33 33
34our $wait_factor = 0.95; 34our $wait_factor = 0.95;
35 35
36our @transfers = ( 36our @transfers = (
37 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 600], 37 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1],
38 [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 600], 38 [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1],
39); 39);
40 40
41my @newcons; 41my @newcons;
42my @pool; 42my @pool;
43 43
44# one "execution thread" 44# one "execution thread"
45sub handler { 45sub handler {
46 while () { 46 while () {
47 my $new = pop @newcons;
48 if ($new) { 47 if (@newcons) {
49 eval { 48 eval {
50 conn->new(@$new)->handle; 49 conn->new(@{pop @newcons})->handle;
51 }; 50 };
52 slog 1, "$@" if $@ && !ref $@; 51 slog 1, "$@" if $@ && !ref $@;
53 $connections->up; 52 $connections->up;
54 } else { 53 } else {
55 last if @pool >= $MAX_POOL; 54 last if @pool >= $MAX_POOL;
57 schedule; 56 schedule;
58 } 57 }
59 } 58 }
60} 59}
61 60
61sub listen_on {
62 my $listen = $_[0];
63
64 push @listen_sockets, $listen;
65
66 # the "main thread"
67 async {
68 slog 1, "accepting connections";
69 while () {
70 $connections->down;
71 push @newcons, [$listen->accept];
72 #slog 3, "accepted @$connections ".scalar(@pool);
73 if (@pool) {
74 (pop @pool)->ready;
75 } else {
76 async \&handler;
77 }
78
79 }
80 };
81}
82
62my $http_port = new Coro::Socket 83my $http_port = new Coro::Socket
63 LocalAddr => $SERVER_HOST, 84 LocalAddr => $SERVER_HOST,
64 LocalPort => $SERVER_PORT, 85 LocalPort => $SERVER_PORT,
65 ReuseAddr => 1, 86 ReuseAddr => 1,
66 Listen => 50, 87 Listen => 50,
67 or die "unable to start server"; 88 or die "unable to start server";
68 89
69push @listen_sockets, $http_port; 90listen_on $http_port;
91
92if ($SERVER_PORT2) {
93 my $http_port = new Coro::Socket
94 LocalAddr => $SERVER_HOST,
95 LocalPort => $SERVER_PORT2,
96 ReuseAddr => 1,
97 Listen => 50,
98 or die "unable to start server";
99
100 listen_on $http_port;
101}
70 102
71our $NOW; 103our $NOW;
72our $HTTP_NOW; 104our $HTTP_NOW;
73 105
74Event->timer(interval => 1, hard => 1, cb => sub { 106Event->timer(interval => 1, hard => 1, cb => sub {
75 $NOW = time; 107 $NOW = time;
76 $HTTP_NOW = time2str $NOW; 108 $HTTP_NOW = time2str $NOW;
77})->now; 109})->now;
78 110
79# the "main thread"
80async {
81 slog 1, "accepting connections";
82 while () {
83 $connections->down;
84 push @newcons, [$http_port->accept];
85 #slog 3, "accepted @$connections ".scalar(@pool);
86 if (@pool) {
87 (pop @pool)->ready;
88 } else {
89 async \&handler;
90 }
91
92 }
93};
94
95package conn; 111package conn;
96 112
97use Socket; 113use Socket;
98use HTTP::Date; 114use HTTP::Date;
99use Convert::Scalar 'weaken'; 115use Convert::Scalar 'weaken';
100use Linux::AIO; 116use Linux::AIO;
101 117
102Linux::AIO::min_parallel $::AIO_PARALLEL; 118Linux::AIO::min_parallel $::AIO_PARALLEL;
103
104my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4;
105 119
106Event->io(fd => Linux::AIO::poll_fileno, 120Event->io(fd => Linux::AIO::poll_fileno,
107 poll => 'r', async => 1, 121 poll => 'r', async => 1,
108 cb => \&Linux::AIO::poll_cb); 122 cb => \&Linux::AIO::poll_cb);
109 123
127 141
128read_mimetypes; 142read_mimetypes;
129 143
130sub new { 144sub new {
131 my $class = shift; 145 my $class = shift;
146 my $fh = shift;
132 my $peername = shift; 147 my $peername = shift;
133 my $fh = shift;
134 my $self = bless { fh => $fh }, $class; 148 my $self = bless { fh => $fh }, $class;
135 my (undef, $iaddr) = unpack_sockaddr_in $peername 149 my (undef, $iaddr) = unpack_sockaddr_in $peername
136 or $self->err(500, "unable to decode peername"); 150 or $self->err(500, "unable to decode peername");
137 151
138 $self->{remote_addr} = inet_ntoa $iaddr; 152 $self->{remote_addr} = inet_ntoa $iaddr;
139 $self->{time} = $::NOW; 153 $self->{time} = $::NOW;
140 154
141 # enter ourselves into various lists
142 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
143
144 $::conns++; 155 $::conns++;
145 156
146 $self; 157 $self;
147} 158}
148 159
149sub DESTROY { 160sub DESTROY {
150 my $self = shift; 161 my $self = shift;
151
152 $::conns--; 162 $::conns--;
153
154 $self->eoconn; 163 $self->eoconn;
155 delete $conn{$self->{remote_addr}}{$self*1};
156} 164}
157 165
158# end of connection 166# end of connection
159sub eoconn { 167sub eoconn {
160 my $self = shift; 168 my $self = shift;
169
170 # clean up hints
171 delete $conn{$self->{remote_id}}{$self*1};
161 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 172 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1};
162} 173}
163 174
164sub slog { 175sub slog {
165 my $self = shift; 176 my $self = shift;
166 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); 177 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]");
168 179
169sub response { 180sub response {
170 my ($self, $code, $msg, $hdr, $content) = @_; 181 my ($self, $code, $msg, $hdr, $content) = @_;
171 my $res = "HTTP/1.1 $code $msg\015\012"; 182 my $res = "HTTP/1.1 $code $msg\015\012";
172 183
173 $self->{h}{connection} ||= $hdr->{Connection}; 184 $self->{h}{connection} = "close" if $hdr->{Connection} =~ /close/;
174 185
175 $res .= "Date: $HTTP_NOW\015\012"; 186 $res .= "Date: $HTTP_NOW\015\012";
176 187
177 while (my ($h, $v) = each %$hdr) { 188 while (my ($h, $v) = each %$hdr) {
178 $res .= "$h: $v\015\012" 189 $res .= "$h: $v\015\012"
193sub err { 204sub err {
194 my $self = shift; 205 my $self = shift;
195 my ($code, $msg, $hdr, $content) = @_; 206 my ($code, $msg, $hdr, $content) = @_;
196 207
197 unless (defined $content) { 208 unless (defined $content) {
198 $content = "$code $msg"; 209 $content = "$code $msg\n";
199 $hdr->{"Content-Type"} = "text/plain"; 210 $hdr->{"Content-Type"} = "text/plain";
200 $hdr->{"Content-Length"} = length $content; 211 $hdr->{"Content-Length"} = length $content;
201 } 212 }
202 $hdr->{"Connection"} = "close"; 213 $hdr->{"Connection"} = "close";
203 214
228 } 239 }
229 240
230 $self->{h} = {}; 241 $self->{h} = {};
231 242
232 $fh->timeout($::RES_TIMEOUT); 243 $fh->timeout($::RES_TIMEOUT);
233 my $ip = $self->{remote_addr};
234
235 if ($blocked{$ip}) {
236 $self->err_blocked($blocked{$ip})
237 if $blocked{$ip} > $::NOW;
238
239 delete $blocked{$ip};
240 }
241
242 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
243 my $delay = 120;
244 while (%{$conn{$ip}} > $::MAX_CONN_IP) {
245 if ($delay <= 0) {
246 $self->slog(2, "blocked ip $ip");
247 $self->err_blocked;
248 } else {
249 Coro::Event::do_timer(after => 3);
250 $delay -= 3;
251 }
252 }
253 }
254 244
255 $req =~ /^(?:\015\012)? 245 $req =~ /^(?:\015\012)?
256 (GET|HEAD) \040+ 246 (GET|HEAD) \040+
257 ([^\040]+) \040+ 247 ([^\040]+) \040+
258 HTTP\/([0-9]+\.[0-9]+) 248 HTTP\/([0-9]+\.[0-9]+)
283 273
284 $self->{h}{$h} = substr $v, 1 274 $self->{h}{$h} = substr $v, 1
285 while ($h, $v) = each %hdr; 275 while ($h, $v) = each %hdr;
286 } 276 }
287 277
278 # remote id should be unique per user
279 my $id = $self->{remote_addr};
280
281 if (exists $self->{h}{"client-ip"}) {
282 $id .= "[".$self->{h}{"client-ip"}."]";
283 } elsif (exists $self->{h}{"x-forwarded-for"}) {
284 $id .= "[".$self->{h}{"x-forwarded-for"}."]";
285 }
286
287 $self->{remote_id} = $id;
288
289 if ($blocked{$id}) {
290 $self->err_blocked($blocked{$id})
291 if $blocked{$id} > $::NOW;
292
293 delete $blocked{$id};
294 }
295
296 if (%{$conn{$id}} >= $::MAX_CONN_IP) {
297 my $delay = $::PER_TIMEOUT + 15;
298 while (%{$conn{$id}} >= $::MAX_CONN_IP) {
299 if ($delay <= 0) {
300 $self->slog(2, "blocked ip $id");
301 $self->err_blocked;
302 } else {
303 Coro::Event::do_timer(after => 4); $delay -= 4;
304 }
305 }
306 }
307
288 # find out server name and port 308 # find out server name and port
289 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { 309 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) {
290 $host = $1; 310 $host = $1;
291 } else { 311 } else {
292 $host = $self->{h}{host}; 312 $host = $self->{h}{host};
294 314
295 if (defined $host) { 315 if (defined $host) {
296 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; 316 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80;
297 } else { 317 } else {
298 ($self->{server_port}, $host) 318 ($self->{server_port}, $host)
299 = unpack_sockaddr_in $self->{fh}->getsockname 319 = unpack_sockaddr_in $self->{fh}->sockname
300 or $self->err(500, "unable to get socket name"); 320 or $self->err(500, "unable to get socket name");
301 $host = inet_ntoa $host; 321 $host = inet_ntoa $host;
302 } 322 }
303 323
304 $self->{server_name} = $host; 324 $self->{server_name} = $host;
305 325
306 # remote id should be unique per user 326 # enter ourselves into various lists
307 $self->{remote_id} = $self->{remote_addr}; 327 weaken ($conn{$id}{$self*1} = $self);
308
309 if (exists $self->{h}{"client-ip"}) {
310 $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]";
311 } elsif (exists $self->{h}{"x-forwarded-for"}) {
312 $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]";
313 }
314
315 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 328 weaken ($uri{$id}{$self->{uri}}{$self*1} = $self);
316 329
317 eval { 330 eval {
318 $self->map_uri; 331 $self->map_uri;
319 $self->respond; 332 $self->respond;
320 }; 333 };
452 } 465 }
453 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; 466 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
454 } 467 }
455 $hdr->{"Content-Range"} = "bytes */$length"; 468 $hdr->{"Content-Range"} = "bytes */$length";
456 $hdr->{"Content-Length"} = $length; 469 $hdr->{"Content-Length"} = $length;
457 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
458 $self->err(416, "not satisfiable", $hdr, ""); 470 $self->err(416, "not satisfiable", $hdr, "");
459 471
460satisfiable: 472satisfiable:
461 # check for segmented downloads 473 # check for segmented downloads
462 if ($l && $::NO_SEGMENTED) { 474 if ($l && $::NO_SEGMENTED) {
463 my $delay = 180; 475 my $delay = $::PER_TIMEOUT + 15;
464 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 476 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
465 if ($delay <= 0) { 477 if ($delay <= 0) {
466 $self->err_segmented_download; 478 $self->err_segmented_download;
467 } else { 479 } else {
468 Coro::Event::do_timer(after => 3); $delay -= 3; 480 Coro::Event::do_timer(after => 4); $delay -= 4;
469 } 481 }
470 } 482 }
471 } 483 }
472 484
473 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 485 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
486 $self->response(@code, $hdr, ""); 498 $self->response(@code, $hdr, "");
487 499
488 if ($self->{method} eq "GET") { 500 if ($self->{method} eq "GET") {
489 $self->{time} = $::NOW; 501 $self->{time} = $::NOW;
490 502
503 my $fudge = $queue->[0]->waiters;
504 $fudge = $fudge ? ($fudge+1)/$fudge : 1;
505
506 $queue->[1] *= $fudge;
491 my $transfer = $queue->[0]->guard; 507 my $transfer = $queue->[0]->guard;
508
509 if ($fudge != 1) {
510 $queue->[1] /= $fudge;
511 $queue->[1] = $queue->[1] * $::wait_factor
512 + ($::NOW - $self->{time}) * (1 - $::wait_factor);
513 }
514 $self->{time} = $::NOW;
515
492 $self->{fh}->writable or return; 516 $self->{fh}->writable or return;
493
494 $queue->[1] = $queue->[1] * $::wait_factor
495 + ($::NOW - $self->{time}) * (1 - $::wait_factor);
496 $self->{time} = $::NOW;
497 517
498 my ($fh, $buf, $r); 518 my ($fh, $buf, $r);
499 my $current = $Coro::current; 519 my $current = $Coro::current;
500 open $fh, "<", $self->{path} 520 open $fh, "<", $self->{path}
501 or die "$self->{path}: late open failure ($!)"; 521 or die "$self->{path}: late open failure ($!)";
511 while ($h > 0) { 531 while ($h > 0) {
512 if (0) { 532 if (0) {
513 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 533 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
514 or last; 534 or last;
515 } else { 535 } else {
516 undef $buf;
517 $aio_requests->down;
518 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 536 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
519 $buf, 0, sub { 537 $buf, 0, sub {
520 $r = $_[0]; 538 $r = $_[0];
521 $current->ready; 539 Coro::ready($current);
522 }); 540 });
523 &Coro::schedule; 541 &Coro::schedule;
524 $aio_requests->up;
525 last unless $r; 542 last unless $r;
526 } 543 }
527 my $w = $self->{fh}->syswrite($buf) 544 my $w = syswrite $self->{fh}, $buf
528 or last; 545 or last;
529 $::written += $w; 546 $::written += $w;
530 $self->{written} += $w; 547 $self->{written} += $w;
531 $l += $r; 548 $l += $r;
532 } 549 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines