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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines