]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Replace cgi which will be deprecated in Python 3.11 (#2309)
authorFlorimond Manca <florimond.manca@protonmail.com>
Thu, 25 Aug 2022 10:23:04 +0000 (12:23 +0200)
committerGitHub <noreply@github.com>
Thu, 25 Aug 2022 10:23:04 +0000 (12:23 +0200)
* Replace cgi which will be deprecated in Python 3.11

* Update httpx/_utils.py

httpx/_models.py
httpx/_utils.py

index fd1d7fe9a14b54dd3f23bb0bb21442a685c2f320..7a3b5885df935a706ff7c727c0dc339db0044279 100644 (file)
@@ -1,4 +1,3 @@
-import cgi
 import datetime
 import email.message
 import json as jsonlib
@@ -47,6 +46,7 @@ from ._utils import (
     normalize_header_key,
     normalize_header_value,
     obfuscate_sensitive_headers,
+    parse_content_type_charset,
     parse_header_links,
 )
 
@@ -608,11 +608,7 @@ class Response:
         if content_type is None:
             return None
 
-        _, params = cgi.parse_header(content_type)
-        if "charset" not in params:
-            return None
-
-        return params["charset"].strip("'\"")
+        return parse_content_type_charset(content_type)
 
     def _get_content_decoder(self) -> ContentDecoder:
         """
index e01c050dfaf7e4d7b0a7784f718a2749463f4254..ecce4f4175e3eb3c230ea6a297e0729de13f548e 100644 (file)
@@ -1,4 +1,5 @@
 import codecs
+import email.message
 import logging
 import mimetypes
 import netrc
@@ -209,6 +210,14 @@ def parse_header_links(value: str) -> typing.List[typing.Dict[str, str]]:
     return links
 
 
+def parse_content_type_charset(content_type: str) -> typing.Optional[str]:
+    # We used to use `cgi.parse_header()` here, but `cgi` became a dead battery.
+    # See: https://peps.python.org/pep-0594/#cgi
+    msg = email.message.Message()
+    msg["content-type"] = content_type
+    return msg.get_content_charset(failobj=None)
+
+
 SENSITIVE_HEADERS = {"authorization", "proxy-authorization"}