1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-15 00:03:28 +01:00

try to remove headers with illegal characters. arquivo/pwa-technologies#774 (#536)

This commit is contained in:
Daniel Bicho 2020-05-01 00:14:04 +01:00 committed by GitHub
parent 92e459bda5
commit 6b014d05bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
"""
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)