From 68a3649fd0543b1ce7687eb22e22fb23857965b0 Mon Sep 17 00:00:00 2001 From: Nicolas Poffley Date: Mon, 22 Sep 2025 16:45:26 +0200 Subject: [PATCH 1/6] DPL Websocket: Add overloaded encode_websocket_handshake_reply which allows a response Sec-WebSocket-Accept in handshake. --- Framework/Core/src/DPLWebSocket.cxx | 2 +- Framework/Core/src/HTTPParser.cxx | 12 ++++++++++++ Framework/Core/src/HTTPParser.h | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Framework/Core/src/DPLWebSocket.cxx b/Framework/Core/src/DPLWebSocket.cxx index a39e98c6f5310..d9b6594d5f07c 100644 --- a/Framework/Core/src/DPLWebSocket.cxx +++ b/Framework/Core/src/DPLWebSocket.cxx @@ -276,7 +276,7 @@ void WSDPLHandler::endHeaders() } /// Create an appropriate reply LOG(debug) << "Got upgrade request with nonce " << mHeaders["sec-websocket-key"].c_str(); - std::string reply = encode_websocket_handshake_reply(mHeaders["sec-websocket-key"].c_str()); + std::string reply = encode_websocket_handshake_reply(mHeaders["sec-websocket-key"].c_str(), "dpl"); mHandshaken = true; uv_buf_t bfr = uv_buf_init(strdup(reply.data()), reply.size()); diff --git a/Framework/Core/src/HTTPParser.cxx b/Framework/Core/src/HTTPParser.cxx index 04ca6e8fdce55..0bacf2654d9c4 100644 --- a/Framework/Core/src/HTTPParser.cxx +++ b/Framework/Core/src/HTTPParser.cxx @@ -225,6 +225,18 @@ std::string encode_websocket_handshake_reply(char const* nonce) return fmt::format(res, HTTPParserHelpers::calculateAccept(nonce)); } +std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol) +{ + constexpr auto res = + "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Access-Control-Allow-Origin: \"*\"\r\n" + "Sec-WebSocket-Protocol: {}\r\n" + "Sec-WebSocket-Accept: {}\r\n\r\n"; + return fmt::format(res, protocol, HTTPParserHelpers::calculateAccept(nonce)); +} + void parse_http_request(char* start, size_t size, HTTPParser* parser) { enum HTTPState state = HTTPState::IN_START; diff --git a/Framework/Core/src/HTTPParser.h b/Framework/Core/src/HTTPParser.h index b4d92393ca5c9..f471a45bd3e13 100644 --- a/Framework/Core/src/HTTPParser.h +++ b/Framework/Core/src/HTTPParser.h @@ -126,6 +126,8 @@ std::string encode_websocket_handshake_request(const char* path, const char* pro /// Encodes the server reply for a given websocket connection /// @a nonce the nonce of the request. std::string encode_websocket_handshake_reply(char const* nonce); +/// @a protocol the websocket subprotocol to confirm (optional) +std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol); /// Encodes the buffer @a src which is @a size long to a number of buffers suitable to be sent via libuv. /// If @a binary is provided the binary bit is set. From 7d6fc54b633536ba913b82c0c2bc76e0b734f093 Mon Sep 17 00:00:00 2001 From: Nicolas Poffley Date: Tue, 23 Sep 2025 11:04:28 +0200 Subject: [PATCH 2/6] Default param "" for protocol instead of overloading for encode_websocket_handshake_request --- Framework/Core/src/HTTPParser.cxx | 39 +++++++++++++++---------------- Framework/Core/src/HTTPParser.h | 3 +-- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/Framework/Core/src/HTTPParser.cxx b/Framework/Core/src/HTTPParser.cxx index 0bacf2654d9c4..b7ccfb786f0f9 100644 --- a/Framework/Core/src/HTTPParser.cxx +++ b/Framework/Core/src/HTTPParser.cxx @@ -214,27 +214,26 @@ std::string HTTPParserHelpers::calculateAccept(const char* nonce) return fmt::format("{}", base); } -std::string encode_websocket_handshake_reply(char const* nonce) +std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol = "") { - constexpr auto res = - "HTTP/1.1 101 Switching Protocols\r\n" - "Upgrade: websocket\r\n" - "Connection: Upgrade\r\n" - "Access-Control-Allow-Origin: \"*\"\r\n" - "Sec-WebSocket-Accept: {}\r\n\r\n"; - return fmt::format(res, HTTPParserHelpers::calculateAccept(nonce)); -} - -std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol) -{ - constexpr auto res = - "HTTP/1.1 101 Switching Protocols\r\n" - "Upgrade: websocket\r\n" - "Connection: Upgrade\r\n" - "Access-Control-Allow-Origin: \"*\"\r\n" - "Sec-WebSocket-Protocol: {}\r\n" - "Sec-WebSocket-Accept: {}\r\n\r\n"; - return fmt::format(res, protocol, HTTPParserHelpers::calculateAccept(nonce)); + if (strlen(protocol) == 0) { + constexpr auto res = + "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Access-Control-Allow-Origin: \"*\"\r\n" + "Sec-WebSocket-Accept: {}\r\n\r\n"; + return fmt::format(res, HTTPParserHelpers::calculateAccept(nonce)); + } else { + constexpr auto res = + "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Access-Control-Allow-Origin: \"*\"\r\n" + "Sec-WebSocket-Protocol: {}\r\n" + "Sec-WebSocket-Accept: {}\r\n\r\n"; + return fmt::format(res, protocol, HTTPParserHelpers::calculateAccept(nonce)); + } } void parse_http_request(char* start, size_t size, HTTPParser* parser) diff --git a/Framework/Core/src/HTTPParser.h b/Framework/Core/src/HTTPParser.h index f471a45bd3e13..a3253c7ca3d39 100644 --- a/Framework/Core/src/HTTPParser.h +++ b/Framework/Core/src/HTTPParser.h @@ -125,9 +125,8 @@ std::string encode_websocket_handshake_request(const char* path, const char* pro /// Encodes the server reply for a given websocket connection /// @a nonce the nonce of the request. -std::string encode_websocket_handshake_reply(char const* nonce); /// @a protocol the websocket subprotocol to confirm (optional) -std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol); +std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol = ""); /// Encodes the buffer @a src which is @a size long to a number of buffers suitable to be sent via libuv. /// If @a binary is provided the binary bit is set. From 3e160df49074ffb639fdf29b9d551d852f20b33b Mon Sep 17 00:00:00 2001 From: Nicolas Elliot Poffley Date: Tue, 23 Sep 2025 12:53:14 +0200 Subject: [PATCH 3/6] Use ternary expression to avoid repetition Co-authored-by: Giulio Eulisse <10544+ktf@users.noreply.github.com> --- Framework/Core/src/HTTPParser.cxx | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/Framework/Core/src/HTTPParser.cxx b/Framework/Core/src/HTTPParser.cxx index b7ccfb786f0f9..93b48e6ebc800 100644 --- a/Framework/Core/src/HTTPParser.cxx +++ b/Framework/Core/src/HTTPParser.cxx @@ -216,24 +216,14 @@ std::string HTTPParserHelpers::calculateAccept(const char* nonce) std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol = "") { - if (strlen(protocol) == 0) { - constexpr auto res = - "HTTP/1.1 101 Switching Protocols\r\n" - "Upgrade: websocket\r\n" - "Connection: Upgrade\r\n" - "Access-Control-Allow-Origin: \"*\"\r\n" - "Sec-WebSocket-Accept: {}\r\n\r\n"; - return fmt::format(res, HTTPParserHelpers::calculateAccept(nonce)); - } else { - constexpr auto res = - "HTTP/1.1 101 Switching Protocols\r\n" - "Upgrade: websocket\r\n" - "Connection: Upgrade\r\n" - "Access-Control-Allow-Origin: \"*\"\r\n" - "Sec-WebSocket-Protocol: {}\r\n" - "Sec-WebSocket-Accept: {}\r\n\r\n"; - return fmt::format(res, protocol, HTTPParserHelpers::calculateAccept(nonce)); - } + constexpr auto res = + "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Access-Control-Allow-Origin: \"*\"\r\n" + "{}" + "Sec-WebSocket-Accept: {}\r\n\r\n"; + return fmt::format(res, protocol && protocol[0] ? fmt::format("Sec-WebSocket-Protocol: {}\r\n", protocol) : "", HTTPParserHelpers::calculateAccept(nonce)); } void parse_http_request(char* start, size_t size, HTTPParser* parser) From 2cea0519046ee7425f33c80fc7e869503d17f5db Mon Sep 17 00:00:00 2001 From: ALICE Action Bot Date: Tue, 23 Sep 2025 10:53:55 +0000 Subject: [PATCH 4/6] Please consider the following formatting changes --- Framework/Core/src/HTTPParser.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Framework/Core/src/HTTPParser.cxx b/Framework/Core/src/HTTPParser.cxx index 93b48e6ebc800..6b0de8939d29c 100644 --- a/Framework/Core/src/HTTPParser.cxx +++ b/Framework/Core/src/HTTPParser.cxx @@ -221,7 +221,7 @@ std::string encode_websocket_handshake_reply(char const* nonce, char const* prot "Upgrade: websocket\r\n" "Connection: Upgrade\r\n" "Access-Control-Allow-Origin: \"*\"\r\n" - "{}" + "{}" "Sec-WebSocket-Accept: {}\r\n\r\n"; return fmt::format(res, protocol && protocol[0] ? fmt::format("Sec-WebSocket-Protocol: {}\r\n", protocol) : "", HTTPParserHelpers::calculateAccept(nonce)); } From 00c55fd6bee6348a0ba3db473d8d97b066353f26 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Tue, 23 Sep 2025 17:38:58 +0200 Subject: [PATCH 5/6] Update Framework/Core/src/HTTPParser.cxx --- Framework/Core/src/HTTPParser.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Framework/Core/src/HTTPParser.cxx b/Framework/Core/src/HTTPParser.cxx index 6b0de8939d29c..6e13180cd5fcb 100644 --- a/Framework/Core/src/HTTPParser.cxx +++ b/Framework/Core/src/HTTPParser.cxx @@ -214,7 +214,7 @@ std::string HTTPParserHelpers::calculateAccept(const char* nonce) return fmt::format("{}", base); } -std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol = "") +std::string encode_websocket_handshake_reply(char const* nonce) { constexpr auto res = "HTTP/1.1 101 Switching Protocols\r\n" From 6cf4c19b07d832484065be0d9d8b503925c75d5e Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Thu, 25 Sep 2025 09:08:50 +0200 Subject: [PATCH 6/6] Update HTTPParser.cxx --- Framework/Core/src/HTTPParser.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Framework/Core/src/HTTPParser.cxx b/Framework/Core/src/HTTPParser.cxx index 6e13180cd5fcb..fa2ba91722eb0 100644 --- a/Framework/Core/src/HTTPParser.cxx +++ b/Framework/Core/src/HTTPParser.cxx @@ -214,7 +214,7 @@ std::string HTTPParserHelpers::calculateAccept(const char* nonce) return fmt::format("{}", base); } -std::string encode_websocket_handshake_reply(char const* nonce) +std::string encode_websocket_handshake_reply(char const* nonce, const char* protocol) { constexpr auto res = "HTTP/1.1 101 Switching Protocols\r\n"