Skip to content

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

Closed
onyb wants to merge 1 commit into
python:masterfrom
onyb:fix-issue-35292
Closed

bpo-35292: Lazily inject system mime.types into SimpleHTTPRequestHandler.extensions_map#11036
onyb wants to merge 1 commit into
python:masterfrom
onyb:fix-issue-35292

Conversation

@onyb
Copy link
Copy Markdown
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
Copy Markdown
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 :)

Comment thread Lib/http/server.py
extensions_map = {
'': 'application/octet-stream', # Default,
**mimetypes.types_map
}
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread Lib/http/server.py
"""
if not mimetypes.inited:
mimetypes.init() # try to read system mime.types
self.extensions_map.update(mimetypes.types_map)
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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