how to translate a virtual path to a physical path in a CGI script

Skip Montanaro skip at mojam.com
Wed Oct 6 13:34:37 EDT 1999


    >> Currently I am running IIS, which provides PATH_INFO and
    >> PATH_TRANSLATED and so on.  So I have no problem getting to know the
    >> physical path for the script itself.  What I really need is a
    >> function that given *any* virtual path, it can translate it into a
    >> physical one.

    >> I managed to write a mappath() function yesterday, which substracts
    >> PATH_INFO from PATH_TRANSLATED to derive the document root, and join
    >> the given logial path to it.  This way is definitely not reliable, is
    >> it?

Unfortunately, no, it's not.  On Apache at least (and probably on other web
servers as well), you can define lots of mappings between virtual paths and
absolute paths within the same server.  I might have a DocumentRoot of
/www/server, alias /images to /www/images, and /cgi-bin to /www/cgi.  In a
CGI script with a PATH_TRANSLATED of /www/cgi/spam, there's no way to tell
that /images/guido.png will live in /www/images/guido.png instead of
/www/cgi/guido.png.  As the author of the script, you just sort of have to
know.

Given that, it may be possible to rummage around in the config file and
build a list of potential directories where files could be located, then try
locating the file using that list.  Still wouldn't be reliable unless you
were able to reliably parse the config file and eliminate all impossible
possibilities. 

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/
847-971-7098   | Python: Programming the way Guido indented...




More information about the Python-list mailing list