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.33 by root, Wed Aug 29 01:40:44 2001 UTC vs.
Revision 1.38 by root, Sun Sep 2 02:27:51 2001 UTC

5 5
6use HTTP::Date; 6use HTTP::Date;
7 7
8no utf8; 8no utf8;
9use bytes; 9use bytes;
10
11our @wait_time = (); # used to calculcate avg. waiting time
12our $wait_time_length = 25;
13 10
14# at least on my machine, this thingy serves files 11# at least on my machine, this thingy serves files
15# quite a bit faster than apache, ;) 12# quite a bit faster than apache, ;)
16# and quite a bit slower than thttpd :( 13# and quite a bit slower than thttpd :(
17 14
31 my $format = shift; 28 my $format = shift;
32 printf "---: $format\n", @_; 29 printf "---: $format\n", @_;
33} 30}
34 31
35our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 32our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
36our $transfers = new Coro::Semaphore $MAX_TRANSFER || 50; 33
34our $wait_factor = 0.95;
35
36our @transfers = (
37 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1],
38 [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1],
39);
37 40
38my @newcons; 41my @newcons;
39my @pool; 42my @pool;
40 43
41# one "execution thread" 44# one "execution thread"
42sub handler { 45sub handler {
43 while () { 46 while () {
44 my $new = pop @newcons;
45 if ($new) { 47 if (@newcons) {
46 eval { 48 eval {
47 conn->new(@$new)->handle; 49 conn->new(@{pop @newcons})->handle;
48 }; 50 };
49 slog 1, "$@" if $@ && !ref $@; 51 slog 1, "$@" if $@ && !ref $@;
50 $connections->up; 52 $connections->up;
51 } else { 53 } else {
52 last if @pool >= $MAX_POOL; 54 last if @pool >= $MAX_POOL;
69our $HTTP_NOW; 71our $HTTP_NOW;
70 72
71Event->timer(interval => 1, hard => 1, cb => sub { 73Event->timer(interval => 1, hard => 1, cb => sub {
72 $NOW = time; 74 $NOW = time;
73 $HTTP_NOW = time2str $NOW; 75 $HTTP_NOW = time2str $NOW;
74}); 76})->now;
75 77
76# the "main thread" 78# the "main thread"
77async { 79async {
78 slog 1, "accepting connections"; 80 slog 1, "accepting connections";
79 while () { 81 while () {
96use Convert::Scalar 'weaken'; 98use Convert::Scalar 'weaken';
97use Linux::AIO; 99use Linux::AIO;
98 100
99Linux::AIO::min_parallel $::AIO_PARALLEL; 101Linux::AIO::min_parallel $::AIO_PARALLEL;
100 102
101my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4;
102
103Event->io(fd => Linux::AIO::poll_fileno, 103Event->io(fd => Linux::AIO::poll_fileno,
104 poll => 'r', async => 1, 104 poll => 'r', async => 1,
105 cb => \&Linux::AIO::poll_cb); 105 cb => \&Linux::AIO::poll_cb);
106 106
107our %conn; # $conn{ip}{self} => connobj 107our %conn; # $conn{ip}{self} => connobj
133 or $self->err(500, "unable to decode peername"); 133 or $self->err(500, "unable to decode peername");
134 134
135 $self->{remote_addr} = inet_ntoa $iaddr; 135 $self->{remote_addr} = inet_ntoa $iaddr;
136 $self->{time} = $::NOW; 136 $self->{time} = $::NOW;
137 137
138 # enter ourselves into various lists
139 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
140
141 $::conns++; 138 $::conns++;
142 139
143 $self; 140 $self;
144} 141}
145 142
147 my $self = shift; 144 my $self = shift;
148 145
149 $::conns--; 146 $::conns--;
150 147
151 $self->eoconn; 148 $self->eoconn;
152 delete $conn{$self->{remote_addr}}{$self*1};
153} 149}
154 150
155# end of connection 151# end of connection
156sub eoconn { 152sub eoconn {
157 my $self = shift; 153 my $self = shift;
154
155 # clean up hints
156 delete $conn{$self->{remote_id}}{$self*1};
158 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 157 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1};
159} 158}
160 159
161sub slog { 160sub slog {
162 my $self = shift; 161 my $self = shift;
163 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); 162 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]");
190sub err { 189sub err {
191 my $self = shift; 190 my $self = shift;
192 my ($code, $msg, $hdr, $content) = @_; 191 my ($code, $msg, $hdr, $content) = @_;
193 192
194 unless (defined $content) { 193 unless (defined $content) {
195 $content = "$code $msg"; 194 $content = "$code $msg\n";
196 $hdr->{"Content-Type"} = "text/plain"; 195 $hdr->{"Content-Type"} = "text/plain";
197 $hdr->{"Content-Length"} = length $content; 196 $hdr->{"Content-Length"} = length $content;
198 } 197 }
199 $hdr->{"Connection"} = "close"; 198 $hdr->{"Connection"} = "close";
200 199
225 } 224 }
226 225
227 $self->{h} = {}; 226 $self->{h} = {};
228 227
229 $fh->timeout($::RES_TIMEOUT); 228 $fh->timeout($::RES_TIMEOUT);
230 my $ip = $self->{remote_addr};
231
232 if ($blocked{$ip}) {
233 $self->err_blocked($blocked{$ip})
234 if $blocked{$ip} > $::NOW;
235
236 delete $blocked{$ip};
237 }
238
239 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
240 my $delay = 120;
241 while (%{$conn{$ip}} > $::MAX_CONN_IP) {
242 if ($delay <= 0) {
243 $self->slog(2, "blocked ip $ip");
244 $self->err_blocked;
245 } else {
246 Coro::Event::do_timer(after => 3);
247 $delay -= 3;
248 }
249 }
250 }
251 229
252 $req =~ /^(?:\015\012)? 230 $req =~ /^(?:\015\012)?
253 (GET|HEAD) \040+ 231 (GET|HEAD) \040+
254 ([^\040]+) \040+ 232 ([^\040]+) \040+
255 HTTP\/([0-9]+\.[0-9]+) 233 HTTP\/([0-9]+\.[0-9]+)
280 258
281 $self->{h}{$h} = substr $v, 1 259 $self->{h}{$h} = substr $v, 1
282 while ($h, $v) = each %hdr; 260 while ($h, $v) = each %hdr;
283 } 261 }
284 262
263 # remote id should be unique per user
264 my $id = $self->{remote_addr};
265
266 if (exists $self->{h}{"client-ip"}) {
267 $id .= "[".$self->{h}{"client-ip"}."]";
268 } elsif (exists $self->{h}{"x-forwarded-for"}) {
269 $id .= "[".$self->{h}{"x-forwarded-for"}."]";
270 }
271
272 $self->{remote_id} = $id;
273
274 if ($blocked{$id}) {
275 $self->err_blocked($blocked{$id})
276 if $blocked{$id} > $::NOW;
277
278 delete $blocked{$id};
279 }
280
281 if (%{$conn{$id}} >= $::MAX_CONN_IP) {
282 my $delay = $::PER_TIMEOUT + 15;
283 while (%{$conn{$id}} >= $::MAX_CONN_IP) {
284 if ($delay <= 0) {
285 $self->slog(2, "blocked ip $id");
286 $self->err_blocked;
287 } else {
288 Coro::Event::do_timer(after => 4); $delay -= 4;
289 }
290 }
291 }
292
285 # find out server name and port 293 # find out server name and port
286 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { 294 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) {
287 $host = $1; 295 $host = $1;
288 } else { 296 } else {
289 $host = $self->{h}{host}; 297 $host = $self->{h}{host};
298 $host = inet_ntoa $host; 306 $host = inet_ntoa $host;
299 } 307 }
300 308
301 $self->{server_name} = $host; 309 $self->{server_name} = $host;
302 310
303 # remote id should be unique per user 311 # enter ourselves into various lists
304 $self->{remote_id} = $self->{remote_addr}; 312 weaken ($conn{$id}{$self*1} = $self);
305
306 if (exists $self->{h}{"client-ip"}) {
307 $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]";
308 } elsif (exists $self->{h}{"x-forwarded-for"}) {
309 $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]";
310 }
311
312 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 313 weaken ($uri{$id}{$self->{uri}}{$self*1} = $self);
313 314
314 eval { 315 eval {
315 $self->map_uri; 316 $self->map_uri;
316 $self->respond; 317 $self->respond;
317 }; 318 };
426 $idx); 427 $idx);
427} 428}
428 429
429sub handle_file { 430sub handle_file {
430 my $self = shift; 431 my $self = shift;
431 my $length = -s _; 432 my $length = $self->{stat}[7];
433 my $queue = $::transfers[$length >= $::TRANSFER_SMALL];
432 my $hdr = { 434 my $hdr = {
433 "Last-Modified" => time2str ((stat _)[9]), 435 "Last-Modified" => time2str ((stat _)[9]),
434 }; 436 };
435 437
436 my @code = (200, "ok"); 438 my @code = (200, "ok");
448 } 450 }
449 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; 451 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
450 } 452 }
451 $hdr->{"Content-Range"} = "bytes */$length"; 453 $hdr->{"Content-Range"} = "bytes */$length";
452 $hdr->{"Content-Length"} = $length; 454 $hdr->{"Content-Length"} = $length;
453 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
454 $self->err(416, "not satisfiable", $hdr, ""); 455 $self->err(416, "not satisfiable", $hdr, "");
455 456
456satisfiable: 457satisfiable:
457 # check for segmented downloads 458 # check for segmented downloads
458 if ($l && $::NO_SEGMENTED) { 459 if ($l && $::NO_SEGMENTED) {
459 my $delay = 180; 460 my $delay = $::PER_TIMEOUT + 15;
460 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 461 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
461 if ($delay <= 0) { 462 if ($delay <= 0) {
462 $self->err_segmented_download; 463 $self->err_segmented_download;
463 } else { 464 } else {
464 Coro::Event::do_timer(after => 3); $delay -= 3; 465 Coro::Event::do_timer(after => 4); $delay -= 4;
465 } 466 }
466 } 467 }
467 } 468 }
468 469
469 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 470 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
482 $self->response(@code, $hdr, ""); 483 $self->response(@code, $hdr, "");
483 484
484 if ($self->{method} eq "GET") { 485 if ($self->{method} eq "GET") {
485 $self->{time} = $::NOW; 486 $self->{time} = $::NOW;
486 487
488 my $fudge = $queue->[0]->waiters;
489 $fudge = $fudge ? ($fudge+1)/$fudge : 1;
490
491 $queue->[1] *= $fudge;
487 my $transfer = $::transfers->guard; 492 my $transfer = $queue->[0]->guard;
493
494 if ($fudge != 1) {
495 $queue->[1] /= $fudge;
496 $queue->[1] = $queue->[1] * $::wait_factor
497 + ($::NOW - $self->{time}) * (1 - $::wait_factor);
498 }
499 $self->{time} = $::NOW;
500
488 $self->{fh}->writable or return; 501 $self->{fh}->writable or return;
489
490 push @::wait_time, $::NOW - $self->{time};
491 shift @::wait_time if @wait_time > $wait_time_length;
492 $self->{time} = $::NOW;
493 502
494 my ($fh, $buf, $r); 503 my ($fh, $buf, $r);
495 my $current = $Coro::current; 504 my $current = $Coro::current;
496 open $fh, "<", $self->{path} 505 open $fh, "<", $self->{path}
497 or die "$self->{path}: late open failure ($!)"; 506 or die "$self->{path}: late open failure ($!)";
507 while ($h > 0) { 516 while ($h > 0) {
508 if (0) { 517 if (0) {
509 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 518 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
510 or last; 519 or last;
511 } else { 520 } else {
512 undef $buf;
513 $aio_requests->down;
514 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 521 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
515 $buf, 0, sub { 522 $buf, 0, sub {
516 $r = $_[0]; 523 $r = $_[0];
517 $current->ready; 524 Coro::ready($current);
518 }); 525 });
519 &Coro::schedule; 526 &Coro::schedule;
520 $aio_requests->up;
521 last unless $r; 527 last unless $r;
522 } 528 }
523 my $w = $self->{fh}->syswrite($buf) 529 my $w = syswrite $self->{fh}, $buf
524 or last; 530 or last;
525 $::written += $w; 531 $::written += $w;
526 $self->{written} += $w; 532 $self->{written} += $w;
527 $l += $r; 533 $l += $r;
528 } 534 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines