http_listener.cpp¶
src/facade/http_listener.cpp
@file http_listener.cpp @brief Streamable-HTTP front end for the external bridge (gh#204).
Owners: @tvanfossen
await_running
std::string entropic::http::HttpListener::await_running()
Return only once the server is accepting, or say why not.
src/facade/http_listener.cpp:531
Source · 8 lines
std::string HttpListener::await_running() {
for (int i = 0; i < 5000; ++i) {
if (impl_->server.is_running() || impl_->listen_returned) { break; }
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return impl_->server.is_running() ? std::string{}
: std::string("listener did not start");
}Activity · 5 steps
Requirements
bind_loopback
static int entropic::http::bind_loopback(httplib::Server &server, int port)
Bind 127.0.0.1 on port , or on any free port when it is 0.
src/facade/http_listener.cpp:456
Source · 8 lines
static int bind_loopback(httplib::Server& server, int port) {
server.set_socket_options([](socket_t sock) {
int one = 1;
::setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
});
return port == 0 ? server.bind_to_any_port("127.0.0.1")
: (server.bind_to_port("127.0.0.1", port) ? port : -1);
}Activity · 2 steps
Requirements
blocked
bool entropic::http::AuthThrottle::blocked(Clock::time_point now) const
Whether requests are refused at now .
src/facade/http_listener.cpp:197
Source · 4 lines
bool AuthThrottle::blocked(Clock::time_point now) const {
std::lock_guard<std::mutex> lock(mu_);
return now < blocked_until_;
}Activity · 2 steps
Requirements
configure_server
static void entropic::http::configure_server(HttpListener::Impl &impl, ExternalBridge *bridge, const std::atomic< int > &port)
Install routes, limits and the worker pool on the server.
src/facade/http_listener.cpp:417
Source · 23 lines
static void configure_server(HttpListener::Impl& impl, ExternalBridge* bridge,
const std::atomic<int>& port) {
auto& s = impl.server;
s.set_payload_max_length(kMaxBodyBytes);
s.set_read_timeout(30, 0);
s.set_write_timeout(30, 0);
s.new_task_queue = [] { return new httplib::ThreadPool(32); };
s.Post("/mcp", [&impl, bridge, &port](const httplib::Request& req,
httplib::Response& res) {
const int refuse = refusal_status(req, port.load(), impl);
if (refuse != 0) {
res.status = refuse;
res.set_content(error_body("refused"), "application/json");
return;
}
handle_post(bridge, req, res);
});
auto not_allowed = [](const httplib::Request&, httplib::Response& res) {
res.status = 405; // no server-initiated stream, no transport session
};
s.Get("/mcp", not_allowed);
s.Delete("/mcp", not_allowed);
}Activity · 9 steps
Requirements
publish_token
std::string entropic::http::HttpListener::publish_token(int bound)
Generate this run's token and write it where clients read it.
src/facade/http_listener.cpp:472
Called by start
Calls generate_token, prepare_private_dir, token_path_for, write_token_file
Source · 13 lines
std::string HttpListener::publish_token(int bound) {
std::string err;
impl_->token = generate_token();
if (impl_->token.empty()) {
err = "no entropy for the bearer token";
} else {
token_file_ = token_path_for(bound);
if (prepare_private_dir(token_file_.parent_path(), err)) {
write_token_file(token_file_, impl_->token, err);
}
}
return err;
}Activity · 9 steps
Requirements
record_failure
void entropic::http::AuthThrottle::record_failure(Clock::time_point now)
Record one failed authentication at now .
src/facade/http_listener.cpp:208
Source · 11 lines
void AuthThrottle::record_failure(Clock::time_point now) {
std::lock_guard<std::mutex> lock(mu_);
if (now - window_start_ > kThrottleWindow) {
window_start_ = now;
failures_ = 0;
}
if (++failures_ >= kThrottleFailures) {
blocked_until_ = now + kThrottleBlock;
failures_ = 0;
}
}Activity · 7 steps
Requirements
start
bool entropic::http::HttpListener::start(std::string &err)
Write the token, bind, and start serving.
src/facade/http_listener.cpp:493
Called by SCENARIO, TEST_CASE, start_http_listener
Calls await_running, bind_loopback, configure_server, publish_token
Source · 22 lines
bool HttpListener::start(std::string& err) {
configure_server(*impl_, bridge_, port_);
const int bound = bind_loopback(impl_->server, port_.load());
if (bound <= 0) {
err = "cannot bind 127.0.0.1:" + std::to_string(port_.load());
} else {
port_ = bound;
err = publish_token(bound);
}
if (err.empty()) {
thread_ = std::thread([this] {
impl_->server.listen_after_bind();
impl_->listen_returned = true;
});
err = await_running();
}
if (err.empty()) {
logger->info("[http] serving MCP at http://127.0.0.1:{}/mcp "
"(token: {})", bound, token_file_.string());
}
return err.empty();
}Activity · 13 steps
Requirements
stop
void entropic::http::HttpListener::stop()
Stop serving and remove the token file.
src/facade/http_listener.cpp:545
Source · 9 lines
void HttpListener::stop() {
impl_->server.stop();
if (thread_.joinable()) { thread_.join(); }
if (!token_file_.empty()) {
std::error_code ec;
std::filesystem::remove(token_file_, ec);
token_file_.clear();
}
}Activity · 7 steps
Requirements
~HttpListener
entropic::http::HttpListener::~HttpListener()
Stops the server and removes the token file.
src/facade/http_listener.cpp:407
The index is older than this file: the lines it records no longer hold this function. Refresh the index to bundle its body.
Requirements
~SseJob
entropic::http::anonymous_namespace{http_listener.cpp}::SseJob::~SseJob()Join the dispatching thread, then close our end of the pipe.
src/facade/http_listener.cpp:252
The index is older than this file: the lines it records no longer hold this function. Refresh the index to bundle its body.