[mod_python] script_name bug?

Russell Yanofsky rey4 at columbia.edu
Sat Aug 9 11:16:48 EST 2003


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



More information about the Mod_python mailing list