Skip to content
Merged
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
23 changes: 17 additions & 6 deletions servercom/implementations/circuitpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def radio(self):
return wifi.radio

def rx_bytes(self) -> bytes:
total_length: int = None
response = b""
while True:
buf = bytearray(256)
Expand All @@ -228,12 +229,21 @@ def rx_bytes(self) -> bytes:
recvd = 0
else:
raise
response += buf
response += bytes(buf).replace(b'\x00', b'')
del buf
collect()
if self.v:
print(f"Received {recvd} bytes")
if recvd == 0:
if response.endswith(b'\r\n\r\n'):
header_chunks = response.split(b'\r\n')
for header in header_chunks:
if header.startswith(b'Content-Length: '):
total_length = len(response) + int(header.split(b' ')[1])
break
if recvd == 0 or (
total_length is not None and \
len(response) >= total_length
):
del recvd
collect()
break
Expand Down Expand Up @@ -267,8 +277,8 @@ def _do_request(
f"{method} {path} HTTP/1.1\r\n" +
"Host: api.local\r\n" +
"Connection: close\r\n" +
f"Authorization: Basic {auth_str}" +
'\r\n'.join(headers) + "\r\n"
f"Authorization: Basic {auth_str}\r\n" +
'\r\n'.join(headers)
)
del auth_str
collect()
Expand All @@ -283,7 +293,8 @@ def _do_request(
f"Content-Length: {len(body)}\r\n" +
f"\r\n{body}\r\n"
)
req_text += '\r\n'
else:
req_text += '\r\n\r\n'

if self.v:
print("Sending request...")
Expand All @@ -302,7 +313,7 @@ def _do_request(
collect()
if self.v:
print("Receiving response...")
self.wrapped_socket.setblocking(False)
self.wrapped_socket.settimeout(self.conf.TIMEOUT)
response = self.rx_bytes()
except Exception as e:
if self.v:
Expand Down