aiohttp: Fix partial reads by using readexactly.

Fixes issue #1012.

Signed-off-by: FuNK3Y <fun__key@hotmail.com>
pull/1034/head
FuNK3Y 2025-07-19 12:14:17 +00:00 zatwierdzone przez Damien George
rodzic f95568da43
commit b95ca2491a
3 zmienionych plików z 12 dodań i 10 usunięć

Wyświetl plik

@ -42,7 +42,9 @@ class ClientResponse:
return data
async def read(self, sz=-1):
return self._decode(await self.content.read(sz))
return self._decode(
await (self.content.read(sz) if sz == -1 else self.content.readexactly(sz))
)
async def text(self, encoding="utf-8"):
return (await self.read(int(self._get_header("content-length", -1)))).decode(encoding)
@ -66,13 +68,13 @@ class ChunkedClientResponse(ClientResponse):
self.chunk_size = int(l, 16)
if self.chunk_size == 0:
# End of message
sep = await self.content.read(2)
sep = await self.content.readexactly(2)
assert sep == b"\r\n"
return b""
data = await self.content.read(min(sz, self.chunk_size))
data = await self.content.readexactly(min(sz, self.chunk_size))
self.chunk_size -= len(data)
if self.chunk_size == 0:
sep = await self.content.read(2)
sep = await self.content.readexactly(2)
assert sep == b"\r\n"
return self._decode(data)

Wyświetl plik

@ -189,7 +189,7 @@ class WebSocketClient:
await self.send(b"", self.CLOSE)
async def _read_frame(self):
header = await self.reader.read(2)
header = await self.reader.readexactly(2)
if len(header) != 2: # pragma: no cover
# raise OSError(32, "Websocket connection closed")
opcode = self.CLOSE
@ -197,13 +197,13 @@ class WebSocketClient:
return opcode, payload
fin, opcode, has_mask, length = self._parse_frame_header(header)
if length == 126: # Magic number, length header is 2 bytes
(length,) = struct.unpack("!H", await self.reader.read(2))
(length,) = struct.unpack("!H", await self.reader.readexactly(2))
elif length == 127: # Magic number, length header is 8 bytes
(length,) = struct.unpack("!Q", await self.reader.read(8))
(length,) = struct.unpack("!Q", await self.reader.readexactly(8))
if has_mask: # pragma: no cover
mask = await self.reader.read(4)
payload = await self.reader.read(length)
mask = await self.reader.readexactly(4)
payload = await self.reader.readexactly(length)
if has_mask: # pragma: no cover
payload = bytes(x ^ mask[i % 4] for i, x in enumerate(payload))
return opcode, payload

Wyświetl plik

@ -1,6 +1,6 @@
metadata(
description="HTTP client module for MicroPython asyncio module",
version="0.0.5",
version="0.0.6",
pypi="aiohttp",
)