machinezone / IXWebSocket

websocket and http client and server library, with TLS support and very few dependencies
BSD 3-Clause "New" or "Revised" License
555 stars 178 forks source link

Adding port to host in WebSocketHandshake::clientHandshake causes HTTP/1.1 404 Not Found #521

Open ArthurChristianus opened 5 months ago

ArthurChristianus commented 5 months ago

Hi! First of all, I would like to thank you for the effort you put in creating this library. The ease of use and clean syntax makes it great to work with.

I am trying to create a websocket connection to a server. However, when adding the port to the host in the initial handshake message, the server returns HTTP/1.1 404 Not Found.

E.g. This can connect to the server:

ss << "GET " << path << " HTTP/1.1\r\n";
ss << "Host: " << host << "\r\n";
ss << "Upgrade: websocket\r\n";

And this cannot:

ss << "GET " << path << " HTTP/1.1\r\n";
ss << "Host: " << host << ":" << port << "\r\n";
ss << "Upgrade: websocket\r\n";

I am completely new to websockets so I am unsure if this is a problem with IXWidgets. If it is, I am more than willing to create a pull request to fix this.

bsergean commented 4 months ago

Hi @ArthurChristianus, thanks for the good words.

I'm surprised that ports aren't working ... we've been using that since forever. Are you using SSL, or SNI ?

Can you see the same problem when you build the builtin ws client (you can run a test server, and a test client).

Here I set the host to ws://localhost:8008

$ ./ws/ws connect ws://localhost:8008
Type Ctrl-D to exit prompt...
Connecting to url: ws://localhost:8008
> [2024-07-23 16:18:57.520] [info] ws_connect: connected
[2024-07-23 16:18:57.521] [info] Uri: /
[2024-07-23 16:18:57.521] [info] Headers:
[2024-07-23 16:18:57.521] [info] Connection: Upgrade
[2024-07-23 16:18:57.521] [info] Sec-WebSocket-Accept: +Bv6//Th2IRsDfBXKW0vl6AXckM=
[2024-07-23 16:18:57.521] [info] Sec-WebSocket-Extensions: permessage-deflate; server_max_window_bits=15; client_max_window_bits=15
[2024-07-23 16:18:57.521] [info] Server: ixwebsocket/11.4.6 macos nossl zlib 1.2.12
[2024-07-23 16:18:57.521] [info] Upgrade: websocket
[2024-07-23 16:18:57.521] [info] Received pong ixwebsocket::heartbeat::30s::0
> ECHO !
> [2024-07-23 16:19:10.601] [info] Received 8 bytes
ws_connect: received message: ECHO !

Server

$ ./ws/ws echo_server
[2024-07-23 16:18:26.193] [info] Listening on 127.0.0.1:8008
[2024-07-23 16:18:57.520] [info] New connection
[2024-07-23 16:18:57.520] [info] remote ip: 127.0.0.1
[2024-07-23 16:18:57.520] [info] id: 0
[2024-07-23 16:18:57.520] [info] Uri: /
[2024-07-23 16:18:57.520] [info] Headers:
[2024-07-23 16:18:57.520] [info] Connection: Upgrade
[2024-07-23 16:18:57.520] [info] Host: localhost:8008
[2024-07-23 16:18:57.520] [info] Origin: ws://localhost:8008
[2024-07-23 16:18:57.520] [info] Sec-WebSocket-Extensions: permessage-deflate; server_max_window_bits=15; client_max_window_bits=15
[2024-07-23 16:18:57.520] [info] Sec-WebSocket-Key: YkZjQzUyRkNEZUNmYjY5Rw==
[2024-07-23 16:18:57.520] [info] Sec-WebSocket-Version: 13
[2024-07-23 16:18:57.520] [info] Upgrade: websocket
[2024-07-23 16:18:57.520] [info] User-Agent: ixwebsocket/11.4.6 macos nossl zlib 1.2.12
[2024-07-23 16:19:10.601] [info] Received 8 bytes