From 10dfca1a753f8a7a00c813b194589313cac1ddd4 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Sun, 19 Aug 2012 23:19:07 +0300 Subject: [PATCH] =?UTF-8?q?Decode=20data=20in=20python3=20=E2=80=93=20Fixe?= =?UTF-8?q?s=20#265.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In both in python2 and python3 parse_qs expects str object. In python2 it worked ok, because self.rfile was open in binary mode and str in python2 is actually a string of bytes. However in python3 str is actually string of unicode literals, not bytes and file was still open in binary mode. Thus, deleting any file with non-ascii byte inside filename failed in python3. Also, cgi.parse_qs is deprecated and shouldn't be used. --- share/tools/web_config/webconfig.py | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/share/tools/web_config/webconfig.py b/share/tools/web_config/webconfig.py index ce44b26c8..c90c58686 100755 --- a/share/tools/web_config/webconfig.py +++ b/share/tools/web_config/webconfig.py @@ -7,9 +7,11 @@ IS_PY2 = sys.version_info[0] == 2 if IS_PY2: import SimpleHTTPServer import SocketServer + from urlparse import parse_qs as parse_qs else: import http.server as SimpleHTTPServer import socketserver as SocketServer + from urllib.parse import parse_qs as parse_qs import webbrowser import subprocess import re, json, socket, os, sys, cgi, select, time @@ -285,6 +287,7 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) else: # Python 3 ctype, pdict = cgi.parse_header(self.headers['content-type']) + if ctype == 'multipart/form-data': postvars = cgi.parse_multipart(self.rfile, pdict) elif ctype == 'application/x-www-form-urlencoded': @@ -292,7 +295,11 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): length = int(self.headers.getheader('content-length')) except AttributeError: length = int(self.headers['content-length']) - postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1) + + data = self.rfile.read(length) + if not IS_PY2: + data = data.decode('utf-8') + postvars = parse_qs(data, keep_blank_values=1) else: postvars = {} @@ -303,18 +310,6 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): bold = postvars.get('bold') underline = postvars.get('underline') - if what == None: #Will be None for python3 - what = postvars.get(b'what') - what[0] = str(what[0]).lstrip("b'").rstrip("'") - color = postvars.get(b'color') - color[0] = str(color[0]).lstrip("b'").rstrip("'") - background_color = postvars.get(b'background_color') - background_color[0] = str(background_color[0]).lstrip("b'").rstrip("'") - bold = postvars.get(b'bold') - bold[0] = str(bold[0]).lstrip("b'").rstrip("'") - underline = postvars.get(b'underline') - underline[0] = str(underline[0]).lstrip("b'").rstrip("'") - if what: # Not sure why we get lists here? output = self.do_set_color_for_variable(what[0], color[0], background_color[0], parse_bool(bold[0]), parse_bool(underline[0])) @@ -322,15 +317,9 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): output = 'Bad request' elif p == '/get_function/': what = postvars.get('what') - if what == None: #Will be None for python3 - what = postvars.get(b'what') - what[0] = str(what[0]).lstrip("b'").rstrip("'") output = [self.do_get_function(what[0])] elif p == '/delete_history_item/': what = postvars.get('what') - if what == None: #Will be None for python3 - what = postvars.get(b'what') - what[0] = str(what[0]).lstrip("b'").rstrip("'") if self.do_delete_history_item(what[0]): output = ["OK"] else: