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.20 by root, Thu Aug 16 21:55:35 2001 UTC vs.
Revision 1.27 by root, Sun Aug 19 23:57:52 2001 UTC

9# at least on my machine, this thingy serves files 9# at least on my machine, this thingy serves files
10# quite a bit faster than apache, ;) 10# quite a bit faster than apache, ;)
11# and quite a bit slower than thttpd :( 11# and quite a bit slower than thttpd :(
12 12
13$SIG{PIPE} = 'IGNORE'; 13$SIG{PIPE} = 'IGNORE';
14 14
15our $accesslog;
16
17if ($ACCESS_LOG) {
18 use IO::Handle;
19 open $accesslog, ">>$ACCESS_LOG"
20 or die "$ACCESS_LOG: $!";
21 $accesslog->autoflush(1);
22}
23
15sub slog { 24sub slog {
16 my $level = shift; 25 my $level = shift;
17 my $format = shift; 26 my $format = shift;
18 printf "---: $format\n", @_; 27 printf "---: $format\n", @_;
19} 28}
76 85
77Linux::AIO::min_parallel $::AIO_PARALLEL; 86Linux::AIO::min_parallel $::AIO_PARALLEL;
78 87
79Event->io(fd => Linux::AIO::poll_fileno, 88Event->io(fd => Linux::AIO::poll_fileno,
80 poll => 'r', async => 1, 89 poll => 'r', async => 1,
81 cb => \&Linux::AIO::poll_cb ); 90 cb => \&Linux::AIO::poll_cb);
82 91
83our %conn; # $conn{ip}{fh} => connobj 92our %conn; # $conn{ip}{self} => connobj
93our %uri; # $uri{ip}{uri}{self}
84our %blocked; 94our %blocked;
85our %mimetype; 95our %mimetype;
86 96
87sub read_mimetypes { 97sub read_mimetypes {
88 local *M; 98 local *M;
127 delete $conn{$self->{remote_addr}}{$self*1}; 137 delete $conn{$self->{remote_addr}}{$self*1};
128} 138}
129 139
130# end of connection 140# end of connection
131sub eoconn { 141sub eoconn {
142 my $self = shift;
132 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 143 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
133} 144}
134 145
135sub slog { 146sub slog {
136 my $self = shift; 147 my $self = shift;
149 } 160 }
150 $res .= "\015\012"; 161 $res .= "\015\012";
151 162
152 $res .= $content if defined $content and $self->{method} ne "HEAD"; 163 $res .= $content if defined $content and $self->{method} ne "HEAD";
153 164
154 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 165 my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";
166
167 print $accesslog $log if $accesslog;
168 print STDERR $log;
155 169
156 $self->{written} += 170 $self->{written} +=
157 print {$self->{fh}} $res; 171 print {$self->{fh}} $res;
158} 172}
159 173
186 "Retry-After" => $::BLOCKTIME, 200 "Retry-After" => $::BLOCKTIME,
187 "Warning" => "Please do NOT retry, you have been blocked", 201 "Warning" => "Please do NOT retry, you have been blocked",
188 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", 202 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"",
189 }, 203 },
190 <<EOF); 204 <<EOF);
191<html><p> 205<html>
206<head>
207<title>Too many connections</title>
208</head>
209<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000">
210
192You have been blocked because you opened too many connections. You 211<p>You have been blocked because you opened too many connections. You
193may retry at</p> 212may retry at</p>
194 213
195 <p><blockquote>$time.</blockquote></p> 214 <p><blockquote>$time.</blockquote></p>
196 215
197<p>Until then, each new access will renew the block. You might want to have a 216<p>Until then, each new access will renew the block. You might want to have a
198look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p> 217look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p>
199</html> 218
219</body></html>
200EOF 220EOF
201} 221}
202 222
203sub handle { 223sub handle {
204 my $self = shift; 224 my $self = shift;
271 291
272 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 292 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
273 293
274 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 294 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
275 295
296 eval {
276 $self->map_uri; 297 $self->map_uri;
277 $self->respond; 298 $self->respond;
299 };
278 300
279 $self->eoconn; 301 $self->eoconn;
302
303 die if $@ && !ref $@;
280 304
281 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; 305 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
282 306
283 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); 307 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
284 $fh->timeout($::PER_TIMEOUT); 308 $fh->timeout($::PER_TIMEOUT);
377 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 401 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
378 } else { 402 } else {
379 $ims < $self->{stat}[9] 403 $ims < $self->{stat}[9]
380 or $self->err(304, "not modified"); 404 or $self->err(304, "not modified");
381 405
382 if ($self->{method} eq "GET") {
383 if (-r "$path/index.html") { 406 if (-r "$path/index.html") {
384 $self->{path} .= "/index.html"; 407 $self->{path} .= "/index.html";
385 $self->handle_file; 408 $self->handle_file;
386 } else { 409 } else {
387 $self->handle_dir; 410 $self->handle_dir;
388 }
389 } 411 }
390 } 412 }
391 } elsif (-f _ && -r _) { 413 } elsif (-f _ && -r _) {
392 -x _ and $self->err(403, "forbidden"); 414 -x _ and $self->err(403, "forbidden");
393 $self->handle_file; 415 $self->handle_file;
426 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 448 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
427 } else { 449 } else {
428 ($l, $h) = (0, $length - 1); 450 ($l, $h) = (0, $length - 1);
429 goto ignore; 451 goto ignore;
430 } 452 }
431 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 453 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
432 } 454 }
433 $hdr->{"Content-Range"} = "bytes */$length"; 455 $hdr->{"Content-Range"} = "bytes */$length";
456 $hdr->{"Content-Length"} = $length;
434 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); 457 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
435 $self->err(416, "not satisfiable", $hdr); 458 $self->err(416, "not satisfiable", $hdr, "");
436 459
437satisfiable: 460satisfiable:
438 # check for segmented downloads 461 # check for segmented downloads
439 if ($l && $::NO_SEGMENTED) { 462 if ($l && $::NO_SEGMENTED) {
440 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 463 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
441 Coro::Event::do_timer(after => 15);
442
443 $self->err(400, "segmented downloads are not allowed"); 464 $self->err(400, "segmented downloads are not allowed");
444 } 465 }
445 } 466 }
446 467
447 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 468 $hdr->{"Content-Range"} = "bytes $l-$h/$length";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines