Skip to content

bpo-35292: Lazily inject system mime.types into SimpleHTTPRequestHandler.extensions_map #11036

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

Closed
wants to merge 1 commit into from

Conversation

onyb
Copy link
Contributor

@onyb onyb commented Dec 8, 2018

Important changes

  • When http.server is imported, the extension_map class attribute of SimpleHTTPRequestHandler is initialized with a default mapping, and some static mapping already available in mimetypes.types_map.
  • The time delay in importing the aforementioned module is now deferred to the first call of SimpleHTTPRequestHandler.guess_type method.
  • The mimetype for .py extension is now text/x-python instead of text/plain.

https://bugs.python.org/issue35292

Copy link
Member

@zooba zooba left a comment

Choose a reason for hiding this comment

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

A couple of things to fix, but view my suggestion on the bug in case you like my next idea better :)

extensions_map = {
'': 'application/octet-stream', # Default,
**mimetypes.types_map
}
Copy link
Member

Choose a reason for hiding this comment

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

This is missing the .py, .c and .h defaults as well. They certainly aren't guaranteed to be available.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I didn't see that types_map is already partially initialized. This bit is fine then - leave it as it is.

@@ -857,6 +862,9 @@ def guess_type(self, path):
slow) to look inside the data to make a better guess.

"""
if not mimetypes.inited:
mimetypes.init() # try to read system mime.types
self.extensions_map.update(mimetypes.types_map)
Copy link
Member

Choose a reason for hiding this comment

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

This is actually going to update in the wrong order - it will overwrite any manual overrides with the original type.

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 will overwrite any manual overrides with the original type

By manual overrides, are you talking about the '': 'application/octet-stream' pair?

Maybe we should replace this line with:

self.extensions_map = {
   '': 'application/octet-stream',
   **mimetypes.types_map
}

What do you think?

@JulienPalard
Copy link
Member

@onyb Did you had time to check for https://bugs.python.org/issue35292#msg331739? I actually think the idea is good.

@zooba
Copy link
Member

zooba commented Jan 8, 2020

The fix in #17822 makes this one unnecessary. Thanks for working on it, though!

@zooba zooba closed this Jan 8, 2020
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.

5 participants