234 lines
6.7 KiB
C++
234 lines
6.7 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Net/Http.hpp>
|
|
|
|
#include <compare>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <shared_mutex>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace Void::Net
|
|
{
|
|
|
|
using RouteParameters = std::map<std::string, std::string, std::less<>>;
|
|
|
|
struct ServerRequest {
|
|
Method method{Method::Get};
|
|
std::string path;
|
|
std::vector<Header> headers;
|
|
std::string body;
|
|
RouteParameters parameters;
|
|
|
|
[[nodiscard]] std::optional<std::string_view>
|
|
header(std::string_view name) const noexcept;
|
|
};
|
|
|
|
struct ServerResponse {
|
|
std::uint16_t status{200};
|
|
std::vector<Header> headers;
|
|
std::string body;
|
|
|
|
[[nodiscard]] std::optional<std::string_view>
|
|
header(std::string_view name) const noexcept;
|
|
};
|
|
|
|
using RouteResult = Void::Result::Result<ServerResponse>;
|
|
using RouteHandler = std::function<RouteResult(const ServerRequest&)>;
|
|
|
|
/// A transport-neutral method/path router. Patterns support exact segments,
|
|
/// `:name` parameters, and a final `*name` catch-all. Route registration
|
|
/// and dispatch are thread-safe; handlers run without the router lock held.
|
|
class Router
|
|
{
|
|
public:
|
|
|
|
Router() = default;
|
|
Router(const Router&) = delete;
|
|
Router& operator=(const Router&) = delete;
|
|
Router(Router&&) = delete;
|
|
Router& operator=(Router&&) = delete;
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
add(Method method, std::string pattern, RouteHandler handler);
|
|
|
|
/// A successful empty optional means that no route matched.
|
|
[[nodiscard]] Void::Result::Result<std::optional<ServerResponse>>
|
|
dispatch(ServerRequest request) const;
|
|
|
|
[[nodiscard]] std::size_t size() const;
|
|
|
|
private:
|
|
|
|
struct Route {
|
|
Method method;
|
|
std::string pattern;
|
|
std::vector<std::string> segments;
|
|
RouteHandler handler;
|
|
};
|
|
|
|
mutable std::shared_mutex mutex_;
|
|
std::vector<Route> routes_;
|
|
};
|
|
|
|
struct StaticFile {
|
|
std::filesystem::path path;
|
|
std::string_view content_type;
|
|
std::uintmax_t size{};
|
|
};
|
|
|
|
[[nodiscard]] std::string_view
|
|
content_type_for(const std::filesystem::path& path);
|
|
|
|
/// Resolves URL paths beneath one canonical document root. Percent-decoded
|
|
/// traversal, backslashes, control characters, and symlink escapes are
|
|
/// rejected. Missing files are represented by a successful empty optional.
|
|
class StaticFileResolver
|
|
{
|
|
public:
|
|
|
|
[[nodiscard]] static Void::Result::Result<StaticFileResolver>
|
|
create(std::filesystem::path root,
|
|
std::string index_file = "index.html");
|
|
|
|
[[nodiscard]] Void::Result::Result<std::optional<StaticFile>>
|
|
resolve(std::string_view request_path) const;
|
|
|
|
[[nodiscard]] const std::filesystem::path& root() const noexcept
|
|
{
|
|
return root_;
|
|
}
|
|
|
|
private:
|
|
|
|
StaticFileResolver(std::filesystem::path root,
|
|
std::string index_file)
|
|
: root_(std::move(root)), index_file_(std::move(index_file))
|
|
{
|
|
}
|
|
|
|
std::filesystem::path root_;
|
|
std::string index_file_;
|
|
};
|
|
|
|
struct WebSocketConnectionId {
|
|
std::uint64_t value{};
|
|
auto operator<=>(const WebSocketConnectionId&) const = default;
|
|
};
|
|
|
|
struct WebSocketClose {
|
|
std::uint16_t code{1000};
|
|
std::string reason;
|
|
};
|
|
|
|
/// Implemented by the concrete WebSocket transport owned by an HTTP server.
|
|
class WebSocketConnection
|
|
{
|
|
public:
|
|
|
|
virtual ~WebSocketConnection() = default;
|
|
[[nodiscard]] virtual WebSocketConnectionId id() const noexcept = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<void>
|
|
send_text(std::string_view message) = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<void>
|
|
send_binary(std::span<const std::byte> message) = 0;
|
|
virtual void close(const WebSocketClose& close) noexcept = 0;
|
|
};
|
|
|
|
struct WebSocketEvents {
|
|
std::function<void(const std::shared_ptr<WebSocketConnection>&)>
|
|
opened;
|
|
std::function<void(const std::shared_ptr<WebSocketConnection>&,
|
|
std::string_view)>
|
|
text;
|
|
std::function<void(const std::shared_ptr<WebSocketConnection>&,
|
|
std::span<const std::byte>)>
|
|
binary;
|
|
std::function<void(WebSocketConnectionId, const WebSocketClose&)>
|
|
closed;
|
|
std::function<void(WebSocketConnectionId,
|
|
const Void::Result::Error&)>
|
|
error;
|
|
};
|
|
|
|
/// Thread-safe connection ownership and lifecycle dispatch. All callbacks
|
|
/// and transport close operations run without the registry lock held. Call
|
|
/// shutdown() for close callbacks; destruction performs a best-effort close
|
|
/// without invoking user code.
|
|
class WebSocketSessions
|
|
{
|
|
public:
|
|
|
|
WebSocketSessions() = default;
|
|
explicit WebSocketSessions(WebSocketEvents events)
|
|
: events_(std::move(events))
|
|
{
|
|
}
|
|
~WebSocketSessions() noexcept;
|
|
|
|
WebSocketSessions(const WebSocketSessions&) = delete;
|
|
WebSocketSessions& operator=(const WebSocketSessions&) = delete;
|
|
WebSocketSessions(WebSocketSessions&&) = delete;
|
|
WebSocketSessions& operator=(WebSocketSessions&&) = delete;
|
|
|
|
void set_events(WebSocketEvents events);
|
|
|
|
/// Register a transport connection and emit `opened`. Returns false
|
|
/// for a duplicate connection ID.
|
|
[[nodiscard]] Void::Result::Result<bool>
|
|
open(std::shared_ptr<WebSocketConnection> connection);
|
|
|
|
[[nodiscard]] std::shared_ptr<WebSocketConnection>
|
|
find(WebSocketConnectionId id) const;
|
|
[[nodiscard]] bool contains(WebSocketConnectionId id) const;
|
|
[[nodiscard]] std::size_t size() const;
|
|
|
|
[[nodiscard]] bool receive_text(WebSocketConnectionId id,
|
|
std::string_view message) const;
|
|
[[nodiscard]] bool
|
|
receive_binary(WebSocketConnectionId id,
|
|
std::span<const std::byte> message) const;
|
|
[[nodiscard]] bool fail(WebSocketConnectionId id,
|
|
const Void::Result::Error& error) const;
|
|
|
|
/// Record a transport-initiated close without closing it again.
|
|
[[nodiscard]] bool disconnect(WebSocketConnectionId id,
|
|
WebSocketClose close = {});
|
|
|
|
/// Remove and actively close one connection.
|
|
[[nodiscard]] bool close(WebSocketConnectionId id,
|
|
WebSocketClose close = {});
|
|
|
|
/// Remove and actively close every connection. Returns the count.
|
|
std::size_t shutdown(WebSocketClose close = {1001,
|
|
"server shutdown"});
|
|
|
|
private:
|
|
|
|
using Connections = std::map<WebSocketConnectionId,
|
|
std::shared_ptr<WebSocketConnection>>;
|
|
|
|
[[nodiscard]] bool remove(WebSocketConnectionId id,
|
|
const WebSocketClose& close,
|
|
bool close_transport);
|
|
[[nodiscard]] Connections take_all(bool stop_accepting = false);
|
|
|
|
mutable std::mutex mutex_;
|
|
Connections connections_;
|
|
WebSocketEvents events_;
|
|
bool accepting_{true};
|
|
};
|
|
|
|
} // namespace Void::Net
|