Below is a Perl script whose sole purpose is to receive an HTTP request, and spit out "503 Service Unavailable" and a short message. It works fine, except in many cases, the connection resets, which causes the browser to show an error message. This is on Win32. I have no idea what's wrong with it.
#!/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>