Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add the remaining endpoints
  • Loading branch information
ngxson committed Nov 12, 2025
commit 473b0e58236fb80944af18c2f12027779fb78395
12 changes: 8 additions & 4 deletions tools/server/server-http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ bool server_http_context::init(const common_params & params) {
// FIXME
GGML_UNUSED(res);
GGML_UNUSED(message);
printf("Exception caught in HTTP server: %s\n", message.c_str());
// try {
// json formatted_error = format_error_response(message, ERROR_TYPE_SERVER);
// LOG_WRN("got exception: %s\n", formatted_error.dump().c_str());
Expand Down Expand Up @@ -306,7 +307,7 @@ void server_http_context::get(const std::string & path, server_http_context::han
pimpl->srv->Get(path_prefix + path, [handler](const httplib::Request & req, httplib::Response & res) {
server_http_resgen_ptr response = handler(server_http_request{
get_params(req),
json{},
req.body,
req.is_connection_closed
});
GGML_ASSERT(!response->is_stream() && "not supported for GET method");
Expand All @@ -320,7 +321,7 @@ void server_http_context::post(const std::string & path, server_http_context::ha
pimpl->srv->Post(path_prefix + path, [handler](const httplib::Request & req, httplib::Response & res) {
server_http_resgen_ptr response = handler(server_http_request{
get_params(req),
json::parse(req.body.empty() ? "{}" : req.body),
req.body,
req.is_connection_closed
});
if (response->is_stream()) {
Expand All @@ -330,15 +331,18 @@ void server_http_context::post(const std::string & path, server_http_context::ha
// convert to shared_ptr as both chunked_content_provider() and on_complete() need to use it
std::shared_ptr<server_http_resgen> r_ptr = std::move(response);
const auto chunked_content_provider = [response = r_ptr](size_t, httplib::DataSink & sink) -> bool {
// TODO: maybe handle sink.write unsuccessful case? for now, we rely on is_connection_closed()
// TODO: maybe handle sink.write unsuccessful? for now, we rely on is_connection_closed()
sink.write(response->data.data(), response->data.size());
SRV_DBG("http: streamed chunk: %s\n", response->data.c_str());
if (!response->next()) {
// flush the remaining data
sink.write(response->data.data(), response->data.size());
SRV_DBG("http: streamed chunk (last): %s\n", response->data.c_str());
SRV_DBG("%s", "http: stream ended\n");
sink.done();
return false; // end of stream
}
return true;
return true; // more data, continue the loop
};
const auto on_complete = [response = r_ptr](bool) mutable {
response.reset(); // trigger the destruction of the response object
Expand Down
4 changes: 2 additions & 2 deletions tools/server/server-http.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// this object response with one of the 2 modes:
// 1) normal response: `data` contains the full response body
// 2) streaming response: each call to next() generates the next chunk, stored in `data`
// when next() returns false, the stream ends
// when next() returns false, no more data after the current chunk
struct server_http_resgen {
std::string content_type = "application/json; charset=utf-8";
int status = 200;
Expand All @@ -33,7 +33,7 @@ using server_http_resgen_ptr = std::unique_ptr<server_http_resgen>;

struct server_http_request {
std::map<std::string, std::string> params; // path_params + query_params
json body;
std::string body;
const std::function<bool()> & should_stop;

std::string get_param(const std::string & key, const std::string & def = "") const {
Expand Down
Loading
Loading