Skip to content

Commit 68a3649

Browse files
DPL Websocket: Add overloaded encode_websocket_handshake_reply which allows a response Sec-WebSocket-Accept in handshake.
1 parent e019d1a commit 68a3649

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ std::string encode_websocket_handshake_reply(char const* nonce)
225225
return fmt::format(res, HTTPParserHelpers::calculateAccept(nonce));
226226
}
227227

228+
std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol)
229+
{
230+
constexpr auto res =
231+
"HTTP/1.1 101 Switching Protocols\r\n"
232+
"Upgrade: websocket\r\n"
233+
"Connection: Upgrade\r\n"
234+
"Access-Control-Allow-Origin: \"*\"\r\n"
235+
"Sec-WebSocket-Protocol: {}\r\n"
236+
"Sec-WebSocket-Accept: {}\r\n\r\n";
237+
return fmt::format(res, protocol, HTTPParserHelpers::calculateAccept(nonce));
238+
}
239+
228240
void parse_http_request(char* start, size_t size, HTTPParser* parser)
229241
{
230242
enum HTTPState state = HTTPState::IN_START;

Framework/Core/src/HTTPParser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ std::string encode_websocket_handshake_request(const char* path, const char* pro
126126
/// Encodes the server reply for a given websocket connection
127127
/// @a nonce the nonce of the request.
128128
std::string encode_websocket_handshake_reply(char const* nonce);
129+
/// @a protocol the websocket subprotocol to confirm (optional)
130+
std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol);
129131

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

0 commit comments

Comments
 (0)