HTTP routing¶
Overview¶
make_handler turns an ordinary callable into an http::handler, and router dispatches requests to such
handlers by exact method and path. Both work with the HTTP/1, HTTP/2 and HTTP/3 servers.
| Header | Package config |
|---|---|
<pace/http.hpp> (content in <pace/http/router.hpp>) |
always included (header-only) |
<pace/http1.hpp> includes it too. The router is a small exact-match dispatcher: there are no path parameters,
wildcards, regular expressions, URL decoding, middleware or automatic OPTIONS replies.
Synopsis¶
#include <pace/http.hpp>
#include <pace/http1.hpp>
pace::http::router make_routes()
{
pace::http::router routes;
routes.get("/health", [] { return pace::http::text("ok\n"); });
routes.post(
"/echo",
[](pace::http::request request) -> pace::task<pace::http::response>
{ co_return pace::http::response{.body = std::move(request.body)}; }
);
routes.add("DELETE", "/items", [](pace::http::request) { return pace::http::response{.status = 204}; });
return routes;
}
pace::task<void> serve(pace::tcp::listener& listener)
{
co_await pace::http::serve(listener, make_routes());
}
Types¶
router¶
class router
{
public:
template <class Function>
router& add(std::string method, std::string path, Function&& function);
template <class Function>
router& get(std::string path, Function&& function);
template <class Function>
router& post(std::string path, Function&& function);
template <class Function>
router& mount(std::string prefix, Function&& function);
task<response> operator()(request incoming) const;
};
A router is itself a handler: it converts to http::handler, so it can be passed to any serve function or
wrapped by another handler. A default-constructed router answers every request with 404.
| Member | Effect |
|---|---|
add(method, path, function) |
Registers function for an exact method and path. Returns *this |
get(path, function) |
add("GET", path, function) |
post(path, function) |
add("POST", path, function) |
mount(prefix, function) |
Registers function for every method and for every path under prefix |
operator()(request) |
Dispatches one request and returns the selected handler's task |
function is adapted with make_handler, so it has the same requirements.
Registration rules. method must be a non-empty HTTP token; path must start with / and contain no space,
control character, ? or #. Registering the same method and path twice throws. A mount prefix must start
with / and contain no space, control character, ?, # or \; trailing slashes are removed, so /static/ and
/static are the same prefix, and mounting the same prefix twice throws.
Matching. The request target's query (from the first ?) is ignored for matching, but the handler receives
the target unchanged. Methods and paths compare case-sensitively and byte for byte; percent escapes are not
decoded, so /%68ello does not match /hello. For a request:
- If a route has the request's exact path and method, it runs.
- A
HEADrequest with noHEADroute on that path runs the path'sGETroute. The handler sees methodHEAD; the protocol writes the response headers and drops the body. - If no route has that exact path, the mount with the longest matching prefix runs. A prefix matches whole path
segments:
/apimatches/apiand/api/x, not/apix. Prefix/matches every path. The mounted handler receives the full, unstripped target. - If no route and no mount matches, the reply is 404 with body
Not Found\n. - If routes exist for the path but none for this method, the reply is 405 with body
Method Not Allowed\nand anAllowfield listing the path's methods in registration order, withHEADadded afterGETwhen there is no explicitHEADroute.
An exact path therefore always wins over a mount, even when its method does not match.
Copies. Copying a router is cheap: copies share one route table. The first registration on a copy gives that copy its own table, so routes added to a copy never change the original. The callables themselves stay shared between the copies. Servers copy their handler for every connection; sharing the table keeps that copy free of allocation.
Functions¶
make_handler¶
Adapts function to http::handler. function is called either with the request or with no arguments (the
request form is used when both are possible), and must return exactly response or task<response>; anything
else fails to compile with a static_assert. The adapter takes ownership of the callable, including one with
move-only captures or an existing http::handler. An invocation's task keeps its callable alive from creation
through completion, even if the adapter or router is destroyed. Objects captured by reference must outlive
the server.
The call is lazy: function runs when the returned task is first resumed, and an exception it throws is stored
in the task and rethrown at co_await. A function that returns response runs synchronously on the server's
event loop and must not block.
The adapter's implementation helpers are private to the library.
Errors¶
| Situation | Result |
|---|---|
| Invalid method, path or prefix, duplicate route or duplicate mount | add/get/post/mount throw std::invalid_argument; the router is unchanged |
| Callable with the wrong return type | Compile error |
| Exception from the selected handler | Propagates out of the returned task. A server converts it to its error reply (500 on HTTP/1) |
| No match | 404 or 405 response, not an exception |
Threads and concurrency¶
Register every route before the router is handed to a server. Registration while the same router (or a copy that still shares its table) is serving is not supported. Dispatch itself only reads the table, so copies of one router may dispatch concurrently on different worker threads. The handlers are shared between those threads; any state they hold or capture must be synchronized by the application.
Lifetime and ownership¶
The router owns its routes and the adapted callables, shared between copies. A server that receives a router by value holds its own copy, so the original may be destroyed or changed afterwards without affecting it.
Limits and defaults¶
| Item | Value |
|---|---|
| Path lookup | One binary search over the sorted paths, then a scan of that path's methods |
| Mount lookup | Linear scan of the mounts, only when no exact path exists |
| Default response for an empty router | 404 Not Found\n |
Default methods of get / post |
GET / POST |
Reading and writing a body incrementally¶
Register a callable with signature task<void>(request, body_reader&, body_writer&) through the same
add, get, post and mount methods. Pass the router or make_handler(callable) to the server. The
handler starts after the request head arrives. The adapter keeps the callable and request head alive through
completion, including when the callable takes const request& or request&&. The head has an empty body.
Await reader.read() for an owned chunk. An empty string marks the end and makes reader.trailers available.
Call co_await writer.start(response_head) once, optionally passing the exact body length as a second argument.
Supply an empty body and trailer map in the head. Await each writer.write(bytes) while its bytes are valid.
The handler may alternate reads and writes. Returning completes the response. Each endpoint supports one
outstanding operation and lives only for this invocation.
Without a declared length, HTTP/1.1 uses chunked framing and HTTP/1.0 closes the connection. HTTP/2 and HTTP/3 use DATA frames and enforce the configured per-stream body limit across all chunks. The transports apply backpressure to both directions. HTTP/1 uses a bounded drain when the handler completes a response with unread request bytes. HTTP/2 and HTTP/3 send 500 if a handler fails before the response head and reset just that stream after the head has been sent. See the HTTP/1 server options for deadlines.
See also¶
- HTTP messages and URL clients
- HTTP/1 application:
http::appregisters routes on an internal router - Gateway services:
static_filesandreverse_proxyare mounted withmount - Tutorial: HTTP server