Skip to content

python-ecosys/aiohttp: Fix partial reads. #1034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions python-ecosys/aiohttp/aiohttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def _decode(self, data):
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)
Expand All @@ -66,13 +68,13 @@ async def read(self, sz=4 * 1024 * 1024):
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)

Expand Down
10 changes: 5 additions & 5 deletions python-ecosys/aiohttp/aiohttp/aiohttp_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,21 @@ async def close(self):
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
payload = b""
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
Expand Down
Loading