From f21679133da997f19df9201f064034bf1adb1757 Mon Sep 17 00:00:00 2001 From: taras Date: Tue, 20 Aug 2024 18:02:04 +0200 Subject: [PATCH] More fixes --- picows/picows.pyx | 5 ++++- tests/test_echo.py | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/picows/picows.pyx b/picows/picows.pyx index 74db54c..e101654 100644 --- a/picows/picows.pyx +++ b/picows/picows.pyx @@ -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"): diff --git a/tests/test_echo.py b/tests/test_echo.py index d5e59d4..824e9e3 100644 --- a/tests/test_echo.py +++ b/tests/test_echo.py @@ -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()