C++ · Requests

Requests

#include <ucall/types.hpp>#include <ucall/server.hpp> 1.0.0-rc.1 · b7577426

Ask your coding agent about this page, or add the docs server to it

curl -s 'https://www.unum.cloud/docs/ucall/cpp/requests.md' | claude -p "How would this fit my code?"

A handler receives each request_t by value, owning it. Its reads view the received bytes: verb, path, query_string, path_parameter, query, header and body, with peer naming the interface it arrived on, the client’s authenticated Noise key and its socket address. Every read that can fail answers an expected, so a missing header answers status_code_t::not_found_k and a body read on a streamed route status_code_t::invalid_argument_k, while the counts answer plain numbers. The path and query values stay percent-encoded as sent, which uc::decode_percent decodes, header names match case-insensitively, and the body arrives whole, decoded from chunked framing if it was sent that way. fields_count and field read every header field in arrival order, names lowercased and values trimmed, as a proxy forwards a head. path_segments_count and path_segment read the path component by component, and parts_count and part read a multipart/form-data body as request_part_ts.

void order(uc::request_t request) noexcept {
    auto [token, token_status] = request.header("authorization");
    if (token_status.failed()) {
        [[maybe_unused]] uc::status_t const refused =
            std::move(request).reply_error(ucall_http_status_unauthorized_k, "sign in first");
        return;
    }
    auto [order_id, order_status] = request.path_parameter("order");
    ucall_bytes_view_t const headers[] {uc::bytes_view("content-type: text/plain"),
                                        uc::bytes_view("cache-control: no-store")};
    ucall_reply_part_t const parts[] {uc::bytes_part(order_id), uc::bytes_part(" by "), uc::bytes_part(token)};
    uc::deferred<> sent;
    [[maybe_unused]] uc::status_t const replied =
        std::move(request).reply_parts(ucall_http_status_created_k, headers, parts, sent);
}

reply, reply_error, reply_parts and reply_stream take the request, spelled std::move(request).reply(...), and Content-Length, Date and Server are generated. A reply copies the bytes it sends before it returns, so a body made of the request’s bytes needs no copy of its own. A request moves to any thread, so a handler may hand it to a worker and return, and the worker replies whenever it is done. A request destroyed without a reply answers 500. The library clears the request’s handle once a reply took it, a closed connection included, which reports status_code_t::connection_closed_k; a reply it refused keeps the handle, and destroying the request then answers 500.

API

Types

request_part_tOne part of a multipart/form-data request body, viewing the received bytes.
server.hpp:37 ↗
struct request_part_t

Fields

namestd::string_viewThe name its Content-Disposition gives.
content_typestd::string_viewIts Content-Type, empty when absent.
datastd::string_viewIts payload, undecoded.
peer_tWho sent a request: the interface it arrived on, the client's authenticated static key, zeroed in plaintext, and its socket address.
server.hpp:51 ↗
struct peer_t

Fields

network_interface_indexstd::size_tThe position of the interface among those the server answers on.
static_keyucall_noise_key_tThe client's authenticated Noise static key, zeroed on plaintext interfaces.
addressucall_peer_address_tThe client's socket address as accepted.
string_view >The bytes a call wrote into a counted view, read back as text once the call is final.
types.hpp:290 ↗
template <>
struct string_view >

Fields

valueucall_bytes_view_tThe view the call writes in place.
field_tOne header field line, its name lowercased and its value trimmed.
types.hpp:354 ↗
struct field_t

Fields

namestd::string_viewThe field name, lowercased as the parser keeps it.
valuestd::string_viewThe field value, without surrounding whitespace.
ClassdeferredA submitted call: its outcome and its outputs, owned until the call is final, while the receiver, inputs, arena and handlers it was handed stay allocated and unchanged until then.7
types.hpp:116 ↗
template <typename value_type_ = void>
class deferred

It never moves, as the library writes into it, and its one owner polls status, blocks in wait or awaits wait on its own loop; destruction waits, so nothing the call writes into goes away under it.

answer_typeWhat wait answers: what the blocking call would have.
types.hpp:122 ↗
using answer_type = std::conditional_t<std::is_void_v<value_type_>, status_t, expected<value_type_>>;
deferredAn empty slot, final from the start, which resubmit fills.
types.hpp:125 ↗
deferred() noexcept=default;
template <typename submit_type_>
requires std::invocable<submit_type_ const&, ucall_outcome_t*, outputs<value_type_>*>
deferred(submit_type_ const & submit) noexcept;

Runs submit with this call's outcome and outputs, where the library reports.

deferred(status_t status) noexcept;

A call refused before it crossed, final from the start.

deferred(deferred const &)=delete;
resubmitWaits only while the slot's previous call still runs, then runs submit in its place.
types.hpp:137 ↗
template <typename submit_type_>
requires std::invocable<submit_type_ const&, ucall_outcome_t*, outputs<value_type_>*>
void resubmit(submit_type_ const & submit) noexcept;
operator=
types.hpp:148 ↗
deferred & operator=(deferred const &)=delete;
~deferred
types.hpp:149 ↗
~deferred() noexcept;
statusstatus_code_t::deferred_k until the call is final, then its final status, polled without blocking.
types.hpp:153 ↗
status_code_t status() noexcept;
waitBlocks until the call is final and answers what a blocking call would have.
types.hpp:156 ↗
answer_type wait() noexcept;
template <a_scheduler scheduler_type_>
awaiter<value_type_, scheduler_type_> wait(
    scheduler_type_ & loop) noexcept;

Awaits the call on loop, which polls its status until it is final.

Classrequest_tA received request, owned by one thread at a time, answered at most once.22
server.hpp:141 ↗
class request_t

Reads view the received bytes until the reply. A reply takes the request, as in std::move(request).reply(...), and a request destroyed without one answers 500. A reply the library refused leaves the request retained, answering 500 once destroyed.

request_t
server.hpp:174 ↗
request_t() noexcept=default;
request_t(
    ucall_server_t server,
    ucall_received_http_request_t request) noexcept;
request_t(request_t && other) noexcept;
request_t(request_t const &)=delete;
operator=
server.hpp:178 ↗
request_t & operator=(request_t && other) noexcept;
request_t & operator=(request_t const &)=delete;
~request_t
server.hpp:186 ↗
~request_t() noexcept;
verbThe HTTP method, which tells apart the requests of an endpoint taking any verb.
server.hpp:200 ↗
ucall_verb_t verb() const noexcept;
pathThe path as sent, still percent-encoded, without the query.
server.hpp:203 ↗
expected<std::string_view> path() const noexcept;
query_stringThe query after the first '?
server.hpp:208 ↗
expected<std::string_view> query_string() const noexcept;

', still percent-encoded, or empty without one.

bodyThe body, decoded from chunked framing if it arrived that way; a streamed endpoint refuses it, as body_chunk pulls its body.
server.hpp:214 ↗
expected<std::string_view> body() const noexcept;
path_parameterThe value the endpoint's template captured under name, case-sensitive.
server.hpp:219 ↗
expected<std::string_view> path_parameter(
    std::string_view name) const noexcept;
queryThe value of query parameter name, case-sensitive and still percent-encoded.
server.hpp:224 ↗
expected<std::string_view> query(std::string_view name) const noexcept;
fields_countHeader field lines, repeated names apart.
server.hpp:234 ↗
std::size_t fields_count() const noexcept;
fieldHeader field line index in arrival order.
server.hpp:237 ↗
expected<field_t> field(std::size_t index) const noexcept;
path_segments_countPath components, as sent and still percent-encoded.
server.hpp:253 ↗
std::size_t path_segments_count() const noexcept;
path_segmentPath component index, counting from zero.
server.hpp:258 ↗
expected<std::string_view> path_segment(
    std::size_t index) const noexcept;
parts_countParts of a multipart/form-data body, zero for any other body.
server.hpp:273 ↗
std::size_t parts_count() const noexcept;
partPart index of a multipart/form-data body, its payload undecoded.
server.hpp:276 ↗
expected<request_part_t> part(std::size_t index) const noexcept;
peerThe interface the request arrived on, the client's key and its address, readable after its connection closed.
server.hpp:294 ↗
expected<peer_t> peer() const noexcept;
body_chunkPulls the body bytes decoded since the last pull into into, empty once the body ended and valid until the next pull or the reply; answers a refusal, or success while the pull runs.
server.hpp:319 ↗
status_t body_chunk(deferred<std::string_view> & into) noexcept;

On a streamed endpoint the pull is final once the bytes arrive, which the polling thread delivers; a pull from the polling thread itself finding nothing answers status_code_t::would_block_k instead. On a buffered one the first pull is the whole body. The slot waits for its previous call first, if that still runs.

replyReplies with http_status, full header lines like "content-type: text/plain", and body, generating the length, date and server lines.
server.hpp:333 ↗
status_t reply(
    ucall_http_status_t http_status,
    std::span<ucall_bytes_view_t const> headers,
    std::string_view body) &&noexcept;
reply_errorReplies with an error http_status, 400 or above, and a plain-text message.
server.hpp:348 ↗
status_t reply_error(
    ucall_http_status_t http_status,
    std::string_view message) &&noexcept;
reply_partsReplies with http_status, header lines and parts concatenated into the body, its length measured from the files.
server.hpp:369 ↗
status_t reply_parts(
    ucall_http_status_t http_status,
    std::span<ucall_bytes_view_t const> headers,
    std::span<ucall_reply_part_t const> parts,
    deferred<> & into) &&noexcept;

Final on return for bytes alone; with a file or path part the caller's slot into stays deferred until every part is sent, which the polling thread does, so await it there or wait elsewhere. Answers a refusal, or success while the parts send. A part failing before the head left replaces the reply with 404, 416 or 500, reported as a status. The slot waits for its previous call first, if that still runs.

reply_streamSends the status line and header lines and opens a body appended part by part: chunked, or of the length a Content-Length line declares; a closed connection ends the reply here.
server.hpp:388 ↗
expected<reply_stream_t> reply_stream(
    ucall_http_status_t http_status,
    std::span<ucall_bytes_view_t const> headers) &&noexcept;

Functions

bytes_partA reply part of bytes in memory, copied before the reply returns.
server.hpp:64 ↗
ucall_reply_part_t bytes_part(std::string_view bytes) noexcept;
bytes_viewViews text as the counted bytes every verb reads, without a copy.
types.hpp:279 ↗
ucall_bytes_view_t bytes_view(std::string_view text) noexcept;
decode_percentDecodes the percent escapes of encoded into decoded, which may alias it, and a '+' as a space in a query, answering the decoded prefix of decoded.
types.hpp:341 ↗
expected<std::string_view> decode_percent(
    std::string_view encoded,
    std::span<char> decoded,
    ucall_percent_mode_t mode) noexcept;

Constants

is_deferredWhether answer_type_ is a call the caller holds rather than a final answer.
types.hpp:197 ↗
bool is_deferred;