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.23 by root, Sat Aug 18 02:58:38 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;
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);
271 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 274 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
272 275
273 $self->map_uri; 276 $self->map_uri;
274 $self->respond; 277 $self->respond;
275 278
279 $self->eoconn;
280
276 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; 281 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
277 282
278 $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); 283 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
279 $fh->timeout($::PER_TIMEOUT); 284 $fh->timeout($::PER_TIMEOUT);
280 } 285 }
281} 286}
282 287
283# uri => path mapping 288# uri => path mapping
424 goto ignore; 429 goto ignore;
425 } 430 }
426 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 431 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l;
427 } 432 }
428 $hdr->{"Content-Range"} = "bytes */$length"; 433 $hdr->{"Content-Range"} = "bytes */$length";
434 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
429 $self->err(416, "not satisfiable", $hdr); 435 $self->err(416, "not satisfiable", $hdr);
430 436
431satisfiable: 437satisfiable:
432 # check for segmented downloads 438 # check for segmented downloads
433 if ($l && $::NO_SEGMENTED) { 439 if ($l && $::NO_SEGMENTED) {
459 open $fh, "<", $self->{path} 465 open $fh, "<", $self->{path}
460 or die "$self->{path}: late open failure ($!)"; 466 or die "$self->{path}: late open failure ($!)";
461 467
462 $h -= $l - 1; 468 $h -= $l - 1;
463 469
470 if (0) {
471 if ($l) {
472 sysseek $fh, $l, 0;
473 }
474 }
475
464 while ($h > 0) { 476 while ($h > 0) {
477 if (0) {
478 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
479 or last;
480 } else {
465 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 481 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
466 $buf, 0, sub { 482 $buf, 0, sub {
467 $r = $_[0]; 483 $r = $_[0];
468 $current->ready; 484 $current->ready;
469 }); 485 });
470 &Coro::schedule; 486 &Coro::schedule;
471 last unless $r; 487 last unless $r;
488 }
472 my $w = $self->{fh}->syswrite($buf) 489 my $w = $self->{fh}->syswrite($buf)
473 or last; 490 or last;
474 $::written += $w; 491 $::written += $w;
475 $self->{written} += $w; 492 $self->{written} += $w;
476 $l += $r; 493 $l += $r;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines