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.18 by root, Wed Aug 15 03:13:36 2001 UTC vs.
Revision 1.25 by root, Sun Aug 19 22:14:26 2001 UTC

76 76
77Linux::AIO::min_parallel $::AIO_PARALLEL; 77Linux::AIO::min_parallel $::AIO_PARALLEL;
78 78
79Event->io(fd => Linux::AIO::poll_fileno, 79Event->io(fd => Linux::AIO::poll_fileno,
80 poll => 'r', async => 1, 80 poll => 'r', async => 1,
81 cb => \&Linux::AIO::poll_cb ); 81 cb => \&Linux::AIO::poll_cb);
82 82
83our %conn; # $conn{ip}{fh} => connobj 83our %conn; # $conn{ip}{fh} => connobj
84our %blocked; 84our %blocked;
85our %mimetype; 85our %mimetype;
86 86
121sub DESTROY { 121sub DESTROY {
122 my $self = shift; 122 my $self = shift;
123 123
124 $::conns--; 124 $::conns--;
125 125
126 $self->eoconn;
126 delete $conn{$self->{remote_addr}}{$self*1}; 127 delete $conn{$self->{remote_addr}}{$self*1};
128}
129
130# end of connection
131sub eoconn {
127 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 132 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
128} 133}
129 134
130sub slog { 135sub slog {
131 my $self = shift; 136 my $self = shift;
144 } 149 }
145 $res .= "\015\012"; 150 $res .= "\015\012";
146 151
147 $res .= $content if defined $content and $self->{method} ne "HEAD"; 152 $res .= $content if defined $content and $self->{method} ne "HEAD";
148 153
149 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 154 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";
150 155
151 $self->{written} += 156 $self->{written} +=
152 print {$self->{fh}} $res; 157 print {$self->{fh}} $res;
153} 158}
154 159
171sub err_blocked { 176sub err_blocked {
172 my $self = shift; 177 my $self = shift;
173 my $ip = $self->{remote_addr}; 178 my $ip = $self->{remote_addr};
174 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 179 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
175 180
176 Coro::Event::do_timer(after => 15); 181 Coro::Event::do_timer(after => 20*rand);
177 182
178 $self->err(401, "too many connections", 183 $self->err(401, "too many connections",
179 { 184 {
180 "Content-Type" => "text/html", 185 "Content-Type" => "text/html",
181 "Retry-After" => $::BLOCKTIME 186 "Retry-After" => $::BLOCKTIME,
187 "Warning" => "Please do NOT retry, you have been blocked",
188 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"",
182 }, 189 },
183 <<EOF); 190 <<EOF);
184<html><p> 191<html><p>
185You have been blocked because you opened too many connections. You 192You have been blocked because you opened too many connections. You
186may retry at</p> 193may retry at</p>
238 245
239 $self->{method} = $1; 246 $self->{method} = $1;
240 $self->{uri} = $2; 247 $self->{uri} = $2;
241 $self->{version} = $3; 248 $self->{version} = $3;
242 249
243 $3 eq "1.0" or $3 eq "1.1" 250 $3 =~ /^1\./
244 or $self->err(506, "http protocol version $3 not supported"); 251 or $self->err(506, "http protocol version $3 not supported");
245 252
246 # parse headers 253 # parse headers
247 { 254 {
248 my (%hdr, $h, $v); 255 my (%hdr, $h, $v);
264 271
265 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 272 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
266 273
267 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 274 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
268 275
276 eval {
269 $self->map_uri; 277 $self->map_uri;
270 $self->respond; 278 $self->respond;
279 };
280
281 die if $@ && !ref $@;
282
283 $self->eoconn;
271 284
272 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; 285 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
273 286
274 $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); 287 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
275 $fh->timeout($::PER_TIMEOUT); 288 $fh->timeout($::PER_TIMEOUT);
276 } 289 }
277} 290}
278 291
279# uri => path mapping 292# uri => path mapping
368 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 381 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
369 } else { 382 } else {
370 $ims < $self->{stat}[9] 383 $ims < $self->{stat}[9]
371 or $self->err(304, "not modified"); 384 or $self->err(304, "not modified");
372 385
373 if ($self->{method} eq "GET") {
374 if (-r "$path/index.html") { 386 if (-r "$path/index.html") {
375 $self->{path} .= "/index.html"; 387 $self->{path} .= "/index.html";
376 $self->handle_file; 388 $self->handle_file;
377 } else { 389 } else {
378 $self->handle_dir; 390 $self->handle_dir;
379 }
380 } 391 }
381 } 392 }
382 } elsif (-f _ && -r _) { 393 } elsif (-f _ && -r _) {
383 -x _ and $self->err(403, "forbidden"); 394 -x _ and $self->err(403, "forbidden");
384 $self->handle_file; 395 $self->handle_file;
420 goto ignore; 431 goto ignore;
421 } 432 }
422 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 433 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l;
423 } 434 }
424 $hdr->{"Content-Range"} = "bytes */$length"; 435 $hdr->{"Content-Range"} = "bytes */$length";
436 $hdr->{"Content-Length"} = $length;
437 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
425 $self->err(416, "not satisfiable", $hdr); 438 $self->err(416, "not satisfiable", $hdr, "");
426 439
427satisfiable: 440satisfiable:
428 # check for segmented downloads 441 # check for segmented downloads
429 if ($l && $::NO_SEGMENTED) { 442 if ($l && $::NO_SEGMENTED) {
430 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 443 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
455 open $fh, "<", $self->{path} 468 open $fh, "<", $self->{path}
456 or die "$self->{path}: late open failure ($!)"; 469 or die "$self->{path}: late open failure ($!)";
457 470
458 $h -= $l - 1; 471 $h -= $l - 1;
459 472
473 if (0) {
474 if ($l) {
475 sysseek $fh, $l, 0;
476 }
477 }
478
460 while ($h > 0) { 479 while ($h > 0) {
480 if (0) {
481 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
482 or last;
483 } else {
461 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 484 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
462 $buf, 0, sub { 485 $buf, 0, sub {
463 $r = $_[0]; 486 $r = $_[0];
464 $current->ready; 487 $current->ready;
465 }); 488 });
466 &Coro::schedule; 489 &Coro::schedule;
467 last unless $r; 490 last unless $r;
491 }
468 my $w = $self->{fh}->syswrite($buf) 492 my $w = $self->{fh}->syswrite($buf)
469 or last; 493 or last;
470 $::written += $w; 494 $::written += $w;
471 $self->{written} += $w; 495 $self->{written} += $w;
472 $l += $r; 496 $l += $r;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines