From c2d80a5acbfdbab14586d3970f88439fea7ccb04 Mon Sep 17 00:00:00 2001 From: Chris Cahoon Date: Thu, 13 Aug 2009 22:35:15 +0000 Subject: [PATCH] [soc2009/http-wsgi-improvements] Clean up charset-related code in HttpResponse. Refs #10190. git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/http-wsgi-improvements@11448 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/http/__init__.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/django/http/__init__.py b/django/http/__init__.py index 656c0d91a5..eaa8cb3232 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -277,15 +277,9 @@ class HttpResponse(object): def __init__(self, content='', mimetype=None, status=None, content_type=None, request=None): from django.conf import settings - accept_charset = None - self._charset = settings.DEFAULT_CHARSET if mimetype: content_type = mimetype # Mimetype arg is an alias for content-type - if request: - accept_charset = request.META.get("ACCEPT_CHARSET") - if accept_charset or content_type: - encoding = get_response_encoding(content_type, accept_charset) - (self._charset, self._codec) = encoding + self.determine_charset(content_type, request) if not content_type: content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, self._charset) @@ -294,8 +288,7 @@ class HttpResponse(object): if hasattr(content, 'close'): content.close() self.cookies = SimpleCookie() - if status: - self.status_code = status + self.status_code = status # _headers is a mapping of the lower-case name to the original case of # the header (required for working with legacy systems) and the header # value. @@ -306,6 +299,15 @@ class HttpResponse(object): return '\n'.join(['%s: %s' % (k, v) for k, v in self._headers.values()]) \ + "\n\n" + self.content + def determine_charset(self, content_type, request): + self._charset = settings.DEFAULT_CHARSET + accept_charset = None + if request: + accept_charset = request.META.get("ACCEPT_CHARSET") + if accept_charset or content_type: + encoding = get_response_encoding(content_type, accept_charset) + (self._charset, self._codec) = encoding + def _convert_to_ascii(self, *values): """Converts all values to ascii strings.""" for value in values: @@ -390,7 +392,8 @@ class HttpResponse(object): return self._status_code def _set_status_code(self, value): - self._status_code = value + if value: + self._status_code = value status_code = property(_get_status_code, _set_status_code)