Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16632 -- Crash on responses without Content-Type with IE. Than…

…ks juan for the report and kenth for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17196 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit affca1369c85116022e42d34f8deae245ce654cd 1 parent 545c315
Aymeric Augustin authored December 11, 2011
3  django/http/utils.py
@@ -76,7 +76,8 @@ def fix_IE_for_vary(request, response):
76 76
 
77 77
     # The first part of the Content-Type field will be the MIME type,
78 78
     # everything after ';', such as character-set, can be ignored.
79  
-    if response['Content-Type'].split(';')[0] not in safe_mime_types:
  79
+    mime_type = response.get('Content-Type', '').partition(';')[0]
  80
+    if mime_type not in safe_mime_types:
80 81
         try:
81 82
             del response['Vary']
82 83
         except KeyError:
47  tests/regressiontests/utils/http.py
... ...
@@ -1,6 +1,8 @@
1 1
 from django.utils import http
2 2
 from django.utils import unittest
3 3
 from django.utils.datastructures import MultiValueDict
  4
+from django.http import HttpResponse, utils
  5
+from django.test import RequestFactory
4 6
 
5 7
 class TestUtilsHttp(unittest.TestCase):
6 8
 
@@ -51,3 +53,48 @@ def test_urlencode(self):
51 53
             'position=Developer&name=Adrian&name=Simon'
52 54
         ]
53 55
         self.assertTrue(result in acceptable_results)
  56
+
  57
+    def test_fix_IE_for_vary(self):
  58
+        """
  59
+        Regression for #16632.
  60
+
  61
+        `fix_IE_for_vary` shouldn't crash when there's no Content-Type header.
  62
+        """
  63
+
  64
+        # functions to generate responses
  65
+        def response_with_unsafe_content_type():
  66
+            r = HttpResponse(content_type="text/unsafe")
  67
+            r['Vary'] = 'Cookie'
  68
+            return r
  69
+
  70
+        def no_content_response_with_unsafe_content_type():
  71
+            # 'Content-Type' always defaulted, so delete it
  72
+            r = response_with_unsafe_content_type()
  73
+            del r['Content-Type']
  74
+            return r
  75
+
  76
+        # request with & without IE user agent
  77
+        rf = RequestFactory()
  78
+        request = rf.get('/')
  79
+        ie_request = rf.get('/', HTTP_USER_AGENT='MSIE')
  80
+
  81
+        # not IE, unsafe_content_type
  82
+        response = response_with_unsafe_content_type()
  83
+        utils.fix_IE_for_vary(request, response)
  84
+        self.assertTrue('Vary' in response)
  85
+
  86
+        # IE, unsafe_content_type
  87
+        response = response_with_unsafe_content_type()
  88
+        utils.fix_IE_for_vary(ie_request, response)
  89
+        self.assertFalse('Vary' in response)
  90
+
  91
+        # not IE, no_content
  92
+        response = no_content_response_with_unsafe_content_type()
  93
+        utils.fix_IE_for_vary(request, response)
  94
+        self.assertTrue('Vary' in response)
  95
+
  96
+        # IE, no_content
  97
+        response = no_content_response_with_unsafe_content_type()
  98
+        utils.fix_IE_for_vary(ie_request, response)
  99
+        self.assertFalse('Vary' in response)
  100
+

0 notes on commit affca13

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