From e33255559624c0b603a4ccfb7f351d4ecb4bd699 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Sat, 2 Jan 2021 16:36:24 +0100 Subject: [PATCH] Webconfig: Remove dependency on cgi module This is slated for removal in python 3.10, see https://www.python.org/dev/peps/pep-0594/#cgi. We currently only use it for three things: - escape_html in old python versions that didn't have that in the html module - Parsing multipart/form-data - Figuring out the charset for json We keep the first one - if loading escape_html from html fails we fall back to cgi. We remove the second - I can't find any case where we use multipart/form-data. Any place we post data we either explicitly pass application/x-www-form-urlencoded or implicitly use application/json. The third is the tricky bit. This drops charset detection under the assumption that we're never going to encounter anything other than utf-8 (or ascii, which is a utf-8 subset). I'm not sure that holds, but if it doesn't we can just add a regex to parse the charset. --- share/tools/web_config/webconfig.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/share/tools/web_config/webconfig.py b/share/tools/web_config/webconfig.py index a46aacbaa..c6529ea26 100755 --- a/share/tools/web_config/webconfig.py +++ b/share/tools/web_config/webconfig.py @@ -1,7 +1,6 @@ from __future__ import unicode_literals from __future__ import print_function import binascii -import cgi try: from html import escape as escape_html @@ -1315,17 +1314,19 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): return self.send_error(403) self.path = p - ctype, pdict = cgi.parse_header(self.headers["content-type"]) + # This is cheesy, we want just the actual content-type. + # In some cases it'll give us the encoding as well, + # ("application/json;charset=utf-8") + # but we don't currently care. + ctype = self.headers["content-type"].split(";")[0] - if ctype == "multipart/form-data": - postvars = cgi.parse_multipart(self.rfile, pdict) - elif ctype == "application/x-www-form-urlencoded": + if ctype == "application/x-www-form-urlencoded": length = int(self.headers["content-length"]) url_str = self.rfile.read(length).decode("utf-8") postvars = parse_qs(url_str, keep_blank_values=1) elif ctype == "application/json": length = int(self.headers["content-length"]) - url_str = self.rfile.read(length).decode(pdict["charset"]) + url_str = self.rfile.read(length).decode("utf-8") postvars = json.loads(url_str) else: postvars = {}