From 95f779f1f878ddf7b4bcb7125802761f82530725 Mon Sep 17 00:00:00 2001 From: Emilio Pozuelo Monfort Date: Thu, 16 Jul 2020 14:46:27 +0200 Subject: web_support.py: decode data when necessary We can't pass bytes under python3 --- lib/python/web_support.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/python/web_support.py b/lib/python/web_support.py index 334b36efe5..807edf2fdf 100644 --- a/lib/python/web_support.py +++ b/lib/python/web_support.py @@ -643,6 +643,12 @@ class RedirectResult(Result): self.status = 302 self.headers['Location'] = str(url) +def maybe_encode(obj): + try: + return obj.encode() + except: + return obj + class HTMLResult(Result): """An object of this class combines a status code with HTML contents.""" def __init__(self, contents, doctype='', status=200): @@ -674,6 +680,7 @@ class HTMLResult(Result): buf.write(s) self.contents.flatten(write_both) buf = buf.getvalue() + buf = maybe_encode(buf) self.headers['Content-Length'] = str(len(buf)) def later(req): headers_later(req) @@ -701,7 +708,7 @@ class BinaryResult(Result): def later(req): headers_later(req) if req.command != 'HEAD': - req.wfile.write(self.contents) + req.wfile.write(maybe_encode(self.contents)) return later class WebServiceBase: -- cgit v1.2.3