CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 3d agoView on Hugging Face
0likes1.1kdownloads
server-cors-proxy.h84 linesDownload Raw Back to server
1#pragma once2 3#include "common.h"4#include "http.h"5 6#include <string>7#include <unordered_set>8#include <list>9#include <map>10#include <algorithm>11#include <cctype>12 13#include "server-http.h"14 15static std::string proxy_header_to_lower(std::string header) {16    std::transform(header.begin(), header.end(), header.begin(), [](unsigned char c) {17        return std::tolower(c);18    });19    return header;20}21 22static server_http_res_ptr proxy_request(const server_http_req & req, std::string method) {23    std::string target_url = req.get_param("url");24    common_http_url parsed_url = common_http_parse_url(target_url);25 26    if (parsed_url.host.empty()) {27        throw std::runtime_error("invalid target URL: missing host");28    }29 30    if (parsed_url.path.empty()) {31        parsed_url.path = "/";32    }33 34    if (!parsed_url.password.empty()) {35        throw std::runtime_error("authentication in target URL is not supported");36    }37 38    if (parsed_url.scheme != "http" && parsed_url.scheme != "https") {39        throw std::runtime_error("unsupported URL scheme in target URL: " + parsed_url.scheme);40    }41 42    SRV_INF("proxying %s request to %s://%s:%i%s\n", method.c_str(), parsed_url.scheme.c_str(), common_http_format_host(parsed_url.host).c_str(), parsed_url.port, parsed_url.path.c_str());43 44    std::map<std::string, std::string> headers;45    const std::string proxy_header_prefix = "x-llama-server-proxy-header-";46    for (auto [key, value] : req.headers) {47        const std::string lowered_key = proxy_header_to_lower(key);48        if (!string_starts_with(lowered_key, proxy_header_prefix)) {49            continue;50        }51 52        auto new_key = key.substr(proxy_header_prefix.size());53        if (new_key.empty()) {54            continue;55        }56 57        headers[new_key] = value;58    }59 60    auto proxy = std::make_unique<server_http_proxy>(61            method,62            parsed_url.scheme,63            parsed_url.host,64            parsed_url.port,65            parsed_url.path,66            headers,67            req.body,68            req.files,69            req.should_stop,70            600, // timeout_read (default to 10 minutes)71            600  // timeout_write (default to 10 minutes)72            );73 74    return proxy;75}76 77static server_http_context::handler_t proxy_handler_post = [](const server_http_req & req) -> server_http_res_ptr {78    return proxy_request(req, "POST");79};80 81static server_http_context::handler_t proxy_handler_get = [](const server_http_req & req) -> server_http_res_ptr {82    return proxy_request(req, "GET");83};84