iq-0 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm working on a sort of fancy directory indexer, which runs with apache. The problem is that I don't know where the requested directory is situated on the local filesystem. The default (relative to the document root) is easy, but for the extra directories (eg. the icons directory, or user directories) it becomes a bit tricky. For users I could make the assumption that a users directory would be ~user/public_html, but that is not always true, for the others it would be pure hacks. Is there some way to ask Apache where an URI would normally reside on the filesystem?

Replies are listed 'Best First'.
Re: Physical location of a requested URI
by iq-0 (Initiate) on Mar 06, 2003 at 14:59 UTC
    After a lot of good coffee an better guinness (just joking) and after spitting through some massive ammounts of docs, we finally found the right Apache module calls.
    use Apache; my $r = shift; my $uri = $ENV{REQUEST_URI}; my $path = $r->lookup_uri($uri)->filename;
    note: we couldn't use the 'pwd' because we use this as a default in the apache config:
    DirectoryIndex    index.html index.mpl /cgi-bin/listing.mpl
    so pwd would be the cgi-bin directory and not the requested directory

    udpate (broquaint): s|<(/)?pre>|<${1}code>|g

•Re: Physical location of a requested URI
by merlyn (Sage) on Mar 06, 2003 at 15:28 UTC
    You could write a simple CGI that displays $ENV{PATH_TRANSLATED}, which is the filesystem translated path for a given PATH_INFO. Then feed your requested URLs one at a time as the path-info, like get "http://your-server/cgi/your-script/path/you/want/to/translate".

    But you should also note that a URI does not necessarily have a corresponding path. There are many URLs on stonehenge.com for example that do not correspond to filesystem resources. So be sure you're asking the right question before you aim for answers.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Well the scripts is called by apache when it is searching a directory for an index file. So we can pretty much rest assure that it is a directory and that we may display it.
Re: Physical location of a requested URI
by Anonymous Monk on Mar 06, 2003 at 14:15 UTC

    Barring any good answer...

    In a pinch you could parse the config file. Should be easy enough; the format is straightforward and well- documented. However, different systems put it in different places, so you may have to change one line of your program any time you move to a new server.

Re: Physical location of a requested URI
by cfreak (Chaplain) on Mar 06, 2003 at 14:33 UTC

    Here's a CPAN module that will give you the current directory name. Or if you prefered not to install a module you can capture the output of the 'pwd' command with backticks:

    my $dirpath = `pwd`;

    If you are on Windows it appears that CPAN module is the better choice as it seems to have support for Windows. I don't know of a windows command that gets the current directory (though I don't use windows so there probably is and I just don't know about it.)

    Hope that helps
    Chris

    Lobster Aliens Are attacking the world!
      Hi,

      pwd won't necessarily give the answer you're looking for on a web server. If it does, it's likely to be a coincidence.

      Oh and instead of using pwd or `pwd`, you probably are looking for use Cwd;. It's part of the standard distribution and platform agnostic (but still won't give a URI's path on the filesystem).