char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
c264f65ff
char/aliceai-t5-moe
1#pragma once 2 3#include <cpp-httplib/httplib.h> 4 5#ifdef _WIN32 6#include <winsock2.h> 7#include <windows.h> 8#else 9#include <sys/socket.h> 10#include <netinet/in.h> 11#include <arpa/inet.h> 12#include <unistd.h> 13#endif 14 15struct common_http_url { 16std ::string scheme ; 17std ::string user ; 18std ::string password ; 19std ::string host ; 20int port ; 21std ::string path ; 22}; 23 24// bracket an IPv6 literal host for a URL authority (RFC 3986) 25static std ::string common_http_format_host (const std ::string & host ) { 26return host .find (':' )!= std ::string ::npos ?"[" + host + "]" :host ; 27} 28 29static common_http_url common_http_parse_url (const std ::string & url ) { 30common_http_url parts ; 31 autoscheme_end = url. find ( "://" ); 32 33if (scheme_end == std::string::npos) { 34throw std::runtime_error( "invalid URL: no scheme" ); 35} 36parts. scheme = url. substr ( 0 , scheme_end); 37 38if (parts. scheme != "http" && parts. scheme != "https" ) { 39throw std::runtime_error( "unsupported URL scheme: " + parts. scheme ); 40} 41 42auto rest = url. substr (scheme_end + 3 ); 43auto at_pos = rest. find ( '@' ); 44 45if (at_pos != std::string::npos) { 46auto auth = rest. substr ( 0 , at_pos); 47auto colon_pos = auth. find ( ':' ); 48if (colon_pos != std::string::npos) { 49parts. user = auth. substr ( 0 , colon_pos); 50parts. password = auth. substr (colon_pos + 1 ); 51} else { 52parts. user = auth; 53} 54rest = rest. substr (at_pos + 1 ); 55} 56 57auto slash_pos = rest. find ( '/' ); 58 59if (slash_pos != std::string::npos) { 60parts. host = rest. substr ( 0 , slash_pos); 61parts. path = rest. substr (slash_pos); 62} else { 63parts. host = rest; 64parts. path = "/" ; 65} 66 67// split the authority into host and optional port, a bracketed IPv6 literal keeps its inner colons (RFC 3986) 68std :: string port_str; 69if (!parts. host . empty () && parts. host . front () == '[' ) { 70auto close = parts. host . find ( ']' ); 71if (close == std::string::npos) { 72throw std::runtime_error( "invalid IPv6 URL authority: " + parts. host ); 73} 74auto after = parts. host . substr (close + 1 ); 75if (!after. empty () && after. front () == ':' ) { 76port_str = after. substr ( 1 ); 77} 78parts. host = parts. host . substr ( 1 , close - 1 ); 79} else { 80auto colon_pos = parts. host . find ( ':' ); 81if (colon_pos != std::string::npos) { 82port_str = parts. host . substr (colon_pos + 1 ); 83parts. host = parts. host . substr ( 0 , colon_pos); 84} 85} 86 87if (!port_str. empty ()) { 88parts. port = std:: stoi (port_str); 89} else if (parts. scheme == "http" ) { 90parts. port = 80 ; 91} else if (parts. scheme == "https" ) { 92parts. port = 443 ; 93} else { 94throw std::runtime_error( "unsupported URL scheme: " + parts. scheme ); 95} 96 97return parts; 98} 99 100static std ::pair < httplib::Client, common_http_url > common_http_client ( const std ::string & url) { 101common_http_url parts = common_http_parse_url (url); 102 103if (parts. host . empty ()) { 104throw std::runtime_error( "error: invalid URL format" ); 105} 106 107#ifndef CPPHTTPLIB_OPENSSL_SUPPORT 108if (parts. scheme == "https" ) { 109throw std::runtime_error( 110"HTTPS is not supported. Please rebuild with one of:\n" 111" -DLLAMA_BUILD_BORINGSSL=ON\n" 112" -DLLAMA_BUILD_LIBRESSL=ON\n" 113" -DLLAMA_OPENSSL=ON (default, requires OpenSSL dev files installed)" 114); 115} 116#endif 117 118httplib:: Client cli(parts. scheme + "://" + common_http_format_host (parts. host ) + ":" + std:: to_string (parts. port )); 119 120if (!parts. user . empty ()) { 121cli. set_basic_auth (parts. user , parts. password ); 122} 123 124cli. set_follow_location (true); 125 126return { std :: move (cli), std::move( parts ) }; 127} 128 129static std:: string common_http_show_masked_url ( const common_http_url & parts) { 130return parts. scheme + "://" + (parts. user . empty () ? "" : "****:****@" ) + common_http_format_host (parts. host ) + parts. path ; 131} 132 133static int common_http_get_free_port () { 134#ifdef _WIN32 135WSADATA wsaData; 136if ( WSAStartup ( MAKEWORD ( 2 , 2 ), & wsaData) != 0 ) { 137return -1 ; 138} 139typedef SOCKET native_socket_t ; 140#define INVALID_SOCKET_VAL INVALID_SOCKET 141#define CLOSE_SOCKET (s) closesocket(s) 142#else 143typedef int native_socket_t ; 144#define INVALID_SOCKET_VAL -1 145#define CLOSE_SOCKET (s) close(s) 146#endif 147 148native_socket_t sock = socket ( AF_INET , SOCK_STREAM , 0 ); 149if (sock == INVALID_SOCKET_VAL ) { 150#ifdef _WIN32 151WSACleanup (); 152#endif 153return -1 ; 154} 155 156struct sockaddr_in serv_addr; 157std :: memset ( & serv_addr, 0 , sizeof (serv_addr)); 158serv_addr. sin_family = AF_INET ; 159serv_addr. sin_addr . s_addr = htonl ( INADDR_ANY ); 160serv_addr. sin_port = htons ( 0 ); 161 162if ( bind (sock, ( struct sockaddr * ) & serv_addr, sizeof (serv_addr)) != 0 ) { 163CLOSE_SOCKET (sock); 164#ifdef _WIN32 165WSACleanup (); 166#endif 167return -1 ; 168} 169 170#ifdef _WIN32 171int namelen = sizeof (serv_addr); 172#else 173socklen_t namelen = sizeof (serv_addr); 174#endif 175if ( getsockname (sock, ( struct sockaddr * ) & serv_addr, & namelen) != 0 ) { 176CLOSE_SOCKET (sock); 177#ifdef _WIN32 178WSACleanup (); 179#endif 180return -1 ; 181} 182 183int port = ntohs (serv_addr. sin_port ); 184 185CLOSE_SOCKET (sock); 186#ifdef _WIN32 187WSACleanup (); 188#endif 189 190return port ; 191}