From 81caeb0f0e1dab7b2045db4e6f02e5b1f6ad46e9 Mon Sep 17 00:00:00 2001 From: Lukas Kremla <155779787+lukaskremla@users.noreply.github.com> Date: Tue, 13 Aug 2024 16:11:04 +0200 Subject: [PATCH 1/3] Fixed gzip automatic content-type assignment and added automatic compression setting This implements the fix for detecting the proper content-type even when the file has the ".gz" extension. It further makes sure the compression headers are set properly if a "gz." file is detected, but the compression headers weren't explicitly set by the user. --- src/microdot/microdot.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/microdot/microdot.py b/src/microdot/microdot.py index 49128ad..f77f9ac 100644 --- a/src/microdot/microdot.py +++ b/src/microdot/microdot.py @@ -774,7 +774,10 @@ def send_file(cls, filename, status_code=200, content_type=None, first. """ if content_type is None: - ext = filename.split('.')[-1] + if filename.endswith('.gz'): + ext = filename[:-3].split('.')[-1] + else: + ext = filename.split('.')[-1] if ext in Response.types_map: content_type = Response.types_map[ext] else: @@ -786,7 +789,7 @@ def send_file(cls, filename, status_code=200, content_type=None, if max_age is not None: headers['Cache-Control'] = 'max-age={}'.format(max_age) - if compressed: + if compressed or filename.endswith('.gz'): headers['Content-Encoding'] = compressed \ if isinstance(compressed, str) else 'gzip' From 58dd6c042c8936aba56d044d2eca12557e71ef0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kremla?= Date: Tue, 13 Aug 2024 20:00:42 +0200 Subject: [PATCH 2/3] Added a test for properly auto-determining mime types and setting content encoding header --- tests/files/test.txt.gz | 1 + tests/test_response.py | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 tests/files/test.txt.gz diff --git a/tests/files/test.txt.gz b/tests/files/test.txt.gz new file mode 100644 index 0000000..1910281 --- /dev/null +++ b/tests/files/test.txt.gz @@ -0,0 +1 @@ +foo \ No newline at end of file diff --git a/tests/test_response.py b/tests/test_response.py index 337f8dc..b88d415 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -280,6 +280,16 @@ def test_send_file_compressed(self): 'application/octet-stream') self.assertEqual(res.headers['Content-Encoding'], 'gzip') + def test_send_file_with_correct_mime_type_and_content_encoding(self): + res = Response.send_file('tests/files/test.txt') + self.assertEqual(res.status_code, 200) + self.assertEqual(res.headers['Content-Type'], 'text/plain') + + res = Response.send_file('tests/files/test.txt.gz') + self.assertEqual(res.status_code, 200) + self.assertEqual(res.headers['Content-Type'], 'text/plain') + self.assertEqual(res.headers['Content-Encoding'], 'gzip') + def test_default_content_type(self): original_content_type = Response.default_content_type res = Response('foo') From 09ac4a6cfa38a21b54ce1b0c2befaa5144c0ee20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kremla?= Date: Tue, 13 Aug 2024 22:43:16 +0200 Subject: [PATCH 3/3] Modified the gzip file header assignments and following tests according to the feedback. --- src/microdot/microdot.py | 4 ++-- tests/test_response.py | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/microdot/microdot.py b/src/microdot/microdot.py index f77f9ac..d60f1bb 100644 --- a/src/microdot/microdot.py +++ b/src/microdot/microdot.py @@ -774,7 +774,7 @@ def send_file(cls, filename, status_code=200, content_type=None, first. """ if content_type is None: - if filename.endswith('.gz'): + if compressed and filename.endswith('.gz'): ext = filename[:-3].split('.')[-1] else: ext = filename.split('.')[-1] @@ -789,7 +789,7 @@ def send_file(cls, filename, status_code=200, content_type=None, if max_age is not None: headers['Cache-Control'] = 'max-age={}'.format(max_age) - if compressed or filename.endswith('.gz'): + if compressed: headers['Content-Encoding'] = compressed \ if isinstance(compressed, str) else 'gzip' diff --git a/tests/test_response.py b/tests/test_response.py index b88d415..e27f0a2 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -280,12 +280,13 @@ def test_send_file_compressed(self): 'application/octet-stream') self.assertEqual(res.headers['Content-Encoding'], 'gzip') - def test_send_file_with_correct_mime_type_and_content_encoding(self): - res = Response.send_file('tests/files/test.txt') + def test_send_file_gzip_handling(self): + res = Response.send_file('tests/files/test.txt.gz') self.assertEqual(res.status_code, 200) - self.assertEqual(res.headers['Content-Type'], 'text/plain') + self.assertEqual(res.headers['Content-Type'], + 'application/octet-stream') - res = Response.send_file('tests/files/test.txt.gz') + res = Response.send_file('tests/files/test.txt.gz', compressed=True) self.assertEqual(res.status_code, 200) self.assertEqual(res.headers['Content-Type'], 'text/plain') self.assertEqual(res.headers['Content-Encoding'], 'gzip')