mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Update micropython aiohttp to 0.0.6
This commit is contained in:
@@ -27,7 +27,6 @@ class ClientResponse:
|
||||
def _decode(self, data):
|
||||
c_encoding = self._get_header("content-encoding", None)
|
||||
if c_encoding in ("gzip", "deflate", "gzip,deflate"):
|
||||
print(f"__init__.py of aiohttp has to decompress {c_encoding}")
|
||||
try:
|
||||
import deflate
|
||||
import io
|
||||
@@ -43,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)
|
||||
@@ -60,20 +61,20 @@ class ChunkedClientResponse(ClientResponse):
|
||||
self.content = reader
|
||||
self.chunk_size = 0
|
||||
|
||||
async def read(self, sz=2 * 1024 * 1024): # reduced from 4 to 2MB
|
||||
async def read(self, sz=4 * 1024 * 1024):
|
||||
if self.chunk_size == 0:
|
||||
l = await self.content.readline()
|
||||
l = l.split(b";", 1)[0]
|
||||
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)
|
||||
|
||||
@@ -137,7 +138,6 @@ class ClientSession:
|
||||
break
|
||||
|
||||
if chunked:
|
||||
print("__init__.py of aiohttp received chunked, creating ChunkedClientResponse")
|
||||
resp = ChunkedClientResponse(reader)
|
||||
else:
|
||||
resp = ClientResponse(reader)
|
||||
|
||||
@@ -96,8 +96,6 @@ class WebSocketClient:
|
||||
return self.PONG, payload
|
||||
elif opcode == self.PONG: # pragma: no branch
|
||||
return None, None
|
||||
else:
|
||||
print(f"Warning: aiohttp_ws.py received unsupported opcode {opcode} with data {payload}")
|
||||
return None, payload
|
||||
|
||||
@classmethod
|
||||
@@ -191,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
|
||||
@@ -199,31 +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_data = await self.reader.read(2)
|
||||
if len(length_data) != 2:
|
||||
print("WARNING: aiohttp_ws.py failed to read 2-byte length, closing")
|
||||
return self.CLOSE, b""
|
||||
(length,) = struct.unpack("!H", length_data)
|
||||
(length,) = struct.unpack("!H", await self.reader.readexactly(2))
|
||||
elif length == 127: # Magic number, length header is 8 bytes
|
||||
length_data = await self.reader.read(8)
|
||||
if len(length_data) != 8:
|
||||
print("WARNING: aiohttp_ws.py failed to read 8-byte length, closing")
|
||||
return self.CLOSE, b""
|
||||
(length,) = struct.unpack("!Q", length_data)
|
||||
(length,) = struct.unpack("!Q", await self.reader.readexactly(8))
|
||||
|
||||
if has_mask: # pragma: no cover
|
||||
mask = await self.reader.read(4)
|
||||
if len(mask) != 4:
|
||||
print("WARNING: aiohttp_ws.py failed to read mask, closing")
|
||||
return self.CLOSE, b""
|
||||
payload = b""
|
||||
remaining_length = length
|
||||
while remaining_length > 0:
|
||||
chunk = await self.reader.read(remaining_length)
|
||||
if not chunk: # Connection closed or error
|
||||
print(f"WARNING: aiohttp_ws.py connection closed while reading payload, got {len(payload)}/{length} bytes, closing")
|
||||
return self.CLOSE, b""
|
||||
payload += chunk
|
||||
remaining_length -= len(chunk)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user