Skip to content
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

bpo-30576 : Add HTTP compression support to http.server #2078

Closed
wants to merge 67 commits into from
Closed

bpo-30576 : Add HTTP compression support to http.server #2078

wants to merge 67 commits into from

Conversation

PierreQuentel
Copy link
Contributor

@PierreQuentel PierreQuentel commented Jun 10, 2017

The Pull Request add support of HTTP compression to the built-in HTTP server. For some files (html, css, js, json...), compression reduces network load by sending the gzipped content instead of the original content. Most browsers support it.

The main change in is method send_head() of SimpleHTTPRequestHandler. If the request includes the header Accept-Encoding and its content includes "gzip", and if the content type determined by the file extension is in a list of compressed types, then the response includes the header Content-Encoding set to "gzip", the content type is set to that of the gzipped content, and the file object returned by send_head() points to the gzipped content.

If the original file is small, its content is loaded in memory and gzipped, the file object is and instance of io.BytesIO. If it is big, the gzipped content is stored in a temporary file.

The PR changes module mimetypes to add the mapping of extension ".json" to content type "application/json".

https://bugs.python.org/issue30576

@@ -219,6 +227,12 @@ math
New :func:`~math.remainder` function, implementing the IEEE 754-style remainder
operation. (Contributed by Mark Dickinson in :issue:`29962`.)

mimetypes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be in an other PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as below about extension .json

@@ -7,6 +7,7 @@
It does, however, optionally implement HTTP/1.1 persistent connections,
as of version 0.3.


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in f9963f0

Lib/mimetypes.py Outdated
@@ -439,6 +439,7 @@ def _default_mime_types():
'.jpeg' : 'image/jpeg',
'.jpg' : 'image/jpeg',
'.js' : 'application/javascript',
'.json' : 'application/json',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in an other PR ? it's out of context

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not totally out of context : files with extension .json are obvious candidates for compression, this is why I have included this minor change in mimetypes, thinking that it doesn't deserve a specific PR. But if you think it's better to open another one, I will.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, any change to mimetypes should be a separate issue/PR.

self.cwd = os.getcwd()
basetempdir = tempfile.gettempdir()
os.chdir(basetempdir)
self.data = 10 * b'We are the knights who say Ni!'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and a test with a big file ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 586b8c3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matrixise
Are you ok with the changes made since the first version, or do you see other things to do before the PR can be merged ?
If you think a separate PR is better for mimetypes I will submit it, it's just that I prefered to avoid the overhead of reviewing 2 PRs instead of one.

Copy link
Member

@bitdancer bitdancer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already told you that a separate issue and PR are required for any change to mimetypes. Make me nosy on the issue, please (r.david.murray on the bug tracker).

Lib/mimetypes.py Outdated
@@ -439,6 +439,7 @@ def _default_mime_types():
'.jpeg' : 'image/jpeg',
'.jpg' : 'image/jpeg',
'.js' : 'application/javascript',
'.json' : 'application/json',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, any change to mimetypes should be a separate issue/PR.

@PierreQuentel
Copy link
Contributor Author

@bitdancer
Ok, I have removed the mimetype changes from this PR and opened an issue on the tracker

@PierreQuentel
Copy link
Contributor Author

@bitdancer @matrixise
With mimetypes now out of the scope, are there other things to do before the PR can be merged ?

@PierreQuentel
Copy link
Contributor Author

@vadmium
I think I have made all the requested changes on the module, the test script and the documentation.

@PierreQuentel
Copy link
Contributor Author

PierreQuentel commented Nov 23, 2017

@rhettinger
Do you think you will have time to review this PR on time for inclusion in Python 3.7 ?

@joshenders
Copy link

Anyone able to review this? Ended up running against an issue today where this would have been nice to have.

@jerrymarino
Copy link

+1 I'd be great to see this one land 👍

@brettcannon
Copy link
Member

Thanks for the PR, @PierreQuentel , but I'm closing this as me and various other core devs can't quite muster enough excitement about what this feature brings to warrant wanting to support it for a couple of decades.

@joshenders
Copy link

joshenders commented Sep 12, 2018 via email

@sijojlouis
Copy link

+1

@PierreQuentel
Copy link
Contributor Author

The module is released as httpcompressionserver on PyPI.

@sijojlouis
Copy link

@PierreQuentel Great! thanks for this, I’ll try it out soon.

I have a suggestion for running (usage command) the module, I loved the simplicity of http.server - so was thinking if this could be so too, like http.eserver (e for encoded from html header or maybe c for compression).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.