Skip to content

Commit 7d6fc54

Browse files
Default param "" for protocol instead of overloading for encode_websocket_handshake_request
1 parent 68a3649 commit 7d6fc54

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

Framework/Core/src/HTTPParser.cxx

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,27 +214,26 @@ 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, char const* protocol = "")
218218
{
219-
constexpr auto res =
220-
"HTTP/1.1 101 Switching Protocols\r\n"
221-
"Upgrade: websocket\r\n"
222-
"Connection: Upgrade\r\n"
223-
"Access-Control-Allow-Origin: \"*\"\r\n"
224-
"Sec-WebSocket-Accept: {}\r\n\r\n";
225-
return fmt::format(res, HTTPParserHelpers::calculateAccept(nonce));
226-
}
227-
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));
219+
if (strlen(protocol) == 0) {
220+
constexpr auto res =
221+
"HTTP/1.1 101 Switching Protocols\r\n"
222+
"Upgrade: websocket\r\n"
223+
"Connection: Upgrade\r\n"
224+
"Access-Control-Allow-Origin: \"*\"\r\n"
225+
"Sec-WebSocket-Accept: {}\r\n\r\n";
226+
return fmt::format(res, HTTPParserHelpers::calculateAccept(nonce));
227+
} else {
228+
constexpr auto res =
229+
"HTTP/1.1 101 Switching Protocols\r\n"
230+
"Upgrade: websocket\r\n"
231+
"Connection: Upgrade\r\n"
232+
"Access-Control-Allow-Origin: \"*\"\r\n"
233+
"Sec-WebSocket-Protocol: {}\r\n"
234+
"Sec-WebSocket-Accept: {}\r\n\r\n";
235+
return fmt::format(res, protocol, HTTPParserHelpers::calculateAccept(nonce));
236+
}
238237
}
239238

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

Framework/Core/src/HTTPParser.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +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);
129128
/// @a protocol the websocket subprotocol to confirm (optional)
130-
std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol);
129+
std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol = "");
131130

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

0 commit comments

Comments
 (0)