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.24 by root, Sat Aug 18 13:32:17 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}
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;
276 eval { 296 eval {
277 $self->map_uri; 297 $self->map_uri;
278 $self->respond; 298 $self->respond;
279 }; 299 };
280 300
301 $self->eoconn;
302
281 die if $@ && !ref $@; 303 die if $@ && !ref $@;
282
283 $self->eoconn;
284 304
285 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; 305 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
286 306
287 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); 307 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
288 $fh->timeout($::PER_TIMEOUT); 308 $fh->timeout($::PER_TIMEOUT);
381 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 401 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
382 } else { 402 } else {
383 $ims < $self->{stat}[9] 403 $ims < $self->{stat}[9]
384 or $self->err(304, "not modified"); 404 or $self->err(304, "not modified");
385 405
386 if ($self->{method} eq "GET") {
387 if (-r "$path/index.html") { 406 if (-r "$path/index.html") {
388 $self->{path} .= "/index.html"; 407 $self->{path} .= "/index.html";
389 $self->handle_file; 408 $self->handle_file;
390 } else { 409 } else {
391 $self->handle_dir; 410 $self->handle_dir;
392 }
393 } 411 }
394 } 412 }
395 } elsif (-f _ && -r _) { 413 } elsif (-f _ && -r _) {
396 -x _ and $self->err(403, "forbidden"); 414 -x _ and $self->err(403, "forbidden");
397 $self->handle_file; 415 $self->handle_file;
430 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 448 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
431 } else { 449 } else {
432 ($l, $h) = (0, $length - 1); 450 ($l, $h) = (0, $length - 1);
433 goto ignore; 451 goto ignore;
434 } 452 }
435 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 453 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
436 } 454 }
437 $hdr->{"Content-Range"} = "bytes */$length"; 455 $hdr->{"Content-Range"} = "bytes */$length";
438 $hdr->{"Content-Length"} = $length; 456 $hdr->{"Content-Length"} = $length;
439 $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"}.")");
440 $self->err(416, "not satisfiable", $hdr, ""); 458 $self->err(416, "not satisfiable", $hdr, "");
441 459
442satisfiable: 460satisfiable:
443 # check for segmented downloads 461 # check for segmented downloads
444 if ($l && $::NO_SEGMENTED) { 462 if ($l && $::NO_SEGMENTED) {
445 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 463 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
446 Coro::Event::do_timer(after => 15);
447
448 $self->err(400, "segmented downloads are not allowed"); 464 $self->err(400, "segmented downloads are not allowed");
449 } 465 }
450 } 466 }
451 467
452 $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