From 6b014d05bfeae070af3e1f150064c1c3d39893a8 Mon Sep 17 00:00:00 2001 From: Daniel Bicho Date: Fri, 1 May 2020 00:14:04 +0100 Subject: [PATCH] try to remove headers with illegal characters. arquivo/pwa-technologies#774 (#536) --- pywb/apps/wbrequestresponse.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/pywb/apps/wbrequestresponse.py b/pywb/apps/wbrequestresponse.py index b0f0f8d9..1d13f748 100644 --- a/pywb/apps/wbrequestresponse.py +++ b/pywb/apps/wbrequestresponse.py @@ -142,6 +142,18 @@ class WbResponse(object): response.add_access_control_headers(env=env) return response + def try_fix_errors(self): + """Utility method to try remove faulty headers from response. + + :return: + :rtype: None + """ + for header in self.status_headers.headers: + try: + header[1].encode('latin1') + except UnicodeError: + self.status_headers.remove_header(header[0]) + def __call__(self, env, start_response): """Callable definition to allow WbResponse control over how the response is sent @@ -149,8 +161,14 @@ class WbResponse(object): :param function start_response: The WSGI start_response function :return: The response body """ - start_response(self.status_headers.statusline, - self.status_headers.headers) + try: + start_response(self.status_headers.statusline, + self.status_headers.headers) + except UnicodeError: + self.try_fix_errors() + start_response(self.status_headers.statusline, + self.status_headers.headers) + request_method = env['REQUEST_METHOD'] if request_method == 'HEAD' or request_method == 'OPTIONS' or self.status_headers.statusline.startswith('304'): no_except_close(self.body)