$SIG{CHLD} = 'IGNORE';
Now, as for writing your own web server... to do this as a learning exercise is cool and all, but I think you'd really be much better off using an already existing one (such as apache) for real use. It's easier to work around the problems of not being able to run the web server as root (which you really shouldn't be doing! there's a good reason apache wants you to setuid to an unpriveleged user!) than to work out all of the stuff that apache can do for you.
That all being said, though, here's a simple web server that I once wrote (as a learning exercise)... just to give you some more example code. I'm not claiming it to be super good or secure or anything... but it is a good example of forking in practice. I think it borrows pretty heavily from an example in the Perl Cookbook (which you might want to get a copy of).
use IO::Socket;
use Sys::Hostname;
use strict;
my $docroot = shift || die "Usage: $0 (document root path) [server por
+t (default 80)]\n";
my $port = shift || 80;
my $serverhandle = IO::Socket::INET->new(
Proto => 'tcp',
Listen => SOMAXCONN,
LocalAddr => Sys::Hostname::hostname(),
LocalPort => $port) or die "Could not listen on port $port... $!\n
+";
my $CRLF = "\015\012";
my $error404 = "<html><head><title>404 Not Found</title><body><h1>Erro
+r 404 File Not Found</h1>
The requested URL could not be located on this server.
+</body></html>";
my $error501 = "<html><head><title>501 Not Implemented</title><body><h
+1>Error 501 Method Not Implemented</h1>
The http method in your request is not implemented by
+this server.</body></html>";
my $otherheaders = "Server: Steve's 60-line web server${CRLF}Connectio
+n: close${CRLF}";
my $htmlheader = "Content-Type: text/html$CRLF";
my $textheader = "Content-Type: text/plain$CRLF";
$/ = "$CRLF$CRLF";
$SIG{CHLD} = 'IGNORE';
while (my $clienthandle = $serverhandle->accept) {
my $inpid = fork;
if (!defined $inpid) {
die "Could not fork: $!";
} elsif ($inpid) {
$clienthandle->close;
next;
}
my $name = $clienthandle->peerhost;
my $request = <$clienthandle>;
my ($method,$url,$httpversion,$headers) = ($request =~ /(\S+)\s+(\
+S+)\s+HTTP\/(\S+)$CRLF(.*)/s);
print localtime()." - $name - \"$method $url HTTP/$httpversion\" -
+";
if (uc $method eq 'GET') {
my $nofile = 0;
open GETFILE,$docroot.$url or $nofile = 1;
if ($nofile) {
print $clienthandle "HTTP/1.1 404 Not Found$CRLF$otherhead
+ers$htmlheader"
."Content-Length: ".length($error404).$CRLF.$CRLF.$err
+or404;
print " 404\n";
} else {
my $buffer = '';
while (<GETFILE>) {
$buffer .= $_;
}
close GETFILE;
my $contenttype = ($url =~ /\.html?$/) ? $htmlheader : $te
+xtheader;
print $clienthandle "HTTP/1.1 200 OK$CRLF$otherheaders"
."Content-Length: ".length($buffer).$CRLF.$CRLF.$buffe
+r;
print " 200\n";
}
} else {
print $clienthandle "HTTP/1.1 501 Not Implemented$CRLF$otherhe
+aders$htmlheader"
."Content-Length: ".length($error501).$CRLF.$CRLF.$error50
+1;
print " 501\n";
}
close $serverhandle;
close $clienthandle;
exit;
}
close $serverhandle;
Enjoy!
------------
:Wq
Not an editor command: Wq
|