Net: add duplex HTTP response streaming (closes #37)

This commit is contained in:
faumaray committed 2026-12-03 16:03:31 +03:00
1 parent 7cfd30e550
commit a6dd7fd927
3 files changed
+1609 -3

No files matched your search

+87
View File
@@ -13,7 +13,9 @@
#include <memory>
#include <optional>
#include <span>
#include <stop_token>
#include <string>
#include <vector>
namespace Void::Net
{
@@ -27,6 +29,10 @@ namespace Void::Net
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}};
/// Bounded per-exchange output buffer used by duplex response
/// writers. Producers block (or observe a short accepted count)
/// once this much framed data is pending.
std::size_t duplex_write_buffer_bytes{64ULL * 1024};
int listen_backlog{128};
};
@@ -64,6 +70,83 @@ namespace Void::Net
using StreamingRouteHandler =
std::function<RouteResult(const ServerRequest&, BodyReader&)>;
/// Scoped, move-only writer for a live duplex HTTP exchange.
///
/// Exactly one writer may be active at a time (`begin`, `write_some` and
/// `finish` fail with `overloaded` if another thread is inside the
/// writer). `write_some` returns how many payload bytes it accepted; the
/// writer applies bounded buffering and downstream backpressure. Fixed
/// length, chunked and bodyless responses are selected by `begin`.
///
/// After the exchange returns the writer is invalidated. A moved-from or
/// retained writer observes `valid() == false` and every operation fails
/// with `invalid_argument` instead of touching the connection.
class ResponseWriter
{
public:
struct State;
ResponseWriter() noexcept = default;
~ResponseWriter() = default;
ResponseWriter(const ResponseWriter&) = delete;
ResponseWriter& operator=(const ResponseWriter&) = delete;
ResponseWriter(ResponseWriter&&) noexcept = default;
ResponseWriter& operator=(ResponseWriter&&) noexcept = default;
[[nodiscard]] bool valid() const noexcept;
[[nodiscard]] bool begun() const noexcept;
[[nodiscard]] bool finished() const noexcept;
[[nodiscard]] bool cancelled() const noexcept;
[[nodiscard]] bool chunked() const noexcept;
[[nodiscard]] std::optional<std::uint64_t>
content_length() const noexcept;
[[nodiscard]] std::uint64_t bytes_accepted() const noexcept;
[[nodiscard]] std::uint64_t bytes_sent() const noexcept;
[[nodiscard]] std::size_t buffered_bytes() const noexcept;
/// Token cancelled by `cancel()`, peer disconnect, server shutdown
/// or the end of the exchange.
[[nodiscard]] std::stop_token stop_token() const noexcept;
void cancel() noexcept;
void set_write_timeout(std::chrono::milliseconds timeout) noexcept;
[[nodiscard]] std::chrono::milliseconds
write_timeout() const noexcept;
/// Send the response head. Chooses fixed length when a
/// `Content-Length` header is supplied, chunked otherwise (HTTP/1.1),
/// and a bodyless frame for HEAD, 1xx, 204 and 304 responses.
[[nodiscard]] Void::Result::Result<void>
begin(std::uint16_t status, std::vector<Header> headers = {});
/// Accept up to `bytes.size()` payload bytes, bounded by the
/// configured write buffer. Returns the accepted count.
[[nodiscard]] Void::Result::Result<std::size_t>
write_some(std::span<const std::byte> bytes);
/// Terminate the framed response. Fails if a fixed-length body was
/// not fully written.
[[nodiscard]] Void::Result::Result<void> finish();
private:
friend struct ResponseWriterAccess;
explicit ResponseWriter(std::shared_ptr<State> state) noexcept
: m_state{std::move(state)}
{
}
std::shared_ptr<State> m_state;
};
/// Handler for a duplex route: the request body can be read while the
/// response is produced. Returning an error before `begin` lets the server
/// emit an error response; after `begin` the stream is terminated without
/// reporting success.
using DuplexRouteHandler = std::function<Void::Result::Result<void>(
const ServerRequest&, BodyReader&, ResponseWriter&)>;
/// Concrete POSIX TCP HTTP/1.1 listener dispatching buffered routes through
/// `Void::Net::Router` and streaming routes via `BodyReader`.
class HttpServer
@@ -90,6 +173,10 @@ namespace Void::Net
add_streaming_route(Method method, std::string pattern,
StreamingRouteHandler handler);
[[nodiscard]] Void::Result::Result<void>
add_duplex_route(Method method, std::string pattern,
DuplexRouteHandler handler);
/// Register a RFC 6455 WebSocket upgrade endpoint backed by
/// `sessions` (caller must keep `sessions` alive until after
/// `shutdown()`).