Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to connect ! invalid handshake #1461

Closed
shay-te opened this issue May 9, 2024 · 7 comments
Closed

Enable to connect ! invalid handshake #1461

shay-te opened this issue May 9, 2024 · 7 comments
Labels

Comments

@shay-te
Copy link

shay-te commented May 9, 2024

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
Copy link
Member

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

What is "our client" here?

@shay-te
Copy link
Author

shay-te commented May 13, 2024

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

@python-websockets python-websockets deleted a comment from Bautisa46 May 14, 2024
@aaugustin
Copy link
Member

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
Copy link
Author

shay-te commented May 14, 2024

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
Copy link
Member

aaugustin commented May 14, 2024

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:

  • terminate TLS in a reverse proxy: recommended because it's usually more foolproof; AWS Network Load Balancer can do this;
  • terminate TLS in the websockets: doable but the security of the system depends on how you configure it.

@shay-te
Copy link
Author

shay-te commented May 21, 2024

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

@aaugustin
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants