Skip to content

Commit 56f514f

Browse files
Carglglzdpgeorge
authored andcommitted
aiohttp: Fix binary data treatment.
- Fix binary data `Content-type` header and data `Content-Length` calculation. - Fix query length when data is included. - Fix `json` and `text` methods of `ClientResponse` to read `Content-Length` size Signed-off-by: Carlos Gil <[email protected]>
1 parent ddb1a27 commit 56f514f

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

python-ecosys/aiohttp/aiohttp/__init__.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ async def read(self, sz=-1):
3838
return self._decode(await self.content.read(sz))
3939

4040
async def text(self, encoding="utf-8"):
41-
return (await self.read(sz=-1)).decode(encoding)
41+
return (await self.read(int(self.headers.get("Content-Length", -1)))).decode(encoding)
4242

4343
async def json(self):
44-
return _json.loads(await self.read())
44+
return _json.loads(await self.read(int(self.headers.get("Content-Length", -1))))
4545

4646
def __repr__(self):
4747
return "<ClientResponse %d %s>" % (self.status, self.headers)
@@ -121,7 +121,7 @@ async def _request(self, method, url, data=None, json=None, ssl=None, params=Non
121121
if b"chunked" in line:
122122
chunked = True
123123
elif line.startswith(b"Location:"):
124-
url = line.rstrip().split(None, 1)[1].decode("latin-1")
124+
url = line.rstrip().split(None, 1)[1].decode()
125125

126126
if 301 <= status <= 303:
127127
redir_cnt += 1
@@ -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()
211+
212+
headers.update(**{"Content-Length": len(data)})
213+
query = b"""%s /%s %s\r\n%s\r\n%s""" % (
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={}):

python-ecosys/aiohttp/manifest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
metadata(
22
description="HTTP client module for MicroPython asyncio module",
3-
version="0.0.1",
3+
version="0.0.2",
44
pypi="aiohttp",
55
)
66

0 commit comments

Comments
 (0)