|
cofetch
Chainable, high-performance async HTTP client for C++ event loops
|
Every public symbol in cofetch.h on one page, each with a minimal snippet. Everything lives in namespace cofetch. The snippets assume:
Any completion token works everywhere a token appears below — a callback, asio::use_future, asio::use_awaitable (C++20), asio::deferred, … The completion signature is always void(cofetch::error_code, cofetch::Response). For runnable programs see the Examples; for per-parameter detail, the class list in the sidebar.
The one object you create. Owns the curl multi handle and the connection pool, and runs on the io_context you hand it. Not thread-safe: one Client and its io_context per thread, and the client must outlive its in-flight requests.
Construct — borrows the io_context; drive it with run()/poll(). The optional second argument caps how many idle connections stay pooled for reuse (default 64) — raise it for high-concurrency servers.
**async_get / async_post** — ASIO-style shortcuts for the common cases.
**async_perform** — the general form; takes a fully-built Request. async_get/async_post are thin wrappers over it.
**request** — start the fluent builder (see below); the verb fires it.
**pending_requests** — number of transfers in flight.
Returned by http.request(url). Setters return *this; the HTTP verb is terminal — it starts the transfer with your completion token. Don't reuse a builder after the verb.
Setters
Verbs (terminal)
For any other method, set it through the escape hatch:
Value-type description of a request: build one, hand it to async_perform. Same setters as the builder, chainable (Request&).
Request::Method — GET, POST, PUT, PATCH, DEL.
Delivered to the completion handler; public fields plus a few helpers.
headers() parses header_data_ on each call into a case-insensitive Response::Headers map (repeated fields comma-combined, the status line dropped). It is not cached, so keep the result if you read it repeatedly; for a single field, header(name) looks it up without building the whole map.
Transport failures arrive as a cofetch::error_code carrying the CURLcode under cofetch::curl_category(). HTTP error statuses are not transport errors — check Response::is_ok() / http_code_ for those.
cofetch::error_code is std::error_code, or boost::system::error_code when built with Boost.Asio (below).
| macro | effect |
|---|---|
COFETCH_USE_BOOST_ASIO | build against Boost.Asio instead of standalone asio; cofetch::error_code becomes boost::system::error_code so Boost completion tokens recognise it |