CoolFace
Datasetpublic

echodict/llama.cpp

version https://git-lfs.github.com/spec/v1 oid sha256:cfc44b7ba25614df70e6b65e3341cae0310163bd32fd31a6b928a542df433faf size 30786

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes765downloads
server-cors-proxy.h67 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 11#include "server-http.h"12 13static server_http_res_ptr proxy_request(const server_http_req & req, std::string method) {14    std::string target_url = req.get_param("url");15    common_http_url parsed_url = common_http_parse_url(target_url);16 17    if (parsed_url.host.empty()) {18        throw std::runtime_error("invalid target URL: missing host");19    }20 21    if (parsed_url.path.empty()) {22        parsed_url.path = "/";23    }24 25    if (!parsed_url.password.empty()) {26        throw std::runtime_error("authentication in target URL is not supported");27    }28 29    if (parsed_url.scheme != "http" && parsed_url.scheme != "https") {30        throw std::runtime_error("unsupported URL scheme in target URL: " + parsed_url.scheme);31    }32 33    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());34 35    std::map<std::string, std::string> headers;36    for (auto [key, value] : req.headers) {37        auto new_key = key;38        if (string_starts_with(new_key, "x-proxy-header-")) {39            string_replace_all(new_key, "x-proxy-header-", "");40        }41        headers[new_key] = value;42    }43 44    auto proxy = std::make_unique<server_http_proxy>(45            method,46            parsed_url.scheme,47            parsed_url.host,48            parsed_url.port,49            parsed_url.path,50            headers,51            req.body,52            req.should_stop,53            600, // timeout_read (default to 10 minutes)54            600  // timeout_write (default to 10 minutes)55            );56 57    return proxy;58}59 60static server_http_context::handler_t proxy_handler_post = [](const server_http_req & req) -> server_http_res_ptr {61    return proxy_request(req, "POST");62};63 64static server_http_context::handler_t proxy_handler_get = [](const server_http_req & req) -> server_http_res_ptr {65    return proxy_request(req, "GET");66};67