From e26fd0cd17d197c6bc46d0d390f481563442fb3b Mon Sep 17 00:00:00 2001 From: SamvanLeipsig Date: Thu, 1 Jul 2021 13:20:24 +0200 Subject: [PATCH] Fix case-sensitivity for Content-Encoding: gzip. as per rfc7231 the Content-Encoding values are case-insensitive https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.2.1 --- tornado/http1connection.py | 2 +- tornado/test/httpserver_test.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tornado/http1connection.py b/tornado/http1connection.py index 72088d602..5ca916888 100644 --- a/tornado/http1connection.py +++ b/tornado/http1connection.py @@ -718,7 +718,7 @@ class _GzipMessageDelegate(httputil.HTTPMessageDelegate): start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine], headers: httputil.HTTPHeaders, ) -> Optional[Awaitable[None]]: - if headers.get("Content-Encoding") == "gzip": + if headers.get("Content-Encoding", "").lower() == "gzip": self._decompressor = GzipDecompressor() # Downstream delegates will only see uncompressed data, # so rename the content-encoding header. diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index 614dec7b8..58696e238 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -1000,6 +1000,21 @@ class GzipTest(GzipBaseTest, AsyncHTTPTestCase): response = self.post_gzip("foo=bar") self.assertEqual(json_decode(response.body), {u"foo": [u"bar"]}) + def test_gzip_case_insensitive(self): + # https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.2.1 + bytesio = BytesIO() + gzip_file = gzip.GzipFile(mode="w", fileobj=bytesio) + gzip_file.write(utf8("foo=bar")) + gzip_file.close() + compressed_body = bytesio.getvalue() + response = self.fetch( + "/", + method="POST", + body=compressed_body, + headers={"Content-Encoding": "GZIP"}, + ) + self.assertEqual(json_decode(response.body), {u"foo": [u"bar"]}) + class GzipUnsupportedTest(GzipBaseTest, AsyncHTTPTestCase): def test_gzip_unsupported(self): -- 2.47.2