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.31 by root, Mon Aug 27 05:05:26 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
250 222
251 delete $blocked{$ip}; 223 delete $blocked{$ip};
252 } 224 }
253 225
254 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 226 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
227 my $delay = 120;
228 while (%{$conn{$ip}} > $::MAX_CONN_IP) {
229 if ($delay <= 0) {
255 $self->slog(2, "blocked ip $ip"); 230 $self->slog(2, "blocked ip $ip");
256 $self->err_blocked; 231 $self->err_blocked;
232 } else {
233 Coro::Event::do_timer(after => 3);
234 $delay -= 3;
235 }
236 }
257 } 237 }
258 238
259 $req =~ /^(?:\015\012)? 239 $req =~ /^(?:\015\012)?
260 (GET|HEAD) \040+ 240 (GET|HEAD) \040+
261 ([^\040]+) \040+ 241 ([^\040]+) \040+
287 267
288 $self->{h}{$h} = substr $v, 1 268 $self->{h}{$h} = substr $v, 1
289 while ($h, $v) = each %hdr; 269 while ($h, $v) = each %hdr;
290 } 270 }
291 271
272 # find out server name and port
273 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) {
274 $host = $1;
275 } else {
276 $host = $self->{h}{host};
277 }
278
279 if (defined $host) {
292 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 280 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80;
281 } else {
282 ($self->{server_port}, $host)
283 = unpack_sockaddr_in $self->{fh}->getsockname
284 or $self->err(500, "unable to get socket name");
285 $host = inet_ntoa $host;
286 }
287
288 $self->{server_name} = $host;
289
290 # remote id should be unique per user
291 $self->{remote_id} = $self->{remote_addr};
292
293 if (exists $self->{h}{"client-ip"}) {
294 $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]";
295 } elsif (exists $self->{h}{"x-forwarded-for"}) {
296 $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]";
297 }
293 298
294 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 299 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
295 300
296 eval { 301 eval {
297 $self->map_uri; 302 $self->map_uri;
300 305
301 $self->eoconn; 306 $self->eoconn;
302 307
303 die if $@ && !ref $@; 308 die if $@ && !ref $@;
304 309
305 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; 310 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1;
306 311
307 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
308 $fh->timeout($::PER_TIMEOUT); 312 $fh->timeout($::PER_TIMEOUT);
309 } 313 }
310} 314}
311 315
312# uri => path mapping 316# uri => path mapping
313sub map_uri { 317sub map_uri {
314 my $self = shift; 318 my $self = shift;
315 my $host = $self->{h}{host} || "default"; 319 my $host = $self->{server_name};
316 my $uri = $self->{uri}; 320 my $uri = $self->{uri};
317 321
318 # some massaging, also makes it more secure 322 # some massaging, also makes it more secure
319 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; 323 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge;
320 $uri =~ s%//+%/%g; 324 $uri =~ s%//+%/%g;
330 $self->{path} = "$::DOCROOT/$host$uri"; 334 $self->{path} = "$::DOCROOT/$host$uri";
331 335
332 $self->access_check; 336 $self->access_check;
333} 337}
334 338
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 { 339sub _cgi {
364 my $self = shift; 340 my $self = shift;
365 my $path = shift; 341 my $path = shift;
366 my $fh; 342 my $fh;
367 343
368 # no two-way xxx supported 344 # no two-way xxx supported
369 if (0 == fork) { 345 if (0 == fork) {
370 open STDOUT, ">&".fileno($self->{fh}); 346 open STDOUT, ">&".fileno($self->{fh});
371 if (chdir $::DOCROOT) { 347 if (chdir $::DOCROOT) {
372 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 348 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
373 $ENV{HTTP_HOST} = $self->server_host; 349 $ENV{HTTP_HOST} = $self->{server_name};
374 $ENV{HTTP_PORT} = $self->{server_host}; 350 $ENV{HTTP_PORT} = $self->{server_port};
375 $ENV{SCRIPT_NAME} = $self->{name}; 351 $ENV{SCRIPT_NAME} = $self->{name};
376 exec $path; 352 exec $path;
377 } 353 }
378 Coro::State::_exit(0); 354 Coro::State::_exit(0);
379 } else { 355 } else {
356 die;
380 } 357 }
358}
359
360sub server_hostport {
361 $_[0]{server_port} == 80
362 ? $_[0]{server_name}
363 : "$_[0]{server_name}:$_[0]{server_port}";
381} 364}
382 365
383sub respond { 366sub respond {
384 my $self = shift; 367 my $self = shift;
385 my $path = $self->{path}; 368 my $path = $self->{path};
395 378
396 if (-d _ && -r _) { 379 if (-d _ && -r _) {
397 # directory 380 # directory
398 if ($path !~ /\/$/) { 381 if ($path !~ /\/$/) {
399 # create a redirect to get the trailing "/" 382 # create a redirect to get the trailing "/"
400 my $host = $self->server_hostport; 383 # we don't try to avoid the :80
401 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 384 $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" });
402 } else { 385 } else {
403 $ims < $self->{stat}[9] 386 $ims < $self->{stat}[9]
404 or $self->err(304, "not modified"); 387 or $self->err(304, "not modified");
405 388
406 if (-r "$path/index.html") { 389 if (-r "$path/index.html") {
458 $self->err(416, "not satisfiable", $hdr, ""); 441 $self->err(416, "not satisfiable", $hdr, "");
459 442
460satisfiable: 443satisfiable:
461 # check for segmented downloads 444 # check for segmented downloads
462 if ($l && $::NO_SEGMENTED) { 445 if ($l && $::NO_SEGMENTED) {
446 my $delay = 180;
463 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 447 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
464 $self->err(400, "segmented downloads are not allowed"); 448 if ($delay <= 0) {
449 $self->err_segmented_download;
450 } else {
451 Coro::Event::do_timer(after => 3); $delay -= 3;
452 }
465 } 453 }
466 } 454 }
467 455
468 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 456 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
469 @code = (206, "partial content"); 457 @code = (206, "partial content");
497 while ($h > 0) { 485 while ($h > 0) {
498 if (0) { 486 if (0) {
499 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 487 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
500 or last; 488 or last;
501 } else { 489 } else {
490 undef $buf;
491 $aio_requests->down;
502 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 492 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
503 $buf, 0, sub { 493 $buf, 0, sub {
504 $r = $_[0]; 494 $r = $_[0];
505 $current->ready; 495 $current->ready;
506 }); 496 });
507 &Coro::schedule; 497 &Coro::schedule;
498 $aio_requests->up;
508 last unless $r; 499 last unless $r;
509 } 500 }
510 my $w = $self->{fh}->syswrite($buf) 501 my $w = $self->{fh}->syswrite($buf)
511 or last; 502 or last;
512 $::written += $w; 503 $::written += $w;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines