babelouest / ulfius

Web Framework to build REST APIs, Webservices or any HTTP endpoint in C language. Can stream large amount of data, integrate JSON data with Jansson, and create websocket services
https://babelouest.github.io/ulfius
GNU Lesser General Public License v2.1
1.07k stars 183 forks source link

Invalid HTTP headers in client handshake #210

Closed mvduin closed 2 years ago

mvduin commented 2 years ago

ulfius_set_websocket_request() adds Connection: Upgrade and Upgrade: websocket headers to request->map_header, but ulfius_websocket_connection_handshake() already emits those headers separately, causing both headers to be duplicated which causes connections to fail (at least to nodejs websocket servers).

ulfius_set_websocket_request() also adds a Content-Length: 0 header but GET-requests do not have a body hence are not supposed to have a Content-Length header.

Fix:

diff --git i/src/u_websocket.c w/src/u_websocket.c
--- i/src/u_websocket.c
+++ w/src/u_websocket.c
@@ -2332,9 +2334,6 @@ int ulfius_set_websocket_request(struct _u_request * request,
       u_map_put(request->map_header, "Sec-WebSocket-Extensions", websocket_extensions);
     }
     u_map_put(request->map_header, "Sec-WebSocket-Version", "13");
-    u_map_put(request->map_header, "Upgrade", "websocket");
-    u_map_put(request->map_header, "Connection", "Upgrade");
-    u_map_put(request->map_header, "Content-Length", "0");
     u_map_put(request->map_header, "User-Agent", U_WEBSOCKET_USER_AGENT "/" STR(ULFIUS_VERSION));
     rand_string(rand_str, 16);
     if (!o_base64_encode((unsigned char *)rand_str, 16, (unsigned char *)rand_str_base64, &out_len)) {
babelouest commented 2 years ago

Thanks for that one too @mvduin !

It's fixed in https://github.com/babelouest/ulfius/commit/a9cebc1c2403650b4c6f8f91a2cedfac8f7b224a