ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/httpd.pl
(Generate patch)

Comparing cvsroot/Coro/myhttpd/httpd.pl (file contents):
Revision 1.27 by root, Sun Aug 19 23:57:52 2001 UTC vs.
Revision 1.30 by root, Sun Aug 26 14:55:46 2001 UTC

83use Convert::Scalar 'weaken'; 83use Convert::Scalar 'weaken';
84use Linux::AIO; 84use Linux::AIO;
85 85
86Linux::AIO::min_parallel $::AIO_PARALLEL; 86Linux::AIO::min_parallel $::AIO_PARALLEL;
87 87
88my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4;
89
88Event->io(fd => Linux::AIO::poll_fileno, 90Event->io(fd => Linux::AIO::poll_fileno,
89 poll => 'r', async => 1, 91 poll => 'r', async => 1,
90 cb => \&Linux::AIO::poll_cb); 92 cb => \&Linux::AIO::poll_cb);
91 93
92our %conn; # $conn{ip}{self} => connobj 94our %conn; # $conn{ip}{self} => connobj
143 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 145 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
144} 146}
145 147
146sub slog { 148sub slog {
147 my $self = shift; 149 my $self = shift;
148 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 150 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]");
149} 151}
150 152
151sub response { 153sub response {
152 my ($self, $code, $msg, $hdr, $content) = @_; 154 my ($self, $code, $msg, $hdr, $content) = @_;
153 my $res = "HTTP/1.1 $code $msg\015\012"; 155 my $res = "HTTP/1.1 $code $msg\015\012";
154 156
155 #$res .= "Connection: close\015\012"; 157 $self->{h}{connection} ||= $hdr->{Connection};
158
156 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 159 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
157 160
158 while (my ($h, $v) = each %$hdr) { 161 while (my ($h, $v) = each %$hdr) {
159 $res .= "$h: $v\015\012" 162 $res .= "$h: $v\015\012"
160 } 163 }
185 $self->response($code, $msg, $hdr, $content); 188 $self->response($code, $msg, $hdr, $content);
186 189
187 die bless {}, err::; 190 die bless {}, err::;
188} 191}
189 192
190sub err_blocked {
191 my $self = shift;
192 my $ip = $self->{remote_addr};
193 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
194
195 Coro::Event::do_timer(after => 20*rand);
196
197 $self->err(401, "too many connections",
198 {
199 "Content-Type" => "text/html",
200 "Retry-After" => $::BLOCKTIME,
201 "Warning" => "Please do NOT retry, you have been blocked",
202 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"",
203 },
204 <<EOF);
205<html>
206<head>
207<title>Too many connections</title>
208</head>
209<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000">
210
211<p>You have been blocked because you opened too many connections. You
212may retry at</p>
213
214 <p><blockquote>$time.</blockquote></p>
215
216<p>Until then, each new access will renew the block. You might want to have a
217look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p>
218
219</body></html>
220EOF
221}
222
223sub handle { 193sub handle {
224 my $self = shift; 194 my $self = shift;
225 my $fh = $self->{fh}; 195 my $fh = $self->{fh};
196
197 my $host;
226 198
227 $fh->timeout($::REQ_TIMEOUT); 199 $fh->timeout($::REQ_TIMEOUT);
228 while() { 200 while() {
229 $self->{reqs}++; 201 $self->{reqs}++;
230 202
287 259
288 $self->{h}{$h} = substr $v, 1 260 $self->{h}{$h} = substr $v, 1
289 while ($h, $v) = each %hdr; 261 while ($h, $v) = each %hdr;
290 } 262 }
291 263
264 # find out server name and port
265 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) {
266 $host = $1;
267 } else {
268 $host = $self->{h}{host};
269 }
270
271 if (defined $host) {
292 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 272 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80;
273 } else {
274 ($self->{server_port}, $host)
275 = unpack_sockaddr_in $self->{fh}->getsockname
276 or $self->err(500, "unable to get socket name");
277 $host = inet_ntoa $host;
278 }
279
280 $self->{server_name} = $host;
281
282 # remote id should be unique per user
283 $self->{remote_id} = $self->{remote_addr};
284
285 if (exists $self->{h}{"client-ip"}) {
286 $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]";
287 } elsif (exists $self->{h}{"x-forwarded-for"}) {
288 $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]";
289 }
293 290
294 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 291 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
295 292
296 eval { 293 eval {
297 $self->map_uri; 294 $self->map_uri;
300 297
301 $self->eoconn; 298 $self->eoconn;
302 299
303 die if $@ && !ref $@; 300 die if $@ && !ref $@;
304 301
305 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; 302 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1;
306 303
307 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
308 $fh->timeout($::PER_TIMEOUT); 304 $fh->timeout($::PER_TIMEOUT);
309 } 305 }
310} 306}
311 307
312# uri => path mapping 308# uri => path mapping
313sub map_uri { 309sub map_uri {
314 my $self = shift; 310 my $self = shift;
315 my $host = $self->{h}{host} || "default"; 311 my $host = $self->{server_name};
316 my $uri = $self->{uri}; 312 my $uri = $self->{uri};
317 313
318 # some massaging, also makes it more secure 314 # some massaging, also makes it more secure
319 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; 315 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge;
320 $uri =~ s%//+%/%g; 316 $uri =~ s%//+%/%g;
330 $self->{path} = "$::DOCROOT/$host$uri"; 326 $self->{path} = "$::DOCROOT/$host$uri";
331 327
332 $self->access_check; 328 $self->access_check;
333} 329}
334 330
335sub server_address {
336 my $self = shift;
337 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname
338 or $self->err(500, "unable to get socket name");
339 ((inet_ntoa $iaddr), $port);
340}
341
342sub server_host {
343 my $self = shift;
344 if (exists $self->{h}{host}) {
345 return $self->{h}{host};
346 } else {
347 return (($self->server_address)[0]);
348 }
349}
350
351sub server_hostport {
352 my $self = shift;
353 my ($host, $port);
354 if (exists $self->{h}{host}) {
355 ($host, $port) = ($self->{h}{host}, $self->{server_port});
356 } else {
357 ($host, $port) = $self->server_address;
358 }
359 $port = $port == 80 ? "" : ":$port";
360 $host.$port;
361}
362
363sub _cgi { 331sub _cgi {
364 my $self = shift; 332 my $self = shift;
365 my $path = shift; 333 my $path = shift;
366 my $fh; 334 my $fh;
367 335
368 # no two-way xxx supported 336 # no two-way xxx supported
369 if (0 == fork) { 337 if (0 == fork) {
370 open STDOUT, ">&".fileno($self->{fh}); 338 open STDOUT, ">&".fileno($self->{fh});
371 if (chdir $::DOCROOT) { 339 if (chdir $::DOCROOT) {
372 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 340 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
373 $ENV{HTTP_HOST} = $self->server_host; 341 $ENV{HTTP_HOST} = $self->{server_name};
374 $ENV{HTTP_PORT} = $self->{server_host}; 342 $ENV{HTTP_PORT} = $self->{server_port};
375 $ENV{SCRIPT_NAME} = $self->{name}; 343 $ENV{SCRIPT_NAME} = $self->{name};
376 exec $path; 344 exec $path;
377 } 345 }
378 Coro::State::_exit(0); 346 Coro::State::_exit(0);
379 } else { 347 } else {
348 die;
380 } 349 }
350}
351
352sub server_hostport {
353 $_[0]{server_port} == 80
354 ? $_[0]{server_name}
355 : "$_[0]{server_name}:$_[0]{server_port}";
381} 356}
382 357
383sub respond { 358sub respond {
384 my $self = shift; 359 my $self = shift;
385 my $path = $self->{path}; 360 my $path = $self->{path};
395 370
396 if (-d _ && -r _) { 371 if (-d _ && -r _) {
397 # directory 372 # directory
398 if ($path !~ /\/$/) { 373 if ($path !~ /\/$/) {
399 # create a redirect to get the trailing "/" 374 # create a redirect to get the trailing "/"
400 my $host = $self->server_hostport; 375 # we don't try to avoid the :80
401 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 376 $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" });
402 } else { 377 } else {
403 $ims < $self->{stat}[9] 378 $ims < $self->{stat}[9]
404 or $self->err(304, "not modified"); 379 or $self->err(304, "not modified");
405 380
406 if (-r "$path/index.html") { 381 if (-r "$path/index.html") {
458 $self->err(416, "not satisfiable", $hdr, ""); 433 $self->err(416, "not satisfiable", $hdr, "");
459 434
460satisfiable: 435satisfiable:
461 # check for segmented downloads 436 # check for segmented downloads
462 if ($l && $::NO_SEGMENTED) { 437 if ($l && $::NO_SEGMENTED) {
438 my $delay = 180;
463 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 439 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
464 $self->err(400, "segmented downloads are not allowed"); 440 if ($delay <= 0) {
441 $self->err_segmented_download;
442 } else {
443 Coro::Event::do_timer(after => 3); $delay -= 3;
444 }
465 } 445 }
466 } 446 }
467 447
468 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 448 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
469 @code = (206, "partial content"); 449 @code = (206, "partial content");
497 while ($h > 0) { 477 while ($h > 0) {
498 if (0) { 478 if (0) {
499 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 479 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
500 or last; 480 or last;
501 } else { 481 } else {
482 undef $buf;
483 $aio_requests->down;
502 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 484 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
503 $buf, 0, sub { 485 $buf, 0, sub {
504 $r = $_[0]; 486 $r = $_[0];
505 $current->ready; 487 $current->ready;
506 }); 488 });
507 &Coro::schedule; 489 &Coro::schedule;
490 $aio_requests->up;
508 last unless $r; 491 last unless $r;
509 } 492 }
510 my $w = $self->{fh}->syswrite($buf) 493 my $w = $self->{fh}->syswrite($buf)
511 or last; 494 or last;
512 $::written += $w; 495 $::written += $w;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines