echodict/llama.cpp
version https://git-lfs.github.com/spec/v1 oid sha256:cfc44b7ba25614df70e6b65e3341cae0310163bd32fd31a6b928a542df433faf size 30786
0479
1#pragma once2 3#include <cpp-httplib/httplib.h>4 5struct common_http_url {6 std::string scheme;7 std::string user;8 std::string password;9 std::string host;10 int port;11 std::string path;12};13 14static common_http_url common_http_parse_url(const std::string & url) {15 common_http_url parts;16 auto scheme_end = url.find("://");17 18 if (scheme_end == std::string::npos) {19 throw std::runtime_error("invalid URL: no scheme");20 }21 parts.scheme = url.substr(0, scheme_end);22 23 if (parts.scheme != "http" && parts.scheme != "https") {24 throw std::runtime_error("unsupported URL scheme: " + parts.scheme);25 }26 27 auto rest = url.substr(scheme_end + 3);28 auto at_pos = rest.find('@');29 30 if (at_pos != std::string::npos) {31 auto auth = rest.substr(0, at_pos);32 auto colon_pos = auth.find(':');33 if (colon_pos != std::string::npos) {34 parts.user = auth.substr(0, colon_pos);35 parts.password = auth.substr(colon_pos + 1);36 } else {37 parts.user = auth;38 }39 rest = rest.substr(at_pos + 1);40 }41 42 auto slash_pos = rest.find('/');43 44 if (slash_pos != std::string::npos) {45 parts.host = rest.substr(0, slash_pos);46 parts.path = rest.substr(slash_pos);47 } else {48 parts.host = rest;49 parts.path = "/";50 }51 52 auto colon_pos = parts.host.find(':');53 54 if (colon_pos != std::string::npos) {55 parts.port = std::stoi(parts.host.substr(colon_pos + 1));56 parts.host = parts.host.substr(0, colon_pos);57 } else if (parts.scheme == "http") {58 parts.port = 80;59 } else if (parts.scheme == "https") {60 parts.port = 443;61 } else {62 throw std::runtime_error("unsupported URL scheme: " + parts.scheme);63 }64 65 return parts;66}67 68static std::pair<httplib::Client, common_http_url> common_http_client(const std::string & url) {69 common_http_url parts = common_http_parse_url(url);70 71 if (parts.host.empty()) {72 throw std::runtime_error("error: invalid URL format");73 }74 75#ifndef CPPHTTPLIB_OPENSSL_SUPPORT76 if (parts.scheme == "https") {77 throw std::runtime_error(78 "HTTPS is not supported. Please rebuild with one of:\n"79 " -DLLAMA_BUILD_BORINGSSL=ON\n"80 " -DLLAMA_BUILD_LIBRESSL=ON\n"81 " -DLLAMA_OPENSSL=ON (default, requires OpenSSL dev files installed)"82 );83 }84#endif85 86 httplib::Client cli(parts.scheme + "://" + parts.host + ":" + std::to_string(parts.port));87 88 if (!parts.user.empty()) {89 cli.set_basic_auth(parts.user, parts.password);90 }91 92 cli.set_follow_location(true);93 94 return { std::move(cli), std::move(parts) };95}96 97static std::string common_http_show_masked_url(const common_http_url & parts) {98 return parts.scheme + "://" + (parts.user.empty() ? "" : "****:****@") + parts.host + parts.path;99}100 