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.29 by root, Sat Aug 25 15:14:03 2001 UTC vs.
Revision 1.34 by root, Thu Aug 30 02:58:17 2001 UTC

1use Coro; 1use Coro;
2use Coro::Semaphore; 2use Coro::Semaphore;
3use Coro::Event; 3use Coro::Event;
4use Coro::Socket; 4use Coro::Socket;
5
6use HTTP::Date;
5 7
6no utf8; 8no utf8;
7use bytes; 9use bytes;
8 10
9# at least on my machine, this thingy serves files 11# at least on my machine, this thingy serves files
25 my $level = shift; 27 my $level = shift;
26 my $format = shift; 28 my $format = shift;
27 printf "---: $format\n", @_; 29 printf "---: $format\n", @_;
28} 30}
29 31
30my $connections = new Coro::Semaphore $MAX_CONNECTS; 32our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
33
34our $wait_factor = 0.95;
35
36our @transfers = (
37 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 600],
38 [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 600],
39);
31 40
32my @newcons; 41my @newcons;
33my @pool; 42my @pool;
34 43
35# one "execution thread" 44# one "execution thread"
57 Listen => 50, 66 Listen => 50,
58 or die "unable to start server"; 67 or die "unable to start server";
59 68
60push @listen_sockets, $http_port; 69push @listen_sockets, $http_port;
61 70
71our $NOW;
72our $HTTP_NOW;
73
74Event->timer(interval => 1, hard => 1, cb => sub {
75 $NOW = time;
76 $HTTP_NOW = time2str $NOW;
77})->now;
78
62# the "main thread" 79# the "main thread"
63async { 80async {
64 slog 1, "accepting connections"; 81 slog 1, "accepting connections";
65 while () { 82 while () {
66 $connections->down; 83 $connections->down;
67 push @newcons, [$http_port->accept]; 84 push @newcons, [$http_port->accept];
68 #slog 3, "accepted @$connections ".scalar(@pool); 85 #slog 3, "accepted @$connections ".scalar(@pool);
69 $::NOW = time;
70 if (@pool) { 86 if (@pool) {
71 (pop @pool)->ready; 87 (pop @pool)->ready;
72 } else { 88 } else {
73 async \&handler; 89 async \&handler;
74 } 90 }
154 my ($self, $code, $msg, $hdr, $content) = @_; 170 my ($self, $code, $msg, $hdr, $content) = @_;
155 my $res = "HTTP/1.1 $code $msg\015\012"; 171 my $res = "HTTP/1.1 $code $msg\015\012";
156 172
157 $self->{h}{connection} ||= $hdr->{Connection}; 173 $self->{h}{connection} ||= $hdr->{Connection};
158 174
159 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 175 $res .= "Date: $HTTP_NOW\015\012";
160 176
161 while (my ($h, $v) = each %$hdr) { 177 while (my ($h, $v) = each %$hdr) {
162 $res .= "$h: $v\015\012" 178 $res .= "$h: $v\015\012"
163 } 179 }
164 $res .= "\015\012"; 180 $res .= "\015\012";
186 $hdr->{"Connection"} = "close"; 202 $hdr->{"Connection"} = "close";
187 203
188 $self->response($code, $msg, $hdr, $content); 204 $self->response($code, $msg, $hdr, $content);
189 205
190 die bless {}, err::; 206 die bless {}, err::;
191}
192
193sub err_blocked {
194 my $self = shift;
195 my $ip = $self->{remote_addr};
196 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
197
198 Coro::Event::do_timer(after => 20*rand);
199
200 $self->err(401, "too many connections",
201 {
202 "Content-Type" => "text/html",
203 "Retry-After" => $::BLOCKTIME,
204 "Warning" => "Please do NOT retry, you have been blocked",
205 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"",
206 "Connection" => "close",
207 },
208 <<EOF);
209<html>
210<head>
211<title>Too many connections</title>
212</head>
213<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000">
214
215<p>You have been blocked because you opened too many connections. You
216may retry at</p>
217
218 <p><blockquote>$time.</blockquote></p>
219
220<p>Until then, each new access will renew the block. You might want to have a
221look at the <a href="http://www.goof.com/pcg/marc/animefaq.html#connectionlimit">FAQ</a>.</p>
222
223</body></html>
224EOF
225} 207}
226 208
227sub handle { 209sub handle {
228 my $self = shift; 210 my $self = shift;
229 my $fh = $self->{fh}; 211 my $fh = $self->{fh};
256 238
257 delete $blocked{$ip}; 239 delete $blocked{$ip};
258 } 240 }
259 241
260 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 242 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
243 my $delay = 120;
244 while (%{$conn{$ip}} > $::MAX_CONN_IP) {
245 if ($delay <= 0) {
261 $self->slog(2, "blocked ip $ip"); 246 $self->slog(2, "blocked ip $ip");
262 $self->err_blocked; 247 $self->err_blocked;
248 } else {
249 Coro::Event::do_timer(after => 3);
250 $delay -= 3;
251 }
252 }
263 } 253 }
264 254
265 $req =~ /^(?:\015\012)? 255 $req =~ /^(?:\015\012)?
266 (GET|HEAD) \040+ 256 (GET|HEAD) \040+
267 ([^\040]+) \040+ 257 ([^\040]+) \040+
439 $idx); 429 $idx);
440} 430}
441 431
442sub handle_file { 432sub handle_file {
443 my $self = shift; 433 my $self = shift;
444 my $length = -s _; 434 my $length = $self->{stat}[7];
435 my $queue = $::transfers[$length >= $::TRANSFER_SMALL];
445 my $hdr = { 436 my $hdr = {
446 "Last-Modified" => time2str ((stat _)[9]), 437 "Last-Modified" => time2str ((stat _)[9]),
447 }; 438 };
448 439
449 my @code = (200, "ok"); 440 my @code = (200, "ok");
467 $self->err(416, "not satisfiable", $hdr, ""); 458 $self->err(416, "not satisfiable", $hdr, "");
468 459
469satisfiable: 460satisfiable:
470 # check for segmented downloads 461 # check for segmented downloads
471 if ($l && $::NO_SEGMENTED) { 462 if ($l && $::NO_SEGMENTED) {
472 my $delay = 60; 463 my $delay = 180;
473 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 464 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
474 if ($delay <= 0) { 465 if ($delay <= 0) {
475 $self->err(400, "segmented downloads are not allowed", 466 $self->err_segmented_download;
476 { "Content-Type" => "text/html", Connection => "close" }, <<EOF);
477<html>
478<head>
479<title>Segmented downloads are not allowed</title>
480</head>
481<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000">
482
483<p>Segmented downloads are not allowed on this server. Please refer to the
484<a href="http://www.goof.com/pcg/marc/animefaq.html#segmented_downloads">FAQ</a>.</p>
485
486</body></html>
487EOF
488 } else { 467 } else {
489 Coro::Event::do_timer(after => 3); $delay -= 3; 468 Coro::Event::do_timer(after => 3); $delay -= 3;
490 } 469 }
491 } 470 }
492 } 471 }
505 $hdr->{"Content-Length"} = $length; 484 $hdr->{"Content-Length"} = $length;
506 485
507 $self->response(@code, $hdr, ""); 486 $self->response(@code, $hdr, "");
508 487
509 if ($self->{method} eq "GET") { 488 if ($self->{method} eq "GET") {
489 $self->{time} = $::NOW;
490
491 my $transfer = $queue->[0]->guard;
492 $self->{fh}->writable or return;
493
494 $queue->[1] = $queue->[1] * $::wait_factor
495 + ($::NOW - $self->{time}) * (1 - $::wait_factor);
496 $self->{time} = $::NOW;
497
510 my ($fh, $buf, $r); 498 my ($fh, $buf, $r);
511 my $current = $Coro::current; 499 my $current = $Coro::current;
512 open $fh, "<", $self->{path} 500 open $fh, "<", $self->{path}
513 or die "$self->{path}: late open failure ($!)"; 501 or die "$self->{path}: late open failure ($!)";
514 502
540 or last; 528 or last;
541 $::written += $w; 529 $::written += $w;
542 $self->{written} += $w; 530 $self->{written} += $w;
543 $l += $r; 531 $l += $r;
544 } 532 }
545 }
546 533
547 close $fh; 534 close $fh;
535 }
548} 536}
549 537
5501; 5381;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines