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.28 by root, Mon Aug 20 16:58:19 2001 UTC vs.
Revision 1.31 by root, Mon Aug 27 05:05:26 2001 UTC

83use Convert::Scalar 'weaken'; 83use Convert::Scalar 'weaken';
84use Linux::AIO; 84use Linux::AIO;
85 85
86Linux::AIO::min_parallel $::AIO_PARALLEL; 86Linux::AIO::min_parallel $::AIO_PARALLEL;
87 87
88my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4;
89
88Event->io(fd => Linux::AIO::poll_fileno, 90Event->io(fd => Linux::AIO::poll_fileno,
89 poll => 'r', async => 1, 91 poll => 'r', async => 1,
90 cb => \&Linux::AIO::poll_cb); 92 cb => \&Linux::AIO::poll_cb);
91 93
92our %conn; # $conn{ip}{self} => connobj 94our %conn; # $conn{ip}{self} => connobj
143 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 145 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
144} 146}
145 147
146sub slog { 148sub slog {
147 my $self = shift; 149 my $self = shift;
148 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 150 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]");
149} 151}
150 152
151sub response { 153sub response {
152 my ($self, $code, $msg, $hdr, $content) = @_; 154 my ($self, $code, $msg, $hdr, $content) = @_;
153 my $res = "HTTP/1.1 $code $msg\015\012"; 155 my $res = "HTTP/1.1 $code $msg\015\012";
186 $self->response($code, $msg, $hdr, $content); 188 $self->response($code, $msg, $hdr, $content);
187 189
188 die bless {}, err::; 190 die bless {}, err::;
189} 191}
190 192
191sub err_blocked {
192 my $self = shift;
193 my $ip = $self->{remote_addr};
194 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
195
196 Coro::Event::do_timer(after => 20*rand);
197
198 $self->err(401, "too many connections",
199 {
200 "Content-Type" => "text/html",
201 "Retry-After" => $::BLOCKTIME,
202 "Warning" => "Please do NOT retry, you have been blocked",
203 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"",
204 "Connection" => "close",
205 },
206 <<EOF);
207<html>
208<head>
209<title>Too many connections</title>
210</head>
211<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000">
212
213<p>You have been blocked because you opened too many connections. You
214may retry at</p>
215
216 <p><blockquote>$time.</blockquote></p>
217
218<p>Until then, each new access will renew the block. You might want to have a
219look at the <a href="http://www.goof.com/pcg/marc/animefaq.html#connectionlimit">FAQ</a>.</p>
220
221</body></html>
222EOF
223}
224
225sub handle { 193sub handle {
226 my $self = shift; 194 my $self = shift;
227 my $fh = $self->{fh}; 195 my $fh = $self->{fh};
196
197 my $host;
228 198
229 $fh->timeout($::REQ_TIMEOUT); 199 $fh->timeout($::REQ_TIMEOUT);
230 while() { 200 while() {
231 $self->{reqs}++; 201 $self->{reqs}++;
232 202
252 222
253 delete $blocked{$ip}; 223 delete $blocked{$ip};
254 } 224 }
255 225
256 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 226 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
227 my $delay = 120;
228 while (%{$conn{$ip}} > $::MAX_CONN_IP) {
229 if ($delay <= 0) {
257 $self->slog(2, "blocked ip $ip"); 230 $self->slog(2, "blocked ip $ip");
258 $self->err_blocked; 231 $self->err_blocked;
232 } else {
233 Coro::Event::do_timer(after => 3);
234 $delay -= 3;
235 }
236 }
259 } 237 }
260 238
261 $req =~ /^(?:\015\012)? 239 $req =~ /^(?:\015\012)?
262 (GET|HEAD) \040+ 240 (GET|HEAD) \040+
263 ([^\040]+) \040+ 241 ([^\040]+) \040+
289 267
290 $self->{h}{$h} = substr $v, 1 268 $self->{h}{$h} = substr $v, 1
291 while ($h, $v) = each %hdr; 269 while ($h, $v) = each %hdr;
292 } 270 }
293 271
272 # find out server name and port
273 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) {
274 $host = $1;
275 } else {
276 $host = $self->{h}{host};
277 }
278
279 if (defined $host) {
294 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 280 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80;
281 } else {
282 ($self->{server_port}, $host)
283 = unpack_sockaddr_in $self->{fh}->getsockname
284 or $self->err(500, "unable to get socket name");
285 $host = inet_ntoa $host;
286 }
287
288 $self->{server_name} = $host;
289
290 # remote id should be unique per user
291 $self->{remote_id} = $self->{remote_addr};
292
293 if (exists $self->{h}{"client-ip"}) {
294 $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]";
295 } elsif (exists $self->{h}{"x-forwarded-for"}) {
296 $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]";
297 }
295 298
296 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 299 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
297 300
298 eval { 301 eval {
299 $self->map_uri; 302 $self->map_uri;
302 305
303 $self->eoconn; 306 $self->eoconn;
304 307
305 die if $@ && !ref $@; 308 die if $@ && !ref $@;
306 309
307 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; 310 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1;
308 311
309 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
310 $fh->timeout($::PER_TIMEOUT); 312 $fh->timeout($::PER_TIMEOUT);
311 } 313 }
312} 314}
313 315
314# uri => path mapping 316# uri => path mapping
315sub map_uri { 317sub map_uri {
316 my $self = shift; 318 my $self = shift;
317 my $host = $self->{h}{host} || "default"; 319 my $host = $self->{server_name};
318 my $uri = $self->{uri}; 320 my $uri = $self->{uri};
319 321
320 # some massaging, also makes it more secure 322 # some massaging, also makes it more secure
321 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; 323 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge;
322 $uri =~ s%//+%/%g; 324 $uri =~ s%//+%/%g;
332 $self->{path} = "$::DOCROOT/$host$uri"; 334 $self->{path} = "$::DOCROOT/$host$uri";
333 335
334 $self->access_check; 336 $self->access_check;
335} 337}
336 338
337sub server_address {
338 my $self = shift;
339 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname
340 or $self->err(500, "unable to get socket name");
341 ((inet_ntoa $iaddr), $port);
342}
343
344sub server_host {
345 my $self = shift;
346 if (exists $self->{h}{host}) {
347 return $self->{h}{host};
348 } else {
349 return (($self->server_address)[0]);
350 }
351}
352
353sub server_hostport {
354 my $self = shift;
355 my ($host, $port);
356 if (exists $self->{h}{host}) {
357 ($host, $port) = ($self->{h}{host}, $self->{server_port});
358 } else {
359 ($host, $port) = $self->server_address;
360 }
361 $port = $port == 80 ? "" : ":$port";
362 $host.$port;
363}
364
365sub _cgi { 339sub _cgi {
366 my $self = shift; 340 my $self = shift;
367 my $path = shift; 341 my $path = shift;
368 my $fh; 342 my $fh;
369 343
370 # no two-way xxx supported 344 # no two-way xxx supported
371 if (0 == fork) { 345 if (0 == fork) {
372 open STDOUT, ">&".fileno($self->{fh}); 346 open STDOUT, ">&".fileno($self->{fh});
373 if (chdir $::DOCROOT) { 347 if (chdir $::DOCROOT) {
374 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 348 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
375 $ENV{HTTP_HOST} = $self->server_host; 349 $ENV{HTTP_HOST} = $self->{server_name};
376 $ENV{HTTP_PORT} = $self->{server_host}; 350 $ENV{HTTP_PORT} = $self->{server_port};
377 $ENV{SCRIPT_NAME} = $self->{name}; 351 $ENV{SCRIPT_NAME} = $self->{name};
378 exec $path; 352 exec $path;
379 } 353 }
380 Coro::State::_exit(0); 354 Coro::State::_exit(0);
381 } else { 355 } else {
356 die;
382 } 357 }
358}
359
360sub server_hostport {
361 $_[0]{server_port} == 80
362 ? $_[0]{server_name}
363 : "$_[0]{server_name}:$_[0]{server_port}";
383} 364}
384 365
385sub respond { 366sub respond {
386 my $self = shift; 367 my $self = shift;
387 my $path = $self->{path}; 368 my $path = $self->{path};
397 378
398 if (-d _ && -r _) { 379 if (-d _ && -r _) {
399 # directory 380 # directory
400 if ($path !~ /\/$/) { 381 if ($path !~ /\/$/) {
401 # create a redirect to get the trailing "/" 382 # create a redirect to get the trailing "/"
402 my $host = $self->server_hostport; 383 # we don't try to avoid the :80
403 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 384 $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" });
404 } else { 385 } else {
405 $ims < $self->{stat}[9] 386 $ims < $self->{stat}[9]
406 or $self->err(304, "not modified"); 387 or $self->err(304, "not modified");
407 388
408 if (-r "$path/index.html") { 389 if (-r "$path/index.html") {
460 $self->err(416, "not satisfiable", $hdr, ""); 441 $self->err(416, "not satisfiable", $hdr, "");
461 442
462satisfiable: 443satisfiable:
463 # check for segmented downloads 444 # check for segmented downloads
464 if ($l && $::NO_SEGMENTED) { 445 if ($l && $::NO_SEGMENTED) {
446 my $delay = 180;
465 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 447 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
466 $self->err(400, "segmented downloads are not allowed", 448 if ($delay <= 0) {
467 { "Content-Type" => "text/html", Connection => "close" }, <<EOF); 449 $self->err_segmented_download;
468<html> 450 } else {
469<head> 451 Coro::Event::do_timer(after => 3); $delay -= 3;
470<title>Segmented downloads are not allowed</title> 452 }
471</head>
472<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000">
473
474<p>Segmented downloads are not allowed on this server. Please refer to the
475<a href="http://www.goof.com/pcg/marc/animefaq.html#segmented_downloads">FAQ</a>.</p>
476
477</body></html>
478EOF
479EOF
480 } 453 }
481 } 454 }
482 455
483 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 456 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
484 @code = (206, "partial content"); 457 @code = (206, "partial content");
512 while ($h > 0) { 485 while ($h > 0) {
513 if (0) { 486 if (0) {
514 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 487 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
515 or last; 488 or last;
516 } else { 489 } else {
490 undef $buf;
491 $aio_requests->down;
517 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 492 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
518 $buf, 0, sub { 493 $buf, 0, sub {
519 $r = $_[0]; 494 $r = $_[0];
520 $current->ready; 495 $current->ready;
521 }); 496 });
522 &Coro::schedule; 497 &Coro::schedule;
498 $aio_requests->up;
523 last unless $r; 499 last unless $r;
524 } 500 }
525 my $w = $self->{fh}->syswrite($buf) 501 my $w = $self->{fh}->syswrite($buf)
526 or last; 502 or last;
527 $::written += $w; 503 $::written += $w;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines