Skip to content

Commit

Permalink
bpo-22347: Update mimetypes.guess_type to allow proper parsing of URLs (
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 authored and miss-islington committed Sep 5, 2019
1 parent 6cd9666 commit 87bd207
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def guess_type(self, url, strict=True):
but non-standard types.
"""
url = os.fspath(url)
scheme, url = urllib.parse._splittype(url)
p = urllib.parse.urlparse(url)
scheme, url = p.scheme, p.path
if scheme == 'data':
# syntax of data URLs:
# dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ def test_non_standard_types(self):
eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')

def test_url(self):
result = self.db.guess_type('http://host.html')
msg = 'URL only has a host name, not a file'
self.assertSequenceEqual(result, (None, None), msg)
result = self.db.guess_type('http://example.com/host.html')
msg = 'Should be text/html'
self.assertSequenceEqual(result, ('text/html', None), msg)

def test_guess_all_types(self):
eq = self.assertEqual
unless = self.assertTrue
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ def connect_ftp(self, user, passwd, host, port, dirs,
["foo", "bar"], "", None),
("ftp://localhost/baz.gif;type=a",
"localhost", ftplib.FTP_PORT, "", "", "A",
[], "baz.gif", None), # XXX really this should guess image/gif
[], "baz.gif", "image/gif"),
]:
req = Request(url)
req.timeout = None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Update mimetypes.guess_type to allow proper parsing of URLs with only a host name.
Patch by Dong-hee Na.

0 comments on commit 87bd207

Please sign in to comment.