CoolFace
Datasetpublic

echodict/llama.cpp

version https://git-lfs.github.com/spec/v1 oid sha256:cfc44b7ba25614df70e6b65e3341cae0310163bd32fd31a6b928a542df433faf size 30786

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes765downloads
server-http.h84 linesDownload Raw Back to server
1#pragma once2 3#include <atomic>4#include <functional>5#include <map>6#include <string>7#include <thread>8#include <vector>9#include <cstdint>10 11struct common_params;12 13// generator-like API for HTTP response generation14// this object response with one of the 2 modes:15// 1) normal response: `data` contains the full response body16// 2) streaming response: each call to next(output) generates the next chunk17//    when next(output) returns false, no more data after the current chunk18//    note: some chunks can be empty, in which case no data is sent for that chunk19struct server_http_res {20    std::string content_type = "application/json; charset=utf-8";21    int status = 200;22    std::string data;23    std::map<std::string, std::string> headers;24 25    // TODO: move this to a virtual function once we have proper polymorphism support26    std::function<bool(std::string &)> next = nullptr;27    bool is_stream() const {28        return next != nullptr;29    }30 31    virtual ~server_http_res() = default;32};33 34// unique pointer, used by set_chunked_content_provider35// httplib requires the stream provider to be stored in heap36using server_http_res_ptr = std::unique_ptr<server_http_res>;37using raw_buffer = std::vector<uint8_t>;38 39struct server_http_req {40    std::map<std::string, std::string> params; // path_params + query_params41    std::map<std::string, std::string> headers; // used by MCP proxy42    std::string path;43    std::string query_string; // query parameters string (e.g. "action=save")44    std::string body;45    std::map<std::string, raw_buffer> files; // used for file uploads (form data)46    const std::function<bool()> & should_stop;47 48    std::string get_param(const std::string & key, const std::string & def = "") const {49        auto it = params.find(key);50        if (it != params.end()) {51            return it->second;52        }53        return def;54    }55};56 57struct server_http_context {58    class Impl;59    std::unique_ptr<Impl> pimpl;60 61    std::thread thread; // server thread62    std::atomic<bool> is_ready = false;63 64    std::string path_prefix;65    std::string hostname;66    int port;67 68    server_http_context();69    ~server_http_context();70 71    bool init(const common_params & params);72    bool start();73    void stop() const;74 75    // note: the handler should never throw exceptions76    using handler_t = std::function<server_http_res_ptr(const server_http_req & req)>;77 78    void get(const std::string & path, const handler_t & handler) const;79    void post(const std::string & path, const handler_t & handler) const;80 81    // for debugging82    std::string listening_address;83};84