nxt::io::http namespace

Classes

template <typename Reader>
class http_body_reader
struct protocol_error
Error raised for HTTP protocol parsing and transfer-framing failures.
struct response_start
Response head returned after sending a request.
struct url
Parsed URL pieces supported by the small HTTP client.

Functions

auto as_text(std::span<const std::byte> bytes) →  std::string_view
Reinterpret bytes as text.
auto has_header_token(const nxt::http::response_head& response, std::string_view name, std::string_view token) →  bool
True when a comma-separated response header contains token.
auto header_value(const nxt::http::response_head& response, std::string_view name) →  std::optional<std::string_view>
Return the first matching response header value, case-insensitively.
auto is_default_port(const url& url) →  bool
True when the URL uses its scheme's default port.
auto parse_chunk_size(std::span<const std::byte> line) →  std::size_t
Parse a hexadecimal chunk-size line.
auto parse_port(std::string_view text) →  std::uint16_t
Parse a decimal TCP port.
auto parse_response_head(std::span<const std::byte> bytes) →  nxt::http::response_head
Parse bytes up to an HTTP response-head boundary.
template <typename Reader>
auto parse_sse_event(Reader& reader) →  nxt::task<std::optional<nxt::http::server_sent_event>>
auto parse_url(std::string_view text) →  url
Parse an HTTP or HTTPS URL into connection and request-target pieces.
template <typename Reader, typename OnChunk>
auto read_chunked(Reader& reader, OnChunk on_chunk) →  nxt::task
Read a chunked transfer-encoded body.
template <typename Reader, typename OnChunk>
auto read_content_length(Reader& reader, std::size_t content_length, OnChunk on_chunk) →  nxt::task
Read exactly content_length bytes and pass chunks to on_chunk.
template <typename Reader>
auto read_expected_crlf(Reader& reader) →  nxt::task
Read and validate an expected CRLF after chunk data.
template <typename Reader, typename OnChunk>
auto read_response_body(Reader& reader, const response_start& response, OnChunk on_chunk) →  nxt::task
Read the response body as chunks.
template <typename Reader, typename OnChunk>
auto read_response_body_chunks(Reader& reader, const nxt::http::response_head& response, OnChunk on_chunk) →  nxt::task
Dispatch to the body reader required by the response headers.
template <typename Reader>
auto read_response_text(Reader& reader, const response_start& response) →  nxt::task<std::string>
Read the full response body into a string.
template <typename Reader, typename OnChunk>
auto read_until_eof(Reader& reader, OnChunk on_chunk) →  nxt::task
Read body bytes until the connection ends.
auto response_content_length(const nxt::http::response_head& response) →  std::optional<std::size_t>
Parsed Content-Length header, when present.
auto response_content_type_is(const nxt::http::response_head& response, std::string_view expected) →  bool
True when the response Content-Type matches the expected media type.
auto response_is_chunked(const nxt::http::response_head& response) →  bool
True when the response uses chunked transfer encoding.
auto response_status_is_success(const nxt::http::response_head& response) →  bool
True for 2xx response status codes.
auto response_status_text(const nxt::http::response_head& response) →  std::string
Human-readable response status line.
template <typename Transport, typename Reader>
auto send_request(Transport& transport, Reader& reader, const nxt::http::request& request) →  nxt::task<response_start>
Write a request and parse the response head.

Function documentation

std::string_view nxt::io::http::as_text(std::span<const std::byte> bytes)

Reinterpret bytes as text.

bool nxt::io::http::has_header_token(const nxt::http::response_head& response, std::string_view name, std::string_view token)

True when a comma-separated response header contains token.

std::optional<std::string_view> nxt::io::http::header_value(const nxt::http::response_head& response, std::string_view name)

Return the first matching response header value, case-insensitively.

bool nxt::io::http::is_default_port(const url& url)

True when the URL uses its scheme's default port.

std::size_t nxt::io::http::parse_chunk_size(std::span<const std::byte> line)

Parse a hexadecimal chunk-size line.

std::uint16_t nxt::io::http::parse_port(std::string_view text)

Parse a decimal TCP port.

nxt::http::response_head nxt::io::http::parse_response_head(std::span<const std::byte> bytes)

Parse bytes up to an HTTP response-head boundary.

template <typename Reader>
nxt::task<std::optional<nxt::http::server_sent_event>> nxt::io::http::parse_sse_event(Reader& reader)

Pull one server-sent event from a buffered byte stream.

Returns std::nullopt on clean end-of-stream. Reader is expected to be a byte_reader-shaped source with take_until("\n").

url nxt::io::http::parse_url(std::string_view text)

Parse an HTTP or HTTPS URL into connection and request-target pieces.

template <typename Reader, typename OnChunk>
nxt::task nxt::io::http::read_chunked(Reader& reader, OnChunk on_chunk)

Read a chunked transfer-encoded body.

template <typename Reader, typename OnChunk>
nxt::task nxt::io::http::read_content_length(Reader& reader, std::size_t content_length, OnChunk on_chunk)

Read exactly content_length bytes and pass chunks to on_chunk.

template <typename Reader>
nxt::task nxt::io::http::read_expected_crlf(Reader& reader)

Read and validate an expected CRLF after chunk data.

template <typename Reader, typename OnChunk>
nxt::task nxt::io::http::read_response_body(Reader& reader, const response_start& response, OnChunk on_chunk)

Read the response body as chunks.

template <typename Reader, typename OnChunk>
nxt::task nxt::io::http::read_response_body_chunks(Reader& reader, const nxt::http::response_head& response, OnChunk on_chunk)

Dispatch to the body reader required by the response headers.

template <typename Reader>
nxt::task<std::string> nxt::io::http::read_response_text(Reader& reader, const response_start& response)

Read the full response body into a string.

template <typename Reader, typename OnChunk>
nxt::task nxt::io::http::read_until_eof(Reader& reader, OnChunk on_chunk)

Read body bytes until the connection ends.

std::optional<std::size_t> nxt::io::http::response_content_length(const nxt::http::response_head& response)

Parsed Content-Length header, when present.

bool nxt::io::http::response_content_type_is(const nxt::http::response_head& response, std::string_view expected)

True when the response Content-Type matches the expected media type.

bool nxt::io::http::response_is_chunked(const nxt::http::response_head& response)

True when the response uses chunked transfer encoding.

bool nxt::io::http::response_status_is_success(const nxt::http::response_head& response)

True for 2xx response status codes.

std::string nxt::io::http::response_status_text(const nxt::http::response_head& response)

Human-readable response status line.

template <typename Transport, typename Reader>
nxt::task<response_start> nxt::io::http::send_request(Transport& transport, Reader& reader, const nxt::http::request& request)

Write a request and parse the response head.