-=Mizo=- has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use Socket; use Carp; use FileHandle; # (1) use port 80 by default, unless overridden on command line $port = 80; # (2) create local TCP socket and set it to listen for connections socket(S,PF_INET,SOCK_STREAM,getprotobyname('tcp')) || die ("ERROR:Con +nection Creation Failed\n"); setsockopt(S, SOL_SOCKET, SO_REUSEADDR, 1) || die ("ERROR: setsockopt( +) Failed\n"); bind(S, sockaddr_in($port, INADDR_ANY)) || die ("ERROR: bind() Failed\ +n"); listen(S, SOCKMAXCONN) || die ("ERROR: listen() Failed\n"); # (3) print a startup message printf(" <<<Type-O-Serve Accepting on Port % d>>>\n\n",$port); while (1) { # (4) wait for a connection C $cport_caddr = accept(C, S); ($cport,$caddr) = sockaddr_in($cport_caddr); C->autoflush(1); # (5) print who the connection is from $cname = gethostbyaddr($caddr,AF_INET); print "Host: $cname\n"; # (6) read request msg until blank line, and print on screen $temp = <C>; ($method, $request, $http) = split(" ",$temp); # check whether it's / in which case make it index.html if($request eq "/"){ $request = "/index.html"; } # remove .. to filter directory traversal attacks $request =~ s/\.\.//; #check file exists if ( -e "./htdocs/$request" ){ print "200 - $cname $request\n"; $page=""; open FILE, "<./htdocs/$request"; while(<FILE>){ $page .= $_; } close(FILE); print C "HTTP/1.0 200 OK\r\n"; print C "Content-type: text/html\r\n"; print C "\r\n"; print C "$page"; }else{ print "404 - $cname $request\n"; print C "HTTP/1.0 404\r\n"; print C "Content-type: text/html\r\n"; print C "\r\n"; print C "<html><head><title>404 Page Not Found</title><head>"; print C "<body>404 - $request not found</body></html>"; } close(C); }
This is a very simple webserver i know that :s but I am joining a compatition and i want to make a simple WAF(Web Application Firewall) so I made the webserver just to make it as a enviroment to make the firewall work the firewall idea is to filter URL content like if some one wrote http://localhost/index.php?p=' He'll be rejected coz there is a rule in a blacklist file doesn't allow this kind of requets so how to get the rules from a text file and match them with the URL?
the rules are Regex rules the idea is like mod_security and if you couldn't make it get the rules from a file just give me a simple code for doing it directly from the code .
please can anyone help me in doing this i really need it i have to give it to them tomorrow

Replies are listed 'Best First'.
Re: webserver - firewall [simple]
by Fletch (Bishop) on Mar 27, 2008 at 03:48 UTC

    A decent webserver, much like Rome, probably can't be built in a day. And writing one from scratch isn't "simple" by any stretch of the imagination. You'd be much better served using something from CPAN as has already been suggested the last time.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: webserver - firewall [simple]
by NetWallah (Canon) on Mar 27, 2008 at 05:28 UTC
    Hook::Filter::RulePool seems simple and feature-sufficient to use for this purpose (Untested - I just searched for modules contain "Rules").

    Update:CountZero's response to your previous query apparently had an almost identical suggestion.

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

      To CountZero's reply he linked to a page on pastebin holding "what he had so far", wich turns out to be an unattributed copy-paste from "HTTP - The definitive guide" and is available as an excercept from the book on Safari. This guy will not do any work or thinking on his own, and he will not attribute any help.
Re: webserver - firewall [simple]
by Anonymous Monk on Mar 27, 2008 at 04:12 UTC
    Don't the rules of the compatition say the work has to be yours