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.32 by root, Wed Aug 29 01:32:50 2001 UTC vs.
Revision 1.36 by root, Sun Sep 2 00:54:00 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"
68our $HTTP_NOW; 72our $HTTP_NOW;
69 73
70Event->timer(interval => 1, hard => 1, cb => sub { 74Event->timer(interval => 1, hard => 1, cb => sub {
71 $NOW = time; 75 $NOW = time;
72 $HTTP_NOW = time2str $NOW; 76 $HTTP_NOW = time2str $NOW;
73}); 77})->now;
74 78
75# the "main thread" 79# the "main thread"
76async { 80async {
77 slog 1, "accepting connections"; 81 slog 1, "accepting connections";
78 while () { 82 while () {
132 or $self->err(500, "unable to decode peername"); 136 or $self->err(500, "unable to decode peername");
133 137
134 $self->{remote_addr} = inet_ntoa $iaddr; 138 $self->{remote_addr} = inet_ntoa $iaddr;
135 $self->{time} = $::NOW; 139 $self->{time} = $::NOW;
136 140
137 # enter ourselves into various lists
138 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
139
140 $::conns++; 141 $::conns++;
141 142
142 $self; 143 $self;
143} 144}
144 145
146 my $self = shift; 147 my $self = shift;
147 148
148 $::conns--; 149 $::conns--;
149 150
150 $self->eoconn; 151 $self->eoconn;
151 delete $conn{$self->{remote_addr}}{$self*1};
152} 152}
153 153
154# end of connection 154# end of connection
155sub eoconn { 155sub eoconn {
156 my $self = shift; 156 my $self = shift;
157
158 # clean up hints
159 delete $conn{$self->{remote_id}}{$self*1};
157 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 160 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1};
158} 161}
159 162
160sub slog { 163sub slog {
161 my $self = shift; 164 my $self = shift;
162 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); 165 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]");
189sub err { 192sub err {
190 my $self = shift; 193 my $self = shift;
191 my ($code, $msg, $hdr, $content) = @_; 194 my ($code, $msg, $hdr, $content) = @_;
192 195
193 unless (defined $content) { 196 unless (defined $content) {
194 $content = "$code $msg"; 197 $content = "$code $msg\n";
195 $hdr->{"Content-Type"} = "text/plain"; 198 $hdr->{"Content-Type"} = "text/plain";
196 $hdr->{"Content-Length"} = length $content; 199 $hdr->{"Content-Length"} = length $content;
197 } 200 }
198 $hdr->{"Connection"} = "close"; 201 $hdr->{"Connection"} = "close";
199 202
224 } 227 }
225 228
226 $self->{h} = {}; 229 $self->{h} = {};
227 230
228 $fh->timeout($::RES_TIMEOUT); 231 $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 232
251 $req =~ /^(?:\015\012)? 233 $req =~ /^(?:\015\012)?
252 (GET|HEAD) \040+ 234 (GET|HEAD) \040+
253 ([^\040]+) \040+ 235 ([^\040]+) \040+
254 HTTP\/([0-9]+\.[0-9]+) 236 HTTP\/([0-9]+\.[0-9]+)
279 261
280 $self->{h}{$h} = substr $v, 1 262 $self->{h}{$h} = substr $v, 1
281 while ($h, $v) = each %hdr; 263 while ($h, $v) = each %hdr;
282 } 264 }
283 265
266 # remote id should be unique per user
267 my $id = $self->{remote_addr};
268
269 if (exists $self->{h}{"client-ip"}) {
270 $id .= "[".$self->{h}{"client-ip"}."]";
271 } elsif (exists $self->{h}{"x-forwarded-for"}) {
272 $id .= "[".$self->{h}{"x-forwarded-for"}."]";
273 }
274
275 $self->{remote_id} = $id;
276
277 if ($blocked{$id}) {
278 $self->err_blocked($blocked{$id})
279 if $blocked{$id} > $::NOW;
280
281 delete $blocked{$id};
282 }
283
284 if (%{$conn{$id}} >= $::MAX_CONN_IP) {
285 my $delay = $::PER_TIMEOUT + 15;
286 while (%{$conn{$id}} >= $::MAX_CONN_IP) {
287 if ($delay <= 0) {
288 $self->slog(2, "blocked ip $id");
289 $self->err_blocked;
290 } else {
291 Coro::Event::do_timer(after => 4); $delay -= 4;
292 }
293 }
294 }
295
284 # find out server name and port 296 # find out server name and port
285 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { 297 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) {
286 $host = $1; 298 $host = $1;
287 } else { 299 } else {
288 $host = $self->{h}{host}; 300 $host = $self->{h}{host};
297 $host = inet_ntoa $host; 309 $host = inet_ntoa $host;
298 } 310 }
299 311
300 $self->{server_name} = $host; 312 $self->{server_name} = $host;
301 313
302 # remote id should be unique per user 314 # enter ourselves into various lists
303 $self->{remote_id} = $self->{remote_addr}; 315 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); 316 weaken ($uri{$id}{$self->{uri}}{$self*1} = $self);
312 317
313 eval { 318 eval {
314 $self->map_uri; 319 $self->map_uri;
315 $self->respond; 320 $self->respond;
316 }; 321 };
425 $idx); 430 $idx);
426} 431}
427 432
428sub handle_file { 433sub handle_file {
429 my $self = shift; 434 my $self = shift;
430 my $length = -s _; 435 my $length = $self->{stat}[7];
436 my $queue = $::transfers[$length >= $::TRANSFER_SMALL];
431 my $hdr = { 437 my $hdr = {
432 "Last-Modified" => time2str ((stat _)[9]), 438 "Last-Modified" => time2str ((stat _)[9]),
433 }; 439 };
434 440
435 my @code = (200, "ok"); 441 my @code = (200, "ok");
447 } 453 }
448 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; 454 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
449 } 455 }
450 $hdr->{"Content-Range"} = "bytes */$length"; 456 $hdr->{"Content-Range"} = "bytes */$length";
451 $hdr->{"Content-Length"} = $length; 457 $hdr->{"Content-Length"} = $length;
452 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
453 $self->err(416, "not satisfiable", $hdr, ""); 458 $self->err(416, "not satisfiable", $hdr, "");
454 459
455satisfiable: 460satisfiable:
456 # check for segmented downloads 461 # check for segmented downloads
457 if ($l && $::NO_SEGMENTED) { 462 if ($l && $::NO_SEGMENTED) {
458 my $delay = 180; 463 my $delay = $::PER_TIMEOUT + 15;
459 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 464 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
460 if ($delay <= 0) { 465 if ($delay <= 0) {
461 $self->err_segmented_download; 466 $self->err_segmented_download;
462 } else { 467 } else {
463 Coro::Event::do_timer(after => 3); $delay -= 3; 468 Coro::Event::do_timer(after => 4); $delay -= 4;
464 } 469 }
465 } 470 }
466 } 471 }
467 472
468 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 473 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
481 $self->response(@code, $hdr, ""); 486 $self->response(@code, $hdr, "");
482 487
483 if ($self->{method} eq "GET") { 488 if ($self->{method} eq "GET") {
484 $self->{time} = $::NOW; 489 $self->{time} = $::NOW;
485 490
491 my $fudge = $queue->[0]->waiters;
492 $fudge = $fudge ? ($fudge+1)/$fudge : 1;
493
494 $queue->[1] *= $fudge;
486 my $transfer = $::transfers->guard; 495 my $transfer = $queue->[0]->guard;
496
497 if ($fudge != 1) {
498 $queue->[1] /= $fudge;
499 $queue->[1] = $queue->[1] * $::wait_factor
500 + ($::NOW - $self->{time}) * (1 - $::wait_factor);
501 }
502 $self->{time} = $::NOW;
503
487 $self->{fh}->writable or return; 504 $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 505
493 my ($fh, $buf, $r); 506 my ($fh, $buf, $r);
494 my $current = $Coro::current; 507 my $current = $Coro::current;
495 open $fh, "<", $self->{path} 508 open $fh, "<", $self->{path}
496 or die "$self->{path}: late open failure ($!)"; 509 or die "$self->{path}: late open failure ($!)";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines