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.28 by root, Mon Aug 20 16:58:19 2001 UTC vs.
Revision 1.30 by root, Sun Aug 26 14:55:46 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
289 259
290 $self->{h}{$h} = substr $v, 1 260 $self->{h}{$h} = substr $v, 1
291 while ($h, $v) = each %hdr; 261 while ($h, $v) = each %hdr;
292 } 262 }
293 263
264 # find out server name and port
265 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) {
266 $host = $1;
267 } else {
268 $host = $self->{h}{host};
269 }
270
271 if (defined $host) {
294 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 272 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80;
273 } else {
274 ($self->{server_port}, $host)
275 = unpack_sockaddr_in $self->{fh}->getsockname
276 or $self->err(500, "unable to get socket name");
277 $host = inet_ntoa $host;
278 }
279
280 $self->{server_name} = $host;
281
282 # remote id should be unique per user
283 $self->{remote_id} = $self->{remote_addr};
284
285 if (exists $self->{h}{"client-ip"}) {
286 $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]";
287 } elsif (exists $self->{h}{"x-forwarded-for"}) {
288 $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]";
289 }
295 290
296 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 291 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
297 292
298 eval { 293 eval {
299 $self->map_uri; 294 $self->map_uri;
302 297
303 $self->eoconn; 298 $self->eoconn;
304 299
305 die if $@ && !ref $@; 300 die if $@ && !ref $@;
306 301
307 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; 302 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1;
308 303
309 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
310 $fh->timeout($::PER_TIMEOUT); 304 $fh->timeout($::PER_TIMEOUT);
311 } 305 }
312} 306}
313 307
314# uri => path mapping 308# uri => path mapping
315sub map_uri { 309sub map_uri {
316 my $self = shift; 310 my $self = shift;
317 my $host = $self->{h}{host} || "default"; 311 my $host = $self->{server_name};
318 my $uri = $self->{uri}; 312 my $uri = $self->{uri};
319 313
320 # some massaging, also makes it more secure 314 # some massaging, also makes it more secure
321 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; 315 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge;
322 $uri =~ s%//+%/%g; 316 $uri =~ s%//+%/%g;
332 $self->{path} = "$::DOCROOT/$host$uri"; 326 $self->{path} = "$::DOCROOT/$host$uri";
333 327
334 $self->access_check; 328 $self->access_check;
335} 329}
336 330
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 { 331sub _cgi {
366 my $self = shift; 332 my $self = shift;
367 my $path = shift; 333 my $path = shift;
368 my $fh; 334 my $fh;
369 335
370 # no two-way xxx supported 336 # no two-way xxx supported
371 if (0 == fork) { 337 if (0 == fork) {
372 open STDOUT, ">&".fileno($self->{fh}); 338 open STDOUT, ">&".fileno($self->{fh});
373 if (chdir $::DOCROOT) { 339 if (chdir $::DOCROOT) {
374 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 340 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
375 $ENV{HTTP_HOST} = $self->server_host; 341 $ENV{HTTP_HOST} = $self->{server_name};
376 $ENV{HTTP_PORT} = $self->{server_host}; 342 $ENV{HTTP_PORT} = $self->{server_port};
377 $ENV{SCRIPT_NAME} = $self->{name}; 343 $ENV{SCRIPT_NAME} = $self->{name};
378 exec $path; 344 exec $path;
379 } 345 }
380 Coro::State::_exit(0); 346 Coro::State::_exit(0);
381 } else { 347 } else {
348 die;
382 } 349 }
350}
351
352sub server_hostport {
353 $_[0]{server_port} == 80
354 ? $_[0]{server_name}
355 : "$_[0]{server_name}:$_[0]{server_port}";
383} 356}
384 357
385sub respond { 358sub respond {
386 my $self = shift; 359 my $self = shift;
387 my $path = $self->{path}; 360 my $path = $self->{path};
397 370
398 if (-d _ && -r _) { 371 if (-d _ && -r _) {
399 # directory 372 # directory
400 if ($path !~ /\/$/) { 373 if ($path !~ /\/$/) {
401 # create a redirect to get the trailing "/" 374 # create a redirect to get the trailing "/"
402 my $host = $self->server_hostport; 375 # we don't try to avoid the :80
403 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 376 $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" });
404 } else { 377 } else {
405 $ims < $self->{stat}[9] 378 $ims < $self->{stat}[9]
406 or $self->err(304, "not modified"); 379 or $self->err(304, "not modified");
407 380
408 if (-r "$path/index.html") { 381 if (-r "$path/index.html") {
460 $self->err(416, "not satisfiable", $hdr, ""); 433 $self->err(416, "not satisfiable", $hdr, "");
461 434
462satisfiable: 435satisfiable:
463 # check for segmented downloads 436 # check for segmented downloads
464 if ($l && $::NO_SEGMENTED) { 437 if ($l && $::NO_SEGMENTED) {
438 my $delay = 180;
465 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 439 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
466 $self->err(400, "segmented downloads are not allowed", 440 if ($delay <= 0) {
467 { "Content-Type" => "text/html", Connection => "close" }, <<EOF); 441 $self->err_segmented_download;
468<html> 442 } else {
469<head> 443 Coro::Event::do_timer(after => 3); $delay -= 3;
470<title>Segmented downloads are not allowed</title> 444 }
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 } 445 }
481 } 446 }
482 447
483 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 448 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
484 @code = (206, "partial content"); 449 @code = (206, "partial content");
512 while ($h > 0) { 477 while ($h > 0) {
513 if (0) { 478 if (0) {
514 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 479 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
515 or last; 480 or last;
516 } else { 481 } else {
482 undef $buf;
483 $aio_requests->down;
517 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 484 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
518 $buf, 0, sub { 485 $buf, 0, sub {
519 $r = $_[0]; 486 $r = $_[0];
520 $current->ready; 487 $current->ready;
521 }); 488 });
522 &Coro::schedule; 489 &Coro::schedule;
490 $aio_requests->up;
523 last unless $r; 491 last unless $r;
524 } 492 }
525 my $w = $self->{fh}->syswrite($buf) 493 my $w = $self->{fh}->syswrite($buf)
526 or last; 494 or last;
527 $::written += $w; 495 $::written += $w;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines