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.18 by root, Wed Aug 15 03:13:36 2001 UTC vs.
Revision 1.22 by root, Fri Aug 17 04:10: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);
82my $scheduler = Event->idle(
83 max => 0, min => 0, prio => 4, parked => 1,
84 cb => \&Coro::schedule);
82 85
83our %conn; # $conn{ip}{fh} => connobj 86our %conn; # $conn{ip}{fh} => connobj
84our %blocked; 87our %blocked;
85our %mimetype; 88our %mimetype;
86 89
121sub DESTROY { 124sub DESTROY {
122 my $self = shift; 125 my $self = shift;
123 126
124 $::conns--; 127 $::conns--;
125 128
129 $self->eoconn;
126 delete $conn{$self->{remote_addr}}{$self*1}; 130 delete $conn{$self->{remote_addr}}{$self*1};
131}
132
133# end of connection
134sub eoconn {
127 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 135 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
128} 136}
129 137
130sub slog { 138sub slog {
131 my $self = shift; 139 my $self = shift;
171sub err_blocked { 179sub err_blocked {
172 my $self = shift; 180 my $self = shift;
173 my $ip = $self->{remote_addr}; 181 my $ip = $self->{remote_addr};
174 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 182 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
175 183
176 Coro::Event::do_timer(after => 15); 184 Coro::Event::do_timer(after => 20*rand);
177 185
178 $self->err(401, "too many connections", 186 $self->err(401, "too many connections",
179 { 187 {
180 "Content-Type" => "text/html", 188 "Content-Type" => "text/html",
181 "Retry-After" => $::BLOCKTIME 189 "Retry-After" => $::BLOCKTIME,
190 "Warning" => "Please do NOT retry, you have been blocked",
191 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"",
182 }, 192 },
183 <<EOF); 193 <<EOF);
184<html><p> 194<html><p>
185You have been blocked because you opened too many connections. You 195You have been blocked because you opened too many connections. You
186may retry at</p> 196may retry at</p>
238 248
239 $self->{method} = $1; 249 $self->{method} = $1;
240 $self->{uri} = $2; 250 $self->{uri} = $2;
241 $self->{version} = $3; 251 $self->{version} = $3;
242 252
243 $3 eq "1.0" or $3 eq "1.1" 253 $3 =~ /^1\./
244 or $self->err(506, "http protocol version $3 not supported"); 254 or $self->err(506, "http protocol version $3 not supported");
245 255
246 # parse headers 256 # parse headers
247 { 257 {
248 my (%hdr, $h, $v); 258 my (%hdr, $h, $v);
267 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 277 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
268 278
269 $self->map_uri; 279 $self->map_uri;
270 $self->respond; 280 $self->respond;
271 281
282 $self->eoconn;
283
272 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; 284 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
273 285
274 $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); 286 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
275 $fh->timeout($::PER_TIMEOUT); 287 $fh->timeout($::PER_TIMEOUT);
276 } 288 }
277} 289}
278 290
279# uri => path mapping 291# uri => path mapping
420 goto ignore; 432 goto ignore;
421 } 433 }
422 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 434 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l;
423 } 435 }
424 $hdr->{"Content-Range"} = "bytes */$length"; 436 $hdr->{"Content-Range"} = "bytes */$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) {
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;
488 $scheduler->now;
465 }); 489 });
466 &Coro::schedule; 490 &Coro::schedule;
467 last unless $r; 491 last unless $r;
492 }
468 my $w = $self->{fh}->syswrite($buf) 493 my $w = $self->{fh}->syswrite($buf)
469 or last; 494 or last;
470 $::written += $w; 495 $::written += $w;
471 $self->{written} += $w; 496 $self->{written} += $w;
472 $l += $r; 497 $l += $r;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines