From 706659d2bb6af350903a5b5ee9d1e09f95f6a9a3 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 20 Oct 2007 05:58:48 +0000 Subject: [PATCH] Changed the way we handle HTTP headers internally so that they appear case-insensitive, but the original case is preserved for output. This increases the chances of working with non-compliant clients without changing the external interface. Fixed #2970. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6546 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/http/__init__.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/django/http/__init__.py b/django/http/__init__.py index d2e1ca0b39..6fe6d0d760 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -261,19 +261,23 @@ class HttpResponse(object): else: self._container = [content] self._is_string = True - self._headers = {'content-type': content_type} self.cookies = SimpleCookie() if 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. + self._headers = {'content-type': ('Content-Type', content_type)} + def __str__(self): "Full HTTP message, including headers" return '\n'.join(['%s: %s' % (key, value) - for key, value in self._headers.items()]) \ + for key, value in self._headers.values()]) \ + '\n\n' + self.content def __setitem__(self, header, value): - self._headers[header.lower()] = value + self._headers[header.lower()] = (header, value) def __delitem__(self, header): try: @@ -282,7 +286,7 @@ class HttpResponse(object): pass def __getitem__(self, header): - return self._headers[header.lower()] + return self._headers[header.lower()][1] def has_header(self, header): "Case-insensitive check for a header" @@ -291,10 +295,10 @@ class HttpResponse(object): __contains__ = has_header def items(self): - return self._headers.items() + return self._headers.values() def get(self, header, alternate): - return self._headers.get(header.lower(), alternate) + return self._headers.get(header.lower(), (None, alternate))[1] def set_cookie(self, key, value='', max_age=None, expires=None, path='/', domain=None, secure=None): self.cookies[key] = value