Websocket extension parses URL path incorrectly for native Linux (possibly other) build (SOLVED)

Describe the bug
When trying to connect to a server using a URL with a non-root path the native build adds an additional ‘/’ character which causes the server to not be able to locate the resource (e.g. turns example.com/websocket into example.com//websocket)

I believe the issue is in handshake.cpp:44 which adds an extra ‘/’ character. A quick hack:

    char* path = conn->m_Url.m_Path + 1;
    WS_SENDALL(path); // WS_SENDALL(conn->m_Url.m_Path);

seems to fix the problem (though because it’s a hack I’m not sure what else it affects).

To Reproduce
Build a Linux native (possible works for any other non-HTML5 builds that use the c++ code directly) and try to connect to any websocket accessed through a non-root path.

Expected behaviour
Connection should be established

Defold version:

  • Defold version: 1.2.174
  • Websocket extension version: 1.4.0

Platforms:

  • Platforms: Linux (possible other non-HTML5 native)

Minimal repro case project:
Websocket.zip (2.9 KB)

Logs:

WARNING:WEBSOCKET: Sent buffer: 'GET /' 5 bytes
WARNING:WEBSOCKET: Sent buffer: '/ws' 3 bytes
WARNING:WEBSOCKET: Sent buffer: ' HTTP/1.1\r\n' 11 bytes
WARNING:WEBSOCKET: Sent buffer: 'Host: ' 6 bytes
WARNING:WEBSOCKET: Sent buffer: '127.0.0.1' 9 bytes
WARNING:WEBSOCKET: Sent buffer: ':8888' 5 bytes
WARNING:WEBSOCKET: Sent buffer: '\r\n' 2 bytes
WARNING:WEBSOCKET: Sent buffer: 'Upgrade: websocket\r\n' 20 bytes
WARNING:WEBSOCKET: Sent buffer: 'Connection: Upgrade\r\n' 21 bytes
WARNING:WEBSOCKET: Sent buffer: 'Sec-WebSocket-Key: ' 19 bytes
WARNING:WEBSOCKET: Sent buffer: 'zMXnzyNoNfe3ArlWnuW9OQ==' 24 bytes
WARNING:WEBSOCKET: Sent buffer: '\r\n' 2 bytes
WARNING:WEBSOCKET: Sent buffer: 'Sec-WebSocket-Version: 13\r\n' 27 bytes
WARNING:WEBSOCKET: Sent buffer: 'Sec-WebSocket-Protocol: binary\r\n' 32 bytes
WARNING:WEBSOCKET: Sent buffer: '\r\n' 2 bytes
WARNING:WEBSOCKET: STATE_HANDSHAKE_WRITE -> STATE_HANDSHAKE_READ
WARNING:WEBSOCKET: Waiting for socket to be available for reading
WARNING:WEBSOCKET: Received bytes: 'HTTP/1.1 301 Moved Permanently\r\nContent-Type: text/html; charset=utf-8\r\nLocation: /ws\r\nDate: Sat, 31 Oct 2020 00:41:49 GMT\r\nContent-Length: 38\r\n\r\n<a href="/ws">Moved Permanently</a>.\n\n' 184 bytes
WARNING:WEBSOCKET: STATUS: 'Missing: 'HTTP/1.1 101' in header'  len: 33
WARNING:WEBSOCKET: STATE_HANDSHAKE_READ -> STATE_DISCONNECTED
2 Likes

GitHub issue: https://github.com/defold/extension-websocket/issues/17

Thanks for the find!
I’m sure I’ve tested this code before, so I’m not exactly sure how that worked (pure luck I would guess)

Well, if the path is “\0” (which it is if you don’t have a subpath), and you skip past that null terminator, you have a garbage pointer, which would be bad to use :slight_smile:

All your reported issues are have fixes awaiting approval, and a new release will be out soon. Thx!

4 Likes

Solved in extension-websocket 1.5.0.

2 Likes