Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.6.x] Fixed #20530 -- Properly decoded non-ASCII query strings on P…

…ython 3.

Thanks mitsuhiko for the report.

Backport of 65b6eff and adaptation of 8aaca65 from master.
  • Loading branch information...
commit 7fcd6aa6695b39370154d6993cdbb3ba4363de91 1 parent 9244447
Aymeric Augustin authored September 07, 2013
5  django/core/handlers/wsgi.py
@@ -132,7 +132,10 @@ def _get_request(self):
132 132
     def _get_get(self):
133 133
         if not hasattr(self, '_get'):
134 134
             # The WSGI spec says 'QUERY_STRING' may be absent.
135  
-            self._get = http.QueryDict(self.environ.get('QUERY_STRING', ''), encoding=self._encoding)
  135
+            raw_query_string = self.environ.get('QUERY_STRING', str(''))
  136
+            if six.PY3:
  137
+                raw_query_string = raw_query_string.encode('iso-8859-1').decode('utf-8')
  138
+            self._get = http.QueryDict(raw_query_string, encoding=self._encoding)
136 139
         return self._get
137 140
 
138 141
     def _set_get(self, get):
13  tests/handlers/tests.py
@@ -41,6 +41,16 @@ def test_bad_path_info(self):
41 41
         response = handler(environ, lambda *a, **k: None)
42 42
         self.assertEqual(response.status_code, 400)
43 43
 
  44
+    def test_non_ascii_query_string(self):
  45
+        """Test that non-ASCII query strings are properly decoded (#20530)."""
  46
+        environ = RequestFactory().get('/').environ
  47
+        raw_query_string = 'want=café'
  48
+        if six.PY3:
  49
+            raw_query_string = raw_query_string.encode('utf-8').decode('iso-8859-1')
  50
+        environ['QUERY_STRING'] = raw_query_string
  51
+        request = WSGIRequest(environ)
  52
+        self.assertEqual(request.GET['want'], "café")
  53
+
44 54
     def test_non_ascii_cookie(self):
45 55
         """Test that non-ASCII cookies set in JavaScript are properly decoded (#20557)."""
46 56
         environ = RequestFactory().get('/').environ
@@ -49,6 +59,9 @@ def test_non_ascii_cookie(self):
49 59
             raw_cookie = raw_cookie.encode('utf-8').decode('iso-8859-1')
50 60
         environ['HTTP_COOKIE'] = raw_cookie
51 61
         request = WSGIRequest(environ)
  62
+        # If would be nicer if request.COOKIES returned unicode values.
  63
+        # However the current cookie parser doesn't do this and fixing it is
  64
+        # much more work than fixing #20557. Feel free to remove force_str()!
52 65
         self.assertEqual(request.COOKIES['want'], force_str("café"))
53 66
 
54 67
 

0 notes on commit 7fcd6aa

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