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.17 by root, Wed Aug 15 03:06:21 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
83Event->add_hooks(prepare => sub {
84 &Coro::cede;
85});
86
87our %conn; # $conn{ip}{fh} => connobj 92our %conn; # $conn{ip}{self} => connobj
93our %uri; # $uri{ip}{uri}{self}
88our %blocked; 94our %blocked;
89our %mimetype; 95our %mimetype;
90 96
91sub read_mimetypes { 97sub read_mimetypes {
92 local *M; 98 local *M;
125sub DESTROY { 131sub DESTROY {
126 my $self = shift; 132 my $self = shift;
127 133
128 $::conns--; 134 $::conns--;
129 135
136 $self->eoconn;
130 delete $conn{$self->{remote_addr}}{$self*1}; 137 delete $conn{$self->{remote_addr}}{$self*1};
138}
139
140# end of connection
141sub eoconn {
142 my $self = shift;
131 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 143 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
132} 144}
133 145
134sub slog { 146sub slog {
135 my $self = shift; 147 my $self = shift;
148 } 160 }
149 $res .= "\015\012"; 161 $res .= "\015\012";
150 162
151 $res .= $content if defined $content and $self->{method} ne "HEAD"; 163 $res .= $content if defined $content and $self->{method} ne "HEAD";
152 164
153 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;
154 169
155 $self->{written} += 170 $self->{written} +=
156 print {$self->{fh}} $res; 171 print {$self->{fh}} $res;
157} 172}
158 173
175sub err_blocked { 190sub err_blocked {
176 my $self = shift; 191 my $self = shift;
177 my $ip = $self->{remote_addr}; 192 my $ip = $self->{remote_addr};
178 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 193 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
179 194
180 Coro::Event::do_timer(after => 15); 195 Coro::Event::do_timer(after => 20*rand);
181 196
182 $self->err(401, "too many connections", 197 $self->err(401, "too many connections",
183 { 198 {
184 "Content-Type" => "text/html", 199 "Content-Type" => "text/html",
185 "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\"",
186 }, 203 },
187 <<EOF); 204 <<EOF);
188<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
189You have been blocked because you opened too many connections. You 211<p>You have been blocked because you opened too many connections. You
190may retry at</p> 212may retry at</p>
191 213
192 <p><blockquote>$time.</blockquote></p> 214 <p><blockquote>$time.</blockquote></p>
193 215
194<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
195look 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>
196</html> 218
219</body></html>
197EOF 220EOF
198} 221}
199 222
200sub handle { 223sub handle {
201 my $self = shift; 224 my $self = shift;
242 265
243 $self->{method} = $1; 266 $self->{method} = $1;
244 $self->{uri} = $2; 267 $self->{uri} = $2;
245 $self->{version} = $3; 268 $self->{version} = $3;
246 269
247 $3 eq "1.0" or $3 eq "1.1" 270 $3 =~ /^1\./
248 or $self->err(506, "http protocol version $3 not supported"); 271 or $self->err(506, "http protocol version $3 not supported");
249 272
250 # parse headers 273 # parse headers
251 { 274 {
252 my (%hdr, $h, $v); 275 my (%hdr, $h, $v);
268 291
269 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 292 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
270 293
271 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 294 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
272 295
296 eval {
273 $self->map_uri; 297 $self->map_uri;
274 $self->respond; 298 $self->respond;
299 };
300
301 $self->eoconn;
302
303 die if $@ && !ref $@;
275 304
276 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; 305 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
277 306
278 $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); 307 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
279 $fh->timeout($::PER_TIMEOUT); 308 $fh->timeout($::PER_TIMEOUT);
280 } 309 }
281} 310}
282 311
283# uri => path mapping 312# uri => path mapping
372 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 401 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
373 } else { 402 } else {
374 $ims < $self->{stat}[9] 403 $ims < $self->{stat}[9]
375 or $self->err(304, "not modified"); 404 or $self->err(304, "not modified");
376 405
377 if ($self->{method} eq "GET") {
378 if (-r "$path/index.html") { 406 if (-r "$path/index.html") {
379 $self->{path} .= "/index.html"; 407 $self->{path} .= "/index.html";
380 $self->handle_file; 408 $self->handle_file;
381 } else { 409 } else {
382 $self->handle_dir; 410 $self->handle_dir;
383 }
384 } 411 }
385 } 412 }
386 } elsif (-f _ && -r _) { 413 } elsif (-f _ && -r _) {
387 -x _ and $self->err(403, "forbidden"); 414 -x _ and $self->err(403, "forbidden");
388 $self->handle_file; 415 $self->handle_file;
421 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 448 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
422 } else { 449 } else {
423 ($l, $h) = (0, $length - 1); 450 ($l, $h) = (0, $length - 1);
424 goto ignore; 451 goto ignore;
425 } 452 }
426 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 453 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
427 } 454 }
428 $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"}.")");
429 $self->err(416, "not satisfiable", $hdr); 458 $self->err(416, "not satisfiable", $hdr, "");
430 459
431satisfiable: 460satisfiable:
432 # check for segmented downloads 461 # check for segmented downloads
433 if ($l && $::NO_SEGMENTED) { 462 if ($l && $::NO_SEGMENTED) {
434 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 463 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
435 Coro::Event::do_timer(after => 15);
436
437 $self->err(400, "segmented downloads are not allowed"); 464 $self->err(400, "segmented downloads are not allowed");
438 } 465 }
439 } 466 }
440 467
441 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 468 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
459 open $fh, "<", $self->{path} 486 open $fh, "<", $self->{path}
460 or die "$self->{path}: late open failure ($!)"; 487 or die "$self->{path}: late open failure ($!)";
461 488
462 $h -= $l - 1; 489 $h -= $l - 1;
463 490
491 if (0) {
492 if ($l) {
493 sysseek $fh, $l, 0;
494 }
495 }
496
464 while ($h > 0) { 497 while ($h > 0) {
498 if (0) {
499 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
500 or last;
501 } else {
465 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 502 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
466 $buf, 0, sub { 503 $buf, 0, sub {
467 $r = $_[0]; 504 $r = $_[0];
468 $current->ready; 505 $current->ready;
469 }); 506 });
470 &Coro::schedule; 507 &Coro::schedule;
471 last unless $r; 508 last unless $r;
509 }
472 my $w = $self->{fh}->syswrite($buf) 510 my $w = $self->{fh}->syswrite($buf)
473 or last; 511 or last;
474 $::written += $w; 512 $::written += $w;
475 $self->{written} += $w; 513 $self->{written} += $w;
476 $l += $r; 514 $l += $r;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines