mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Add braces to C++ single-line loops and conditionals (NFC) (#2973)
This makes code easier to read and more consistent between C++ and Java. Also update clang-format settings to always add a line break (even if no braces are used).
This commit is contained in:
@@ -15,7 +15,9 @@ WebSocketServerHelper::WebSocketServerHelper(HttpParser& req) {
|
||||
if (name.equals_lower("host")) {
|
||||
m_gotHost = true;
|
||||
} else if (name.equals_lower("upgrade")) {
|
||||
if (value.equals_lower("websocket")) m_websocket = true;
|
||||
if (value.equals_lower("websocket")) {
|
||||
m_websocket = true;
|
||||
}
|
||||
} else if (name.equals_lower("sec-websocket-key")) {
|
||||
m_key = value;
|
||||
} else if (name.equals_lower("sec-websocket-version")) {
|
||||
@@ -26,22 +28,29 @@ WebSocketServerHelper::WebSocketServerHelper(HttpParser& req) {
|
||||
value.split(protocols, ",", -1, false);
|
||||
for (auto protocol : protocols) {
|
||||
protocol = protocol.trim();
|
||||
if (!protocol.empty()) m_protocols.emplace_back(protocol);
|
||||
if (!protocol.empty()) {
|
||||
m_protocols.emplace_back(protocol);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
req.headersComplete.connect([&req, this](bool) {
|
||||
if (req.IsUpgrade() && IsUpgrade()) upgrade();
|
||||
if (req.IsUpgrade() && IsUpgrade()) {
|
||||
upgrade();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
std::pair<bool, StringRef> WebSocketServerHelper::MatchProtocol(
|
||||
ArrayRef<StringRef> protocols) {
|
||||
if (protocols.empty() && m_protocols.empty())
|
||||
if (protocols.empty() && m_protocols.empty()) {
|
||||
return std::make_pair(true, StringRef{});
|
||||
}
|
||||
for (auto protocol : protocols) {
|
||||
for (auto&& clientProto : m_protocols) {
|
||||
if (protocol == clientProto) return std::make_pair(true, protocol);
|
||||
if (protocol == clientProto) {
|
||||
return std::make_pair(true, protocol);
|
||||
}
|
||||
}
|
||||
}
|
||||
return std::make_pair(false, StringRef{});
|
||||
@@ -59,24 +68,31 @@ WebSocketServer::WebSocketServer(uv::Stream& stream,
|
||||
m_req.header.connect([this](StringRef name, StringRef value) {
|
||||
if (name.equals_lower("host")) {
|
||||
if (m_options.checkHost) {
|
||||
if (!m_options.checkHost(value)) Abort(401, "Unrecognized Host");
|
||||
if (!m_options.checkHost(value)) {
|
||||
Abort(401, "Unrecognized Host");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
m_req.url.connect([this](StringRef name) {
|
||||
if (m_options.checkUrl) {
|
||||
if (!m_options.checkUrl(name)) Abort(404, "Not Found");
|
||||
if (!m_options.checkUrl(name)) {
|
||||
Abort(404, "Not Found");
|
||||
}
|
||||
}
|
||||
});
|
||||
m_req.headersComplete.connect([this](bool) {
|
||||
// We only accept websocket connections
|
||||
if (!m_helper.IsUpgrade() || !m_req.IsUpgrade())
|
||||
if (!m_helper.IsUpgrade() || !m_req.IsUpgrade()) {
|
||||
Abort(426, "Upgrade Required");
|
||||
}
|
||||
});
|
||||
|
||||
// Handle upgrade event
|
||||
m_helper.upgrade.connect([this] {
|
||||
if (m_aborted) return;
|
||||
if (m_aborted) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Negotiate sub-protocol
|
||||
SmallVector<StringRef, 2> protocols{m_protocols.begin(), m_protocols.end()};
|
||||
@@ -103,9 +119,13 @@ WebSocketServer::WebSocketServer(uv::Stream& stream,
|
||||
stream.StartRead();
|
||||
m_dataConn =
|
||||
stream.data.connect_connection([this](uv::Buffer& buf, size_t size) {
|
||||
if (m_aborted) return;
|
||||
if (m_aborted) {
|
||||
return;
|
||||
}
|
||||
m_req.Execute(StringRef{buf.base, size});
|
||||
if (m_req.HasError()) Abort(400, "Bad Request");
|
||||
if (m_req.HasError()) {
|
||||
Abort(400, "Bad Request");
|
||||
}
|
||||
});
|
||||
m_errorConn =
|
||||
stream.error.connect_connection([this](uv::Error) { m_stream.Close(); });
|
||||
@@ -122,7 +142,9 @@ std::shared_ptr<WebSocketServer> WebSocketServer::Create(
|
||||
}
|
||||
|
||||
void WebSocketServer::Abort(uint16_t code, StringRef reason) {
|
||||
if (m_aborted) return;
|
||||
if (m_aborted) {
|
||||
return;
|
||||
}
|
||||
m_aborted = true;
|
||||
|
||||
// Build response
|
||||
@@ -131,10 +153,14 @@ void WebSocketServer::Abort(uint16_t code, StringRef reason) {
|
||||
|
||||
// Handle unsupported version
|
||||
os << "HTTP/1.1 " << code << ' ' << reason << "\r\n";
|
||||
if (code == 426) os << "Upgrade: WebSocket\r\n";
|
||||
if (code == 426) {
|
||||
os << "Upgrade: WebSocket\r\n";
|
||||
}
|
||||
os << "\r\n";
|
||||
m_stream.Write(bufs, [this](auto bufs, uv::Error) {
|
||||
for (auto& buf : bufs) buf.Deallocate();
|
||||
for (auto& buf : bufs) {
|
||||
buf.Deallocate();
|
||||
}
|
||||
m_stream.Shutdown([this] { m_stream.Close(); });
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user