python-websockets / websockets

Library for building WebSocket servers and clients in Python
https://websockets.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
5.06k stars 505 forks source link

Enable to connect ! invalid handshake #1461

Closed shay-te closed 1 month ago

shay-te commented 1 month ago

Hello. Thank you in advance for reading.

I have tested it with a small NodeJS WS service, and our client can open the connection. When switching to python-websockets, I get an error. We are connecting to the WebSocket using AWS Network Load Balancer. (same load balancer working with node)

We are stuck on this for two weeks. We are experiencing this error.

Thank you

[2024-05-09 06:49:28,569][websockets.server][DEBUG] - = connection is CONNECTING
[2024-05-09 06:49:28,569][websockets.server][DEBUG] - ! invalid handshake
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 74, in read_request
    request_line = await read_line(stream)
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 200, in read_line
    raise EOFError("line without CRLF")
EOFError: line without CRLF

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 279, in read_http_request
    path, headers = await read_request(self.reader)
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 76, in read_request
    raise EOFError("connection closed while reading HTTP request line") from exc
EOFError: connection closed while reading HTTP request line

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 167, in handler
    await self.handshake(
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 581, in handshake
    path, request_headers = await self.read_http_request()
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 283, in read_http_request
    raise InvalidMessage("did not receive a valid HTTP request") from exc
websockets.exceptions.InvalidMessage: did not receive a valid HTTP request
[2024-05-09 06:49:28,570][websockets.server][DEBUG] - > HTTP/1.1 400 Bad Request
[2024-05-09 06:49:28,570][websockets.server][DEBUG] - > Date: Thu, 09 May 2024 06:49:28 GMT
[2024-05-09 06:49:28,570][websockets.server][DEBUG] - > Server: Python/3.8 websockets/12.0
[2024-05-09 06:49:28,570][websockets.server][DEBUG] - > Content-Length: 77
[2024-05-09 06:49:28,571][websockets.server][DEBUG] - > Content-Type: text/plain
[2024-05-09 06:49:28,571][websockets.server][DEBUG] - > Connection: close
[2024-05-09 06:49:28,571][websockets.server][DEBUG] - > [body] (77 bytes)
[2024-05-09 06:49:28,571][websockets.server][INFO] - connection rejected (400 Bad Request)
[2024-05-09 06:49:28,571][websockets.server][DEBUG] - x closing TCP connection
[2024-05-09 06:49:28,571][websockets.server][DEBUG] - = connection is CLOSED
[2024-05-09 06:49:28,572][websockets.server][INFO] - connection closed
[2024-05-09 06:49:28,599][websockets.server][DEBUG] - = connection is CONNECTING
[2024-05-09 06:49:28,600][websockets.server][DEBUG] - ! invalid handshake
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 74, in read_request
    request_line = await read_line(stream)
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 200, in read_line
    raise EOFError("line without CRLF")
EOFError: line without CRLF

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 279, in read_http_request
    path, headers = await read_request(self.reader)
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 76, in read_request
    raise EOFError("connection closed while reading HTTP request line") from exc
EOFError: connection closed while reading HTTP request line

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 167, in handler
    await self.handshake(
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 581, in handshake
    path, request_headers = await self.read_http_request()
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 283, in read_http_request
    raise InvalidMessage("did not receive a valid HTTP request") from exc
websockets.exceptions.InvalidMessage: did not receive a valid HTTP request
[2024-05-09 06:49:28,601][websockets.server][DEBUG] - > HTTP/1.1 400 Bad Request
[2024-05-09 06:49:28,601][websockets.server][DEBUG] - > Date: Thu, 09 May 2024 06:49:28 GMT
[2024-05-09 06:49:28,601][websockets.server][DEBUG] - > Server: Python/3.8 websockets/12.0
[2024-05-09 06:49:28,601][websockets.server][DEBUG] - > Content-Length: 77
[2024-05-09 06:49:28,601][websockets.server][DEBUG] - > Content-Type: text/plain
[2024-05-09 06:49:28,601][websockets.server][DEBUG] - > Connection: close
[2024-05-09 06:49:28,602][websockets.server][DEBUG] - > [body] (77 bytes)
[2024-05-09 06:49:28,602][websockets.server][INFO] - connection rejected (400 Bad Request)
[2024-05-09 06:49:28,602][websockets.server][DEBUG] - x closing TCP connection
[2024-05-09 06:49:28,602][websockets.server][DEBUG] - = connection is CLOSED
[2024-05-09 06:49:28,603][websockets.server][INFO] - connection closed

[2024-05-09 06:49:29,799][websockets.server][DEBUG] - = connection is CONNECTING
[2024-05-09 06:49:29,800][websockets.server][DEBUG] - ! invalid handshake
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 74, in read_request
    request_line = await read_line(stream)
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 200, in read_line
    raise EOFError("line without CRLF")
EOFError: line without CRLF

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 279, in read_http_request
    path, headers = await read_request(self.reader)
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 76, in read_request
    raise EOFError("connection closed while reading HTTP request line") from exc
EOFError: connection closed while reading HTTP request line

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 167, in handler
    await self.handshake(
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 581, in handshake
    path, request_headers = await self.read_http_request()
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 283, in read_http_request
    raise InvalidMessage("did not receive a valid HTTP request") from exc
websockets.exceptions.InvalidMessage: did not receive a valid HTTP request
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > HTTP/1.1 400 Bad Request
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > Date: Thu, 09 May 2024 06:49:29 GMT
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > Server: Python/3.8 websockets/12.0
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > Content-Length: 77
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > Content-Type: text/plain
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > Connection: close
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > [body] (77 bytes)
[2024-05-09 06:49:29,801][websockets.server][INFO] - connection rejected (400 Bad Request)
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - x closing TCP connection
[2024-05-09 06:49:29,802][websockets.server][DEBUG] - = connection is CLOSED
[2024-05-09 06:49:29,802][websockets.server][INFO] - connection closed
aaugustin commented 1 month ago

From websockets' perspective, it never receives the first line of a WebSocket request.

What is "our client" here?

shay-te commented 1 month ago

From websockets' perspective, it never receives the first line of a WebSocket request.

What is "our client" here?

the client is Chrome and also React-Native

aaugustin commented 1 month ago

This rules out a problem with the client. Could you try connecting to a server running locally? If that works, then it means that the problem is at the level of your AWS Network Load Balancer setup (and I suspect that it's there).

shay-te commented 1 month ago

Thank you for the response. It is working well when connecting with WS (not secured) directly to the IP address. But JavaScript WS can handle the same setup.

aaugustin commented 1 month ago

If that's your question, one websockets server process cannot serve secure (wss://) and insecure (ws://) connections. I don't plan to add this feature either.

There's two ways to handle secure connections:

shay-te commented 1 month ago

Thank you. We moved the entire project to nodejs. Shame

aaugustin commented 1 month ago

I'm sorry it didn't work for you 🥲