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.51 by root, Thu Nov 29 01:50:41 2001 UTC vs.
Revision 1.61 by root, Thu Jan 3 01:20:17 2002 UTC

15# and quite a bit slower than thttpd :( 15# and quite a bit slower than thttpd :(
16 16
17$SIG{PIPE} = 'IGNORE'; 17$SIG{PIPE} = 'IGNORE';
18 18
19our $accesslog; 19our $accesslog;
20our $errorlog;
21
22our $NOW;
23our $HTTP_NOW;
24
25Event->timer(interval => 1, hard => 1, cb => sub {
26 $NOW = time;
27 $HTTP_NOW = time2str $NOW;
28})->now;
29
30if ($ERROR_LOG) {
31 use IO::Handle;
32 open $errorlog, ">>$ERROR_LOG"
33 or die "$ERROR_LOG: $!";
34 $errorlog->autoflush(1);
35}
20 36
21if ($ACCESS_LOG) { 37if ($ACCESS_LOG) {
22 use IO::Handle; 38 use IO::Handle;
23 open $accesslog, ">>$ACCESS_LOG" 39 open $accesslog, ">>$ACCESS_LOG"
24 or die "$ACCESS_LOG: $!"; 40 or die "$ACCESS_LOG: $!";
26} 42}
27 43
28sub slog { 44sub slog {
29 my $level = shift; 45 my $level = shift;
30 my $format = shift; 46 my $format = shift;
47 my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW);
31 printf "---: $format\n", @_; 48 printf "$NOW: $format\n", @_;
49 printf $errorlog "$NOW: $format\n", @_ if $errorlog;
32} 50}
33 51
34our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 52our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
35our $httpevent = new Coro::Signal; 53our $httpevent = new Coro::Signal;
36 54
37our $wait_factor = 0.95; 55our $queue_file = new transferqueue slots => $MAX_TRANSFERS, maxsize => 256*1024*1024;
38 56our $queue_index = new transferqueue slots => 10;
39our @transfers = (
40 (new transferqueue $MAX_TRANSFERS_SMALL),
41 (new transferqueue $MAX_TRANSFERS_LARGE),
42);
43 57
44my @newcons; 58my @newcons;
45my @pool; 59my @pool;
46 60
47# one "execution thread" 61# one "execution thread"
50 if (@newcons) { 64 if (@newcons) {
51 eval { 65 eval {
52 conn->new(@{pop @newcons})->handle; 66 conn->new(@{pop @newcons})->handle;
53 }; 67 };
54 slog 1, "$@" if $@ && !ref $@; 68 slog 1, "$@" if $@ && !ref $@;
69
70 $httpevent->broadcast; # only for testing, but doesn't matter much
71
55 $connections->up; 72 $connections->up;
56 } else { 73 } else {
57 last if @pool >= $MAX_POOL; 74 last if @pool >= $MAX_POOL;
58 push @pool, $Coro::current; 75 push @pool, $Coro::current;
59 schedule; 76 schedule;
101 or die "unable to start server"; 118 or die "unable to start server";
102 119
103 listen_on $http_port; 120 listen_on $http_port;
104} 121}
105 122
106our $NOW;
107our $HTTP_NOW;
108
109Event->timer(interval => 1, hard => 1, cb => sub {
110 $NOW = time;
111 $HTTP_NOW = time2str $NOW;
112})->now;
113
114package conn; 123package conn;
115 124
116use Socket; 125use Socket;
117use HTTP::Date; 126use HTTP::Date;
118use Convert::Scalar 'weaken'; 127use Convert::Scalar 'weaken';
152 my (undef, $iaddr) = unpack_sockaddr_in $peername 161 my (undef, $iaddr) = unpack_sockaddr_in $peername
153 or $self->err(500, "unable to decode peername"); 162 or $self->err(500, "unable to decode peername");
154 163
155 $self->{remote_addr} = 164 $self->{remote_addr} =
156 $self->{remote_id} = inet_ntoa $iaddr; 165 $self->{remote_id} = inet_ntoa $iaddr;
166
157 $self->{time} = $::NOW; 167 $self->{time} = $::NOW;
158 168
159 weaken ($Coro::current->{conn} = $self); 169 weaken ($Coro::current->{conn} = $self);
160 170
161 $::conns++; 171 $::conns++;
172 $::maxconns = $::conns if $::conns > $::maxconns;
162 173
163 $self; 174 $self;
164} 175}
165 176
166sub DESTROY { 177sub DESTROY {
167 my $self = shift; 178 #my $self = shift;
168 $::conns--; 179 $::conns--;
169 $self->eoconn;
170}
171
172# end of connection
173sub eoconn {
174 my $self = shift;
175
176 # clean up hints
177 delete $conn{$self->{remote_id}}{$self*1};
178 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1};
179
180 $httpevent->broadcast;
181} 180}
182 181
183sub slog { 182sub slog {
184 my $self = shift; 183 my $self = shift;
185 main::slog($_[0], "$self->{remote_id}> $_[1]"); 184 main::slog($_[0], "$self->{remote_id}> $_[1]");
306 $id .= "[".$self->{h}{"x-forwarded-for"}."]"; 305 $id .= "[".$self->{h}{"x-forwarded-for"}."]";
307 } 306 }
308 307
309 $self->{remote_id} = $id; 308 $self->{remote_id} = $id;
310 309
310 weaken (local $conn{$id}{$self*1} = $self);
311
311 if ($blocked{$id}) { 312 if ($blocked{$id}) {
312 $self->err_blocked($blocked{$id}) 313 $self->err_blocked
313 if $blocked{$id} > $::NOW; 314 if $blocked{$id}[0] > $::NOW;
314 315
315 delete $blocked{$id}; 316 delete $blocked{$id};
316 }
317
318 if (%{$conn{$id}} >= $::MAX_CONN_IP) {
319 my $delay = $::PER_TIMEOUT + $::NOW + 15;
320 while (%{$conn{$id}} >= $::MAX_CONN_IP) {
321 if ($delay < $::NOW) {
322 $self->slog(2, "blocked ip $id");
323 $self->err_blocked;
324 } else {
325 $httpevent->wait;
326 }
327 }
328 } 317 }
329 318
330 # find out server name and port 319 # find out server name and port
331 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { 320 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) {
332 $host = $1; 321 $host = $1;
343 $host = inet_ntoa $host; 332 $host = inet_ntoa $host;
344 } 333 }
345 334
346 $self->{server_name} = $host; 335 $self->{server_name} = $host;
347 336
348 # enter ourselves into various lists
349 weaken ($conn{$id}{$self*1} = $self);
350 weaken ($uri{$id}{$self->{uri}}{$self*1} = $self); 337 weaken (local $uri{$id}{$self->{uri}}{$self*1} = $self);
351 338
352 eval { 339 eval {
353 $self->map_uri; 340 $self->map_uri;
354 $self->respond; 341 $self->respond;
355 }; 342 };
356 343
357 $self->eoconn;
358
359 die if $@ && !ref $@; 344 die if $@ && !ref $@;
360 345
361 last if $self->{h}{connection} =~ /close/; 346 last if $self->{h}{connection} =~ /close/i;
362 347
363 $httpevent->broadcast; 348 $httpevent->broadcast;
364 349
365 $fh->timeout($::PER_TIMEOUT); 350 $fh->timeout($::PER_TIMEOUT);
366 } 351 }
352}
353
354sub block {
355 my $self = shift;
356
357 $blocked{$self->{remote_id}} = [$::NOW + $_[0], $_[1]];
358 $self->slog(2, "blocked ip $self->{remote_id}");
359 $self->err_blocked;
367} 360}
368 361
369# uri => path mapping 362# uri => path mapping
370sub map_uri { 363sub map_uri {
371 my $self = shift; 364 my $self = shift;
447 $ims < $self->{stat}[9] 440 $ims < $self->{stat}[9]
448 or $self->err(304, "not modified"); 441 or $self->err(304, "not modified");
449 442
450 if (-r "$path/index.html") { 443 if (-r "$path/index.html") {
451 # replace directory "size" by index.html filesize 444 # replace directory "size" by index.html filesize
452 $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; 445 $self->{stat} = [stat ($self->{path} .= "/index.html")];
453 $self->handle_file; 446 $self->handle_file($queue_index);
454 } else { 447 } else {
455 $self->handle_dir; 448 $self->handle_dir;
456 } 449 }
457 } 450 }
458 } elsif (-f _ && -r _) { 451 } elsif (-f _ && -r _) {
459 -x _ and $self->err(403, "forbidden"); 452 -x _ and $self->err(403, "forbidden");
453
454 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
455 my $timeout = $::NOW + 10;
456 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
457 if ($timeout < $::NOW) {
458 $self->block($::BLOCKTIME, "too many connections");
459 } else {
460 $httpevent->wait;
461 }
462 }
463 }
464
460 $self->handle_file; 465 $self->handle_file($queue_file);
461 } else { 466 } else {
462 $self->err(404, "not found"); 467 $self->err(404, "not found");
463 } 468 }
464 } 469 }
465} 470}
470 475
471 $self->response(200, "ok", 476 $self->response(200, "ok",
472 { 477 {
473 "Content-Type" => "text/html", 478 "Content-Type" => "text/html",
474 "Content-Length" => length $idx, 479 "Content-Length" => length $idx,
475 #d# directories change all the time, so X-
476 "X-Last-Modified" => time2str ((stat _)[9]), 480 "Last-Modified" => time2str ($self->{stat}[9]),
477 }, 481 },
478 $idx); 482 $idx);
479} 483}
480 484
481sub handle_file { 485sub handle_file {
482 my $self = shift; 486 my ($self, $queue) = @_;
483 my $length = $self->{stat}[7]; 487 my $length = $self->{stat}[7];
484 my $queue = $::transfers[$length >= $::TRANSFER_SMALL];
485 my $hdr = { 488 my $hdr = {
486 "Last-Modified" => time2str ((stat _)[9]), 489 "Last-Modified" => time2str ((stat _)[9]),
487 }; 490 };
488 491
489 my @code = (200, "ok"); 492 my @code = (200, "ok");
506 $self->err(416, "not satisfiable", $hdr, ""); 509 $self->err(416, "not satisfiable", $hdr, "");
507 510
508satisfiable: 511satisfiable:
509 # check for segmented downloads 512 # check for segmented downloads
510 if ($l && $::NO_SEGMENTED) { 513 if ($l && $::NO_SEGMENTED) {
511 my $delay = $::NOW + $::PER_TIMEOUT + 15; 514 my $timeout = $::NOW + 15;
512 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 515 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
513 if ($delay <= $::NOW) { 516 if ($timeout <= $::NOW) {
517 $self->block($::BLOCKTIME, "segmented downloads are forbidden");
514 $self->err_segmented_download; 518 #$self->err_segmented_download;
515 } else { 519 } else {
516 $httpevent->wait; 520 $httpevent->wait;
517 } 521 }
518 } 522 }
519 } 523 }
549 if ($l) { 553 if ($l) {
550 sysseek $fh, $l, 0; 554 sysseek $fh, $l, 0;
551 } 555 }
552 } 556 }
553 557
554 my $transfer = $queue->start_transfer; 558 my $transfer = $queue->start_transfer($h);
555 my $locked; 559 my $locked;
556 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size 560 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
557 561
558 while ($h > 0) { 562 while ($h > 0) {
559 unless ($locked) { 563 unless ($locked) {
560 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 564 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) {
561 $bufsize = $::BUFSIZE; 565 $bufsize = $::BUFSIZE;
562 $self->{time} = $::NOW; 566 $self->{time} = $::NOW;
563 } 567 }
568 }
569
570 if ($blocked{$self->{remote_id}}) {
571 $self->{h}{connection} = "close";
572 die bless {}, err::;
564 } 573 }
565 574
566 if (0) { # !AIO 575 if (0) { # !AIO
567 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h 576 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
568 or last; 577 or last;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines