Skip to content

Commit 3e160df

Browse files
nicolaspoffleyktf
andauthored
Use ternary expression to avoid repetition
Co-authored-by: Giulio Eulisse <10544+ktf@users.noreply.github.com>
1 parent 7d6fc54 commit 3e160df

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

Framework/Core/src/HTTPParser.cxx

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -216,24 +216,14 @@ std::string HTTPParserHelpers::calculateAccept(const char* nonce)
216216

217217
std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol = "")
218218
{
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-
}
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+
"{}"
225+
"Sec-WebSocket-Accept: {}\r\n\r\n";
226+
return fmt::format(res, protocol && protocol[0] ? fmt::format("Sec-WebSocket-Protocol: {}\r\n", protocol) : "", HTTPParserHelpers::calculateAccept(nonce));
237227
}
238228

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

0 commit comments

Comments
 (0)