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.32 by root, Wed Aug 29 01:32:50 2001 UTC vs.
Revision 1.41 by root, Mon Sep 10 22:16:20 2001 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines