Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9654daf

Browse files
gh-66543: Fix mimetype.guess_type() (GH-117217)
Fix parsing of the following corner cases: * URLs with only a host name * URLs containing a fragment * URLs containing a query * filenames with only a UNC sharepoint on Windows Co-authored-by: Dong-hee Na <[email protected]>
1 parent 8bef34f commit 9654daf

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

Lib/mimetypes.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ def guess_type(self, url, strict=True):
120120
but non-standard types.
121121
"""
122122
url = os.fspath(url)
123-
scheme, url = urllib.parse._splittype(url)
123+
p = urllib.parse.urlparse(url)
124+
if p.scheme and len(p.scheme) > 1:
125+
scheme = p.scheme
126+
url = p.path
127+
else:
128+
scheme = None
129+
url = os.path.splitdrive(url)[1]
124130
if scheme == 'data':
125131
# syntax of data URLs:
126132
# dataurl := "data:" [ mediatype ] [ ";base64" ] "," data

Lib/test/test_mimetypes.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import io
22
import mimetypes
3+
import os
34
import pathlib
45
import sys
56
import unittest.mock
@@ -109,15 +110,40 @@ def test_filename_with_url_delimiters(self):
109110
# compared to when interpreted as filename because of the semicolon.
110111
eq = self.assertEqual
111112
gzip_expected = ('application/x-tar', 'gzip')
112-
eq(self.db.guess_type(";1.tar.gz"), gzip_expected)
113-
eq(self.db.guess_type("?1.tar.gz"), gzip_expected)
114-
eq(self.db.guess_type("#1.tar.gz"), gzip_expected)
115-
eq(self.db.guess_type("#1#.tar.gz"), gzip_expected)
116-
eq(self.db.guess_type(";1#.tar.gz"), gzip_expected)
117-
eq(self.db.guess_type(";&1=123;?.tar.gz"), gzip_expected)
118-
eq(self.db.guess_type("?k1=v1&k2=v2.tar.gz"), gzip_expected)
113+
for name in (
114+
';1.tar.gz',
115+
'?1.tar.gz',
116+
'#1.tar.gz',
117+
'#1#.tar.gz',
118+
';1#.tar.gz',
119+
';&1=123;?.tar.gz',
120+
'?k1=v1&k2=v2.tar.gz',
121+
):
122+
for prefix in ('', '/', '\\',
123+
'c:', 'c:/', 'c:\\', 'c:/d/', 'c:\\d\\',
124+
'//share/server/', '\\\\share\\server\\'):
125+
path = prefix + name
126+
with self.subTest(path=path):
127+
eq(self.db.guess_type(path), gzip_expected)
128+
expected = (None, None) if os.name == 'nt' else gzip_expected
129+
for prefix in ('//', '\\\\', '//share/', '\\\\share\\'):
130+
path = prefix + name
131+
with self.subTest(path=path):
132+
eq(self.db.guess_type(path), expected)
119133
eq(self.db.guess_type(r" \"\`;b&b&c |.tar.gz"), gzip_expected)
120134

135+
def test_url(self):
136+
result = self.db.guess_type('http://host.html')
137+
msg = 'URL only has a host name, not a file'
138+
self.assertSequenceEqual(result, (None, None), msg)
139+
result = self.db.guess_type('http://example.com/host.html')
140+
msg = 'Should be text/html'
141+
self.assertSequenceEqual(result, ('text/html', None), msg)
142+
result = self.db.guess_type('http://example.com/host.html#x.tar')
143+
self.assertSequenceEqual(result, ('text/html', None))
144+
result = self.db.guess_type('http://example.com/host.html?q=x.tar')
145+
self.assertSequenceEqual(result, ('text/html', None))
146+
121147
def test_guess_all_types(self):
122148
# First try strict. Use a set here for testing the results because if
123149
# test_urllib2 is run before test_mimetypes, global state is modified

Lib/test/test_urllib2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ def connect_ftp(self, user, passwd, host, port, dirs,
777777
["foo", "bar"], "", None),
778778
("ftp://localhost/baz.gif;type=a",
779779
"localhost", ftplib.FTP_PORT, "", "", "A",
780-
[], "baz.gif", None), # XXX really this should guess image/gif
780+
[], "baz.gif", "image/gif"),
781781
]:
782782
req = Request(url)
783783
req.timeout = None
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Make :func:`mimetypes.guess_type` properly parsing of URLs with only a host
2+
name, URLs containing fragment or query, and filenames with only a UNC
3+
sharepoint on Windows.
4+
Based on patch by Dong-hee Na.

0 commit comments

Comments
 (0)