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
mimetypes.guess_type("//example.com") misinterprets host name as file name #66543
Comments
The documentation says that guess_type() takes a URL, but: >>> mimetypes.guess_type("http://example.com")
('application/x-msdownload', None) I suspect the MS download is a reference to *.com files (like DOS's command.com). My current workaround is to strip out the host name from the URL, since I cannot imagine it would be useful for determining the content type. I am also stripping the fragment part. An argument could probably be made for stripping the “;parameters” and “?query” parts as well. >>> # Workaround for mimetypes.guess_type("//example.com")
... # interpreting host name as file name
... url = urlparse("http://example.com")
>>> url = net.url_replace(url, netloc="", fragment="")
>>> url
'http://'
>>> mimetypes.guess_type(url, strict=False)
(None, None) |
Posting a patch to fix this. It passes the URL through a urlsplit() → urlunsplit() stage, while removing the scheme://netloc parts. |
The proposed patch I mentioned on bpo-35939 also solve the above situation. Python 3.8.0a1+ (heads/bpo-12317:96d37dbcd2, Feb 8 2019, 12:03:40)
[Clang 9.1.0 (clang-902.0.39.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import mimetypes
>>> mimetypes.guess_type("http://example.com")
(None, None)
>>> mimetypes.guess_type("example.com")
('application/x-msdownload', None)
>>> I've also added the unit tests of mimetypes-host.patch. It works well. Thanks alot! |
I think so, yes. Also, while you are at it, can you also close bpo-35939 with a comment that points to this issue and the right PR for the fix? |
Great! I will close bpo-35939 also. |
This change introduces a potential 3.7 regression; see bpo-38449. |
I am going to re-open this since the fixes were reverted in all the branches. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: