# Requests

> A handler receives each request_t by value, owning it.

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

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_t` s.

```cpp
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_t`

One part of a `multipart/form-data` request body, viewing the received bytes.

[server.hpp:37 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L37)

```cpp
struct request_part_t
```

#### Fields

| `name` | `std::string_view` | The name its `Content-Disposition` gives. |
| - | - | - |
| `content_type` | `std::string_view` | Its `Content-Type`, empty when absent. |
| `data` | `std::string_view` | Its payload, undecoded. |

#### `peer_t`

Who sent a request: the interface it arrived on, the client's authenticated static key, zeroed in plaintext, and its socket address.

[server.hpp:51 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L51)

```cpp
struct peer_t
```

#### Fields

| `network_interface_index` | `std::size_t` | The position of the interface among those the server answers on. |
| - | - | - |
| `static_key` | `ucall_noise_key_t` | The client's authenticated Noise static key, zeroed on plaintext interfaces. |
| `address` | `ucall_peer_address_t` | The 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 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L290)

```cpp
template <>
struct string_view >
```

#### Fields

| `value` | `ucall_bytes_view_t` | The view the call writes in place. |
| - | - | - |

#### `field_t`

One header field line, its name lowercased and its value trimmed.

[types.hpp:354 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L354)

```cpp
struct field_t
```

#### Fields

| `name` | `std::string_view` | The field name, lowercased as the parser keeps it. |
| - | - | - |
| `value` | `std::string_view` | The field value, without surrounding whitespace. |

#### Class `deferred`

A 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 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L116)

```cpp
template <typename value_type_ = void>
class deferred
```

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

##### `answer_type`

What [`wait`](#wait) answers: what the blocking call would have.

[types.hpp:122 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L122)

```cpp
using answer_type = std::conditional_t<std::is_void_v<value_type_>, status_t, expected<value_type_>>;
```

##### `deferred`

An empty slot, final from the start, which [`resubmit`](#resubmit) fills.

[types.hpp:125 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L125)

```cpp
deferred() noexcept=default;
```

```cpp
template <typename submit_type_>
requires std::invocable<submit_type_ const&, ucall_outcome_t*, outputs<value_type_>*>
deferred(submit_type_ const & submit) noexcept;
```

Runs [`submit`](https://www.unum.cloud/docs/ucall/cpp/calling.md#submit) with this call's outcome and outputs, where the library reports.

```cpp
deferred(status_t status) noexcept;
```

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

```cpp
deferred(deferred const &)=delete;
```

##### `resubmit`

Waits only while the slot's previous call still runs, then runs [`submit`](https://www.unum.cloud/docs/ucall/cpp/calling.md#submit) in its place.

[types.hpp:137 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L137)

```cpp
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 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L148)

```cpp
deferred & operator=(deferred const &)=delete;
```

##### `~deferred`

[types.hpp:149 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L149)

```cpp
~deferred() noexcept;
```

##### `status`

[`status_code_t::deferred_k`](https://www.unum.cloud/docs/ucall/cpp/errors.md#deferred_k) until the call is final, then its final status, polled without blocking.

[types.hpp:153 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L153)

```cpp
status_code_t status() noexcept;
```

##### `wait`

Blocks until the call is final and answers what a blocking call would have.

[types.hpp:156 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L156)

```cpp
answer_type wait() noexcept;
```

```cpp
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.

#### Class `request_t`

A received request, owned by one thread at a time, answered at most once.22

[server.hpp:141 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L141)

```cpp
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 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L174)

```cpp
request_t() noexcept=default;
```

```cpp
request_t(
    ucall_server_t server,
    ucall_received_http_request_t request) noexcept;
```

```cpp
request_t(request_t && other) noexcept;
```

```cpp
request_t(request_t const &)=delete;
```

##### `operator=`

[server.hpp:178 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L178)

```cpp
request_t & operator=(request_t && other) noexcept;
```

```cpp
request_t & operator=(request_t const &)=delete;
```

##### `~request_t`

[server.hpp:186 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L186)

```cpp
~request_t() noexcept;
```

##### `verb`

The HTTP method, which tells apart the requests of an endpoint taking any verb.

[server.hpp:200 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L200)

```cpp
ucall_verb_t verb() const noexcept;
```

##### `path`

The path as sent, still percent-encoded, without the query.

[server.hpp:203 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L203)

```cpp
expected<std::string_view> path() const noexcept;
```

##### `query_string`

The query after the first '?

[server.hpp:208 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L208)

```cpp
expected<std::string_view> query_string() const noexcept;
```

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

##### `body`

The body, decoded from chunked framing if it arrived that way; a streamed endpoint refuses it, as [`body_chunk`](https://www.unum.cloud/docs/ucall/cpp/calling.md#body_chunk) pulls its body.

[server.hpp:214 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L214)

```cpp
expected<std::string_view> body() const noexcept;
```

##### `path_parameter`

The value the endpoint's template captured under `name`, case-sensitive.

[server.hpp:219 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L219)

```cpp
expected<std::string_view> path_parameter(
    std::string_view name) const noexcept;
```

##### `query`

The value of query parameter `name`, case-sensitive and still percent-encoded.

[server.hpp:224 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L224)

```cpp
expected<std::string_view> query(std::string_view name) const noexcept;
```

##### `header`

The value of header `name`, matched case-insensitively.

[server.hpp:229 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L229)

```cpp
expected<std::string_view> header(std::string_view name) const noexcept;
```

##### `fields_count`

Header field lines, repeated names apart.

[server.hpp:234 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L234)

```cpp
std::size_t fields_count() const noexcept;
```

##### `field`

Header field line `index` in arrival order.

[server.hpp:237 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L237)

```cpp
expected<field_t> field(std::size_t index) const noexcept;
```

##### `path_segments_count`

Path components, as sent and still percent-encoded.

[server.hpp:253 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L253)

```cpp
std::size_t path_segments_count() const noexcept;
```

##### `path_segment`

Path component `index`, counting from zero.

[server.hpp:258 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L258)

```cpp
expected<std::string_view> path_segment(
    std::size_t index) const noexcept;
```

##### `parts_count`

Parts of a `multipart/form-data` body, zero for any other body.

[server.hpp:273 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L273)

```cpp
std::size_t parts_count() const noexcept;
```

##### `part`

Part `index` of a `multipart/form-data` body, its payload undecoded.

[server.hpp:276 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L276)

```cpp
expected<request_part_t> part(std::size_t index) const noexcept;
```

##### `peer`

The interface the request arrived on, the client's key and its address, readable after its connection closed.

[server.hpp:294 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L294)

```cpp
expected<peer_t> peer() const noexcept;
```

##### `body_chunk`

Pulls 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 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L319)

```cpp
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`](https://www.unum.cloud/docs/ucall/cpp/errors.md#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.

##### `reply`

Replies with `http_status`, full header lines like "content-type: text/plain", and [`body`](https://www.unum.cloud/docs/ucall/cpp/calling.md#body), generating the length, date and server lines.

[server.hpp:333 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L333)

```cpp
status_t reply(
    ucall_http_status_t http_status,
    std::span<ucall_bytes_view_t const> headers,
    std::string_view body) &&noexcept;
```

##### `reply_error`

Replies with an error `http_status`, 400 or above, and a plain-text `message`.

[server.hpp:348 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L348)

```cpp
status_t reply_error(
    ucall_http_status_t http_status,
    std::string_view message) &&noexcept;
```

##### `reply_parts`

Replies with `http_status`, header lines and `parts` concatenated into the body, its length measured from the files.

[server.hpp:369 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L369)

```cpp
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_stream`

Sends 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 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L388)

```cpp
expected<reply_stream_t> reply_stream(
    ucall_http_status_t http_status,
    std::span<ucall_bytes_view_t const> headers) &&noexcept;
```

### Functions

#### `bytes_part`

A reply part of bytes in memory, copied before the reply returns.

[server.hpp:64 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/server.hpp#L64)

```cpp
ucall_reply_part_t bytes_part(std::string_view bytes) noexcept;
```

#### `bytes_view`

Views `text` as the counted bytes every verb reads, without a copy.

[types.hpp:279 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L279)

```cpp
ucall_bytes_view_t bytes_view(std::string_view text) noexcept;
```

#### `decode_percent`

Decodes 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 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L341)

```cpp
expected<std::string_view> decode_percent(
    std::string_view encoded,
    std::span<char> decoded,
    ucall_percent_mode_t mode) noexcept;
```

### Constants

#### `is_deferred`

Whether `answer_type_` is a call the caller holds rather than a final answer.

[types.hpp:197 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L197)

```cpp
bool is_deferred;
```
