Skip to content

Commit 78bd542

Browse files
aiohttp_ws: minimize diff to original
1 parent f21e89d commit 78bd542

File tree

1 file changed

+6
-65
lines changed

1 file changed

+6
-65
lines changed

internal_filesystem/lib/aiohttp/aiohttp_ws.py

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -190,98 +190,40 @@ async def close(self):
190190
self.closed = True
191191
await self.send(b"", self.CLOSE)
192192

193-
# this fixes the partial self.reader.read()s by requesting the remaining bytes
194193
async def _read_frame(self):
195-
import micropython
196-
import struct
197-
print(f"aiohttp_ws.py thread stack used: {micropython.stack_use()}")
198-
199-
# Read the 2-byte header
200194
header = await self.reader.read(2)
201195
if len(header) != 2: # pragma: no cover
202-
print("aiohttp_ws.py got len header not 2")
196+
# raise OSError(32, "Websocket connection closed")
203197
opcode = self.CLOSE
204198
payload = b""
205199
return opcode, payload
206-
207-
# Parse the frame header
208200
fin, opcode, has_mask, length = self._parse_frame_header(header)
209-
print(f"aiohttp_ws.py _read_frame got {fin} {opcode} {has_mask} {length}")
210-
211-
# Handle extended length headers
212201
if length == 126: # Magic number, length header is 2 bytes
213-
print("aiohttp_ws.py Magic number, length header is 2 bytes")
214202
length_data = await self.reader.read(2)
215203
if len(length_data) != 2:
216-
print("aiohttp_ws.py failed to read 2-byte length header")
204+
print("WARNING: aiohttp_ws.py failed to read 2-byte length, closing")
217205
return self.CLOSE, b""
218206
(length,) = struct.unpack("!H", length_data)
219207
elif length == 127: # Magic number, length header is 8 bytes
220-
print("aiohttp_ws.py Magic number, length header is 8 bytes")
221208
length_data = await self.reader.read(8)
222209
if len(length_data) != 8:
223-
print("aiohttp_ws.py failed to read 8-byte length header")
210+
print("WARNING: aiohttp_ws.py failed to read 8-byte length, closing")
224211
return self.CLOSE, b""
225212
(length,) = struct.unpack("!Q", length_data)
226-
227-
print(f"actual length is {length}")
228-
229-
# Read mask if present
230213
if has_mask: # pragma: no cover
231214
mask = await self.reader.read(4)
232215
if len(mask) != 4:
233-
print("aiohttp_ws.py failed to read mask")
216+
print("WARNING: aiohttp_ws.py failed to read mask, closing")
234217
return self.CLOSE, b""
235-
print(f"mask is {mask}")
236-
237-
# Read payload in chunks until the full length is received
238218
payload = b""
239219
remaining_length = length
240220
while remaining_length > 0:
241221
chunk = await self.reader.read(remaining_length)
242222
if not chunk: # Connection closed or error
243-
print(f"aiohttp_ws.py connection closed while reading payload, got {len(payload)}/{length} bytes")
223+
print(f"WARNING: aiohttp_ws.py connection closed while reading payload, got {len(payload)}/{length} bytes, closing")
244224
return self.CLOSE, b""
245225
payload += chunk
246226
remaining_length -= len(chunk)
247-
print(f"aiohttp_ws.py read chunk of {len(chunk)} bytes, {remaining_length} bytes remaining")
248-
249-
print(f"payload is {payload} with actual length {len(payload)}")
250-
if len(payload) != length:
251-
print(f"\n\n\nwrong payload length: expected {length}, got {len(payload)}\n\n\n")
252-
253-
# Unmask payload if necessary
254-
if has_mask: # pragma: no cover
255-
payload = bytes(x ^ mask[i % 4] for i, x in enumerate(payload))
256-
257-
return opcode, payload
258-
259-
async def _read_frame_original(self):
260-
import micropython
261-
print(f"aiohttp_ws.py thread stack used: {micropython.stack_use()}")
262-
header = await self.reader.read(2)
263-
if len(header) != 2: # pragma: no cover
264-
# raise OSError(32, "Websocket connection closed")
265-
print("aiohttp_ws.py got len header not 2")
266-
opcode = self.CLOSE
267-
payload = b""
268-
return opcode, payload
269-
fin, opcode, has_mask, length = self._parse_frame_header(header)
270-
print(f"aiohttp_ws.py _read_frame got {fin} {opcode} {has_mask} {length}")
271-
if length == 126: # Magic number, length header is 2 bytes
272-
print("aiohttp_ws.py Magic number, length header is 2 bytes")
273-
(length,) = struct.unpack("!H", await self.reader.read(2))
274-
elif length == 127: # Magic number, length header is 8 bytes
275-
print("aiohttp_ws.py Magic number, length header is 8 bytes")
276-
(length,) = struct.unpack("!Q", await self.reader.read(8))
277-
print(f"actual length is {length}")
278-
if has_mask: # pragma: no cover
279-
mask = await self.reader.read(4)
280-
print(f"mask is {mask}")
281-
payload = await self.reader.read(length)
282-
print(f"payload is {payload} with actual length {len(payload)}")
283-
if len(payload) != length:
284-
print("\n\n\nwrong payload length, this should be ignored!\n\n\n")
285227
if has_mask: # pragma: no cover
286228
payload = bytes(x ^ mask[i % 4] for i, x in enumerate(payload))
287229
return opcode, payload
@@ -296,8 +238,7 @@ def __aiter__(self):
296238

297239
async def __anext__(self):
298240
msg = WebSocketMessage(*await self.ws.receive())
299-
print("ClientWebSocketResponse doing __anext__")
300-
print(msg.data, msg.type) # DEBUG
241+
# print(msg.data, msg.type) # DEBUG
301242
if (not msg.data and msg.type == self.ws.CLOSE) or self.ws.closed:
302243
raise StopAsyncIteration
303244
return msg

0 commit comments

Comments
 (0)