$SIG{CHLD} = 'IGNORE'; #### use IO::Socket; use Sys::Hostname; use strict; my $docroot = shift || die "Usage: $0 (document root path) [server port (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 = "404 Not Found

Error 404 File Not Found

The requested URL could not be located on this server."; my $error501 = "501 Not Implemented

Error 501 Method Not Implemented

The http method in your request is not implemented by this server."; my $otherheaders = "Server: Steve's 60-line web server${CRLF}Connection: 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$otherheaders$htmlheader" ."Content-Length: ".length($error404).$CRLF.$CRLF.$error404; print " 404\n"; } else { my $buffer = ''; while () { $buffer .= $_; } close GETFILE; my $contenttype = ($url =~ /\.html?$/) ? $htmlheader : $textheader; print $clienthandle "HTTP/1.1 200 OK$CRLF$otherheaders" ."Content-Length: ".length($buffer).$CRLF.$CRLF.$buffer; print " 200\n"; } } else { print $clienthandle "HTTP/1.1 501 Not Implemented$CRLF$otherheaders$htmlheader" ."Content-Length: ".length($error501).$CRLF.$CRLF.$error501; print " 501\n"; } close $serverhandle; close $clienthandle; exit; } close $serverhandle; ##
## ------------ :Wq Not an editor command: Wq