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.34 by root, Thu Aug 30 02:58:17 2001 UTC vs.
Revision 1.39 by root, Sun Sep 2 12:23:04 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;
99use Convert::Scalar 'weaken'; 98use Convert::Scalar 'weaken';
100use Linux::AIO; 99use Linux::AIO;
101 100
102Linux::AIO::min_parallel $::AIO_PARALLEL; 101Linux::AIO::min_parallel $::AIO_PARALLEL;
103 102
104my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4;
105
106Event->io(fd => Linux::AIO::poll_fileno, 103Event->io(fd => Linux::AIO::poll_fileno,
107 poll => 'r', async => 1, 104 poll => 'r', async => 1,
108 cb => \&Linux::AIO::poll_cb); 105 cb => \&Linux::AIO::poll_cb);
109 106
110our %conn; # $conn{ip}{self} => connobj 107our %conn; # $conn{ip}{self} => connobj
136 or $self->err(500, "unable to decode peername"); 133 or $self->err(500, "unable to decode peername");
137 134
138 $self->{remote_addr} = inet_ntoa $iaddr; 135 $self->{remote_addr} = inet_ntoa $iaddr;
139 $self->{time} = $::NOW; 136 $self->{time} = $::NOW;
140 137
141 # enter ourselves into various lists
142 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
143
144 $::conns++; 138 $::conns++;
145 139
146 $self; 140 $self;
147} 141}
148 142
149sub DESTROY { 143sub DESTROY {
150 my $self = shift; 144 my $self = shift;
151
152 $::conns--; 145 $::conns--;
153
154 $self->eoconn; 146 $self->eoconn;
155 delete $conn{$self->{remote_addr}}{$self*1};
156} 147}
157 148
158# end of connection 149# end of connection
159sub eoconn { 150sub eoconn {
160 my $self = shift; 151 my $self = shift;
152
153 # clean up hints
154 delete $conn{$self->{remote_id}}{$self*1};
161 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 155 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1};
162} 156}
163 157
164sub slog { 158sub slog {
165 my $self = shift; 159 my $self = shift;
166 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); 160 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]");
193sub err { 187sub err {
194 my $self = shift; 188 my $self = shift;
195 my ($code, $msg, $hdr, $content) = @_; 189 my ($code, $msg, $hdr, $content) = @_;
196 190
197 unless (defined $content) { 191 unless (defined $content) {
198 $content = "$code $msg"; 192 $content = "$code $msg\n";
199 $hdr->{"Content-Type"} = "text/plain"; 193 $hdr->{"Content-Type"} = "text/plain";
200 $hdr->{"Content-Length"} = length $content; 194 $hdr->{"Content-Length"} = length $content;
201 } 195 }
202 $hdr->{"Connection"} = "close"; 196 $hdr->{"Connection"} = "close";
203 197
228 } 222 }
229 223
230 $self->{h} = {}; 224 $self->{h} = {};
231 225
232 $fh->timeout($::RES_TIMEOUT); 226 $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 227
255 $req =~ /^(?:\015\012)? 228 $req =~ /^(?:\015\012)?
256 (GET|HEAD) \040+ 229 (GET|HEAD) \040+
257 ([^\040]+) \040+ 230 ([^\040]+) \040+
258 HTTP\/([0-9]+\.[0-9]+) 231 HTTP\/([0-9]+\.[0-9]+)
283 256
284 $self->{h}{$h} = substr $v, 1 257 $self->{h}{$h} = substr $v, 1
285 while ($h, $v) = each %hdr; 258 while ($h, $v) = each %hdr;
286 } 259 }
287 260
261 # remote id should be unique per user
262 my $id = $self->{remote_addr};
263
264 if (exists $self->{h}{"client-ip"}) {
265 $id .= "[".$self->{h}{"client-ip"}."]";
266 } elsif (exists $self->{h}{"x-forwarded-for"}) {
267 $id .= "[".$self->{h}{"x-forwarded-for"}."]";
268 }
269
270 $self->{remote_id} = $id;
271
272 if ($blocked{$id}) {
273 $self->err_blocked($blocked{$id})
274 if $blocked{$id} > $::NOW;
275
276 delete $blocked{$id};
277 }
278
279 if (%{$conn{$id}} >= $::MAX_CONN_IP) {
280 my $delay = $::PER_TIMEOUT + 15;
281 while (%{$conn{$id}} >= $::MAX_CONN_IP) {
282 if ($delay <= 0) {
283 $self->slog(2, "blocked ip $id");
284 $self->err_blocked;
285 } else {
286 Coro::Event::do_timer(after => 4); $delay -= 4;
287 }
288 }
289 }
290
288 # find out server name and port 291 # find out server name and port
289 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { 292 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) {
290 $host = $1; 293 $host = $1;
291 } else { 294 } else {
292 $host = $self->{h}{host}; 295 $host = $self->{h}{host};
301 $host = inet_ntoa $host; 304 $host = inet_ntoa $host;
302 } 305 }
303 306
304 $self->{server_name} = $host; 307 $self->{server_name} = $host;
305 308
306 # remote id should be unique per user 309 # enter ourselves into various lists
307 $self->{remote_id} = $self->{remote_addr}; 310 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); 311 weaken ($uri{$id}{$self->{uri}}{$self*1} = $self);
316 312
317 eval { 313 eval {
318 $self->map_uri; 314 $self->map_uri;
319 $self->respond; 315 $self->respond;
320 }; 316 };
452 } 448 }
453 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; 449 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
454 } 450 }
455 $hdr->{"Content-Range"} = "bytes */$length"; 451 $hdr->{"Content-Range"} = "bytes */$length";
456 $hdr->{"Content-Length"} = $length; 452 $hdr->{"Content-Length"} = $length;
457 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
458 $self->err(416, "not satisfiable", $hdr, ""); 453 $self->err(416, "not satisfiable", $hdr, "");
459 454
460satisfiable: 455satisfiable:
461 # check for segmented downloads 456 # check for segmented downloads
462 if ($l && $::NO_SEGMENTED) { 457 if ($l && $::NO_SEGMENTED) {
463 my $delay = 180; 458 my $delay = $::PER_TIMEOUT + 15;
464 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 459 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
465 if ($delay <= 0) { 460 if ($delay <= 0) {
466 $self->err_segmented_download; 461 $self->err_segmented_download;
467 } else { 462 } else {
468 Coro::Event::do_timer(after => 3); $delay -= 3; 463 Coro::Event::do_timer(after => 4); $delay -= 4;
469 } 464 }
470 } 465 }
471 } 466 }
472 467
473 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 468 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
486 $self->response(@code, $hdr, ""); 481 $self->response(@code, $hdr, "");
487 482
488 if ($self->{method} eq "GET") { 483 if ($self->{method} eq "GET") {
489 $self->{time} = $::NOW; 484 $self->{time} = $::NOW;
490 485
486 my $fudge = $queue->[0]->waiters;
487 $fudge = $fudge ? ($fudge+1)/$fudge : 1;
488
489 $queue->[1] *= $fudge;
491 my $transfer = $queue->[0]->guard; 490 my $transfer = $queue->[0]->guard;
491
492 if ($fudge != 1) {
493 $queue->[1] /= $fudge;
494 $queue->[1] = $queue->[1] * $::wait_factor
495 + ($::NOW - $self->{time}) * (1 - $::wait_factor);
496 }
497 $self->{time} = $::NOW;
498
492 $self->{fh}->writable or return; 499 $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 500
498 my ($fh, $buf, $r); 501 my ($fh, $buf, $r);
499 my $current = $Coro::current; 502 my $current = $Coro::current;
500 open $fh, "<", $self->{path} 503 open $fh, "<", $self->{path}
501 or die "$self->{path}: late open failure ($!)"; 504 or die "$self->{path}: late open failure ($!)";
511 while ($h > 0) { 514 while ($h > 0) {
512 if (0) { 515 if (0) {
513 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 516 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
514 or last; 517 or last;
515 } else { 518 } else {
516 undef $buf;
517 $aio_requests->down;
518 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 519 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
519 $buf, 0, sub { 520 $buf, 0, sub {
520 $r = $_[0]; 521 $r = $_[0];
521 $current->ready; 522 Coro::ready($current);
522 }); 523 });
523 &Coro::schedule; 524 &Coro::schedule;
524 $aio_requests->up;
525 last unless $r; 525 last unless $r;
526 } 526 }
527 my $w = $self->{fh}->syswrite($buf) 527 my $w = syswrite $self->{fh}, $buf
528 or last; 528 or last;
529 $::written += $w; 529 $::written += $w;
530 $self->{written} += $w; 530 $self->{written} += $w;
531 $l += $r; 531 $l += $r;
532 } 532 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines