Skip to content

Commit 10dd8dc

Browse files
DPL Websocket: Add protocol param to encode_websocket_handshake_reply which allows a response Sec-WebSocket-Accept in handshake. (#14687)
DPL Websocket: Add overloaded encode_websocket_handshake_reply which allows a response Sec-WebSocket-Accept in handshake.
1 parent 4dfc128 commit 10dd8dc

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

Framework/Core/src/DPLWebSocket.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ void WSDPLHandler::endHeaders()
276276
}
277277
/// Create an appropriate reply
278278
LOG(debug) << "Got upgrade request with nonce " << mHeaders["sec-websocket-key"].c_str();
279-
std::string reply = encode_websocket_handshake_reply(mHeaders["sec-websocket-key"].c_str());
279+
std::string reply = encode_websocket_handshake_reply(mHeaders["sec-websocket-key"].c_str(), "dpl");
280280
mHandshaken = true;
281281

282282
uv_buf_t bfr = uv_buf_init(strdup(reply.data()), reply.size());

Framework/Core/src/HTTPParser.cxx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,16 @@ std::string HTTPParserHelpers::calculateAccept(const char* nonce)
214214
return fmt::format("{}", base);
215215
}
216216

217-
std::string encode_websocket_handshake_reply(char const* nonce)
217+
std::string encode_websocket_handshake_reply(char const* nonce, const char* protocol)
218218
{
219219
constexpr auto res =
220220
"HTTP/1.1 101 Switching Protocols\r\n"
221221
"Upgrade: websocket\r\n"
222222
"Connection: Upgrade\r\n"
223223
"Access-Control-Allow-Origin: \"*\"\r\n"
224+
"{}"
224225
"Sec-WebSocket-Accept: {}\r\n\r\n";
225-
return fmt::format(res, HTTPParserHelpers::calculateAccept(nonce));
226+
return fmt::format(res, protocol && protocol[0] ? fmt::format("Sec-WebSocket-Protocol: {}\r\n", protocol) : "", HTTPParserHelpers::calculateAccept(nonce));
226227
}
227228

228229
void parse_http_request(char* start, size_t size, HTTPParser* parser)

Framework/Core/src/HTTPParser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ std::string encode_websocket_handshake_request(const char* path, const char* pro
125125

126126
/// Encodes the server reply for a given websocket connection
127127
/// @a nonce the nonce of the request.
128-
std::string encode_websocket_handshake_reply(char const* nonce);
128+
/// @a protocol the websocket subprotocol to confirm (optional)
129+
std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol = "");
129130

130131
/// Encodes the buffer @a src which is @a size long to a number of buffers suitable to be sent via libuv.
131132
/// If @a binary is provided the binary bit is set.

0 commit comments

Comments
 (0)