Commit 87bd2071 authored by Dong-hee Na's avatar Dong-hee Na Committed by Miss Islington (bot)

bpo-22347: Update mimetypes.guess_type to allow proper parsing of URLs (GH-15522)



https://bugs.python.org/issue22347
parent 6cd9666c
...@@ -114,7 +114,8 @@ class MimeTypes: ...@@ -114,7 +114,8 @@ class MimeTypes:
but non-standard types. but non-standard types.
""" """
url = os.fspath(url) url = os.fspath(url)
scheme, url = urllib.parse._splittype(url) p = urllib.parse.urlparse(url)
scheme, url = p.scheme, p.path
if scheme == 'data': if scheme == 'data':
# syntax of data URLs: # syntax of data URLs:
# dataurl := "data:" [ mediatype ] [ ";base64" ] "," data # dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
......
...@@ -51,6 +51,14 @@ class MimeTypesTestCase(unittest.TestCase): ...@@ -51,6 +51,14 @@ class MimeTypesTestCase(unittest.TestCase):
eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None)) eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
eq(self.db.guess_extension('image/jpg', strict=False), '.jpg') 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): def test_guess_all_types(self):
eq = self.assertEqual eq = self.assertEqual
unless = self.assertTrue unless = self.assertTrue
......
...@@ -742,7 +742,7 @@ class HandlerTests(unittest.TestCase): ...@@ -742,7 +742,7 @@ class HandlerTests(unittest.TestCase):
["foo", "bar"], "", None), ["foo", "bar"], "", None),
("ftp://localhost/baz.gif;type=a", ("ftp://localhost/baz.gif;type=a",
"localhost", ftplib.FTP_PORT, "", "", "A", "localhost", ftplib.FTP_PORT, "", "", "A",
[], "baz.gif", None), # XXX really this should guess image/gif [], "baz.gif", "image/gif"),
]: ]:
req = Request(url) req = Request(url)
req.timeout = None req.timeout = None
......
Update mimetypes.guess_type to allow proper parsing of URLs with only a host name.
Patch by Dong-hee Na.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment