-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -639,6 +639,11 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): | |
|
||
server_version = "SimpleHTTP/" + __version__ | ||
|
||
extensions_map = { | ||
'': 'application/octet-stream', # Default, | ||
**mimetypes.types_map | ||
} | ||
|
||
def __init__(self, *args, directory=None, **kwargs): | ||
if directory is None: | ||
directory = os.getcwd() | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
By manual overrides, are you talking about the Maybe we should replace this line with: self.extensions_map = {
'': 'application/octet-stream',
**mimetypes.types_map
} What do you think? |
||
|
||
base, ext = posixpath.splitext(path) | ||
if ext in self.extensions_map: | ||
|
@@ -867,16 +875,6 @@ def guess_type(self, path): | |
else: | ||
return self.extensions_map[''] | ||
|
||
if not mimetypes.inited: | ||
mimetypes.init() # try to read system mime.types | ||
extensions_map = mimetypes.types_map.copy() | ||
extensions_map.update({ | ||
'': 'application/octet-stream', # Default | ||
'.py': 'text/plain', | ||
'.c': 'text/plain', | ||
'.h': 'text/plain', | ||
}) | ||
|
||
|
||
# Utilities for CGIHTTPRequestHandler | ||
|
||
|
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 is missing the
.py
,.c
and.h
defaults as well. They certainly aren't guaranteed to be available.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.
Oh, I didn't see that
types_map
is already partially initialized. This bit is fine then - leave it as it is.