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>
|
|
|
|
|
|
|
|
|
|
#include <wpi/MimeTypes.h>
|
|
|
|
|
#include <wpi/SmallVector.h>
|
|
|
|
|
#include <wpi/UrlParser.h>
|
2021-06-01 21:50:35 -07:00
|
|
|
#include <wpi/fs.h>
|
2020-08-20 01:14:03 -04:00
|
|
|
#include <wpi/raw_istream.h>
|
|
|
|
|
#include <wpi/raw_ostream.h>
|
|
|
|
|
#include <wpi/raw_uv_ostream.h>
|
|
|
|
|
#include <wpi/uv/Request.h>
|
|
|
|
|
|
|
|
|
|
namespace uv = wpi::uv;
|
|
|
|
|
|
2020-09-04 10:57:05 -07:00
|
|
|
using namespace wpilibws;
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
bool HALSimHttpConnection::IsValidWsUpgrade(wpi::StringRef 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() {
|
|
|
|
|
m_websocket->open.connect_extended([this](auto conn, wpi::StringRef) {
|
|
|
|
|
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;
|
|
|
|
|
wpi::errs() << "HALWebSim: websocket connected\n";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// parse incoming JSON, dispatch to parent
|
|
|
|
|
m_websocket->text.connect([this](wpi::StringRef 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
|
|
|
});
|
|
|
|
|
|
|
|
|
|
m_websocket->closed.connect([this](uint16_t, wpi::StringRef) {
|
|
|
|
|
// unset the global, allow another websocket to connect
|
|
|
|
|
if (m_isWsConnected) {
|
|
|
|
|
wpi::errs() << "HALWebSim: websocket disconnected\n";
|
|
|
|
|
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) {
|
|
|
|
|
// 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) {
|
|
|
|
|
wpi::errs() << err.str() << "\n";
|
|
|
|
|
wpi::errs().flush();
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-08-20 01:14:03 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HALSimHttpConnection::SendFileResponse(int code,
|
|
|
|
|
const wpi::Twine& codeText,
|
|
|
|
|
const wpi::Twine& contentType,
|
|
|
|
|
const wpi::Twine& filename,
|
|
|
|
|
const wpi::Twine& extraHeader) {
|
2021-06-01 21:50:35 -07:00
|
|
|
std::string fn = filename.str();
|
|
|
|
|
std::error_code ec;
|
2020-08-20 01:14:03 -04:00
|
|
|
|
2021-06-01 21:50:35 -07:00
|
|
|
// get file size
|
|
|
|
|
auto size = fs::file_size(fn, ec);
|
|
|
|
|
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
|
|
|
|
|
wpi::raw_fd_istream is{fn, ec, true};
|
|
|
|
|
if (ec) {
|
|
|
|
|
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};
|
|
|
|
|
|
|
|
|
|
std::string fileBuf;
|
|
|
|
|
size_t oldSize = 0;
|
|
|
|
|
|
2021-06-01 21:50:35 -07:00
|
|
|
while (fileBuf.size() < size) {
|
2020-08-20 01:14:03 -04:00
|
|
|
oldSize = fileBuf.size();
|
|
|
|
|
fileBuf.resize(oldSize + 1);
|
|
|
|
|
is.read(&(*fileBuf.begin()) + oldSize, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bodyOs << fileBuf;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpi::StringRef path;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (url.HasPath()) {
|
|
|
|
|
path = url.GetPath();
|
|
|
|
|
}
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
if (m_request.GetMethod() == wpi::HTTP_GET && path.startswith("/") &&
|
2021-06-01 21:50:35 -07:00
|
|
|
!path.contains("..") && !path.contains("//")) {
|
2020-08-20 01:14:03 -04:00
|
|
|
// convert to fs native representation
|
2021-06-01 21:50:35 -07:00
|
|
|
fs::path nativePath;
|
2020-08-20 01:14:03 -04:00
|
|
|
if (path.startswith("/user/")) {
|
2021-06-01 21:50:35 -07:00
|
|
|
nativePath = fs::path{std::string{m_server->GetWebrootSys()}} /
|
|
|
|
|
fs::path{std::string{path.drop_front(6)},
|
|
|
|
|
fs::path::format::generic_format};
|
2020-08-20 01:14:03 -04:00
|
|
|
} else {
|
2021-06-01 21:50:35 -07:00
|
|
|
nativePath = fs::path{std::string{m_server->GetWebrootSys()}} /
|
|
|
|
|
fs::path{std::string{path.drop_front(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)) {
|
2020-08-20 01:14:03 -04:00
|
|
|
MySendError(404, "Resource '" + path + "' not found");
|
|
|
|
|
} 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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HALSimHttpConnection::MySendError(int code, const wpi::Twine& message) {
|
|
|
|
|
Log(code);
|
|
|
|
|
SendError(code, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HALSimHttpConnection::Log(int code) {
|
|
|
|
|
auto method = wpi::http_method_str(m_request.GetMethod());
|
|
|
|
|
wpi::errs() << method << " " << m_request.GetUrl() << " HTTP/"
|
|
|
|
|
<< m_request.GetMajor() << "." << m_request.GetMinor() << " "
|
|
|
|
|
<< code << "\n";
|
|
|
|
|
}
|