Repository navigation
mimetypes.guess_type("//example.com") misinterprets host name as file name #66543
Copy link
Copy link
Closed
Labels
3.11only security fixesonly security fixes3.12only security fixesonly security fixes3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
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:
assignee = None
closed_at = None
created_at = <Date 2014-09-06.02:52:37.225>
labels = ['3.7', '3.8', 'type-bug', 'library']
title = 'mimetypes.guess_type("//example.com") misinterprets host name as file name'
updated_at = <Date 2019-10-15.07:30:25.613>
user = 'https://github.com/vadmium'bugs.python.org fields:
activity = <Date 2019-10-15.07:30:25.613>
actor = 'ned.deily'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2014-09-06.02:52:37.225>
creator = 'martin.panter'
dependencies = ['35939']
files = ['38222']
hgrepos = []
issue_num = 22347
keywords = ['patch']
message_count = 15.0
messages = ['226467', '236479', '335123', '351156', '351157', '351158', '351162', '351164', '351167', '354471', '354521', '354535', '354538', '354543', '354697']
nosy_count = 5.0
nosy_names = ['ned.deily', 'martin.panter', 'maxking', 'corona10', 'miss-islington']
pr_nums = ['15522', '15685', '15687', '16724', '16725', '16727', '16728']
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue22347'
versions = ['Python 3.7', 'Python 3.8']Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.11only security fixesonly security fixes3.12only security fixesonly security fixes3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
vadmium commentedon Sep 6, 2014
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.
vadmium commentedon Feb 24, 2015
Posting a patch to fix this. It passes the URL through a urlsplit() â urlunsplit() stage, while removing the scheme://netloc parts.
corona10 commentedon Feb 9, 2019
The proposed patch I mentioned on bpo-35939 also solve the above situation.
I've also added the unit tests of mimetypes-host.patch. It works well.
I think that we close this issue also when the bpo-35939 is closed.
Thanks alot!
miss-islington commentedon Sep 5, 2019
New changeset 87bd207 by Miss Islington (bot) (Dong-hee Na) in branch 'master':
bpo-22347: Update mimetypes.guess_type to allow proper parsing of URLs (GH-15522)
87bd207
miss-islington commentedon Sep 5, 2019
New changeset 6d7a786 by Miss Islington (bot) in branch '3.8':
bpo-22347: Update mimetypes.guess_type to allow proper parsing of URLs (GH-15522)
6d7a786
miss-islington commentedon Sep 5, 2019
New changeset 8873bff by Miss Islington (bot) (Dong-hee Na) in branch '3.7':
[3.7] bpo-22347: Update mimetypes.guess_type to allow proper parsing of URLs (GH-15522) (GH-15687)
8873bff
corona10 commentedon Sep 5, 2019
@vstinner(my mentor) @maxking
Now this issue is solved.
I'd like to close this issue. Is it okay?
maxking commentedon Sep 5, 2019
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?
corona10 commentedon Sep 5, 2019
Great! I will close bpo-35939 also.
ned-deily commentedon Oct 11, 2019
This change introduces a potential 3.7 regression; see bpo-38449.
15 remaining items
gh-66543: Fix mimetype.guess_type() (GH-117217)
pythongh-66543: Fix mimetype.guess_type() (pythonGH-117217)
pythongh-66543: Fix mimetype.guess_type() (pythonGH-117217)
[3.12] gh-66543: Fix mimetype.guess_type() (GH-117217) (GH-117256)
[3.11] gh-66543: Fix mimetype.guess_type() (GH-117217) (GH-117257)
pythongh-66543: Add mimetypes.guess_file_type()
serhiy-storchaka commentedon Mar 26, 2024
#117258 adds
guess_file_type(). Passing file path inguess_type()is deprecated in documentation only. There are too many uses ofguess_type()with a file path, so emitting a deprecation warning at runtime will be added many years later.In addition,
guess_file_type()works with bytes paths.guess_type()always returns(None, None)and may emit a bytes warning.Yet one difference: file
foo\.htmlis considered an HTML file on Posix, but has an unknown MIME type on Windows.pythongh-66543: Add mimetypes.guess_file_type()
Squashed commit of the following:
Squashed commit of the following:
pythongh-66543: Fix mimetype.guess_type() (pythonGH-117217)
gh-66543: Add mimetypes.guess_file_type() (GH-117258)
pythongh-66543: Add mimetypes.guess_file_type() (pythonGH-117258)