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,
- req.headers,
+ headers,
req.body,
req.should_stop,
600, // timeout_read (default to 10 minutes)
struct server_http_req {
std::map<std::string, std::string> params; // path_params + query_params
- std::map<std::string, std::string> headers; // reserved for future use
+ std::map<std::string, std::string> headers; // used by MCP proxy
std::string path;
std::string query_string; // query parameters string (e.g. "action=save")
std::string body;
MCPResourceContent,
MCPReadResourceResult
} from '$lib/types';
-import { buildProxiedUrl, throwIfAborted, isAbortError, createBase64DataUrl } from '$lib/utils';
+import {
+ buildProxiedUrl,
+ buildProxiedHeaders,
+ throwIfAborted,
+ isAbortError,
+ createBase64DataUrl
+} from '$lib/utils';
interface ToolResultContentItem {
type: string;
const requestInit: RequestInit = {};
if (config.headers) {
- requestInit.headers = config.headers;
+ requestInit.headers = buildProxiedHeaders(config.headers);
}
if (config.credentials) {
return proxyUrl;
}
+/**
+ * Wrap original headers for proxying through the CORS proxy. This avoids issues with duplicated llama.cpp-specific and target headers when using the CORS proxy.
+ * @param headers - The original headers to be proxied to target
+ * @returns List of "wrapped" headers to be sent to the CORS proxy
+ */
+export function buildProxiedHeaders(headers: Record<string, string>): Record<string, string> {
+ const proxiedHeaders: Record<string, string> = {};
+
+ for (const [key, value] of Object.entries(headers)) {
+ proxiedHeaders[`X-Proxy-Header-${key}`] = value;
+ }
+
+ return proxiedHeaders;
+}
+
/**
* Get a proxied URL string for use in fetch requests.
* @param targetUrl - The original URL to proxy
export { setConfigValue, getConfigValue, configToParameterRecord } from './config-helpers';
// CORS Proxy
-export { buildProxiedUrl, getProxiedUrlString } from './cors-proxy';
+export { buildProxiedUrl, getProxiedUrlString, buildProxiedHeaders } from './cors-proxy';
// Conversation utilities
export { createMessageCountMap, getMessageCount } from './conversation-utils';