Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Replace Apache

by Weathros (Novice)
on Mar 04, 2003 at 14:40 UTC ( [id://240360]=obfuscated: print w/replies, xml ) Need Help??

I've written a small replacement for Apache in my lunch break today. It's still missing support for a few things, but it's getting there!
use IO::Socket;$R="/tmp/";$P=1280;$A="HTTP/1.1 200 OK\nServer: PimpHTT +PD\nConnection: close\n\n";$S=new IO::Socket::INET->new(Listen=>1,Loc +alPort=>$P)||die("Can't listen: $!");while($C=$S->accept()){while($D= +<$C>){$F=$1 if($D=~/^GET\s+(\S+)/i);last if($D=~/^\r?\s*\r?$/);}$F=~s +/%([0-9A-Fa-f][0-9A-Fa-f])/hex($1)/g;if($F=~/\.\.\//){$A="INVALID URL +"}else{if(open(I,"<$R$F")){while(<I>){$A.=$_;};close(I);}else{$A="$R$ +F:$!"}};print $C $A;$C->close;}

Replies are listed 'Best First'.
Re: Replace Apache
by hardburn (Abbot) on Mar 04, 2003 at 14:58 UTC

    You mean you wrote a quick-and-dirty HTTP server. That's different from 'replacing Apache' (:

    But then, I would question the sanity of any admin that runs their webserver off a obfu Perl script.

    ----
    Reinvent a rounder wheel.

    Note: All code is untested, unless otherwise stated

Re: Replace Apache
by zeno (Friar) on Mar 07, 2003 at 21:46 UTC

    Nicely done. I appreciate your sense of humor. I realize you are joking when you say "replacing apache". Here's my attempt to deobfuscate it.

    <SPOILER FOLLOWS>
    # 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(<INFILE>) { $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; }
    -- Zeno - Barcelona Perl Mongers http://barcelona.pm.org http://www.javamonks.org

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://240360]
Approved by erzuuli
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found