Skip to content

Commit 527a0b6

Browse files
fix: correctly handle WebSocket message fragmentation
Handle WebSocket fragmentation was properly by taking into account the "fin" flag to know if a frame is "final" or whether there will be continuations before it's final. Signed-off-by: Thomas Farstrike <111072251+ThomasFarstrike@users.noreply.github.com>
1 parent 3eaf027 commit 527a0b6

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

python-ecosys/aiohttp/aiohttp/aiohttp_ws.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ async def handshake(self, uri, ssl, req):
166166

167167
async def receive(self):
168168
while True:
169-
opcode, payload = await self._read_frame()
169+
opcode, payload, final = await self._read_frame()
170+
while not final:
171+
_, morepayload, final = await self._read_frame() # original opcode must be preserved
172+
payload += morepayload
170173
send_opcode, data = self._process_websocket_frame(opcode, payload)
171174
if send_opcode: # pragma: no cover
172175
await self.send(data, send_opcode)
@@ -206,7 +209,7 @@ async def _read_frame(self):
206209
payload = await self.reader.readexactly(length)
207210
if has_mask: # pragma: no cover
208211
payload = bytes(x ^ mask[i % 4] for i, x in enumerate(payload))
209-
return opcode, payload
212+
return opcode, payload, fin
210213

211214

212215
class ClientWebSocketResponse:

0 commit comments

Comments
 (0)