Skip to content

Commit 5427072

Browse files
authored
chore(cgi): include parse_header() from the deprecated cgi module (#46)
* Include functions from the deprecated cgi module * Document source of vendored functions from cgi.py * Fix linter error
1 parent fa7dd33 commit 5427072

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

mimeparse.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import cgi
2-
31
__version__ = '1.6.0'
42
__author__ = 'Joe Gregorio'
53
__email__ = 'joe@bitworking.org'
@@ -11,6 +9,44 @@ class MimeTypeParseException(ValueError):
119
pass
1210

1311

12+
# Vendored version of cgi._parseparam from Python 3.11 (deprecated and slated
13+
# for removal in 3.13)
14+
def _parseparam(s):
15+
while s[:1] == ';':
16+
s = s[1:]
17+
end = s.find(';')
18+
while end > 0 and (s.count('"', 0, end) - s.count('\\"', 0, end)) % 2:
19+
end = s.find(';', end + 1)
20+
if end < 0:
21+
end = len(s)
22+
f = s[:end]
23+
yield f.strip()
24+
s = s[end:]
25+
26+
27+
# Vendored version of cgi.parse_header from Python 3.11 (deprecated and slated
28+
# for removal in 3.13)
29+
def _parse_header(line):
30+
"""Parse a Content-type like header.
31+
32+
Return the main content-type and a dictionary of options.
33+
34+
"""
35+
parts = _parseparam(';' + line)
36+
key = parts.__next__()
37+
pdict = {}
38+
for p in parts:
39+
i = p.find('=')
40+
if i >= 0:
41+
name = p[:i].strip().lower()
42+
value = p[i + 1:].strip()
43+
if len(value) >= 2 and value[0] == value[-1] == '"':
44+
value = value[1:-1]
45+
value = value.replace('\\\\', '\\').replace('\\"', '"')
46+
pdict[name] = value
47+
return key, pdict
48+
49+
1450
def parse_mime_type(mime_type):
1551
"""Parses a mime-type into its component parts.
1652
@@ -23,7 +59,7 @@ def parse_mime_type(mime_type):
2359
2460
:rtype: (str,str,dict)
2561
"""
26-
full_type, params = cgi.parse_header(mime_type)
62+
full_type, params = _parse_header(mime_type)
2763
# Java URLConnection class sends an Accept header that includes a
2864
# single '*'. Turn it into a legal wildcard.
2965
if full_type == '*':

0 commit comments

Comments
 (0)