下面是一个 Perl 脚本,其唯一目的是接收 HTTP 请求,并输出“503 Service Unavailable”和一条短消息。它工作正常,但在许多情况下,连接会重置,这会导致浏览器显示错误消息。这是在 Win32 上。我不知道它有什么问题。
#!/usr/local/bin/perl
use strict;
use IO::Socket::INET;
my $f = join('', <DATA>);
$SIG{CHLD} = 'IGNORE';
my $sock = IO::Socket::INET->new(ReuseAddr => 1, Listen => 512, LocalPort => 80, LocalHost => '0.0.0.0', Proto => 'tcp');
die "Cant't create a listening socket: $@" unless $sock;
while (my $connection = $sock->accept) {
my $child;
die "Can't fork: $!" unless defined ($child = fork());
if ($child == 0) {
#print "Child $$ running. ";
$sock->close;
do_it($connection);
#print "Child $$ exiting.\n";
exit 0;
} else {
print "Connection from ".$connection->peerhost."\n";
$connection->close();
}
}
sub do_it {
my $socket = shift;
my $pr = print $socket $f;
if (!$pr) {
$socket->close();
exit(0);
}
}
__DATA__
HTTP/1.1 503 Service Unavailable
Date: Mon, 12 Mar 2009 19:12:16 GMT
Server: Down
Connection: close
Content-Type: text/html
<html>
<head><title>Down for Maintenance</title></head>
<body>
<h2>Down for Maintenance</h2>
<p>The site is down for maintenance. It will be online again shortly.</p>
</body>
</html>