Skip to content

Commit 47eb12b

Browse files
server: fix query params lost when proxying requests in multi-model router mode (ggml-org#19854)
* server: fix query params lost when proxying requests in multi-model router mode * server: re-encode query params using httplib::encode_query_component in proxy
1 parent 418dea3 commit 47eb12b

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

tools/server/server-http.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,17 @@ static std::map<std::string, std::string> get_headers(const httplib::Request & r
339339
return headers;
340340
}
341341

342+
static std::string build_query_string(const httplib::Request & req) {
343+
std::string qs;
344+
for (const auto & [key, value] : req.params) {
345+
if (!qs.empty()) {
346+
qs += '&';
347+
}
348+
qs += httplib::encode_query_component(key) + "=" + httplib::encode_query_component(value);
349+
}
350+
return qs;
351+
}
352+
342353
// using unique_ptr for request to allow safe capturing in lambdas
343354
using server_http_req_ptr = std::unique_ptr<server_http_req>;
344355

@@ -382,6 +393,7 @@ void server_http_context::get(const std::string & path, const server_http_contex
382393
get_params(req),
383394
get_headers(req),
384395
req.path,
396+
build_query_string(req),
385397
req.body,
386398
req.is_connection_closed
387399
});
@@ -396,6 +408,7 @@ void server_http_context::post(const std::string & path, const server_http_conte
396408
get_params(req),
397409
get_headers(req),
398410
req.path,
411+
build_query_string(req),
399412
req.body,
400413
req.is_connection_closed
401414
});

tools/server/server-http.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ using server_http_res_ptr = std::unique_ptr<server_http_res>;
3636
struct server_http_req {
3737
std::map<std::string, std::string> params; // path_params + query_params
3838
std::map<std::string, std::string> headers; // reserved for future use
39-
std::string path; // reserved for future use
39+
std::string path;
40+
std::string query_string; // query parameters string (e.g. "action=save")
4041
std::string body;
4142
const std::function<bool()> & should_stop;
4243

tools/server/server-models.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,11 +697,15 @@ server_http_res_ptr server_models::proxy_request(const server_http_req & req, co
697697
mapping[name].meta.last_used = ggml_time_ms();
698698
}
699699
SRV_INF("proxying request to model %s on port %d\n", name.c_str(), meta->port);
700+
std::string proxy_path = req.path;
701+
if (!req.query_string.empty()) {
702+
proxy_path += '?' + req.query_string;
703+
}
700704
auto proxy = std::make_unique<server_http_proxy>(
701705
method,
702706
CHILD_ADDR,
703707
meta->port,
704-
req.path,
708+
proxy_path,
705709
req.headers,
706710
req.body,
707711
req.should_stop,

0 commit comments

Comments
 (0)