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

In httpd.conf, lets say I have this:
<Location /foo> SetHandler perl-script PerlHandler My::Apache::Handler </Location>
How can I get the "/foo" value via request object? I'm lost in mod_perl docs :p I'm using (testing) mod_perl2 under win32...

Replies are listed 'Best First'.
Re: How to get the value of <Location> directive (mod_perl)?
by ChemBoy (Priest) on Dec 10, 2006 at 18:17 UTC

    I'm not sure this is what you're after, but it seems like it might be a problem I had recently: if your module is handling all of the requests below a certain root URI, and you're taking actions based on the path after that root, how do you know (without hardcoding it or some other such evil tactic) how much of the uri is what you're supposed to be processing?

    My solution, which may or may not have been a good one, was to do the Apache configuration in Perl. The root URL for each handler is set in a constants file, and the configuration section refers to that constant when setting up the server. (I can't remember precisely the syntax, but it's roughly $Location{$MY::ROOT_URL} = { PerlResponseHandler=>'My::Apache::Handler', SetHandler=>"perl-script'};) Then the handler itself refers to that same constant when determining its root path: (my $path = $r->uri) =~ s/^\Q$MY::ROOT_URL//;.

    If that wasn't the problem you were trying to solve, my apologies, but it seemed like a decent shot. :-)



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

      It looks like you've revealed my evil action :) But, it seems like your method will also need hardcoding at least in two places. I'm currently using PerlSetVar to set the location:
      <Location /foo> SetHandler perl-script PerlSetVar NAME "/foo" PerlHandler My::Apache::Handler </Location>
      It looks like Apache2::Directive may help, but I still need to know what the location is:
      use Apache2::Directive (); my $tree = Apache2::Directive::conftree(); my $node = $tree->lookup('Location', '/foo/');
Re: How to get the value of <Location> directive (mod_perl)?
by perrin (Chancellor) on Dec 10, 2006 at 17:53 UTC
    Is $r->uri() what you're looking for?
      No. If the requested uri is "/foo/bar/baz" then $r->uri == "/foo/bar/baz" not "/foo"
        I don't think you can get what you're asking for, because I don't think apache provides it. When I want to know which location I matched, I do something like this:
        <Location /blah> PerlSetVar matched "blah" </Location>
        And then in my handler:
        if ($r->dir_config('matched') eq 'blah') {
        That serves my needs. However, if you want a definitive answer about whether this can be retrieved directly from apache or not, you need to ask on the mod_perl mailing list.
        I think your looking into APR::URI.
        Haven't had much time to play with mod_perl2, but I think your looking at rpath and not path.

        Now I'm off to play with mod_perl2 instead of working on the real work... thanks :)

        --

        Even smart people are dumb in most things...
Re: How to get the value of <Location> directive (mod_perl)?
by Burak (Chaplain) on Dec 27, 2006 at 12:09 UTC
    D'oh! You'll be surprised, but it looks like there is a $r->location method for this job :) Problem solved...