[mod_python] Bug in setting up of config_dir from Handler directives.

Graham Dumpleton grahamd at dscpl.com.au
Wed Nov 10 17:18:59 EST 2004


On Nov 10 17:32, David Fraser <davidf at sjsoft.com> wrote:
>
> Subject: Re: [mod_python] Directory separator in uri/filename on Win32.
>
> Graham Dumpleton wrote:
> 
> >> One more question though. Does the use of POSIX path conventions 
> >> extend to
> >> other areas where a path might be defined internally by mod_python?
> >>
> >> For example, I use the following code to effectively determine the 
> >> directory
> >> where the PythonHandler which applies to the request being serviced 
> >> was set.
> >>
> >>     if hasattr(req,"hlist"):
> >>       # In mod_python 3.X have the req.hlist member.
> >>       root = req.hlist.directory
> >>
> >>     elif hasattr(req,"get_dirs"):
> >>       # In mod_python 2.X have the req.get_dirs() method.
> >>       root = req.get_dirs()["PythonHandler"]
> >>
> >> Is "root" going to also use POSIX path convention. This may depend 
> >> more on how
> >> mod_python is implemented than Apache depending on how mod_python 
> >> determines
> >> what value is used for this.
> >
> >
> > Is anyone able to run the check for me to answer my extra question about
> > what the handler directory is set to on Win32 platform? Ie., confirmation
> > that it also still uses POSIX directory separate convention and not 
> > Win32.
> >
> > The required publisher code is:
> >
> > def index(req):
> >
> >   root = "???"
> >
> >   if hasattr(req,"hlist"):
> >     # In mod_python 3.X have the req.hlist member.
> >     root = req.hlist.directory
> >
> >   elif hasattr(req,"get_dirs"):
> >     # In mod_python 2.X have the req.get_dirs() method.
> >     root = req.get_dirs()["PythonHandler"]
> >
> >   return root
> >
> > Thanks in advance.
> 
> Ran this on my Windows machine: displayed the following in my browser:
> 
> C:/Temp/jSuite.py/jLogbook/html/pubtest/\
> 
> Not sure what the extra \ is for

The trailing '\' is there because mod_python has a bug in it.

The problem is that mod_python doesn't seem to take into account that the
directory is given to it by Apache in POSIX directory naming convention.
Instead, because mod_python knows it is on Win32, it looks for a trailing '\'
and since it doesn't see one, because it is actually '/', it goes and adds a '\'.

The code in mod_python.c is:

static void *python_create_dir_config(apr_pool_t *p, char *dir)  
{
 
    py_config *conf = python_create_config(p);
 
    /* make sure directory ends with a slash */
    if (dir && (dir[strlen(dir) - 1] != SLASH)) 
        conf->config_dir = apr_pstrcat(p, dir, SLASH_S, NULL);
    else
        conf->config_dir = apr_pstrdup(p, dir);
 
    return conf;
}

It should in this case ignore whether it is on Win32 and instead use:

static void *python_create_dir_config(apr_pool_t *p, char *dir)  
{
 
    py_config *conf = python_create_config(p);
 
    /* make sure directory ends with a slash */
    if (dir && (dir[strlen(dir) - 1] != '/')) 
        conf->config_dir = apr_pstrcat(p, dir, "/", NULL);
    else
        conf->config_dir = apr_pstrdup(p, dir);
 
    return conf;
}

BTW, the definitions of SLASH and SLASH_S are in mod_python.h.

#ifdef WIN32
#define SLASH '\\'
#define SLASH_S "\\"
#else
#define SLASH '/'
#define SLASH_S "/"
#endif

I wander where else there are little mistakes like this in mod_python
where it doesn't take into consideration that Apache passed it POSIX
style pathnames and not Win32 ones.

Anyway, possibly another one for the TODO list of things to fix. Someone
want to confirm this analysis or make the change and see what happens
then.

--
Graham Dumpleton (grahamd at dscpl.com.au)


More information about the Mod_python mailing list