Skip to content

Commit 2d356e7

Browse files
author
jweigel
committed
Properly decode html response data based on specified charset.
1 parent 23471b2 commit 2d356e7

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

ebaysdk/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from ebaysdk.utils import xml2dict, dict2xml, list2xml, object_dict
2727

28+
content_type_re = re.compile('^Content\-Type:\s([^;]+);charset=(.*?)$', re.I)
2829

2930
def get_version():
3031
"Get the version."
@@ -357,6 +358,24 @@ def _process_http_request(self):
357358
self._response_reason = re.match(r'^HTTP.+? +\d+ +(.*) *$', self._response_status).group(1)
358359
response_data = self._response_body.getvalue()
359360

361+
# Look for charset specifier and decode the response_data
362+
# based on it.
363+
for header in self._response_header.getvalue().splitlines():
364+
header_match = content_type_re.match(header)
365+
366+
try:
367+
if header_match and header_match.group(2):
368+
response_data = response_data.decode(header_match.group(2))
369+
break
370+
except Exception as e:
371+
charset = ''
372+
try:
373+
charset = header_match.group(2)
374+
except Exception:
375+
pass
376+
377+
sys.stderr.write("Failed to decode response using charset %s (%s)\n" % (charset, str(e)))
378+
360379
self._response_header = None
361380
self._response_body = None
362381
self._curl.close()

0 commit comments

Comments
 (0)