Opened 10 years ago

Closed 10 years ago

Last modified 9 years ago

#112 closed defect (fixed)

WSGI requires a HTTP reason in the status

Reported by: sune.kirkeby@… Owned by: adrian
Component: Core (Other) Version:
Severity: normal Keywords: wsgi
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The WSGI-spec clearly requires a reason-phrase when calling start_response, and Apache breaks proxied responses that are missing the reason phrase. So the Django wsgi-adapter is quite broken in the respect.

Below is a patch which fixes this.

The mapping of HTTP reason-phrases is incomplete, but at least now Django works when being proxied by Apache.

Index: core/handlers/wsgi.py
===================================================================
--- core/handlers/wsgi.py       (revision 241)
+++ core/handlers/wsgi.py       (working copy)
@@ -1,6 +1,12 @@
 from django.utils import datastructures, httpwrappers
 from pprint import pformat

+reasons = {
+    200: 'Ok',
+    404: 'Not found',
+    500: 'Internal Server Error',
+}
+
 class WSGIRequest(httpwrappers.HttpRequest):
     def __init__(self, environ):
         self.environ = environ
@@ -121,7 +127,7 @@
         for middleware_method in self._response_middleware:
             response = middleware_method(request, response)

-        status = str(response.status_code) + ' ' # TODO: Extra space here is a hack.
+        status = '%d %s' % (response.status_code, reasons.get(response.status_code, 'Unknown status-code'))
         response_headers = response.headers
         if response.cookies:
             response_headers['Set-Cookie'] = response.cookies.output(header='')

Change History (4)

comment:1 Changed 10 years ago by Manuzhai

This code should use BaseHTTPRequestHandler.responses to do this in a much more complete/compliant way.

comment:2 Changed 10 years ago by sune.kirkeby@…

Index: django/core/handlers/wsgi.py
===================================================================
--- django/core/handlers/wsgi.py        (revision 241)
+++ django/core/handlers/wsgi.py        (working copy)
@@ -1,6 +1,9 @@
 from django.utils import datastructures, httpwrappers
 from pprint import pformat
+import BaseHTTPServer
 
+responses = BaseHTTPServer.BaseHTTPRequestHandler.responses
+
 class WSGIRequest(httpwrappers.HttpRequest):
     def __init__(self, environ):
         self.environ = environ
@@ -121,7 +124,7 @@
         for middleware_method in self._response_middleware:
             response = middleware_method(request, response)
 
-        status = str(response.status_code) + ' ' # TODO: Extra space here is a hack.
+        status = '%d %s' % (response.status_code, responses.get(response.status_code, ('Unknown status-code', None))[0])
         response_headers = response.headers
         if response.cookies:
             response_headers['Set-Cookie'] = response.cookies.output(header='')

comment:3 Changed 10 years ago by adrian

  • Status changed from new to assigned

comment:4 Changed 10 years ago by adrian

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [297]) Fixed #112 -- WSGI handler now displays proper text status. Thanks, sune.kirkeby@…!

Note: See TracTickets for help on using tickets.
Back to Top