Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
Proxy.hpp
Go to the documentation of this file.
1#pragma once
4#include <string>
5#include <string_view>
6#include <utility>
7#include <vector>
8
9namespace orbit::middleware {
10
16 std::string target_host;
18 std::string strip_prefix = "";
19 bool use_tls = false;
20 bool verify_tls = true;
21 std::string ca_file;
23};
24
33
42routing::Middleware proxy(const std::string& target_host, int target_port);
43
48struct TargetNode {
49 std::string host;
50 int port;
51 bool use_tls = false;
52};
53
59 std::vector<TargetNode> nodes;
60 std::string strip_prefix = "";
61 bool verify_tls = true;
62 std::string ca_file;
64};
65
76
84routing::Middleware load_balancer(const std::vector<TargetNode>& nodes);
85
86namespace detail {
87
94 std::string method;
95 std::string target;
96 std::vector<std::pair<std::string, std::string>> headers;
97 std::string body;
98 std::string client_ip;
99};
100
102
109std::string build_upstream_request(const ProxiedRequest& request, const std::string& host, int port,
110 const std::string& strip_prefix, bool trust_forwarded_headers);
111
116public:
117 enum class Result { NeedMore, Done, Error };
118
120 Result feed(std::string_view data, std::string& out);
121
122private:
123 enum class State { Size, Data, DataEnd, Trailer, Finished };
125 std::string line_;
126 size_t remaining_{0};
127};
128
129} // namespace detail
130
131} // namespace middleware
Incremental decoder for a chunked upstream response body.
Definition Proxy.hpp:115
State state_
Definition Proxy.hpp:124
size_t remaining_
Definition Proxy.hpp:126
Result feed(std::string_view data, std::string &out)
Decodes as much of data as possible, appending body bytes to out.
std::string line_
Definition Proxy.hpp:125
routing::Middleware load_balancer(LoadBalancerOptions options)
Returns a middleware that load-balances requests across multiple upstream nodes.
routing::Middleware proxy(ProxyOptions options)
Returns a middleware that proxies requests to a single target URL (ip/hostname and port).
ProxiedRequest snapshot_request(const http::HttpRequest &request)
std::string build_upstream_request(const ProxiedRequest &request, const std::string &host, int port, const std::string &strip_prefix, bool trust_forwarded_headers)
Serializes the request sent upstream. Hop-by-hop headers are dropped case-insensitively,...
Definition HttpRequest.hpp:19
std::function< bool(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> Middleware
Definition Router.hpp:19
Represents an incoming HTTP request.
Definition HttpRequest.hpp:54
Options for LoadBalancer middleware.
Definition Proxy.hpp:58
bool verify_tls
Definition Proxy.hpp:61
std::string ca_file
Definition Proxy.hpp:62
std::vector< TargetNode > nodes
Definition Proxy.hpp:59
std::string strip_prefix
Definition Proxy.hpp:60
bool trust_forwarded_headers
Definition Proxy.hpp:63
Options for Proxy middleware.
Definition Proxy.hpp:15
std::string strip_prefix
Definition Proxy.hpp:18
bool trust_forwarded_headers
Keep and extend client-supplied X-Forwarded-* headers.
Definition Proxy.hpp:22
bool use_tls
Connect to the upstream over TLS (also implied by port 443)
Definition Proxy.hpp:19
int target_port
Definition Proxy.hpp:17
std::string ca_file
Optional PEM CA bundle for upstream verification.
Definition Proxy.hpp:21
std::string target_host
Definition Proxy.hpp:16
bool verify_tls
Verify the upstream certificate chain and hostname.
Definition Proxy.hpp:20
A target node for the load balancer.
Definition Proxy.hpp:48
int port
Definition Proxy.hpp:50
std::string host
Definition Proxy.hpp:49
bool use_tls
Connect over TLS (also implied by port 443)
Definition Proxy.hpp:51
An owned copy of the parts of a request the proxy forwards. The original HttpRequest,...
Definition Proxy.hpp:93
std::string body
Definition Proxy.hpp:97
std::string target
Definition Proxy.hpp:95
std::string client_ip
Definition Proxy.hpp:98
std::string method
Definition Proxy.hpp:94
std::vector< std::pair< std::string, std::string > > headers
Definition Proxy.hpp:96