2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
#include "HALSimHttpConnection.h"
|
|
|
|
|
|
|
|
|
|
#include <uv.h>
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
2023-09-21 23:20:09 -07:00
|
|
|
#include <wpi/MemoryBuffer.h>
|
2020-08-20 01:14:03 -04:00
|
|
|
#include <wpi/SmallVector.h>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <wpi/StringExtras.h>
|
2021-06-01 21:50:35 -07:00
|
|
|
#include <wpi/fs.h>
|
2022-05-07 10:54:14 -07:00
|
|
|
#include <wpinet/MimeTypes.h>
|
|
|
|
|
#include <wpinet/UrlParser.h>
|
|
|
|
|
#include <wpinet/raw_uv_ostream.h>
|
|
|
|
|
#include <wpinet/uv/Request.h>
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
namespace uv = wpi::uv;
|
|
|
|
|
|
2020-09-04 10:57:05 -07:00
|
|
|
using namespace wpilibws;
|
2020-08-20 01:14:03 -04:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool HALSimHttpConnection::IsValidWsUpgrade(std::string_view protocol) {
|
2020-09-04 10:57:05 -07:00
|
|
|
if (m_request.GetUrl() != m_server->GetServerUri()) {
|
2020-08-20 01:14:03 -04:00
|
|
|
MySendError(404, "invalid websocket address");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HALSimHttpConnection::ProcessWsUpgrade() {
|
2021-06-06 16:13:58 -07:00
|
|
|
m_websocket->open.connect_extended([this](auto conn, auto) {
|
2020-08-20 01:14:03 -04:00
|
|
|
conn.disconnect(); // one-shot
|
|
|
|
|
|
2020-09-04 10:57:05 -07:00
|
|
|
if (!m_server->RegisterWebsocket(shared_from_this())) {
|
2020-08-20 01:14:03 -04:00
|
|
|
Log(409);
|
|
|
|
|
m_websocket->Fail(409, "Only a single simulation websocket is allowed");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log(200);
|
|
|
|
|
m_isWsConnected = true;
|
2021-06-06 16:13:58 -07:00
|
|
|
std::fputs("HALWebSim: websocket connected\n", stderr);
|
2020-08-20 01:14:03 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// parse incoming JSON, dispatch to parent
|
2021-06-06 16:13:58 -07:00
|
|
|
m_websocket->text.connect([this](auto msg, bool) {
|
2020-09-04 10:57:05 -07:00
|
|
|
if (!m_isWsConnected) {
|
2020-08-20 01:14:03 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpi::json j;
|
|
|
|
|
try {
|
|
|
|
|
j = wpi::json::parse(msg);
|
|
|
|
|
} catch (const wpi::json::parse_error& e) {
|
|
|
|
|
std::string err("JSON parse failed: ");
|
|
|
|
|
err += e.what();
|
|
|
|
|
m_websocket->Fail(400, err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-04 10:57:05 -07:00
|
|
|
m_server->OnNetValueChanged(j);
|
2020-08-20 01:14:03 -04:00
|
|
|
});
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
m_websocket->closed.connect([this](uint16_t, auto) {
|
2020-08-20 01:14:03 -04:00
|
|
|
// unset the global, allow another websocket to connect
|
|
|
|
|
if (m_isWsConnected) {
|
2021-06-06 16:13:58 -07:00
|
|
|
std::fputs("HALWebSim: websocket disconnected\n", stderr);
|
2020-08-20 01:14:03 -04:00
|
|
|
m_isWsConnected = false;
|
|
|
|
|
|
2020-09-04 10:57:05 -07:00
|
|
|
m_server->CloseWebsocket(shared_from_this());
|
2020-08-20 01:14:03 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HALSimHttpConnection::OnSimValueChanged(const wpi::json& msg) {
|
2023-06-20 11:26:03 -04:00
|
|
|
// Skip sending if this message is not in the allowed filter list
|
|
|
|
|
try {
|
|
|
|
|
auto& type = msg.at("type").get_ref<const std::string&>();
|
|
|
|
|
if (!m_server->CanSendMessage(type)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} catch (wpi::json::exception& e) {
|
|
|
|
|
fmt::print(stderr, "Error with message: {}\n", e.what());
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 01:14:03 -04:00
|
|
|
// render json to buffers
|
|
|
|
|
wpi::SmallVector<uv::Buffer, 4> sendBufs;
|
|
|
|
|
wpi::raw_uv_ostream os{sendBufs, [this]() -> uv::Buffer {
|
|
|
|
|
std::lock_guard lock(m_buffers_mutex);
|
2020-09-04 10:57:05 -07:00
|
|
|
return m_buffers.Allocate();
|
2020-08-20 01:14:03 -04:00
|
|
|
}};
|
|
|
|
|
os << msg;
|
|
|
|
|
|
|
|
|
|
// call the websocket send function on the uv loop
|
2020-09-04 10:57:05 -07:00
|
|
|
m_server->GetExec().Send([self = shared_from_this(), sendBufs] {
|
|
|
|
|
self->m_websocket->SendText(sendBufs,
|
|
|
|
|
[self](auto bufs, wpi::uv::Error err) {
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard lock(self->m_buffers_mutex);
|
|
|
|
|
self->m_buffers.Release(bufs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (err) {
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::print(stderr, "{}\n", err.str());
|
|
|
|
|
std::fflush(stderr);
|
2020-09-04 10:57:05 -07:00
|
|
|
}
|
|
|
|
|
});
|
2020-08-20 01:14:03 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void HALSimHttpConnection::SendFileResponse(int code, std::string_view codeText,
|
|
|
|
|
std::string_view contentType,
|
|
|
|
|
std::string_view filename,
|
|
|
|
|
std::string_view extraHeader) {
|
2021-06-01 21:50:35 -07:00
|
|
|
std::error_code ec;
|
2020-08-20 01:14:03 -04:00
|
|
|
|
2021-06-01 21:50:35 -07:00
|
|
|
// get file size
|
2021-06-06 16:13:58 -07:00
|
|
|
auto size = fs::file_size(filename, ec);
|
2021-06-01 21:50:35 -07:00
|
|
|
if (ec) {
|
2020-08-20 01:14:03 -04:00
|
|
|
MySendError(404, "error getting file size");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 21:50:35 -07:00
|
|
|
// open file
|
2023-09-21 23:20:09 -07:00
|
|
|
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
|
|
|
|
|
wpi::MemoryBuffer::GetFile(filename, ec);
|
|
|
|
|
if (fileBuffer == nullptr || ec) {
|
2021-06-01 21:50:35 -07:00
|
|
|
MySendError(404, "error opening file");
|
2020-08-20 01:14:03 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpi::SmallVector<uv::Buffer, 4> toSend;
|
|
|
|
|
wpi::raw_uv_ostream os{toSend, 4096};
|
2021-06-01 21:50:35 -07:00
|
|
|
BuildHeader(os, code, codeText, contentType, size, extraHeader);
|
2020-08-20 01:14:03 -04:00
|
|
|
SendData(os.bufs(), false);
|
|
|
|
|
|
|
|
|
|
Log(code);
|
|
|
|
|
|
|
|
|
|
// Read the file byte by byte
|
|
|
|
|
wpi::SmallVector<uv::Buffer, 4> bodyData;
|
|
|
|
|
wpi::raw_uv_ostream bodyOs{bodyData, 4096};
|
|
|
|
|
|
2023-09-21 23:20:09 -07:00
|
|
|
bodyOs << fileBuffer->GetBuffer();
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
SendData(bodyOs.bufs(), false);
|
|
|
|
|
if (!m_keepAlive) {
|
|
|
|
|
m_stream.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HALSimHttpConnection::ProcessRequest() {
|
|
|
|
|
wpi::UrlParser url{m_request.GetUrl(),
|
|
|
|
|
m_request.GetMethod() == wpi::HTTP_CONNECT};
|
|
|
|
|
if (!url.IsValid()) {
|
|
|
|
|
// failed to parse URL
|
|
|
|
|
MySendError(400, "Invalid URL");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view path;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (url.HasPath()) {
|
|
|
|
|
path = url.GetPath();
|
|
|
|
|
}
|
2020-08-20 01:14:03 -04:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
if (m_request.GetMethod() == wpi::HTTP_GET && wpi::starts_with(path, '/') &&
|
|
|
|
|
!wpi::contains(path, "..") && !wpi::contains(path, "//")) {
|
2020-08-20 01:14:03 -04:00
|
|
|
// convert to fs native representation
|
2021-06-01 21:50:35 -07:00
|
|
|
fs::path nativePath;
|
2021-06-06 16:13:58 -07:00
|
|
|
if (wpi::starts_with(path, "/user/")) {
|
|
|
|
|
nativePath =
|
|
|
|
|
fs::path{m_server->GetWebrootSys()} /
|
|
|
|
|
fs::path{wpi::drop_front(path, 6), fs::path::format::generic_format};
|
2020-08-20 01:14:03 -04:00
|
|
|
} else {
|
2021-06-06 16:13:58 -07:00
|
|
|
nativePath =
|
|
|
|
|
fs::path{m_server->GetWebrootSys()} /
|
|
|
|
|
fs::path{wpi::drop_front(path, 1), fs::path::format::generic_format};
|
2020-08-20 01:14:03 -04:00
|
|
|
}
|
|
|
|
|
|
2021-06-01 21:50:35 -07:00
|
|
|
if (fs::is_directory(nativePath)) {
|
|
|
|
|
nativePath.append("index.html");
|
2020-08-20 01:14:03 -04:00
|
|
|
}
|
|
|
|
|
|
2021-06-01 21:50:35 -07:00
|
|
|
if (!fs::exists(nativePath) || fs::is_directory(nativePath)) {
|
2021-06-06 16:13:58 -07:00
|
|
|
MySendError(404, fmt::format("Resource '{}' not found", path));
|
2020-08-20 01:14:03 -04:00
|
|
|
} else {
|
2021-06-01 21:50:35 -07:00
|
|
|
auto contentType = wpi::MimeTypeFromPath(nativePath.string());
|
|
|
|
|
SendFileResponse(200, "OK", contentType, nativePath.string());
|
2020-08-20 01:14:03 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
MySendError(404, "Resource not found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void HALSimHttpConnection::MySendError(int code, std::string_view message) {
|
2020-08-20 01:14:03 -04:00
|
|
|
Log(code);
|
|
|
|
|
SendError(code, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HALSimHttpConnection::Log(int code) {
|
|
|
|
|
auto method = wpi::http_method_str(m_request.GetMethod());
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::print(stderr, "{} {} HTTP/{}.{} {}\n", method, m_request.GetUrl(),
|
|
|
|
|
m_request.GetMajor(), m_request.GetMinor(), code);
|
2020-08-20 01:14:03 -04:00
|
|
|
}
|