shug94 has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys,

I am running an Apache HTTP Server, and I am using Perl 5.8 from ActivePerl.

Is it possible to redirect ALL GET requests to a particular Perl script? I want all POST requests to go to one Perl script, all GET requests to go to another, and all PUT requests to go to a third Perl script.

I suppose there may be an Apache server configuration way of doing this, but I think there should be a Perl way of doing it too.

If I have a Perl script which all requests are redirected to, then can I somehow gain access to the Header content of the HTTP Request which resulted in the script being called. If I can pull the method (POST, GET, PUT, etc) from this then I can just delegate to other perl scripts.

Any ideas, or suggestions?

Cheers,

Shug
  • Comment on HTTP Header retrieval from Perl on Apache Server

Replies are listed 'Best First'.
Re: HTTP Header retrieval from Perl on Apache Server
by Utilitarian (Vicar) on Sep 02, 2009 at 10:10 UTC
      It's going to be a lot faster and more efficient to use Apache to redirect the requests.

      That being said, the request method is always available in $ENV{REQUEST_METHOD}, or in the CGI module as $query->request_method().
Re: HTTP Header retrieval from Perl on Apache Server
by Sewi (Friar) on Sep 02, 2009 at 12:41 UTC
    I'm using a small tool when running in issues like this. It has been very helpful and could easily adapted to most environments:
    #!/usr/bin/perl print "Content-type: text/html\n\n"; for (sort(keys(%ENV))) { print "$_ = $ENV{$_}<br>\n"; }
    Apache used to write the method used to a enviroment variable. I never tried Apache on Windows for this, but you could test it easily.

    Once you got the method, all you need is a simple if...

Re: HTTP Header retrieval from Perl on Apache Server
by bluescreen (Friar) on Sep 03, 2009 at 00:33 UTC
    Use mod_perl, mode perl allows you to define an script that will handle all request to an specific virtual directory. when a new request comes along it call YourModule::handler method and there you can determine if the nature of the call GET/POST and redirect to any module you want.