summaryrefslogtreecommitdiff
path: root/django/utils/http.py
diff options
context:
space:
mode:
authorCarlton Gibson <carlton.gibson@noumenal.es>2022-05-10 12:12:17 +0200
committerCarlton Gibson <carlton@noumenal.es>2022-05-11 14:06:31 +0200
commit34e2148fc725e7200050f74130d7523e3cd8507a (patch)
treeb4bc74515b1d4911c6c07d5cd7697c78eff91407 /django/utils/http.py
parent02dbf1667c6da61ea9346f7c9f174a158b896811 (diff)
Refs #33173 -- Removed use of deprecated cgi module.
https://peps.python.org/pep-0594/#cgi
Diffstat (limited to 'django/utils/http.py')
-rw-r--r--django/utils/http.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/django/utils/http.py b/django/utils/http.py
index 0292713235..6e2091bf52 100644
--- a/django/utils/http.py
+++ b/django/utils/http.py
@@ -366,3 +366,36 @@ def escape_leading_slashes(url):
if url.startswith("//"):
url = "/%2F{}".format(url[2:])
return url
+
+
+def _parseparam(s):
+ while s[:1] == ";":
+ s = s[1:]
+ end = s.find(";")
+ while end > 0 and (s.count('"', 0, end) - s.count('\\"', 0, end)) % 2:
+ end = s.find(";", end + 1)
+ if end < 0:
+ end = len(s)
+ f = s[:end]
+ yield f.strip()
+ s = s[end:]
+
+
+def parse_header_parameters(line):
+ """
+ Parse a Content-type like header.
+ Return the main content-type and a dictionary of options.
+ """
+ parts = _parseparam(";" + line)
+ key = parts.__next__()
+ pdict = {}
+ for p in parts:
+ i = p.find("=")
+ if i >= 0:
+ name = p[:i].strip().lower()
+ value = p[i + 1 :].strip()
+ if len(value) >= 2 and value[0] == value[-1] == '"':
+ value = value[1:-1]
+ value = value.replace("\\\\", "\\").replace('\\"', '"')
+ pdict[name] = value
+ return key, pdict