mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
SCRIPT: wpiformat
This commit is contained in:
committed by
Peter Johnson
parent
ae6bdc9d25
commit
2109161534
@@ -91,9 +91,9 @@ void HALSimHttpConnection::OnSimValueChanged(const wpi::util::json& msg) {
|
||||
// render json to buffers
|
||||
wpi::util::SmallVector<uv::Buffer, 4> sendBufs;
|
||||
wpi::net::raw_uv_ostream os{sendBufs, [this]() -> uv::Buffer {
|
||||
std::lock_guard lock(m_buffers_mutex);
|
||||
return m_buffers.Allocate();
|
||||
}};
|
||||
std::lock_guard lock(m_buffers_mutex);
|
||||
return m_buffers.Allocate();
|
||||
}};
|
||||
os << msg;
|
||||
|
||||
// call the websocket send function on the uv loop
|
||||
@@ -154,7 +154,7 @@ void HALSimHttpConnection::SendFileResponse(int code, std::string_view codeText,
|
||||
|
||||
void HALSimHttpConnection::ProcessRequest() {
|
||||
wpi::net::UrlParser url{m_request.GetUrl(),
|
||||
m_request.GetMethod() == wpi::net::HTTP_CONNECT};
|
||||
m_request.GetMethod() == wpi::net::HTTP_CONNECT};
|
||||
if (!url.IsValid()) {
|
||||
// failed to parse URL
|
||||
MySendError(400, "Invalid URL");
|
||||
@@ -166,17 +166,18 @@ void HALSimHttpConnection::ProcessRequest() {
|
||||
path = url.GetPath();
|
||||
}
|
||||
|
||||
if (m_request.GetMethod() == wpi::net::HTTP_GET && wpi::util::starts_with(path, '/') &&
|
||||
!wpi::util::contains(path, "..") && !wpi::util::contains(path, "//")) {
|
||||
if (m_request.GetMethod() == wpi::net::HTTP_GET &&
|
||||
wpi::util::starts_with(path, '/') && !wpi::util::contains(path, "..") &&
|
||||
!wpi::util::contains(path, "//")) {
|
||||
// convert to fs native representation
|
||||
fs::path nativePath;
|
||||
if (auto userPath = wpi::util::remove_prefix(path, "/user/")) {
|
||||
nativePath = fs::path{m_server->GetWebrootSys()} /
|
||||
fs::path{*userPath, fs::path::format::generic_format};
|
||||
} else {
|
||||
nativePath =
|
||||
fs::path{m_server->GetWebrootSys()} /
|
||||
fs::path{wpi::util::drop_front(path), fs::path::format::generic_format};
|
||||
nativePath = fs::path{m_server->GetWebrootSys()} /
|
||||
fs::path{wpi::util::drop_front(path),
|
||||
fs::path::format::generic_format};
|
||||
}
|
||||
|
||||
if (fs::is_directory(nativePath)) {
|
||||
@@ -202,5 +203,5 @@ void HALSimHttpConnection::MySendError(int code, std::string_view message) {
|
||||
void HALSimHttpConnection::Log(int code) {
|
||||
auto method = wpi::net::http_method_str(m_request.GetMethod());
|
||||
wpi::util::print(stderr, "{} {} HTTP/{}.{} {}\n", method, m_request.GetUrl(),
|
||||
m_request.GetMajor(), m_request.GetMinor(), code);
|
||||
m_request.GetMajor(), m_request.GetMinor(), code);
|
||||
}
|
||||
|
||||
@@ -72,7 +72,8 @@ bool HALSimWeb::Initialize() {
|
||||
try {
|
||||
m_port = std::stoi(port);
|
||||
} catch (const std::invalid_argument& err) {
|
||||
wpi::util::print(stderr, "Error decoding HALSIMWS_PORT ({})\n", err.what());
|
||||
wpi::util::print(stderr, "Error decoding HALSIMWS_PORT ({})\n",
|
||||
err.what());
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
@@ -82,8 +83,9 @@ bool HALSimWeb::Initialize() {
|
||||
const char* msgFilters = std::getenv("HALSIMWS_FILTERS");
|
||||
if (msgFilters != nullptr) {
|
||||
m_useMsgFiltering = true;
|
||||
wpi::util::split(wpi::util::trim(msgFilters), ',', -1, false,
|
||||
[&](auto val) { m_msgFilters[wpi::util::trim(val)] = true; });
|
||||
wpi::util::split(
|
||||
wpi::util::trim(msgFilters), ',', -1, false,
|
||||
[&](auto val) { m_msgFilters[wpi::util::trim(val)] = true; });
|
||||
} else {
|
||||
m_useMsgFiltering = false;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,8 @@ class HALSimHttpConnection
|
||||
public:
|
||||
HALSimHttpConnection(std::shared_ptr<HALSimWeb> server,
|
||||
std::shared_ptr<wpi::net::uv::Stream> stream)
|
||||
: wpi::net::HttpWebSocketServerConnection<HALSimHttpConnection>(stream, {}),
|
||||
: wpi::net::HttpWebSocketServerConnection<HALSimHttpConnection>(stream,
|
||||
{}),
|
||||
m_server(std::move(server)),
|
||||
m_buffers(128) {}
|
||||
|
||||
|
||||
@@ -25,8 +25,8 @@ namespace wpilibws {
|
||||
void WebServerClientTest::InitializeWebSocket(const std::string& host, int port,
|
||||
const std::string& uri) {
|
||||
wpi::util::print("Will attempt to connect to: {}:{}{}\n", host, port, uri);
|
||||
m_websocket = wpi::net::WebSocket::CreateClient(*m_tcp_client.get(), uri,
|
||||
fmt::format("{}:{}", host, port));
|
||||
m_websocket = wpi::net::WebSocket::CreateClient(
|
||||
*m_tcp_client.get(), uri, fmt::format("{}:{}", host, port));
|
||||
|
||||
// Hook up events
|
||||
m_websocket->open.connect_extended([this](auto conn, auto) {
|
||||
@@ -68,8 +68,9 @@ void WebServerClientTest::InitializeWebSocket(const std::string& host, int port,
|
||||
|
||||
// Create tcp client, specify callbacks, and create timers for loop
|
||||
bool WebServerClientTest::Initialize() {
|
||||
m_loop.error.connect(
|
||||
[](uv::Error err) { wpi::util::print(stderr, "uv Error: {}\n", err.str()); });
|
||||
m_loop.error.connect([](uv::Error err) {
|
||||
wpi::util::print(stderr, "uv Error: {}\n", err.str());
|
||||
});
|
||||
|
||||
m_tcp_client = uv::Tcp::Create(m_loop);
|
||||
if (!m_tcp_client) {
|
||||
@@ -135,9 +136,9 @@ void WebServerClientTest::SendMessage(const wpi::util::json& msg) {
|
||||
wpi::util::SmallVector<uv::Buffer, 4> sendBufs;
|
||||
|
||||
wpi::net::raw_uv_ostream os{sendBufs, [this]() -> uv::Buffer {
|
||||
std::lock_guard lock(m_buffers_mutex);
|
||||
return m_buffers->Allocate();
|
||||
}};
|
||||
std::lock_guard lock(m_buffers_mutex);
|
||||
return m_buffers->Allocate();
|
||||
}};
|
||||
os << msg;
|
||||
|
||||
// Call the websocket send function on the uv loop
|
||||
|
||||
@@ -58,7 +58,7 @@ TEST_F(WebServerIntegrationTest, DISABLED_DigitalOutput) {
|
||||
}
|
||||
if (IsConnectedClientWS()) {
|
||||
wpi::util::print("***** Setting DIO value for pin {} to {}\n", PIN,
|
||||
(EXPECTED_VALUE ? "true" : "false"));
|
||||
(EXPECTED_VALUE ? "true" : "false"));
|
||||
HALSIM_SetDIOValue(PIN, EXPECTED_VALUE);
|
||||
done = true;
|
||||
}
|
||||
@@ -109,8 +109,8 @@ TEST_F(WebServerIntegrationTest, DISABLED_DigitalInput) {
|
||||
}
|
||||
if (IsConnectedClientWS()) {
|
||||
wpi::util::json msg = {{"type", "DIO"},
|
||||
{"device", std::to_string(PIN)},
|
||||
{"data", {{"<>value", EXPECTED_VALUE}}}};
|
||||
{"device", std::to_string(PIN)},
|
||||
{"data", {{"<>value", EXPECTED_VALUE}}}};
|
||||
wpi::util::print("***** Input JSON: {}\n", msg.dump());
|
||||
m_webserverClient->SendMessage(msg);
|
||||
done = true;
|
||||
|
||||
Reference in New Issue
Block a user