Skip to content

Ignore utf-8 decode errors when processing text content types bodies #595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions internal/devtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,25 @@ def get_response_body(self, request_id, wait):
if os.path.exists(body_file_path):
self.netlog_requests[netlog_id]['body_claimed'] = True
found = True
logging.debug('Matched netlog response body %s for %s', netlog_id, request['url'])
self.process_response_body(request_id, None, body_file_path)
logging.debug('Matched netlog response body %s to %s for %s', netlog_id, request_id, request['url'])
# For text-based responses, ignore any utf-8 decode errors so we can err on the side of getting more text bodies
errors=None
try:
if 'response_headers' in self.netlog_requests[netlog_id]:
headers = self.extract_headers(self.netlog_requests[netlog_id]['response_headers'])
content_type = self.get_header_value(headers, 'Content-Type')
if content_type is not None:
content_type = content_type.lower()
text_types = ['application/json',
'application/xhtml+xml',
'application/xml',
'application/ld+json',
'application/javascript']
if content_type.startswith('text/') or content_type in text_types:
errors = 'ignore'
except Exception:
logging.exception('Error processing content type for response body')
self.process_response_body(request_id, None, body_file_path, errors)
if not found and len(self.netlog_requests):
logging.debug('Unable to match netlog response body for %s', request['url'])
except Exception:
Expand Down Expand Up @@ -736,7 +753,7 @@ def get_response_body(self, request_id, wait):
if wait:
self.process_response_body(request_id, response)

def process_response_body(self, request_id, response, netlog_body_file=None):
def process_response_body(self, request_id, response, netlog_body_file=None, errors=None):
try:
request = self.get_request(request_id, True)
path = os.path.join(self.task['dir'], 'bodies')
Expand All @@ -747,7 +764,7 @@ def process_response_body(self, request_id, response, netlog_body_file=None):
body = None
if netlog_body_file is not None:
try:
with open(netlog_body_file, 'r', encoding='utf-8') as f:
with open(netlog_body_file, 'r', encoding='utf-8', errors=errors) as f:
body = f.read()
body = body.encode('utf-8')
is_text = True
Expand Down