Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
taras committed Aug 20, 2024
1 parent fddcec9 commit f216791
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
5 changes: 4 additions & 1 deletion picows/picows.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,10 @@ cdef class WSProtocol:

headers = {}
for line in lines[1:]:
name, value = line.split(b":", maxsplit=1)
parts = line.split(b":", maxsplit=1)
if len(parts) != 2:
raise RuntimeError(f"Mailformed header in upgrade request: {raw_headers}")
name, value = parts
headers[name.strip().decode().lower()] = value.strip().decode()

if "websocket" != headers.get("upgrade"):
Expand Down
18 changes: 14 additions & 4 deletions tests/test_echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,20 @@ async def test_server_bad_request():
async with ServerAsyncContext(server):
r, w = await asyncio.open_connection("127.0.0.1", server.sockets[0].getsockname()[1])

w.write(b"zzzz\r\n\r\n")
w.write(b"zzzz\r\nasdfasdf\r\n\r\n")
resp_header = await r.readuntil(b"\r\n\r\n")
assert b"400 Bad Request" in resp_header
resp_data = await r.read()
await r.read()
assert r.at_eof()
# TODO: Why this fails?
# assert w.transport.is_closing()

# From ChatGPT:
# Common Misconception
# It's a common misconception that w.wait_closed() should detect the server's disconnection and automatically close the connection. In reality:
#
# w.wait_closed() only waits for the client-side closure process to finish.
# You must explicitly call w.close() to initiate the closing process after detecting that the server has closed its side of the connection.
#
# I didn't know that? Isn't that stupid and defies the whole purpose of
# wait_closed?
w.close()
await w.wait_closed()

0 comments on commit f216791

Please sign in to comment.