[mod_python] script_name bug?

Gregory (Grisha) Trubetskoy grisha at modpython.org
Sat Aug 9 13:13:09 EST 2003


Russel -

Since mod_python isn't a CGI environment (and doesn't care to be one
either), the definition of SCRIPT_NAME is rather blurry.

In CGI it is meant to refer to the script which is executed as a separate
process - in mod_python there is no separate process, and the file
containing the code isn't located by the web server, but rather follows
the Python sys.path search rules.

I don't think it would be right to tinker with what req.subprocess_env
contains - it should return whatever apache put there, uncensored, so to
that degree it's not a bug as far as I am concerned.

P.S. If you look at build_cgi_env() in apache.py, it seems to adress this
issue.

Grisha

On Sat, 9 Aug 2003, Russell Yanofsky wrote:

> I've noticed that req.subprocess_env['SCRIPT_NAME'] seems to have the wrong
> value whenever the requested URL ends in a forward slash. This happens with
> mod_python 3.0.3 and apache 2.0.47. Here are some examples:
>
>   Requested URL: /mptest/mptest.py/
>     Actual SCRIPT_NAME = /mptest/mptest.py/
>     Expected SCRIPT_NAME = /mptest/mptest.py
>     PATH_INFO = /
>
>   Requested URL: /mptest/mptest.py/abc/
>     Actual SCRIPT_NAME = /mptest/mptest.py/abc
>     Expected SCRIPT_NAME = /mptest/mptest.py
>     PATH_INFO = /abc/
>
>   Requested URL: /mptest/mptest.py/abc/123/
>     Actual SCRIPT_NAME = /mptest/mptest.py/abc/123
>     Expected SCRIPT_NAME = /mptest/mptest.py
>     PATH_INFO = /abc/123/
>
> When the request doesn't end with a forward slash there's no problem:
>
>   Requested URL: /mptest/mptest.py
>     Actual SCRIPT_NAME = /mptest/mptest.py
>     Expected SCRIPT_NAME = /mptest/mptest.py
>     PATH_INFO =
>
>   Requested URL: /mptest/mptest.py/abc
>     Actual SCRIPT_NAME = /mptest/mptest.py
>     Expected SCRIPT_NAME = /mptest/mptest.py
>     PATH_INFO = /abc
>
>   Requested URL: /mptest/mptest.py/abc/123
>     Actual SCRIPT_NAME = /mptest/mptest.py
>     Expected SCRIPT_NAME = /mptest/mptest.py
>     PATH_INFO = /abc/123
>
> Here's the code I used to produce the above output:
>
> -- begin .htaccess listing --
> AddHandler python-program .py
> PythonHandler mptest
> PythonDebug On
> -- end .htaccess listing --
>
> -- begin mptest.py listing --
> from mod_python import apache
>
> def handler(req):
>   req.content_type = 'text/plain'
>   env = req.subprocess_env
>   req.write('Requested URL: %s\n' % env['REQUEST_URI'])
>   req.write('  Actual SCRIPT_NAME = %s\n' % env['SCRIPT_NAME'])
>   fixenv(env)
>   req.write('  Expected SCRIPT_NAME = %s\n' % env['SCRIPT_NAME'])
>   req.write('  PATH_INFO = %s\n' % env.get('PATH_INFO', ''))
>   return apache.OK
>
> # This is only a partial fix, does not work in the presence
> # of double slashes in the PATH_INFO segment
> def fixenv(env):
>   path_info = env.get('PATH_INFO', '')
>   if path_info and path_info[-1] == '/':
>     script_name = env['SCRIPT_NAME']
>     path_len = len(path_info) - 1
>     if path_len:
>       assert script_name[-path_len:] == path_info[:-1]
>       env['SCRIPT_NAME'] = script_name[:-path_len]
>     else:
>       assert script_name[-1] == '/'
>       env['SCRIPT_NAME'] = script_name[:-1]
> -- end mptest.py listing --
>
> This behavior only seems to occur in mod_python. SCRIPT_NAME does seem to
> have the right value in CGI scripts.
>
> - Russ
>
>
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>


More information about the Mod_python mailing list