Skip to content

Commit

Permalink
aiohttp: Fix binary data treatment.
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
Carglglz authored and dpgeorge committed Feb 8, 2024
1 parent ddb1a27 commit 56f514f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions python-ecosys/aiohttp/aiohttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ async def read(self, sz=-1):
return self._decode(await self.content.read(sz))

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

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

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

if 301 <= status <= 303:
redir_cnt += 1
Expand Down Expand Up @@ -195,28 +195,33 @@ async def request_raw(
if "Host" not in headers:
headers.update(Host=host)
if not data:
query = "%s /%s %s\r\n%s\r\n" % (
query = b"%s /%s %s\r\n%s\r\n" % (
method,
path,
version,
"\r\n".join(f"{k}: {v}" for k, v in headers.items()) + "\r\n" if headers else "",
)
else:
headers.update(**{"Content-Length": len(str(data))})
if json:
headers.update(**{"Content-Type": "application/json"})
query = """%s /%s %s\r\n%s\r\n%s\r\n\r\n""" % (
if isinstance(data, bytes):
headers.update(**{"Content-Type": "application/octet-stream"})
else:
data = data.encode()

headers.update(**{"Content-Length": len(data)})
query = b"""%s /%s %s\r\n%s\r\n%s""" % (
method,
path,
version,
"\r\n".join(f"{k}: {v}" for k, v in headers.items()) + "\r\n",
data,
)
if not is_handshake:
await writer.awrite(query.encode("latin-1"))
await writer.awrite(query)
return reader
else:
await writer.awrite(query.encode())
await writer.awrite(query)
return reader, writer

def request(self, method, url, data=None, json=None, ssl=None, params=None, headers={}):
Expand Down
2 changes: 1 addition & 1 deletion python-ecosys/aiohttp/manifest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
metadata(
description="HTTP client module for MicroPython asyncio module",
version="0.0.1",
version="0.0.2",
pypi="aiohttp",
)

Expand Down

0 comments on commit 56f514f

Please sign in to comment.