From 636860fbfb4e34e08749d4576bc9571028150526 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 7 Sep 2013 09:25:51 -0500 Subject: [PATCH] Moved two WSGI-specific functions to the WSGI handler. They were defined in base when the mod_python handler used them. See bfcecbff. --- django/contrib/staticfiles/handlers.py | 3 +- django/core/handlers/base.py | 42 --------------------- django/core/handlers/wsgi.py | 52 ++++++++++++++++++++++++-- django/test/testcases.py | 3 +- 4 files changed, 50 insertions(+), 50 deletions(-) diff --git a/django/contrib/staticfiles/handlers.py b/django/contrib/staticfiles/handlers.py index e6db3dcc1fc..e036f6b141a 100644 --- a/django/contrib/staticfiles/handlers.py +++ b/django/contrib/staticfiles/handlers.py @@ -1,6 +1,5 @@ from django.conf import settings -from django.core.handlers.base import get_path_info -from django.core.handlers.wsgi import WSGIHandler +from django.core.handlers.wsgi import get_path_info, WSGIHandler from django.utils.six.moves.urllib.parse import urlparse from django.utils.six.moves.urllib.request import url2pathname diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py index 59118656c6b..fe7abed9037 100644 --- a/django/core/handlers/base.py +++ b/django/core/handlers/base.py @@ -246,45 +246,3 @@ class BaseHandler(object): for func in self.response_fixes: response = func(request, response) return response - - -def get_path_info(environ): - """ - Returns the HTTP request's PATH_INFO as a unicode string. - """ - path_info = environ.get('PATH_INFO', str('/')) - # Under Python 3, strings in environ are decoded with ISO-8859-1; - # re-encode to recover the original bytestring provided by the web server. - if six.PY3: - path_info = path_info.encode('iso-8859-1') - # It'd be better to implement URI-to-IRI decoding, see #19508. - return path_info.decode('utf-8') - - -def get_script_name(environ): - """ - Returns the equivalent of the HTTP request's SCRIPT_NAME environment - variable. If Apache mod_rewrite has been used, returns what would have been - the script name prior to any rewriting (so it's the script name as seen - from the client's perspective), unless the FORCE_SCRIPT_NAME setting is - set (to anything). - """ - if settings.FORCE_SCRIPT_NAME is not None: - return force_text(settings.FORCE_SCRIPT_NAME) - - # If Apache's mod_rewrite had a whack at the URL, Apache set either - # SCRIPT_URL or REDIRECT_URL to the full resource URL before applying any - # rewrites. Unfortunately not every Web server (lighttpd!) passes this - # information through all the time, so FORCE_SCRIPT_NAME, above, is still - # needed. - script_url = environ.get('SCRIPT_URL', environ.get('REDIRECT_URL', str(''))) - if script_url: - script_name = script_url[:-len(environ.get('PATH_INFO', str('')))] - else: - script_name = environ.get('SCRIPT_NAME', str('')) - # Under Python 3, strings in environ are decoded with ISO-8859-1; - # re-encode to recover the original bytestring provided by the web server. - if six.PY3: - script_name = script_name.encode('iso-8859-1') - # It'd be better to implement URI-to-IRI decoding, see #19508. - return script_name.decode('utf-8') diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py index 967254dfac4..6737094ab5a 100644 --- a/django/core/handlers/wsgi.py +++ b/django/core/handlers/wsgi.py @@ -7,11 +7,13 @@ from io import BytesIO from threading import Lock from django import http +from django.conf import settings from django.core import signals from django.core.handlers import base from django.core.urlresolvers import set_script_prefix from django.utils import datastructures -from django.utils.encoding import force_str +from django.utils.encoding import force_str, force_text +from django.utils import six # For backwards compatibility -- lots of code uses this in the wild! from django.http.response import REASON_PHRASES as STATUS_CODE_TEXT @@ -73,8 +75,8 @@ class LimitedStream(object): class WSGIRequest(http.HttpRequest): def __init__(self, environ): - script_name = base.get_script_name(environ) - path_info = base.get_path_info(environ) + script_name = get_script_name(environ) + path_info = get_path_info(environ) if not path_info: # Sometimes PATH_INFO exists, but is empty (e.g. accessing # the SCRIPT_NAME URL without a trailing slash). We really need to @@ -183,7 +185,7 @@ class WSGIHandler(base.BaseHandler): self._request_middleware = None raise - set_script_prefix(base.get_script_name(environ)) + set_script_prefix(get_script_name(environ)) signals.request_started.send(sender=self.__class__) try: request = self.request_class(environ) @@ -206,3 +208,45 @@ class WSGIHandler(base.BaseHandler): response_headers.append((str('Set-Cookie'), str(c.output(header='')))) start_response(force_str(status), response_headers) return response + + +def get_path_info(environ): + """ + Returns the HTTP request's PATH_INFO as a unicode string. + """ + path_info = environ.get('PATH_INFO', str('/')) + # Under Python 3, strings in environ are decoded with ISO-8859-1; + # re-encode to recover the original bytestring provided by the web server. + if six.PY3: + path_info = path_info.encode('iso-8859-1') + # It'd be better to implement URI-to-IRI decoding, see #19508. + return path_info.decode('utf-8') + + +def get_script_name(environ): + """ + Returns the equivalent of the HTTP request's SCRIPT_NAME environment + variable. If Apache mod_rewrite has been used, returns what would have been + the script name prior to any rewriting (so it's the script name as seen + from the client's perspective), unless the FORCE_SCRIPT_NAME setting is + set (to anything). + """ + if settings.FORCE_SCRIPT_NAME is not None: + return force_text(settings.FORCE_SCRIPT_NAME) + + # If Apache's mod_rewrite had a whack at the URL, Apache set either + # SCRIPT_URL or REDIRECT_URL to the full resource URL before applying any + # rewrites. Unfortunately not every Web server (lighttpd!) passes this + # information through all the time, so FORCE_SCRIPT_NAME, above, is still + # needed. + script_url = environ.get('SCRIPT_URL', environ.get('REDIRECT_URL', str(''))) + if script_url: + script_name = script_url[:-len(environ.get('PATH_INFO', str('')))] + else: + script_name = environ.get('SCRIPT_NAME', str('')) + # Under Python 3, strings in environ are decoded with ISO-8859-1; + # re-encode to recover the original bytestring provided by the web server. + if six.PY3: + script_name = script_name.encode('iso-8859-1') + # It'd be better to implement URI-to-IRI decoding, see #19508. + return script_name.decode('utf-8') diff --git a/django/test/testcases.py b/django/test/testcases.py index 29d3a881498..ab658a04fa3 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -17,8 +17,7 @@ from unittest.util import safe_repr from django.conf import settings from django.core import mail from django.core.exceptions import ValidationError, ImproperlyConfigured -from django.core.handlers.wsgi import WSGIHandler -from django.core.handlers.base import get_path_info +from django.core.handlers.wsgi import get_path_info, WSGIHandler from django.core.management import call_command from django.core.management.color import no_style from django.core.management.commands import flush