173 lines
4.8 KiB
C++
173 lines
4.8 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Net/Http.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::Net
|
|
{
|
|
struct HttpCodecLimits {
|
|
std::size_t max_start_line_bytes{8ULL * 1024};
|
|
std::size_t max_header_bytes{32ULL * 1024};
|
|
std::size_t max_headers{100};
|
|
std::uint64_t max_body_bytes{8ULL * 1024 * 1024};
|
|
std::size_t max_chunk_line_bytes{1024};
|
|
std::size_t max_trailer_bytes{8ULL * 1024};
|
|
std::size_t max_informational_responses{8};
|
|
};
|
|
|
|
enum class HttpParseStatus : std::uint8_t {
|
|
NeedMoreData,
|
|
Complete,
|
|
};
|
|
|
|
struct ParsedHttpRequest {
|
|
Method method{Method::Get};
|
|
std::string target{};
|
|
std::uint8_t version_major{1};
|
|
std::uint8_t version_minor{1};
|
|
std::vector<Header> headers{};
|
|
std::string body{};
|
|
std::vector<Header> trailers{};
|
|
bool keep_alive{true};
|
|
bool chunked{false};
|
|
|
|
[[nodiscard]] std::optional<std::string_view>
|
|
header(std::string_view name) const noexcept;
|
|
};
|
|
|
|
struct ParsedHttpResponse {
|
|
std::uint16_t status{200};
|
|
std::string reason{};
|
|
std::uint8_t version_major{1};
|
|
std::uint8_t version_minor{1};
|
|
std::vector<Header> headers{};
|
|
std::string body{};
|
|
std::vector<Header> trailers{};
|
|
std::vector<std::uint16_t> informational_statuses{};
|
|
bool keep_alive{true};
|
|
bool chunked{false};
|
|
bool close_delimited{false};
|
|
|
|
[[nodiscard]] std::optional<std::string_view>
|
|
header(std::string_view name) const noexcept;
|
|
};
|
|
|
|
/// Native incremental RFC 9110/9112 HTTP/1.1 request parser.
|
|
/// Enforces strict CRLF framing, header/body limits, and rejects
|
|
/// Transfer-Encoding + Content-Length smuggling ambiguity and conflicting
|
|
/// Content-Length values.
|
|
class HttpRequestParser
|
|
{
|
|
public:
|
|
|
|
explicit HttpRequestParser(HttpCodecLimits limits = {}) noexcept;
|
|
|
|
void reset() noexcept;
|
|
|
|
/// Feed arbitrary byte chunks. Returns Complete once a full message
|
|
/// has been parsed; `consumed` reports bytes consumed from `input`.
|
|
[[nodiscard]] Void::Result::Result<HttpParseStatus>
|
|
feed(std::span<const std::byte> input, std::size_t& consumed);
|
|
|
|
[[nodiscard]] Void::Result::Result<HttpParseStatus>
|
|
feed(std::string_view input, std::size_t& consumed);
|
|
|
|
[[nodiscard]] bool complete() const noexcept;
|
|
[[nodiscard]] bool connection_reusable() const noexcept;
|
|
[[nodiscard]] const ParsedHttpRequest& request() const noexcept;
|
|
[[nodiscard]] ParsedHttpRequest take_request();
|
|
|
|
private:
|
|
|
|
enum class Phase : std::uint8_t {
|
|
Headers,
|
|
FixedBody,
|
|
ChunkSize,
|
|
ChunkData,
|
|
ChunkDataCrlf,
|
|
Trailers,
|
|
Complete,
|
|
Failed,
|
|
};
|
|
|
|
HttpCodecLimits m_limits{};
|
|
Phase m_phase{Phase::Headers};
|
|
std::string m_buf{};
|
|
std::size_t m_header_bytes_seen{0};
|
|
std::size_t m_trailer_bytes_seen{0};
|
|
std::uint64_t m_remaining{0};
|
|
std::uint64_t m_body_received{0};
|
|
ParsedHttpRequest m_req{};
|
|
|
|
[[nodiscard]] Void::Result::Result<void> parse_header_block(
|
|
std::string_view block);
|
|
};
|
|
|
|
/// Native incremental RFC 9110/9112 HTTP/1.1 response parser.
|
|
/// Supports 1xx informational responses, HEAD/204/304 no-body responses,
|
|
/// fixed Content-Length, chunked transfer encoding with extensions and
|
|
/// trailers, and close-delimited bodies (completed via `finish_eof()`).
|
|
class HttpResponseParser
|
|
{
|
|
public:
|
|
|
|
explicit HttpResponseParser(
|
|
HttpCodecLimits limits = {},
|
|
Method request_method = Method::Get) noexcept;
|
|
|
|
void reset(Method request_method = Method::Get) noexcept;
|
|
|
|
[[nodiscard]] Void::Result::Result<HttpParseStatus>
|
|
feed(std::span<const std::byte> input, std::size_t& consumed);
|
|
|
|
[[nodiscard]] Void::Result::Result<HttpParseStatus>
|
|
feed(std::string_view input, std::size_t& consumed);
|
|
|
|
/// Signal peer EOF. Completes close-delimited responses cleanly;
|
|
/// errors on truncated fixed-length, chunked, or header frames.
|
|
[[nodiscard]] Void::Result::Result<void> finish_eof();
|
|
|
|
[[nodiscard]] bool complete() const noexcept;
|
|
[[nodiscard]] bool connection_reusable() const noexcept;
|
|
[[nodiscard]] const ParsedHttpResponse& response() const noexcept;
|
|
[[nodiscard]] ParsedHttpResponse take_response();
|
|
|
|
private:
|
|
|
|
enum class Phase : std::uint8_t {
|
|
Headers,
|
|
FixedBody,
|
|
ChunkSize,
|
|
ChunkData,
|
|
ChunkDataCrlf,
|
|
Trailers,
|
|
CloseDelimitedBody,
|
|
Complete,
|
|
Failed,
|
|
};
|
|
|
|
HttpCodecLimits m_limits{};
|
|
Method m_request_method{Method::Get};
|
|
Phase m_phase{Phase::Headers};
|
|
std::string m_buf{};
|
|
std::size_t m_header_bytes_seen{0};
|
|
std::size_t m_trailer_bytes_seen{0};
|
|
std::uint64_t m_remaining{0};
|
|
std::uint64_t m_body_received{0};
|
|
ParsedHttpResponse m_resp{};
|
|
|
|
[[nodiscard]] Void::Result::Result<bool> parse_header_block(
|
|
std::string_view block);
|
|
};
|
|
|
|
} // namespace Void::Net
|