Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#112 closed defect (fixed)

WSGI requires a HTTP reason in the status

Reported by: sune.kirkeby@… Owned by: Adrian Holovaty
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: no UI/UX: no

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 by Manuzhai, 19 years ago

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

comment:2 by sune.kirkeby@…, 19 years ago

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 by Adrian Holovaty, 19 years ago

Status: newassigned

comment:4 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: assignedclosed

(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