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
This commit is contained in:
Malcolm Tredinnick 2007-10-20 05:58:48 +00:00
parent e172e7be57
commit 706659d2bb
1 changed files with 10 additions and 6 deletions

View File

@ -261,19 +261,23 @@ class HttpResponse(object):
else: else:
self._container = [content] self._container = [content]
self._is_string = True self._is_string = True
self._headers = {'content-type': content_type}
self.cookies = SimpleCookie() self.cookies = SimpleCookie()
if status: 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.
self._headers = {'content-type': ('Content-Type', content_type)}
def __str__(self): def __str__(self):
"Full HTTP message, including headers" "Full HTTP message, including headers"
return '\n'.join(['%s: %s' % (key, value) 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 + '\n\n' + self.content
def __setitem__(self, header, value): def __setitem__(self, header, value):
self._headers[header.lower()] = value self._headers[header.lower()] = (header, value)
def __delitem__(self, header): def __delitem__(self, header):
try: try:
@ -282,7 +286,7 @@ class HttpResponse(object):
pass pass
def __getitem__(self, header): def __getitem__(self, header):
return self._headers[header.lower()] return self._headers[header.lower()][1]
def has_header(self, header): def has_header(self, header):
"Case-insensitive check for a header" "Case-insensitive check for a header"
@ -291,10 +295,10 @@ class HttpResponse(object):
__contains__ = has_header __contains__ = has_header
def items(self): def items(self):
return self._headers.items() return self._headers.values()
def get(self, header, alternate): 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): def set_cookie(self, key, value='', max_age=None, expires=None, path='/', domain=None, secure=None):
self.cookies[key] = value self.cookies[key] = value