-
-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed gzip automatic content-type assignment and added automatic compression header configuration #251
Fixed gzip automatic content-type assignment and added automatic compression header configuration #251
Conversation
…ression 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.
…tent encoding header
src/microdot/microdot.py
Outdated
@@ -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'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if filename.endswith('.gz'): | |
if compressed and filename.endswith('.gz'): |
I think it is best to rely on the compression
argument here.
src/microdot/microdot.py
Outdated
@@ -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'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if compressed or filename.endswith('.gz'): | |
if compressed: |
I think this is fine as it was. If the user passes compressed=False
then the response should not be compressed.
tests/test_response.py
Outdated
self.assertEqual(res.status_code, 200) | ||
self.assertEqual(res.headers['Content-Type'], 'text/plain') | ||
|
||
res = Response.send_file('tests/files/test.txt.gz') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test should use octet-stream as content file, because the compressed
argument is False
by default.
There should be another test that passes compressed=True
and only then the we want the content type to be text and the content-encoding header to be present.
I do not like the idea of making it "automatic". If the call has |
…ng to the feedback.
Your suggestions make sense. I have modified the microdot.py and test files accordingly. |
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.