From dd67c79a26e3cfd811ecedbf7fdf2af3421c77ef Mon Sep 17 00:00:00 2001 From: Xuan Son Nguyen Date: Tue, 6 Jan 2026 18:52:56 +0100 Subject: [PATCH] demo cors proxy --- tools/server/server-models.cpp | 7 ++++- tools/server/server-models.h | 50 ++++++++++++++++++++++++++++++++-- tools/server/server.cpp | 3 ++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/tools/server/server-models.cpp b/tools/server/server-models.cpp index 803cb02e6e..ab495cdb46 100644 --- a/tools/server/server-models.cpp +++ b/tools/server/server-models.cpp @@ -1004,6 +1004,7 @@ server_http_proxy::server_http_proxy( auto pipe = std::make_shared>(); // 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; diff --git a/tools/server/server-models.h b/tools/server/server-models.h index a397abda4a..7ffd6bef5c 100644 --- a/tools/server/server-models.h +++ b/tools/server/server-models.h @@ -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 & headers, const std::string & body, const std::function 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( + 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"); +}; diff --git a/tools/server/server.cpp b/tools/server/server.cpp index 1d9abf6055..e8f16d5b23 100644 --- a/tools/server/server.cpp +++ b/tools/server/server.cpp @@ -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