char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
799fcc04a
char/aliceai-t5-moe
1#pragma once 2 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 ) { 16std ::transform (header .begin (),header .end (),header .begin (), [](unsignedchar c ) { 17return std ::tolower (c ); 18 }); 19return header ; 20} 21 22static server_http_res_ptr proxy_request (const server_http_req & req ,std ::string method ) { 23std ::string target_url = req .get_param ("url" ); 24common_http_url parsed_url = common_http_parse_url (target_url ); 25 26if (parsed_url .host .empty ()) { 27throw std ::runtime_error ("invalid target URL: missing host" ); 28 } 29 30if (parsed_url .path .empty ()) { 31parsed_url .path = "/" ; 32 } 33 34if (!parsed_url .password .empty ()) { 35throw std ::runtime_error ("authentication in target URL is not supported" ); 36 } 37 38if (parsed_url .scheme != "http" && parsed_url .scheme != "https" ) { 39throw std ::runtime_error ("unsupported URL scheme in target URL: " + parsed_url .scheme ); 40 } 41 42SRV_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 44std ::map < std ::string ,std ::string > headers ; 45const std ::string proxy_header_prefix = "x-llama-server-proxy-header-" ; 46for (auto [key ,value ] :req .headers ) { 47const std ::string lowered_key = proxy_header_to_lower (key ); 48if (!string_starts_with (lowered_key ,proxy_header_prefix )) { 49continue ; 50 } 51 52 autonew_key = key .substr (proxy_header_prefix .size ()); 53if (new_key .empty ()) { 54continue ; 55 } 56 57headers [new_key ]= value ; 58 } 59 60 autoproxy = std ::make_unique < server_http_proxy > ( 61method , 62parsed_url .scheme , 63parsed_url .host , 64parsed_url .port , 65parsed_url .path , 66headers , 67req .body , 68req .files , 69req .should_stop , 70600 ,// timeout_read (default to 10 minutes) 71600 // timeout_write (default to 10 minutes) 72 ); 73 74return proxy ; 75} 76 77static server_http_context ::handler_t proxy_handler_post = [](const server_http_req & req) -> server_http_res_ptr { 78return proxy_request(req, "POST" ); 79}; 80 81static server_http_context ::handler_t proxy_handler_get = []( const server_http_req & req) -> server_http_res_ptr { 82return proxy_request (req, "GET" ); 83};