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.33 by root, Wed Aug 29 01:40:44 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 5
6use HTTP::Date;
7
6no utf8; 8no utf8;
7use bytes; 9use bytes;
10
11our @wait_time = (); # used to calculcate avg. waiting time
12our $wait_time_length = 25;
8 13
9# at least on my machine, this thingy serves files 14# at least on my machine, this thingy serves files
10# quite a bit faster than apache, ;) 15# quite a bit faster than apache, ;)
11# and quite a bit slower than thttpd :( 16# and quite a bit slower than thttpd :(
12 17
25 my $level = shift; 30 my $level = shift;
26 my $format = shift; 31 my $format = shift;
27 printf "---: $format\n", @_; 32 printf "---: $format\n", @_;
28} 33}
29 34
30my $connections = new Coro::Semaphore $MAX_CONNECTS; 35our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
36our $transfers = new Coro::Semaphore $MAX_TRANSFER || 50;
31 37
32my @newcons; 38my @newcons;
33my @pool; 39my @pool;
34 40
35# one "execution thread" 41# one "execution thread"
57 Listen => 50, 63 Listen => 50,
58 or die "unable to start server"; 64 or die "unable to start server";
59 65
60push @listen_sockets, $http_port; 66push @listen_sockets, $http_port;
61 67
68our $NOW;
69our $HTTP_NOW;
70
71Event->timer(interval => 1, hard => 1, cb => sub {
72 $NOW = time;
73 $HTTP_NOW = time2str $NOW;
74});
75
62# the "main thread" 76# the "main thread"
63async { 77async {
64 slog 1, "accepting connections"; 78 slog 1, "accepting connections";
65 while () { 79 while () {
66 $connections->down; 80 $connections->down;
67 push @newcons, [$http_port->accept]; 81 push @newcons, [$http_port->accept];
68 #slog 3, "accepted @$connections ".scalar(@pool); 82 #slog 3, "accepted @$connections ".scalar(@pool);
69 $::NOW = time;
70 if (@pool) { 83 if (@pool) {
71 (pop @pool)->ready; 84 (pop @pool)->ready;
72 } else { 85 } else {
73 async \&handler; 86 async \&handler;
74 } 87 }
154 my ($self, $code, $msg, $hdr, $content) = @_; 167 my ($self, $code, $msg, $hdr, $content) = @_;
155 my $res = "HTTP/1.1 $code $msg\015\012"; 168 my $res = "HTTP/1.1 $code $msg\015\012";
156 169
157 $self->{h}{connection} ||= $hdr->{Connection}; 170 $self->{h}{connection} ||= $hdr->{Connection};
158 171
159 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 172 $res .= "Date: $HTTP_NOW\015\012";
160 173
161 while (my ($h, $v) = each %$hdr) { 174 while (my ($h, $v) = each %$hdr) {
162 $res .= "$h: $v\015\012" 175 $res .= "$h: $v\015\012"
163 } 176 }
164 $res .= "\015\012"; 177 $res .= "\015\012";
186 $hdr->{"Connection"} = "close"; 199 $hdr->{"Connection"} = "close";
187 200
188 $self->response($code, $msg, $hdr, $content); 201 $self->response($code, $msg, $hdr, $content);
189 202
190 die bless {}, err::; 203 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} 204}
226 205
227sub handle { 206sub handle {
228 my $self = shift; 207 my $self = shift;
229 my $fh = $self->{fh}; 208 my $fh = $self->{fh};
256 235
257 delete $blocked{$ip}; 236 delete $blocked{$ip};
258 } 237 }
259 238
260 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 239 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
240 my $delay = 120;
241 while (%{$conn{$ip}} > $::MAX_CONN_IP) {
242 if ($delay <= 0) {
261 $self->slog(2, "blocked ip $ip"); 243 $self->slog(2, "blocked ip $ip");
262 $self->err_blocked; 244 $self->err_blocked;
245 } else {
246 Coro::Event::do_timer(after => 3);
247 $delay -= 3;
248 }
249 }
263 } 250 }
264 251
265 $req =~ /^(?:\015\012)? 252 $req =~ /^(?:\015\012)?
266 (GET|HEAD) \040+ 253 (GET|HEAD) \040+
267 ([^\040]+) \040+ 254 ([^\040]+) \040+
467 $self->err(416, "not satisfiable", $hdr, ""); 454 $self->err(416, "not satisfiable", $hdr, "");
468 455
469satisfiable: 456satisfiable:
470 # check for segmented downloads 457 # check for segmented downloads
471 if ($l && $::NO_SEGMENTED) { 458 if ($l && $::NO_SEGMENTED) {
472 my $delay = 60; 459 my $delay = 180;
473 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 460 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
474 if ($delay <= 0) { 461 if ($delay <= 0) {
475 $self->err(400, "segmented downloads are not allowed", 462 $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 { 463 } else {
489 Coro::Event::do_timer(after => 3); $delay -= 3; 464 Coro::Event::do_timer(after => 3); $delay -= 3;
490 } 465 }
491 } 466 }
492 } 467 }
505 $hdr->{"Content-Length"} = $length; 480 $hdr->{"Content-Length"} = $length;
506 481
507 $self->response(@code, $hdr, ""); 482 $self->response(@code, $hdr, "");
508 483
509 if ($self->{method} eq "GET") { 484 if ($self->{method} eq "GET") {
485 $self->{time} = $::NOW;
486
487 my $transfer = $::transfers->guard;
488 $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
510 my ($fh, $buf, $r); 494 my ($fh, $buf, $r);
511 my $current = $Coro::current; 495 my $current = $Coro::current;
512 open $fh, "<", $self->{path} 496 open $fh, "<", $self->{path}
513 or die "$self->{path}: late open failure ($!)"; 497 or die "$self->{path}: late open failure ($!)";
514 498
540 or last; 524 or last;
541 $::written += $w; 525 $::written += $w;
542 $self->{written} += $w; 526 $self->{written} += $w;
543 $l += $r; 527 $l += $r;
544 } 528 }
545 }
546 529
547 close $fh; 530 close $fh;
531 }
548} 532}
549 533
5501; 5341;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines