Skip to content

Commit

Permalink
Try different encoding approach in post_ratelimited
Browse files Browse the repository at this point in the history
We were getting some SSL bad write retry errors, and mostly the internet
just seems to think that passing in unicode is a bad idea. This tries to
encode the data instead. Try utf-8 first, and if that fails we see if
chardet can get the encoding for us. Also had to encode the
authorization header because it was causing the data body to be coerced
(unsuccessfully) to unicode.
  • Loading branch information
Halla Moore authored and hallamoore committed Jul 13, 2020
1 parent 573b783 commit f60807f
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions exchangelib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,15 @@ def post_ratelimited(protocol, session, url, headers, data, allow_redirects=Fals
# Always create a dummy response for logging purposes, in case we fail in the following
r = DummyResponse(url=url, headers={}, request_headers=headers)
try:
data = data.decode('utf-8')
request = session.prepare_request(Request('POST', url=url, data=data, headers=headers))
request.headers['Content-Length'] = str(len(data.encode('utf-8')))
r = session.send(request,
data = data.encode('utf-8')
except UnicodeDecodeError:
import chardet
encoding_info = chardet.detect(data)
data = data.decode('utf-8').encode(encoding_info['encoding'])
try:
r = session.post(url=url,
headers=headers,
data=data,
allow_redirects=False,
timeout=(timeout or protocol.TIMEOUT),
stream=stream)
Expand Down Expand Up @@ -681,5 +686,5 @@ def __init__(self, token):
self.token = token

def __call__(self, r):
r.headers['Authorization'] = 'Bearer {}'.format(self.token)
r.headers[b'Authorization'] = b'Bearer {}'.format(self.token.encode('utf-8'))
return r

0 comments on commit f60807f

Please sign in to comment.