Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
Client.hpp
Go to the documentation of this file.
1#pragma once
3#include <chrono>
4#include <coroutine>
5#include <functional>
6#include <memory>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <utility>
11#include <vector>
12
13namespace orbit::http {
14
17 std::string method = "GET";
18 std::string url;
19 std::vector<std::pair<std::string, std::string>> headers;
20 std::string body;
21
22 std::chrono::milliseconds timeout{30000};
23 std::chrono::milliseconds connect_timeout{10000};
24 bool follow_redirects = true;
25 long max_redirects = 5;
26 bool verify_tls = true;
27 std::string ca_file;
28 size_t max_response_size = 10 * 1024 * 1024;
29};
30
33 long status = 0;
34 std::vector<std::pair<std::string, std::string>> headers;
35 std::string body;
36 std::string effective_url;
37 std::string error;
38
40 bool ok() const { return error.empty(); }
42 std::optional<std::string> header(std::string_view name) const;
43};
44
46 std::string user_agent = "Orbit-Framework";
47};
48
49class Client;
50
56
57 bool await_ready() const noexcept { return false; }
58 void await_suspend(std::coroutine_handle<> h);
59 ClientResponse await_resume() { return std::move(response); }
60};
61
81class Client {
82public:
83 explicit Client(ClientOptions options = {});
86
87 Client(const Client&) = delete;
88 Client& operator=(const Client&) = delete;
89
91 void send(ClientRequest request, std::function<void(ClientResponse)> on_done);
92
94 ClientSendAwaiter send_async(ClientRequest request) { return ClientSendAwaiter{this, std::move(request), {}}; }
95
104
106 static Client& shared();
107
108private:
109 struct Impl;
110 std::unique_ptr<Impl> impl_;
111};
112
113inline void ClientSendAwaiter::await_suspend(std::coroutine_handle<> h) {
114 client->send(std::move(request), [this, h](ClientResponse r) {
115 response = std::move(r);
116 h.resume();
117 });
118}
119
120} // namespace http
Asynchronous HTTP/1.1 and HTTP/2 client (libcurl's multi interface).
Definition Client.hpp:81
static Client & shared()
A process-wide client, created on first use.
void send(ClientRequest request, std::function< void(ClientResponse)> on_done)
Starts request; on_done is called once, on the client's thread.
Client(ClientOptions options={})
~Client()
Requests still in flight complete with error "client shut down".
Client & operator=(const Client &)=delete
ClientResponse send_sync(ClientRequest request)
Sends request and waits for the response, blocking the calling thread (for code that cannot be asynch...
Client(const Client &)=delete
std::unique_ptr< Impl > impl_
Definition Client.hpp:110
ClientSendAwaiter send_async(ClientRequest request)
Awaitable form of send(); the coroutine resumes on the client's thread.
Definition Client.hpp:94
Definition Task.hpp:10
Definition Client.hpp:45
std::string user_agent
Definition Client.hpp:46
An outbound HTTP request for Client.
Definition Client.hpp:16
long max_redirects
Definition Client.hpp:25
size_t max_response_size
Larger bodies fail the request.
Definition Client.hpp:28
std::chrono::milliseconds connect_timeout
Definition Client.hpp:23
std::string ca_file
PEM bundle; empty = system store.
Definition Client.hpp:27
std::string url
http:// or https:// only
Definition Client.hpp:18
std::string method
Definition Client.hpp:17
std::string body
Definition Client.hpp:20
bool verify_tls
Certificate chain and host name.
Definition Client.hpp:26
std::vector< std::pair< std::string, std::string > > headers
Definition Client.hpp:19
bool follow_redirects
To http(s) URLs only.
Definition Client.hpp:24
std::chrono::milliseconds timeout
Whole request, redirects included.
Definition Client.hpp:22
The result of a ClientRequest.
Definition Client.hpp:32
std::string effective_url
After redirects.
Definition Client.hpp:36
std::string error
Empty on success.
Definition Client.hpp:37
bool ok() const
True if a response arrived (any status, including 4xx and 5xx).
Definition Client.hpp:40
std::vector< std::pair< std::string, std::string > > headers
Of the final response.
Definition Client.hpp:34
long status
0 if no response arrived
Definition Client.hpp:33
std::string body
Definition Client.hpp:35
std::optional< std::string > header(std::string_view name) const
The last header named name (case-insensitive).
co_await Client::send_async(...): resumes with the ClientResponse.
Definition Client.hpp:52
bool await_ready() const noexcept
Definition Client.hpp:57
void await_suspend(std::coroutine_handle<> h)
Definition Client.hpp:113
ClientRequest request
Definition Client.hpp:54
ClientResponse response
Definition Client.hpp:55
Client * client
Definition Client.hpp:53
ClientResponse await_resume()
Definition Client.hpp:59