Skip to content

Commit 7182ef7

Browse files
texasichtexasichmarecabo
authored
common: honor HTTP_PROXY/HTTPS_PROXY env vars in http client (ggml-org#21752)
* common: honor HTTP_PROXY/HTTPS_PROXY env vars in http client common_http_client() was ignoring standard proxy env vars, causing downloads via -hf and similar flags to fail silently in environments behind an HTTP proxy (corporate networks, air-gapped infra, etc.). Checks HTTPS_PROXY/https_proxy for https:// URLs and HTTP_PROXY/http_proxy for http:// URLs, in that order. If a proxy URL is set and parseable, passes it to httplib's set_proxy() and set_proxy_basic_auth() as appropriate. Malformed proxy URLs are silently ignored so existing callers aren't affected. Fixes ggml-org#21694 * common: drop misleading comment about generic proxy fallback The comment referenced a 'generic one' that doesn't exist in the code — both HTTP_PROXY and HTTPS_PROXY are scheme-specific. Reviewer noted the mismatch. Addresses review feedback from @ServeurpersoCom. * common : warn when the proxy URL from the environment is malformed * common : honor NO_PROXY when selecting the http proxy * ci : retrigger checks (test-thread-safety flake) * common : bypass the proxy for loopback destinations llama cli talks to its spawned server on 127.0.0.1 through common_http_client, so with HTTP_PROXY set and no NO_PROXY the health polling went to the corporate proxy, which rejects loopback targets, and the wait loop never exits ("Loading model..." hangs forever). A remote proxy can never reach the client machine's own loopback, so these destinations are now always contacted directly, before NO_PROXY is consulted: localhost, *.localhost, 127.0.0.0/8, ::1 and 0.0.0.0. Similar to Go's net/http. Proxy handling for downloads and remote --server-base endpoints is unchanged. Signed-off-by: texasich <101962694+texasich@users.noreply.github.com> * docs: move proxy tip from README to server docs per review: the top level README stays lean; the proxy howto now lives with the server docs under Usage Signed-off-by: texasich <101962694+texasich@users.noreply.github.com> --------- Signed-off-by: texasich <101962694+texasich@users.noreply.github.com> Co-authored-by: texasich <texasich@users.noreply.github.com> Co-authored-by: texasich <texasich@gmail.com> Co-authored-by: marecabo <23156476+marecabo@users.noreply.github.com>
1 parent 2a66703 commit 7182ef7

2 files changed

Lines changed: 145 additions & 0 deletions

File tree

‎common/http.h‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "log.h"
4+
35
#include <cpp-httplib/httplib.h>
46

57
#ifdef _WIN32
@@ -97,6 +99,118 @@ static common_http_url common_http_parse_url(const std::string & url) {
9799
return parts;
98100
}
99101

102+
// match a loopback destination: a remote proxy can never reach the client machine's own
103+
// loopback, so these hosts are always contacted directly, before NO_PROXY is consulted
104+
// (similar to Go's net/http)
105+
static bool common_http_is_loopback(const std::string & host) {
106+
auto lower = [](std::string s) {
107+
for (auto & c : s) {
108+
if (c >= 'A' && c <= 'Z') {
109+
c += 'a' - 'A';
110+
}
111+
}
112+
return s;
113+
};
114+
115+
const std::string h = lower(host);
116+
117+
if (h == "localhost" || h == "::1" || h == "0.0.0.0") {
118+
return true;
119+
}
120+
121+
if (h.size() > 10 && h.compare(h.size() - 10, 10, ".localhost") == 0) {
122+
return true;
123+
}
124+
125+
// IPv4 literal: exactly four all-digit octets with values 0-255, loopback is 127.0.0.0/8
126+
int octets[4] = { 0, 0, 0, 0 };
127+
size_t pos = 0;
128+
int n = 0;
129+
bool complete = false;
130+
while (n < 4 && pos <= h.size()) {
131+
size_t dot = h.find('.', pos);
132+
if (dot == std::string::npos) {
133+
dot = h.size();
134+
}
135+
if (dot == pos || dot - pos > 3) {
136+
return false;
137+
}
138+
int val = 0;
139+
for (size_t i = pos; i < dot; i++) {
140+
if (h[i] < '0' || h[i] > '9') {
141+
return false;
142+
}
143+
val = val * 10 + (h[i] - '0');
144+
}
145+
if (val > 255) {
146+
return false;
147+
}
148+
octets[n++] = val;
149+
if (dot == h.size()) {
150+
complete = (n == 4);
151+
break;
152+
}
153+
pos = dot + 1;
154+
}
155+
156+
return complete && octets[0] == 127;
157+
}
158+
159+
// match a host against a NO_PROXY list: comma separated entries, "*" bypasses the proxy for every host,
160+
// an entry matches the host itself or any of its subdomains, a leading dot is optional, ports are ignored
161+
static bool common_http_proxy_bypass(const std::string & no_proxy, const std::string & host) {
162+
auto lower = [](std::string s) {
163+
for (auto & c : s) {
164+
if (c >= 'A' && c <= 'Z') {
165+
c += 'a' - 'A';
166+
}
167+
}
168+
return s;
169+
};
170+
171+
const std::string h = lower(host);
172+
173+
for (size_t pos = 0; pos < no_proxy.size(); ) {
174+
size_t end = no_proxy.find(',', pos);
175+
if (end == std::string::npos) {
176+
end = no_proxy.size();
177+
}
178+
179+
std::string entry = lower(no_proxy.substr(pos, end - pos));
180+
pos = end + 1;
181+
182+
entry.erase(0, entry.find_first_not_of(" \t"));
183+
auto last = entry.find_last_not_of(" \t");
184+
if (last == std::string::npos) {
185+
continue;
186+
}
187+
entry.erase(last + 1);
188+
189+
if (entry == "*") {
190+
return true;
191+
}
192+
193+
if (entry.front() == '.') {
194+
entry.erase(0, 1);
195+
}
196+
197+
if (entry.empty()) {
198+
continue;
199+
}
200+
201+
if (h == entry) {
202+
return true;
203+
}
204+
205+
if (h.size() > entry.size() && h[h.size() - entry.size() - 1] == '.' &&
206+
h.compare(h.size() - entry.size(), entry.size(), entry) == 0) {
207+
return true;
208+
}
209+
}
210+
211+
return false;
212+
}
213+
100214
static std::pair<httplib::Client, common_http_url> common_http_client(const std::string & url) {
101215
common_http_url parts = common_http_parse_url(url);
102216

@@ -123,6 +237,33 @@ static std::pair<httplib::Client, common_http_url> common_http_client(const std:
123237

124238
cli.set_follow_location(true);
125239

240+
// Honor HTTP_PROXY / HTTPS_PROXY environment variables (both lower- and upper-case).
241+
auto getenv_s = [](const char * upper, const char * lower) -> std::string {
242+
const char * val = std::getenv(upper);
243+
if (!val) {
244+
val = std::getenv(lower);
245+
}
246+
return val ? val : "";
247+
};
248+
249+
const std::string proxy_url = (parts.scheme == "https")
250+
? getenv_s("HTTPS_PROXY", "https_proxy")
251+
: getenv_s("HTTP_PROXY", "http_proxy");
252+
253+
if (!proxy_url.empty() && !common_http_is_loopback(parts.host) &&
254+
!common_http_proxy_bypass(getenv_s("NO_PROXY", "no_proxy"), parts.host)) {
255+
try {
256+
common_http_url proxy = common_http_parse_url(proxy_url);
257+
cli.set_proxy(proxy.host, proxy.port);
258+
if (!proxy.user.empty()) {
259+
cli.set_proxy_basic_auth(proxy.user, proxy.password);
260+
}
261+
} catch (const std::exception & e) {
262+
// fall back to a direct connection, the URL itself is not logged as it can carry credentials
263+
LOG_WRN("%s: ignoring malformed proxy URL from the environment: %s\n", __func__, e.what());
264+
}
265+
}
266+
126267
return { std::move(cli), std::move(parts) };
127268
}
128269

‎tools/server/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@ Recommended `--cors-origins` setting, depending on where the server runs:
405405

406406
Related flags: `--cors-origins`, `--cors-methods`, `--cors-headers`, `--cors-credentials` / `--no-cors-credentials`. Background and rationale: [#25655](https://github.com/ggml-org/llama.cpp/pull/25655).
407407

408+
### Running behind a proxy
409+
410+
If your host reaches the internet through a corporate proxy, export `HTTP_PROXY` and `HTTPS_PROXY` before running commands that download models (e.g. `export HTTPS_PROXY=http://proxy.example.com:8080`). Hosts listed in `NO_PROXY` and loopback addresses are contacted directly.
411+
408412
## Build
409413

410414
`llama-server` is built alongside everything else from the root of the project

0 commit comments

Comments
 (0)