get_script_name

  1. def get_script_name(environ):
  2. """
  3. Returns the equivalent of the HTTP request's SCRIPT_NAME environment
  4. variable. If Apache mod_rewrite has been used, returns what would have been
  5. the script name prior to any rewriting (so it's the script name as seen
  6. from the client's perspective), unless the FORCE_SCRIPT_NAME setting is
  7. set (to anything).
  8. """
  9. if settings.FORCE_SCRIPT_NAME is not None:
  10. return force_text(settings.FORCE_SCRIPT_NAME)
  11.  
  12. # If Apache's mod_rewrite had a whack at the URL, Apache set either
  13. # SCRIPT_URL or REDIRECT_URL to the full resource URL before applying any
  14. # rewrites. Unfortunately not every Web server (lighttpd!) passes this
  15. # information through all the time, so FORCE_SCRIPT_NAME, above, is still
  16. # needed.
  17. script_url = environ.get('SCRIPT_URL', environ.get('REDIRECT_URL', str('')))
  18. if script_url:
  19. script_name = script_url[:-len(environ.get('PATH_INFO', str('')))]
  20. else:
  21. script_name = environ.get('SCRIPT_NAME', str(''))
  22. # Under Python 3, strings in environ are decoded with ISO-8859-1;
  23. # re-encode to recover the original bytestring provided by the webserver.
  24. if six.PY3:
  25. script_name = script_name.encode('iso-8859-1')
  26. # It'd be better to implement URI-to-IRI decoding, see #19508.
  27. return script_name.decode('utf-8')
  28.  
Programming language: 
Python