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.62 by root, Sun Jan 6 05:29:39 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 => 250_000_000;
56our $queue_index = new transferqueue slots => 10;
38 57
39our @transfers = ( 58our $requests;
40 (new transferqueue $MAX_TRANSFERS_SMALL),
41 (new transferqueue $MAX_TRANSFERS_LARGE),
42);
43 59
44my @newcons; 60my @newcons;
45my @pool; 61my @pool;
46 62
47# one "execution thread" 63# one "execution thread"
50 if (@newcons) { 66 if (@newcons) {
51 eval { 67 eval {
52 conn->new(@{pop @newcons})->handle; 68 conn->new(@{pop @newcons})->handle;
53 }; 69 };
54 slog 1, "$@" if $@ && !ref $@; 70 slog 1, "$@" if $@ && !ref $@;
71
72 $httpevent->broadcast; # only for testing, but doesn't matter much
73
55 $connections->up; 74 $connections->up;
56 } else { 75 } else {
57 last if @pool >= $MAX_POOL; 76 last if @pool >= $MAX_POOL;
58 push @pool, $Coro::current; 77 push @pool, $Coro::current;
59 schedule; 78 schedule;
101 or die "unable to start server"; 120 or die "unable to start server";
102 121
103 listen_on $http_port; 122 listen_on $http_port;
104} 123}
105 124
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; 125package conn;
115 126
116use Socket; 127use Socket;
117use HTTP::Date; 128use HTTP::Date;
118use Convert::Scalar 'weaken'; 129use Convert::Scalar 'weaken';
152 my (undef, $iaddr) = unpack_sockaddr_in $peername 163 my (undef, $iaddr) = unpack_sockaddr_in $peername
153 or $self->err(500, "unable to decode peername"); 164 or $self->err(500, "unable to decode peername");
154 165
155 $self->{remote_addr} = 166 $self->{remote_addr} =
156 $self->{remote_id} = inet_ntoa $iaddr; 167 $self->{remote_id} = inet_ntoa $iaddr;
168
157 $self->{time} = $::NOW; 169 $self->{time} = $::NOW;
158 170
159 weaken ($Coro::current->{conn} = $self); 171 weaken ($Coro::current->{conn} = $self);
160 172
161 $::conns++; 173 $::conns++;
174 $::maxconns = $::conns if $::conns > $::maxconns;
162 175
163 $self; 176 $self;
164} 177}
165 178
166sub DESTROY { 179sub DESTROY {
167 my $self = shift; 180 #my $self = shift;
168 $::conns--; 181 $::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} 182}
182 183
183sub slog { 184sub slog {
184 my $self = shift; 185 my $self = shift;
185 main::slog($_[0], "$self->{remote_id}> $_[1]"); 186 main::slog($_[0], "$self->{remote_id}> $_[1]");
295 296
296 $self->{h}{$h} = substr $v, 1 297 $self->{h}{$h} = substr $v, 1
297 while ($h, $v) = each %hdr; 298 while ($h, $v) = each %hdr;
298 } 299 }
299 300
301 $requests++;
302
300 # remote id should be unique per user 303 # remote id should be unique per user
301 my $id = $self->{remote_addr}; 304 my $id = $self->{remote_addr};
302 305
303 if (exists $self->{h}{"client-ip"}) { 306 if (exists $self->{h}{"client-ip"}) {
304 $id .= "[".$self->{h}{"client-ip"}."]"; 307 $id .= "[".$self->{h}{"client-ip"}."]";
306 $id .= "[".$self->{h}{"x-forwarded-for"}."]"; 309 $id .= "[".$self->{h}{"x-forwarded-for"}."]";
307 } 310 }
308 311
309 $self->{remote_id} = $id; 312 $self->{remote_id} = $id;
310 313
314 weaken (local $conn{$id}{$self*1} = $self);
315
311 if ($blocked{$id}) { 316 if ($blocked{$id}) {
312 $self->err_blocked($blocked{$id}) 317 $self->err_blocked
313 if $blocked{$id} > $::NOW; 318 if $blocked{$id}[0] > $::NOW;
314 319
315 delete $blocked{$id}; 320 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 } 321 }
329 322
330 # find out server name and port 323 # find out server name and port
331 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { 324 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) {
332 $host = $1; 325 $host = $1;
343 $host = inet_ntoa $host; 336 $host = inet_ntoa $host;
344 } 337 }
345 338
346 $self->{server_name} = $host; 339 $self->{server_name} = $host;
347 340
348 # enter ourselves into various lists
349 weaken ($conn{$id}{$self*1} = $self);
350 weaken ($uri{$id}{$self->{uri}}{$self*1} = $self); 341 weaken (local $uri{$id}{$self->{uri}}{$self*1} = $self);
351 342
352 eval { 343 eval {
353 $self->map_uri; 344 $self->map_uri;
354 $self->respond; 345 $self->respond;
355 }; 346 };
356 347
357 $self->eoconn;
358
359 die if $@ && !ref $@; 348 die if $@ && !ref $@;
360 349
361 last if $self->{h}{connection} =~ /close/; 350 last if $self->{h}{connection} =~ /close/i;
362 351
363 $httpevent->broadcast; 352 $httpevent->broadcast;
364 353
365 $fh->timeout($::PER_TIMEOUT); 354 $fh->timeout($::PER_TIMEOUT);
366 } 355 }
356}
357
358sub block {
359 my $self = shift;
360
361 $blocked{$self->{remote_id}} = [$::NOW + $_[0], $_[1]];
362 $self->slog(2, "blocked ip $self->{remote_id}");
363 $self->err_blocked;
367} 364}
368 365
369# uri => path mapping 366# uri => path mapping
370sub map_uri { 367sub map_uri {
371 my $self = shift; 368 my $self = shift;
447 $ims < $self->{stat}[9] 444 $ims < $self->{stat}[9]
448 or $self->err(304, "not modified"); 445 or $self->err(304, "not modified");
449 446
450 if (-r "$path/index.html") { 447 if (-r "$path/index.html") {
451 # replace directory "size" by index.html filesize 448 # replace directory "size" by index.html filesize
452 $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; 449 $self->{stat} = [stat ($self->{path} .= "/index.html")];
453 $self->handle_file; 450 $self->handle_file($queue_index);
454 } else { 451 } else {
455 $self->handle_dir; 452 $self->handle_dir;
456 } 453 }
457 } 454 }
458 } elsif (-f _ && -r _) { 455 } elsif (-f _ && -r _) {
459 -x _ and $self->err(403, "forbidden"); 456 -x _ and $self->err(403, "forbidden");
457
458 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
459 my $timeout = $::NOW + 10;
460 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
461 if ($timeout < $::NOW) {
462 $self->block($::BLOCKTIME, "too many connections");
463 } else {
464 $httpevent->wait;
465 }
466 }
467 }
468
460 $self->handle_file; 469 $self->handle_file($queue_file);
461 } else { 470 } else {
462 $self->err(404, "not found"); 471 $self->err(404, "not found");
463 } 472 }
464 } 473 }
465} 474}
470 479
471 $self->response(200, "ok", 480 $self->response(200, "ok",
472 { 481 {
473 "Content-Type" => "text/html", 482 "Content-Type" => "text/html",
474 "Content-Length" => length $idx, 483 "Content-Length" => length $idx,
475 #d# directories change all the time, so X-
476 "X-Last-Modified" => time2str ((stat _)[9]), 484 "Last-Modified" => time2str ($self->{stat}[9]),
477 }, 485 },
478 $idx); 486 $idx);
479} 487}
480 488
481sub handle_file { 489sub handle_file {
482 my $self = shift; 490 my ($self, $queue) = @_;
483 my $length = $self->{stat}[7]; 491 my $length = $self->{stat}[7];
484 my $queue = $::transfers[$length >= $::TRANSFER_SMALL];
485 my $hdr = { 492 my $hdr = {
486 "Last-Modified" => time2str ((stat _)[9]), 493 "Last-Modified" => time2str ((stat _)[9]),
487 }; 494 };
488 495
489 my @code = (200, "ok"); 496 my @code = (200, "ok");
506 $self->err(416, "not satisfiable", $hdr, ""); 513 $self->err(416, "not satisfiable", $hdr, "");
507 514
508satisfiable: 515satisfiable:
509 # check for segmented downloads 516 # check for segmented downloads
510 if ($l && $::NO_SEGMENTED) { 517 if ($l && $::NO_SEGMENTED) {
511 my $delay = $::NOW + $::PER_TIMEOUT + 15; 518 my $timeout = $::NOW + 15;
512 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 519 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
513 if ($delay <= $::NOW) { 520 if ($timeout <= $::NOW) {
521 $self->block($::BLOCKTIME, "segmented downloads are forbidden");
514 $self->err_segmented_download; 522 #$self->err_segmented_download;
515 } else { 523 } else {
516 $httpevent->wait; 524 $httpevent->wait;
517 } 525 }
518 } 526 }
519 } 527 }
549 if ($l) { 557 if ($l) {
550 sysseek $fh, $l, 0; 558 sysseek $fh, $l, 0;
551 } 559 }
552 } 560 }
553 561
554 my $transfer = $queue->start_transfer; 562 my $transfer = $queue->start_transfer($h);
555 my $locked; 563 my $locked;
556 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size 564 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
557 565
558 while ($h > 0) { 566 while ($h > 0) {
559 unless ($locked) { 567 unless ($locked) {
560 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { 568 if ($locked ||= $transfer->try($::WAIT_INTERVAL)) {
561 $bufsize = $::BUFSIZE; 569 $bufsize = $::BUFSIZE;
562 $self->{time} = $::NOW; 570 $self->{time} = $::NOW;
563 } 571 }
572 }
573
574 if ($blocked{$self->{remote_id}}) {
575 $self->{h}{connection} = "close";
576 die bless {}, err::;
564 } 577 }
565 578
566 if (0) { # !AIO 579 if (0) { # !AIO
567 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h 580 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
568 or last; 581 or last;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines