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

Hello all I am developing a webserver/firewall the firewall is depending on URL filtering using regex but in fact it isn't working with me when i use a simple reg like if($request =~ m/index\.php/) { print "something"; } even when i do print C "<html><body>...</body></html>; where C is my socket so how i can get regex from a file called rules which content regex rule in each line and match the URL with each rule from the text?? another question : How i can interprete php files when they're in my localhost document i know it's by getting a php interpreter but how to make it work I am getting the file from the url so how to intrpret it directly when the client ask for it and thank you ^^,

Replies are listed 'Best First'.
Re: Perl - webserver
by CountZero (Bishop) on Mar 25, 2008 at 21:52 UTC
    If you really want to write your own webserver, you could do worse than taking a look at HTTP::Server::Simple or HTTP::Server::Brick for inspiration.

    There does not seem to be firewall modules on CPAN, but there is a module called Hook::Filter which builds a "firewall" around subroutines and has a rule-based set-up with the rules found in a configuration file. Again you can "steal" probably some techniques from this module.

    Not being a PHP-person, I guess that you will somehow have to call the PHP interpreter with the PHP-file as an argument. It does seem possible to run a PHP program outside of a web-server: Stand-alone PHP.

    Update: Some more info on how to use PHP "stand-alone" is in this article:PHP Standalone Scripts. If the PHP-interpreter is in your PATH it could be as simple as: my $return_code = system('php my_standalone_script.php');

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      I am happy you consider my HTTP::Server::Brick a reasonable example of Perl webserver implementation - thanks!
      http://pastebin.com/f19194081 that's what i have done so far :s any idea ?
        I had a look at your code, but even when not being a networking-guru, I can see some problems:
        • You should use strict; and use warnings; to guard against silly mistakes, bad constructs and simple typos.
        • It is really a bare-bones, down-to-earth, ultra-simple type of webserver and as little as I know of it, web-servers are all but bare-bones, down-to-earth, ultra-simple kind of things. I'm not sure it is not full of ugly security holes, although you do some sanitizing of the request string.
        • It can handle only one connection at a time.
        • It can do no authentication / authorization.
        • ...
        Don't misunderstand me, I am not saying your code is not an honest try at writing (the beginnings of) a webserver, but these things are quite complicated by nature and without a good game-plan you are likely to bump into some issues later on and will have to start all over from scratch.

        As I am against re-inventing wheels, personally I would start with an existing design (see the links in my previous post) and adapt it as needed.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Perl - webserver
by apl (Monsignor) on Mar 25, 2008 at 19:43 UTC
    Showing us the (minimal) code that produces the problem would help as well. If you do post it, please bracket it with <code> and </code>.

    In any event, welcome to the Monastery!

Re: Perl - webserver
by stiller (Friar) on Mar 25, 2008 at 19:30 UTC
    It is very much not at all clear what you are doing, care to elaborate?

    Are you really developing a webserver?
    And a firewall?
    And a php interpreter?
    Good luck with that!