Skip to content

Commit de5ec7f

Browse files
committed
python-ecosys/aiohttp: Fix binary data treatment.
Fix binary data `Content-type` header and data `Content-Length` calculation. Signed-off-by: Carlos Gil <carlosgilglez@gmail.com>
1 parent 7cdf708 commit de5ec7f

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

python-ecosys/aiohttp/aiohttp/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,28 +195,33 @@ async def request_raw(
195195
if "Host" not in headers:
196196
headers.update(Host=host)
197197
if not data:
198-
query = "%s /%s %s\r\n%s\r\n" % (
198+
query = b"%s /%s %s\r\n%s\r\n" % (
199199
method,
200200
path,
201201
version,
202202
"\r\n".join(f"{k}: {v}" for k, v in headers.items()) + "\r\n" if headers else "",
203203
)
204204
else:
205-
headers.update(**{"Content-Length": len(str(data))})
206205
if json:
207206
headers.update(**{"Content-Type": "application/json"})
208-
query = """%s /%s %s\r\n%s\r\n%s\r\n\r\n""" % (
207+
if isinstance(data, bytes):
208+
headers.update(**{"Content-Type": "application/octet-stream"})
209+
else:
210+
data = data.encode("latin-1")
211+
212+
headers.update(**{"Content-Length": len(data)})
213+
query = b"""%s /%s %s\r\n%s\r\n%s\r\n\r\n""" % (
209214
method,
210215
path,
211216
version,
212217
"\r\n".join(f"{k}: {v}" for k, v in headers.items()) + "\r\n",
213218
data,
214219
)
215220
if not is_handshake:
216-
await writer.awrite(query.encode("latin-1"))
221+
await writer.awrite(query)
217222
return reader
218223
else:
219-
await writer.awrite(query.encode())
224+
await writer.awrite(query)
220225
return reader, writer
221226

222227
def request(self, method, url, data=None, json=None, ssl=None, params=None, headers={}):

0 commit comments

Comments
 (0)