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.19 by root, Thu Aug 16 16:40:07 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
176sub err_blocked { 190sub err_blocked {
177 my $self = shift; 191 my $self = shift;
178 my $ip = $self->{remote_addr}; 192 my $ip = $self->{remote_addr};
179 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 193 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
180 194
181 Coro::Event::do_timer(after => 15); 195 Coro::Event::do_timer(after => 20*rand);
182 196
183 $self->err(401, "too many connections", 197 $self->err(401, "too many connections",
184 { 198 {
185 "Content-Type" => "text/html", 199 "Content-Type" => "text/html",
186 "Retry-After" => $::BLOCKTIME 200 "Retry-After" => $::BLOCKTIME,
201 "Warning" => "Please do NOT retry, you have been blocked",
202 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"",
187 }, 203 },
188 <<EOF); 204 <<EOF);
189<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
190You have been blocked because you opened too many connections. You 211<p>You have been blocked because you opened too many connections. You
191may retry at</p> 212may retry at</p>
192 213
193 <p><blockquote>$time.</blockquote></p> 214 <p><blockquote>$time.</blockquote></p>
194 215
195<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
196look 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>
197</html> 218
219</body></html>
198EOF 220EOF
199} 221}
200 222
201sub handle { 223sub handle {
202 my $self = shift; 224 my $self = shift;
243 265
244 $self->{method} = $1; 266 $self->{method} = $1;
245 $self->{uri} = $2; 267 $self->{uri} = $2;
246 $self->{version} = $3; 268 $self->{version} = $3;
247 269
248 $3 eq "1.0" or $3 eq "1.1" 270 $3 =~ /^1\./
249 or $self->err(506, "http protocol version $3 not supported"); 271 or $self->err(506, "http protocol version $3 not supported");
250 272
251 # parse headers 273 # parse headers
252 { 274 {
253 my (%hdr, $h, $v); 275 my (%hdr, $h, $v);
269 291
270 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 292 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
271 293
272 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 294 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
273 295
296 eval {
274 $self->map_uri; 297 $self->map_uri;
275 $self->respond; 298 $self->respond;
299 };
276 300
277 $self->eoconn; 301 $self->eoconn;
302
303 die if $@ && !ref $@;
278 304
279 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; 305 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
280 306
281 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); 307 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
282 $fh->timeout($::PER_TIMEOUT); 308 $fh->timeout($::PER_TIMEOUT);
375 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 401 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
376 } else { 402 } else {
377 $ims < $self->{stat}[9] 403 $ims < $self->{stat}[9]
378 or $self->err(304, "not modified"); 404 or $self->err(304, "not modified");
379 405
380 if ($self->{method} eq "GET") {
381 if (-r "$path/index.html") { 406 if (-r "$path/index.html") {
382 $self->{path} .= "/index.html"; 407 $self->{path} .= "/index.html";
383 $self->handle_file; 408 $self->handle_file;
384 } else { 409 } else {
385 $self->handle_dir; 410 $self->handle_dir;
386 }
387 } 411 }
388 } 412 }
389 } elsif (-f _ && -r _) { 413 } elsif (-f _ && -r _) {
390 -x _ and $self->err(403, "forbidden"); 414 -x _ and $self->err(403, "forbidden");
391 $self->handle_file; 415 $self->handle_file;
424 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 448 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
425 } else { 449 } else {
426 ($l, $h) = (0, $length - 1); 450 ($l, $h) = (0, $length - 1);
427 goto ignore; 451 goto ignore;
428 } 452 }
429 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 453 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
430 } 454 }
431 $hdr->{"Content-Range"} = "bytes */$length"; 455 $hdr->{"Content-Range"} = "bytes */$length";
456 $hdr->{"Content-Length"} = $length;
457 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
432 $self->err(416, "not satisfiable", $hdr); 458 $self->err(416, "not satisfiable", $hdr, "");
433 459
434satisfiable: 460satisfiable:
435 # check for segmented downloads 461 # check for segmented downloads
436 if ($l && $::NO_SEGMENTED) { 462 if ($l && $::NO_SEGMENTED) {
437 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 463 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
438 Coro::Event::do_timer(after => 15);
439
440 $self->err(400, "segmented downloads are not allowed"); 464 $self->err(400, "segmented downloads are not allowed");
441 } 465 }
442 } 466 }
443 467
444 $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