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