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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,11 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

server_version = "SimpleHTTP/" + __version__

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.


def __init__(self, *args, directory=None, **kwargs):
if directory is None:
directory = os.getcwd()
Expand Down Expand Up @@ -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?


base, ext = posixpath.splitext(path)
if ext in self.extensions_map:
Expand All @@ -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

Expand Down