1
- import cgi
2
-
3
1
__version__ = '1.6.0'
4
2
__author__ = 'Joe Gregorio'
5
3
__email__ = 'joe@bitworking.org'
@@ -11,6 +9,44 @@ class MimeTypeParseException(ValueError):
11
9
pass
12
10
13
11
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
+
14
50
def parse_mime_type (mime_type ):
15
51
"""Parses a mime-type into its component parts.
16
52
@@ -23,7 +59,7 @@ def parse_mime_type(mime_type):
23
59
24
60
:rtype: (str,str,dict)
25
61
"""
26
- full_type , params = cgi . parse_header (mime_type )
62
+ full_type , params = _parse_header (mime_type )
27
63
# Java URLConnection class sends an Accept header that includes a
28
64
# single '*'. Turn it into a legal wildcard.
29
65
if full_type == '*' :
0 commit comments