118 lines
3.5 KiB
C++
118 lines
3.5 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Net/Http.hpp>
|
|
#include <Void/Net/Server.hpp>
|
|
#include <Void/Net/WebSocket.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <string>
|
|
|
|
namespace Void::Net
|
|
{
|
|
struct HttpServerOptions {
|
|
std::string bind_host{"127.0.0.1"};
|
|
std::uint16_t port{0};
|
|
std::size_t max_header_bytes{64ULL * 1024};
|
|
std::size_t max_headers{128};
|
|
std::uint64_t max_body_bytes{16ULL * 1024 * 1024};
|
|
std::uint64_t max_streaming_body_bytes{512ULL * 1024 * 1024};
|
|
std::size_t max_concurrent_connections{64};
|
|
std::chrono::milliseconds read_timeout{std::chrono::seconds{30}};
|
|
std::chrono::milliseconds write_timeout{std::chrono::seconds{30}};
|
|
int listen_backlog{128};
|
|
};
|
|
|
|
/// Streaming request body reader passed to streaming HTTP route handlers.
|
|
class BodyReader
|
|
{
|
|
public:
|
|
|
|
struct State;
|
|
|
|
explicit BodyReader(State& state) noexcept : m_state(&state) {}
|
|
BodyReader(const BodyReader&) = delete;
|
|
BodyReader& operator=(const BodyReader&) = delete;
|
|
|
|
[[nodiscard]] std::optional<std::uint64_t>
|
|
content_length() const noexcept;
|
|
[[nodiscard]] bool is_chunked() const noexcept;
|
|
[[nodiscard]] std::uint64_t bytes_read() const noexcept;
|
|
|
|
/// Read up to dst.size() decoded body bytes. Returns 0 at EOF.
|
|
[[nodiscard]] Void::Result::Result<std::size_t>
|
|
read_some(std::span<std::byte> dst);
|
|
|
|
/// Stream the request body atomically to `destination`, enforcing
|
|
/// `max_bytes` (or server max_streaming_body_bytes when 0).
|
|
[[nodiscard]] Void::Result::Result<std::uint64_t>
|
|
read_to_file(const std::filesystem::path& destination,
|
|
std::uint64_t max_bytes = 0);
|
|
|
|
private:
|
|
|
|
State* m_state{nullptr};
|
|
};
|
|
|
|
using StreamingRouteHandler =
|
|
std::function<RouteResult(const ServerRequest&, BodyReader&)>;
|
|
|
|
/// Concrete POSIX TCP HTTP/1.1 listener dispatching buffered routes through
|
|
/// `Void::Net::Router` and streaming routes via `BodyReader`.
|
|
class HttpServer
|
|
{
|
|
public:
|
|
|
|
explicit HttpServer(HttpServerOptions options = {});
|
|
explicit HttpServer(Router& external_router,
|
|
HttpServerOptions options = {});
|
|
~HttpServer() noexcept;
|
|
|
|
HttpServer(HttpServer&&) noexcept;
|
|
HttpServer& operator=(HttpServer&&) noexcept;
|
|
HttpServer(const HttpServer&) = delete;
|
|
HttpServer& operator=(const HttpServer&) = delete;
|
|
|
|
[[nodiscard]] Router& router() noexcept;
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
add_route(Method method, std::string pattern,
|
|
RouteHandler handler);
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
add_streaming_route(Method method, std::string pattern,
|
|
StreamingRouteHandler handler);
|
|
|
|
/// Register a RFC 6455 WebSocket upgrade endpoint backed by
|
|
/// `sessions` (caller must keep `sessions` alive until after
|
|
/// `shutdown()`).
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
add_websocket_route(std::string pattern,
|
|
WebSocketSessions& sessions,
|
|
WebSocketUpgradeOptions options = {});
|
|
|
|
[[nodiscard]] Void::Result::Result<void> start();
|
|
|
|
void shutdown(std::chrono::milliseconds drain_timeout =
|
|
std::chrono::seconds{5}) noexcept;
|
|
|
|
[[nodiscard]] bool running() const noexcept;
|
|
[[nodiscard]] std::uint16_t bound_port() const noexcept;
|
|
[[nodiscard]] std::string bind_address() const;
|
|
[[nodiscard]] std::string base_url() const;
|
|
|
|
private:
|
|
|
|
struct Impl;
|
|
std::unique_ptr<Impl> m_impl;
|
|
};
|
|
|
|
} // namespace Void::Net
|