1
0
mirror of https://github.com/django/django.git synced 2025-07-04 01:39:20 +00:00

[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
This commit is contained in:
Chris Cahoon 2009-08-13 22:35:15 +00:00
parent d76879d1b3
commit c2d80a5acb

View File

@ -277,15 +277,9 @@ class HttpResponse(object):
def __init__(self, content='', mimetype=None, status=None, def __init__(self, content='', mimetype=None, status=None,
content_type=None, request=None): content_type=None, request=None):
from django.conf import settings from django.conf import settings
accept_charset = None
self._charset = settings.DEFAULT_CHARSET
if mimetype: if mimetype:
content_type = mimetype # Mimetype arg is an alias for content-type content_type = mimetype # Mimetype arg is an alias for content-type
if request: self.determine_charset(content_type, 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
if not content_type: if not content_type:
content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
self._charset) self._charset)
@ -294,7 +288,6 @@ class HttpResponse(object):
if hasattr(content, 'close'): if hasattr(content, 'close'):
content.close() content.close()
self.cookies = SimpleCookie() 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 # _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 # the header (required for working with legacy systems) and the header
@ -306,6 +299,15 @@ class HttpResponse(object):
return '\n'.join(['%s: %s' % (k, v) for k, v in self._headers.values()]) \ return '\n'.join(['%s: %s' % (k, v) for k, v in self._headers.values()]) \
+ "\n\n" + self.content + "\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): def _convert_to_ascii(self, *values):
"""Converts all values to ascii strings.""" """Converts all values to ascii strings."""
for value in values: for value in values:
@ -390,6 +392,7 @@ class HttpResponse(object):
return self._status_code return self._status_code
def _set_status_code(self, value): def _set_status_code(self, value):
if value:
self._status_code = value self._status_code = value
status_code = property(_get_status_code, _set_status_code) status_code = property(_get_status_code, _set_status_code)