Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 706659d2bb6af350903a5b5ee9d1e09f95f6a9a3 1 parent e172e7b
Malcolm Tredinnick authored October 20, 2007

Showing 1 changed file with 10 additions and 6 deletions. Show diff stats Hide diff stats

  1. 16  django/http/__init__.py
16  django/http/__init__.py
@@ -261,19 +261,23 @@ def __init__(self, content='', mimetype=None, status=None,
261 261
         else:
262 262
             self._container = [content]
263 263
             self._is_string = True
264  
-        self._headers = {'content-type': content_type}
265 264
         self.cookies = SimpleCookie()
266 265
         if status:
267 266
             self.status_code = status
268 267
 
  268
+        # _headers is a mapping of the lower-case name to the original case of
  269
+        # the header (required for working with legacy systems) and the header
  270
+        # value.
  271
+        self._headers = {'content-type': ('Content-Type', content_type)}
  272
+
269 273
     def __str__(self):
270 274
         "Full HTTP message, including headers"
271 275
         return '\n'.join(['%s: %s' % (key, value)
272  
-            for key, value in self._headers.items()]) \
  276
+            for key, value in self._headers.values()]) \
273 277
             + '\n\n' + self.content
274 278
 
275 279
     def __setitem__(self, header, value):
276  
-        self._headers[header.lower()] = value
  280
+        self._headers[header.lower()] = (header, value)
277 281
 
278 282
     def __delitem__(self, header):
279 283
         try:
@@ -282,7 +286,7 @@ def __delitem__(self, header):
282 286
             pass
283 287
 
284 288
     def __getitem__(self, header):
285  
-        return self._headers[header.lower()]
  289
+        return self._headers[header.lower()][1]
286 290
 
287 291
     def has_header(self, header):
288 292
         "Case-insensitive check for a header"
@@ -291,10 +295,10 @@ def has_header(self, header):
291 295
     __contains__ = has_header
292 296
 
293 297
     def items(self):
294  
-        return self._headers.items()
  298
+        return self._headers.values()
295 299
 
296 300
     def get(self, header, alternate):
297  
-        return self._headers.get(header.lower(), alternate)
  301
+        return self._headers.get(header.lower(), (None, alternate))[1]
298 302
 
299 303
     def set_cookie(self, key, value='', max_age=None, expires=None, path='/', domain=None, secure=None):
300 304
         self.cookies[key] = value

0 notes on commit 706659d

Please sign in to comment.
Something went wrong with that request. Please try again.