983ca8992e
* This commit enables the router to forward form-data to model server. Fixes #22044 (enabling to use the /v1/audio/transcriptions in router mode) * * Applied the suggestion from Copilots first comment: using the non-throwing json::parse overload. * Addressed Copilots third comment by extending the files representation to also include filename and content-type * Addressed Copilots fourth comment by making the RNG thread_local * Changed variable body from std::string to std::ostringstream in build_multipart_body as suggested by ngxson in https://github.com/ggml-org/llama.cpp/pull/22118#discussion_r3127099053 * Added sanitize_field lambda in build_multipart_body for key, filename and content_type as suggested by ngxson in https://github.com/ggml-org/llama.cpp/pull/22118#discussion_r3127104647 * explicitly checking if value/item is string before calling value/item.get<std::string>() as requested by ngxson in https://github.com/ggml-org/llama.cpp/pull/22118#discussion_r3127111279 * Added double quote to the sanitize lambda and throw on json parse failure --------- Co-authored-by: Ralph Paßgang <ralph@trust-it.de>
68 lines
2.0 KiB
C++
68 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include "common.h"
|
|
#include "http.h"
|
|
|
|
#include <string>
|
|
#include <unordered_set>
|
|
#include <list>
|
|
#include <map>
|
|
|
|
#include "server-http.h"
|
|
|
|
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:%i%s\n", method.c_str(), parsed_url.scheme.c_str(), parsed_url.host.c_str(), parsed_url.port, parsed_url.path.c_str());
|
|
|
|
std::map<std::string, std::string> headers;
|
|
for (auto [key, value] : req.headers) {
|
|
auto new_key = key;
|
|
if (string_starts_with(new_key, "x-proxy-header-")) {
|
|
string_replace_all(new_key, "x-proxy-header-", "");
|
|
}
|
|
headers[new_key] = value;
|
|
}
|
|
|
|
auto proxy = std::make_unique<server_http_proxy>(
|
|
method,
|
|
parsed_url.scheme,
|
|
parsed_url.host,
|
|
parsed_url.port,
|
|
parsed_url.path,
|
|
headers,
|
|
req.body,
|
|
req.files,
|
|
req.should_stop,
|
|
600, // timeout_read (default to 10 minutes)
|
|
600 // timeout_write (default to 10 minutes)
|
|
);
|
|
|
|
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");
|
|
};
|