diff --git a/django/core/handlers/modpython.py b/django/core/handlers/modpython.py index 52419d9e6c..2a3e03f3dd 100644 --- a/django/core/handlers/modpython.py +++ b/django/core/handlers/modpython.py @@ -14,7 +14,7 @@ import os class ModPythonRequest(http.HttpRequest): def __init__(self, req): self._req = req - self.path = force_unicode(req.uri, errors='ignore') + self.path = force_unicode(req.uri) def __repr__(self): # Since this is called as part of error handling, we need to be very @@ -136,6 +136,8 @@ class ModPythonRequest(http.HttpRequest): method = property(_get_method) class ModPythonHandler(BaseHandler): + request_class = ModPythonRequest + def __call__(self, req): # mod_python fakes the environ, and thus doesn't process SetEnv. This fixes that os.environ.update(req.subprocess_env) @@ -150,13 +152,16 @@ class ModPythonHandler(BaseHandler): dispatcher.send(signal=signals.request_started) try: - request = ModPythonRequest(req) - response = self.get_response(request) - - # Apply response middleware - for middleware_method in self._response_middleware: - response = middleware_method(request, response) + try: + request = self.request_class(req) + except UnicodeDecodeError: + response = http.HttpResponseBadRequest() + else: + response = self.get_response(request) + # Apply response middleware + for middleware_method in self._response_middleware: + response = middleware_method(request, response) finally: dispatcher.send(signal=signals.request_finished) diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py index 95c5f9cbda..a67c302102 100644 --- a/django/core/handlers/wsgi.py +++ b/django/core/handlers/wsgi.py @@ -75,7 +75,7 @@ def safe_copyfileobj(fsrc, fdst, length=16*1024, size=0): class WSGIRequest(http.HttpRequest): def __init__(self, environ): self.environ = environ - self.path = force_unicode(environ['PATH_INFO'], errors='ignore') + self.path = force_unicode(environ['PATH_INFO']) self.META = environ self.method = environ['REQUEST_METHOD'].upper() @@ -179,6 +179,7 @@ class WSGIRequest(http.HttpRequest): class WSGIHandler(BaseHandler): initLock = Lock() + request_class = WSGIRequest def __call__(self, environ, start_response): from django.conf import settings @@ -194,13 +195,16 @@ class WSGIHandler(BaseHandler): dispatcher.send(signal=signals.request_started) try: - request = WSGIRequest(environ) - response = self.get_response(request) - - # Apply response middleware - for middleware_method in self._response_middleware: - response = middleware_method(request, response) + try: + request = self.request_class(environ) + except UnicodeDecodeError: + response = http.HttpResponseBadRequest() + else: + response = self.get_response(request) + # Apply response middleware + for middleware_method in self._response_middleware: + response = middleware_method(request, response) finally: dispatcher.send(signal=signals.request_finished)