# Modified a little to get the spacing right # and to use strict. # Some of this code looks like an example from # The Perl Cookbook by Tom Christiansen p.606 use strict; use IO::Socket; # Specify the server home directory, port, # and "page found" status my $home_dir="/tmp/"; my $port=1280; my $response="HTTP/1.1 200 OK\nServer: "; $response .= "PimpHTTPD\nConnection: close\n\n"; my $file_name; # Open a listener server on the port we specified my $server=new IO::Socket::INET->new(Listen=>1, LocalPort=>$port)||die("Can't listen: $!"); # Accept equests from the client while(my $client=$server->accept()) { # Read client requests into scalar while(my $request=<$client>) { # Parse the file name to read in from the # GET request $file_name=$1 if($request=~/^GET\s+(\S+)/i); # Request is finished if there is a blank line last if($request=~/^\r?\s*\r?$/); } # Unescape escaped characters in the file name $file_name=~s/%([0-9A-Fa-f][0-9A-Fa-f])/hex($1)/g; # Ignore requests that try to read from the # parent directory. if($file_name =~ /\.\.\//) { $response="INVALID URL" } else { # It's a good file name-- let's read it. # Open the in file and add it to the response. if(open(INFILE,"<$home_dir$file_name")) { while() { $response.=$_; } close(INFILE); } else { # If we can't open the file, show the error. $response="$home_dir$file_name:$!" } } # Output the response to the client. print $client $response; # Close the client. $client->close; }