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