Fixes non UTF-8 surrogateescapes (#612)
Fixes non UTF-8 surrogateescapes Surrogate escapes in Unicode (non UTF-8 encoding) will be properly escaped with backslashes when encountered, versus breaking the transport layer. Fixes #611
This commit is contained in:
committed by
Honza Král
parent
9320ee7eaf
commit
d6fb95353e
@@ -292,7 +292,7 @@ class Transport(object):
|
|||||||
|
|
||||||
if body is not None:
|
if body is not None:
|
||||||
try:
|
try:
|
||||||
body = body.encode('utf-8')
|
body = body.encode('utf-8', 'surrogatepass')
|
||||||
except (UnicodeDecodeError, AttributeError):
|
except (UnicodeDecodeError, AttributeError):
|
||||||
# bytes/str - no need to re-encode
|
# bytes/str - no need to re-encode
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -105,6 +105,13 @@ class TestTransport(TestCase):
|
|||||||
self.assertEquals(1, len(t.get_connection().calls))
|
self.assertEquals(1, len(t.get_connection().calls))
|
||||||
self.assertEquals(('GET', '/', None, body), t.get_connection().calls[0][0])
|
self.assertEquals(('GET', '/', None, body), t.get_connection().calls[0][0])
|
||||||
|
|
||||||
|
def test_body_surrogates_replaced_encoded_into_bytes(self):
|
||||||
|
t = Transport([{}], connection_class=DummyConnection)
|
||||||
|
|
||||||
|
t.perform_request('GET', '/', body='你好\uda6a')
|
||||||
|
self.assertEquals(1, len(t.get_connection().calls))
|
||||||
|
self.assertEquals(('GET', '/', None, b'\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa'), t.get_connection().calls[0][0])
|
||||||
|
|
||||||
def test_kwargs_passed_on_to_connections(self):
|
def test_kwargs_passed_on_to_connections(self):
|
||||||
t = Transport([{'host': 'google.com'}], port=123)
|
t = Transport([{'host': 'google.com'}], port=123)
|
||||||
self.assertEquals(1, len(t.connection_pool.connections))
|
self.assertEquals(1, len(t.connection_pool.connections))
|
||||||
|
|||||||
Reference in New Issue
Block a user