demo cors proxy

This commit is contained in:
Xuan Son Nguyen 2026-01-06 18:52:56 +01:00
parent 090b137e56
commit dd67c79a26
3 changed files with 57 additions and 3 deletions

View File

@ -1004,6 +1004,7 @@ server_http_proxy::server_http_proxy(
auto pipe = std::make_shared<pipe_t<msg_t>>();
// setup Client
cli->set_follow_location(true);
cli->set_connection_timeout(0, 200000); // 200 milliseconds
cli->set_write_timeout(timeout_read, 0); // reversed for cli (client) vs srv (server)
cli->set_read_timeout(timeout_write, 0);
@ -1053,7 +1054,11 @@ server_http_proxy::server_http_proxy(
req.method = method;
req.path = path;
for (const auto & [key, value] : headers) {
req.set_header(key, value);
if (key == "Host" || key == "host") {
req.set_header(key, host + ":" + std::to_string(port));
} else {
req.set_header(key, value);
}
}
req.body = body;
req.response_handler = response_handler;

View File

@ -2,6 +2,7 @@
#include "common.h"
#include "preset.h"
#include "http.h"
#include "server-common.h"
#include "server-http.h"
@ -184,8 +185,8 @@ public:
const std::map<std::string, std::string> & headers,
const std::string & body,
const std::function<bool()> should_stop,
int32_t timeout_read,
int32_t timeout_write
int32_t timeout_read = 600,
int32_t timeout_write = 600
);
~server_http_proxy() {
if (cleanup) {
@ -201,3 +202,48 @@ private:
std::string content_type;
};
};
// BELOW IS DEMO CODE FOR PROXY HANDLERS
// DO NOT MERGE IT AS-IS
static server_http_res_ptr proxy_request(const server_http_req & req, std::string method) {
std::string target_url = req.get_param("url");
common_http_url parsed_url = common_http_parse_url(target_url);
if (parsed_url.host.empty()) {
throw std::runtime_error("invalid target URL: missing host");
}
if (parsed_url.path.empty()) {
parsed_url.path = "/";
}
if (!parsed_url.password.empty()) {
throw std::runtime_error("authentication in target URL is not supported");
}
if (parsed_url.scheme != "http" && parsed_url.scheme != "https") {
throw std::runtime_error("unsupported URL scheme in target URL: " + parsed_url.scheme);
}
SRV_INF("proxying %s request to %s://%s%s\n", method.c_str(), parsed_url.scheme.c_str(), parsed_url.host.c_str(), parsed_url.path.c_str());
auto proxy = std::make_unique<server_http_proxy>(
method,
parsed_url.host,
parsed_url.scheme == "http" ? 80 : 443,
parsed_url.path,
req.headers,
req.body,
req.should_stop);
return proxy;
}
static server_http_context::handler_t proxy_handler_post = [](const server_http_req & req) -> server_http_res_ptr {
return proxy_request(req, "POST");
};
static server_http_context::handler_t proxy_handler_get = [](const server_http_req & req) -> server_http_res_ptr {
return proxy_request(req, "GET");
};

View File

@ -195,6 +195,9 @@ int main(int argc, char ** argv) {
// Save & load slots
ctx_http.get ("/slots", ex_wrapper(routes.get_slots));
ctx_http.post("/slots/:id_slot", ex_wrapper(routes.post_slots));
// CORS proxy
ctx_http.get ("/cors-proxy", ex_wrapper(proxy_handler_get));
ctx_http.post("/cors-proxy", ex_wrapper(proxy_handler_post));
//
// Start the server