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.26 by root, Sun Aug 19 22:59:35 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
83Event->add_hooks(prepare => sub {
84 &Coro::cede;
85});
86
87our %conn; # $conn{ip}{fh} => connobj 83our %conn; # $conn{ip}{self} => connobj
84our %uri; # $uri{ip}{uri}{self}
88our %blocked; 85our %blocked;
89our %mimetype; 86our %mimetype;
90 87
91sub read_mimetypes { 88sub read_mimetypes {
92 local *M; 89 local *M;
125sub DESTROY { 122sub DESTROY {
126 my $self = shift; 123 my $self = shift;
127 124
128 $::conns--; 125 $::conns--;
129 126
127 $self->eoconn;
130 delete $conn{$self->{remote_addr}}{$self*1}; 128 delete $conn{$self->{remote_addr}}{$self*1};
129}
130
131# end of connection
132sub eoconn {
133 my $self = shift;
131 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 134 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
132} 135}
133 136
134sub slog { 137sub slog {
135 my $self = shift; 138 my $self = shift;
148 } 151 }
149 $res .= "\015\012"; 152 $res .= "\015\012";
150 153
151 $res .= $content if defined $content and $self->{method} ne "HEAD"; 154 $res .= $content if defined $content and $self->{method} ne "HEAD";
152 155
153 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 156 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";
154 157
155 $self->{written} += 158 $self->{written} +=
156 print {$self->{fh}} $res; 159 print {$self->{fh}} $res;
157} 160}
158 161
175sub err_blocked { 178sub err_blocked {
176 my $self = shift; 179 my $self = shift;
177 my $ip = $self->{remote_addr}; 180 my $ip = $self->{remote_addr};
178 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 181 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
179 182
180 Coro::Event::do_timer(after => 15); 183 Coro::Event::do_timer(after => 20*rand);
181 184
182 $self->err(401, "too many connections", 185 $self->err(401, "too many connections",
183 { 186 {
184 "Content-Type" => "text/html", 187 "Content-Type" => "text/html",
185 "Retry-After" => $::BLOCKTIME 188 "Retry-After" => $::BLOCKTIME,
189 "Warning" => "Please do NOT retry, you have been blocked",
190 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"",
186 }, 191 },
187 <<EOF); 192 <<EOF);
188<html><p> 193<html><p>
189You have been blocked because you opened too many connections. You 194You have been blocked because you opened too many connections. You
190may retry at</p> 195may retry at</p>
242 247
243 $self->{method} = $1; 248 $self->{method} = $1;
244 $self->{uri} = $2; 249 $self->{uri} = $2;
245 $self->{version} = $3; 250 $self->{version} = $3;
246 251
247 $3 eq "1.0" or $3 eq "1.1" 252 $3 =~ /^1\./
248 or $self->err(506, "http protocol version $3 not supported"); 253 or $self->err(506, "http protocol version $3 not supported");
249 254
250 # parse headers 255 # parse headers
251 { 256 {
252 my (%hdr, $h, $v); 257 my (%hdr, $h, $v);
268 273
269 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 274 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
270 275
271 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 276 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
272 277
278 eval {
273 $self->map_uri; 279 $self->map_uri;
274 $self->respond; 280 $self->respond;
281 };
282
283 $self->eoconn;
284
285 die if $@ && !ref $@;
275 286
276 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; 287 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
277 288
278 $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); 289 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
279 $fh->timeout($::PER_TIMEOUT); 290 $fh->timeout($::PER_TIMEOUT);
280 } 291 }
281} 292}
282 293
283# uri => path mapping 294# uri => path mapping
372 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 383 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
373 } else { 384 } else {
374 $ims < $self->{stat}[9] 385 $ims < $self->{stat}[9]
375 or $self->err(304, "not modified"); 386 or $self->err(304, "not modified");
376 387
377 if ($self->{method} eq "GET") {
378 if (-r "$path/index.html") { 388 if (-r "$path/index.html") {
379 $self->{path} .= "/index.html"; 389 $self->{path} .= "/index.html";
380 $self->handle_file; 390 $self->handle_file;
381 } else { 391 } else {
382 $self->handle_dir; 392 $self->handle_dir;
383 }
384 } 393 }
385 } 394 }
386 } elsif (-f _ && -r _) { 395 } elsif (-f _ && -r _) {
387 -x _ and $self->err(403, "forbidden"); 396 -x _ and $self->err(403, "forbidden");
388 $self->handle_file; 397 $self->handle_file;
421 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 430 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
422 } else { 431 } else {
423 ($l, $h) = (0, $length - 1); 432 ($l, $h) = (0, $length - 1);
424 goto ignore; 433 goto ignore;
425 } 434 }
426 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 435 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
427 } 436 }
428 $hdr->{"Content-Range"} = "bytes */$length"; 437 $hdr->{"Content-Range"} = "bytes */$length";
438 $hdr->{"Content-Length"} = $length;
439 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
429 $self->err(416, "not satisfiable", $hdr); 440 $self->err(416, "not satisfiable", $hdr, "");
430 441
431satisfiable: 442satisfiable:
432 # check for segmented downloads 443 # check for segmented downloads
433 if ($l && $::NO_SEGMENTED) { 444 if ($l && $::NO_SEGMENTED) {
434 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 445 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"); 446 $self->err(400, "segmented downloads are not allowed");
438 } 447 }
439 } 448 }
440 449
441 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 450 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
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