From 6da7e22a4ab07f4b5a573d37a6149a2d5cfdb7af Mon Sep 17 00:00:00 2001 From: Denis Ryzhkov Date: Mon, 11 Feb 2013 15:37:58 +0300 Subject: [PATCH 1/2] Fix of UnicodeDecodeError on unicode header name that can be converted to ascii. --- AUTHORS.rst | 1 + requests/models.py | 2 ++ test_requests.py | 6 +++++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 078ed993..6bbddfde 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -120,3 +120,4 @@ Patches and Suggestions - David Bonner @rascalking - Vinod Chandru - Johnny Goodnow +- Denis Ryzhkov diff --git a/requests/models.py b/requests/models.py index a845b44e..2ca5faa4 100644 --- a/requests/models.py +++ b/requests/models.py @@ -325,6 +325,8 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): """Prepares the given HTTP headers.""" if headers: + if is_py2: + headers = dict((builtin_str(name), value) for name, value in headers.items()) self.headers = CaseInsensitiveDict(headers) else: self.headers = CaseInsensitiveDict() diff --git a/test_requests.py b/test_requests.py index 6c4c3dd2..e506ffae 100644 --- a/test_requests.py +++ b/test_requests.py @@ -10,7 +10,7 @@ import unittest import requests from requests.auth import HTTPDigestAuth -from requests.compat import str +from requests.compat import is_py2, str try: import StringIO @@ -251,6 +251,10 @@ class RequestsTestCase(unittest.TestCase): requests.get(url, params={'foo': 'foo'}) requests.get(httpbin('ø'), params={'foo': 'foo'}) + def test_unicode_header_name(self): + if is_py2: + requests.put(httpbin('put'), headers={unicode('Content-Type'): 'application/octet-stream'}, data='\xff') + def test_urlencoded_get_query_multivalued_param(self): r = requests.get(httpbin('get'), params=dict(test=['foo', 'baz'])) From 56f4b7ca68d75795112b673673de2b90f05c9e93 Mon Sep 17 00:00:00 2001 From: Denis Ryzhkov Date: Tue, 12 Feb 2013 09:51:46 +0300 Subject: [PATCH 2/2] Deleted is_py2 check from unicode_header_name fix thanks to Lukasa. --- requests/models.py | 3 +-- test_requests.py | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/requests/models.py b/requests/models.py index 2ca5faa4..4120a8ee 100644 --- a/requests/models.py +++ b/requests/models.py @@ -325,8 +325,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): """Prepares the given HTTP headers.""" if headers: - if is_py2: - headers = dict((builtin_str(name), value) for name, value in headers.items()) + headers = dict((name.encode('ascii'), value) for name, value in headers.items()) self.headers = CaseInsensitiveDict(headers) else: self.headers = CaseInsensitiveDict() diff --git a/test_requests.py b/test_requests.py index e506ffae..e12722d1 100644 --- a/test_requests.py +++ b/test_requests.py @@ -10,7 +10,7 @@ import unittest import requests from requests.auth import HTTPDigestAuth -from requests.compat import is_py2, str +from requests.compat import str try: import StringIO @@ -252,8 +252,7 @@ class RequestsTestCase(unittest.TestCase): requests.get(httpbin('ø'), params={'foo': 'foo'}) def test_unicode_header_name(self): - if is_py2: - requests.put(httpbin('put'), headers={unicode('Content-Type'): 'application/octet-stream'}, data='\xff') + requests.put(httpbin('put'), headers={str('Content-Type'): 'application/octet-stream'}, data='\xff') # compat.str is unicode. def test_urlencoded_get_query_multivalued_param(self):