Skip to content

Commit 8873bff

Browse files
corona10miss-islington
authored andcommitted
[3.7] bpo-22347: Update mimetypes.guess_type to allow proper parsing of URLs (GH-15522) (GH-15687)
https://bugs.python.org/issue22347 (cherry picked from commit 87bd207) https://bugs.python.org/issue22347 Automerge-Triggered-By: @maxking
1 parent b4f6e44 commit 8873bff

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

Lib/mimetypes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ def guess_type(self, url, strict=True):
114114
but non-standard types.
115115
"""
116116
url = os.fspath(url)
117-
scheme, url = urllib.parse.splittype(url)
117+
p = urllib.parse.urlparse(url)
118+
scheme, url = p.scheme, p.path
118119
if scheme == 'data':
119120
# syntax of data URLs:
120121
# dataurl := "data:" [ mediatype ] [ ";base64" ] "," data

Lib/test/test_mimetypes.py

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ def test_non_standard_types(self):
5050
eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
5151
eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
5252

53+
def test_url(self):
54+
result = self.db.guess_type('http://host.html')
55+
msg = 'URL only has a host name, not a file'
56+
self.assertSequenceEqual(result, (None, None), msg)
57+
result = self.db.guess_type('http://example.com/host.html')
58+
msg = 'Should be text/html'
59+
self.assertSequenceEqual(result, ('text/html', None), msg)
60+
5361
def test_guess_all_types(self):
5462
eq = self.assertEqual
5563
unless = self.assertTrue

Lib/test/test_urllib2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ def connect_ftp(self, user, passwd, host, port, dirs,
746746
["foo", "bar"], "", None),
747747
("ftp://localhost/baz.gif;type=a",
748748
"localhost", ftplib.FTP_PORT, "", "", "A",
749-
[], "baz.gif", None), # XXX really this should guess image/gif
749+
[], "baz.gif", "image/gif"),
750750
]:
751751
req = Request(url)
752752
req.timeout = None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update mimetypes.guess_type to allow proper parsing of URLs with only a host name.
2+
Patch by Dong-hee Na.

0 commit comments

Comments
 (0)