From 75ca67eb181e129dd0cd84b6f5ab3e7abbf76ba3 Mon Sep 17 00:00:00 2001 From: Phil Weir Date: Thu, 20 Oct 2022 08:43:49 +0100 Subject: [PATCH 1/3] datetime isoformat returns a unicode string --- sword2/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sword2/utils.py b/sword2/utils.py index 46b52ec..f30883e 100644 --- a/sword2/utils.py +++ b/sword2/utils.py @@ -188,7 +188,7 @@ def create_multipart_related(payloads): """ # Generate random boundary code # TODO check that it does not occur in the payload data - bhash = md5(datetime.now().isoformat()).hexdigest() # eg 'd8bb3ea6f4e0a4b4682be0cfb4e0a24e' + bhash = md5(datetime.now().isoformat().encode()).hexdigest() # eg 'd8bb3ea6f4e0a4b4682be0cfb4e0a24e' BOUNDARY = '===========%s_$' % bhash CRLF = '\r\n' # As some servers might barf without this. body = [] From 7f52987a1da9b5e97d0b5f59d4de1749697116c5 Mon Sep 17 00:00:00 2001 From: Phil Weir Date: Thu, 20 Oct 2022 08:59:55 +0100 Subject: [PATCH 2/3] etree conversion will fail because str returns Unicode --- sword2/utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sword2/utils.py b/sword2/utils.py index f30883e..3db17b4 100644 --- a/sword2/utils.py +++ b/sword2/utils.py @@ -221,7 +221,10 @@ def create_multipart_related(payloads): if hasattr(payload['data'], 'read'): body.append(b64encode(payload['data'].read())) else: - body.append(b64encode(payload['data'])) + try: + body.append(b64encode(payload['data'])) + except TypeError: + body.append(b64encode(payload['data'].encode("utf-8"))) body.append('--' + BOUNDARY + '--') body.append('') body_bytes = CRLF.join(body) From 67acd330d24f96c434ca28648d192f42c8028f61 Mon Sep 17 00:00:00 2001 From: Phil Weir Date: Thu, 20 Oct 2022 09:39:44 +0100 Subject: [PATCH 3/3] ensure base64 is wrapped with enc/dec for utf-8] --- sword2/http_layer.py | 2 +- sword2/utils.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/sword2/http_layer.py b/sword2/http_layer.py index deb2c25..3c25946 100644 --- a/sword2/http_layer.py +++ b/sword2/http_layer.py @@ -85,7 +85,7 @@ def __init__(self, username, password): self.password = password def http_request(self, request): - request.add_header(self.auth_header, 'Basic %s' % base64.b64encode(self.username + ':' + self.password)) + request.add_header(self.auth_header, 'Basic %s' % base64.b64encode((self.username + ':' + self.password).encode()).decode("utf-8")) return request https_request = http_request diff --git a/sword2/utils.py b/sword2/utils.py index 3db17b4..bfda645 100644 --- a/sword2/utils.py +++ b/sword2/utils.py @@ -190,7 +190,7 @@ def create_multipart_related(payloads): # TODO check that it does not occur in the payload data bhash = md5(datetime.now().isoformat().encode()).hexdigest() # eg 'd8bb3ea6f4e0a4b4682be0cfb4e0a24e' BOUNDARY = '===========%s_$' % bhash - CRLF = '\r\n' # As some servers might barf without this. + CRLF = b'\r\n' # As some servers might barf without this. body = [] for payload in payloads: # predicatable ordering... body.append('--' + BOUNDARY) @@ -213,20 +213,20 @@ def create_multipart_related(payloads): body.append('Content-Transfer-Encoding: base64') body.append('') if hasattr(payload['data'], 'read'): - body.append(b64encode(payload['data'].read())) + body.append(b64encode(payload['data'].read()).decode()) else: - body.append(b64encode(payload['data'])) + body.append(b64encode(payload['data']).decode()) else: body.append('') if hasattr(payload['data'], 'read'): - body.append(b64encode(payload['data'].read())) + body.append(b64encode(payload['data'].read()).decode()) else: try: - body.append(b64encode(payload['data'])) + body.append(b64encode(payload['data']).decode()) except TypeError: - body.append(b64encode(payload['data'].encode("utf-8"))) + body.append(b64encode(payload['data'].encode("utf-8")).decode()) body.append('--' + BOUNDARY + '--') body.append('') - body_bytes = CRLF.join(body) + body_bytes = CRLF.join([line.encode("utf-8") for line in body]) content_type = 'multipart/related; boundary="%s"' % BOUNDARY return content_type, body_bytes