From e890c48f3f418edb4fe869cdb51ca209cb3ba49d Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 22 Jun 2016 09:44:10 -0700 Subject: [PATCH] Fixed #210: Fix query string to be bytes and not decoded --- channels/handler.py | 8 +++----- channels/tests/test_request.py | 4 ++-- docs/asgi.rst | 5 +---- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/channels/handler.py b/channels/handler.py index 02e474b..37793bf 100644 --- a/channels/handler.py +++ b/channels/handler.py @@ -135,11 +135,9 @@ class AsgiRequest(http.HttpRequest): @cached_property def GET(self): - # Django will try and re-urldecode the string and interpret + as space; - # we re-encode + here to fix this. - return http.QueryDict( - self.message.get('query_string', '').replace("+", "%2b").encode("utf8"), - ) + # Django will try and re-urldecode the query string and interpret + as space; + # we re-encode it here to fix this. + return http.QueryDict(self.message.get('query_string', '')) def _get_post(self): if not hasattr(self, '_post'): diff --git a/channels/tests/test_request.py b/channels/tests/test_request.py index 7dd90bf..66fae9e 100644 --- a/channels/tests/test_request.py +++ b/channels/tests/test_request.py @@ -46,7 +46,7 @@ class RequestTests(ChannelTestCase): "http_version": "1.1", "method": "GET", "path": "/test2/", - "query_string": "x=1&y=foo bar+baz", + "query_string": b"x=1&y=%26foo+bar%2Bbaz", "headers": { "host": b"example.com", "cookie": b"test-time=1448995585123; test-value=yeah", @@ -65,7 +65,7 @@ class RequestTests(ChannelTestCase): self.assertEqual(request.META["SERVER_NAME"], "10.0.0.2") self.assertEqual(request.META["SERVER_PORT"], 80) self.assertEqual(request.GET["x"], "1") - self.assertEqual(request.GET["y"], "foo bar+baz") + self.assertEqual(request.GET["y"], "&foo bar+baz") self.assertEqual(request.COOKIES["test-time"], "1448995585123") self.assertEqual(request.COOKIES["test-value"], "yeah") self.assertFalse(request.POST) diff --git a/docs/asgi.rst b/docs/asgi.rst index 234b359..d53207b 100644 --- a/docs/asgi.rst +++ b/docs/asgi.rst @@ -526,10 +526,7 @@ Keys: * ``path``: Unicode string HTTP path from URL, with percent escapes decoded and UTF8 byte sequences decoded into characters. -* ``query_string``: Unicode string URL portion after the ``?``, already - url-decoded, like ``path``. Optional, default is ``""``. ``+`` characters in - this portion should be interpreted by the application to be literal pluses, - not spaces. +* ``query_string``: Byte string URL portion after the ``?``, not url-decoded. * ``root_path``: Unicode string that indicates the root path this application is mounted at; same as ``SCRIPT_NAME`` in WSGI. Optional, defaults