#!/usr/bin/perl -w

use Socket;

$port=8080;
$proto=getprotobyname('tcp');
socket(Server, PF_INET, SOCK_STREAM, $proto) or die;
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) or die;
bind(Server, sockaddr_in($port, INADDR_ANY)) or die;
listen(Server, SOMAXCONN) or die;

while(1) {
    accept(Client, Server);
    $foo=<Client>;
    @bar=split(/ /, $foo);
    if(uc($bar[0]) eq "GET") {
    	if ($bar[1] eq "/") { $bar[1]="/index.html"; }
    	$_ = $bar[1];
    	s/\.\.//g;
    	$bar[1]=".$_";
    	if ( open(BAZ, $bar[1]) ) {
    		while(<BAZ>) { print Client; }
    		close BAZ;
    	} else { print Client "404 / File not found\r\n"; }
    } else { 
    	print Client "501 / I don't know how to do what you're asking!\r\n";
    }
    close Client;
}