Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
http_listener.cpp File Reference

Streamable-HTTP front end for the external bridge (gh#204). More...

#include "http_listener.h"
#include <entropic/mcp/external_bridge.h>
#include <entropic/types/logging.h>
#include <httplib.h>
#include <nlohmann/json.hpp>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <poll.h>
#include <sys/random.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <unistd.h>
Include dependency graph for http_listener.cpp:

Go to the source code of this file.

Classes

struct  entropic::http::HttpListener::Impl
 httplib state, kept out of the header (no third-party headers). More...
 

Namespaces

namespace  entropic
 Activate model on GPU (WARM → ACTIVE).
 

Functions

std::string entropic::http::generate_token ()
 A fresh bearer token: 256 bits from getrandom(), hex-encoded.
 
bool entropic::http::tokens_equal (const std::string &a, const std::string &b)
 Compare two tokens in time independent of where they differ.
 
bool entropic::http::host_allowed (const std::string &host, int port)
 Whether a Host header names this listener's loopback endpoint.
 
std::string entropic::http::bearer_token (const std::string &header)
 The token in an Authorization: Bearer <t> header, or "".
 
std::filesystem::path entropic::http::token_path_for (int port)
 Where the token for a port is written.
 
bool entropic::http::prepare_private_dir (const std::filesystem::path &dir, std::string &err)
 Create or verify the token directory: 0700, ours, not a symlink.
 
bool entropic::http::write_token_file (const std::filesystem::path &path, const std::string &token, std::string &err)
 Write token to path atomically (temp + rename), mode 0600.
 
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.
 
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.
 

Detailed Description

Streamable-HTTP front end for the external bridge (gh#204).

See http_listener.h for the transport and the security bar.

Version
2.14.0

Definition in file http_listener.cpp.

Function Documentation

◆ bearer_token()

ENTROPIC_EXPORT std::string entropic::http::bearer_token ( const std::string &  header)

The token in an Authorization: Bearer <t> header, or "".

Parameters
headerAuthorization header value.
Returns
The token, or "" when the scheme is not Bearer. @utility
Version
2.14.0

Definition at line 119 of file http_listener.cpp.

◆ bind_loopback()

static int entropic::http::bind_loopback ( httplib::Server &  server,
int  port 
)
static

Bind 127.0.0.1 on port, or on any free port when it is 0.

Parameters
serverThe server.
portRequested port.
Returns
The bound port, or <= 0 on failure. @utility
Version
2.14.0

Definition at line 449 of file http_listener.cpp.

◆ configure_server()

static void entropic::http::configure_server ( HttpListener::Impl &  impl,
ExternalBridge *  bridge,
const std::atomic< int > &  port 
)
static

Install routes, limits and the worker pool on the server.

Parameters
implServer state.
bridgeBridge to dispatch to.
portBound port (for the Host check). @utility
Version
2.14.0

Definition at line 417 of file http_listener.cpp.

◆ generate_token()

ENTROPIC_EXPORT std::string entropic::http::generate_token ( )

A fresh bearer token: 256 bits from getrandom(), hex-encoded.

Returns
64 lowercase hex characters, or "" if the kernel gave no entropy. @utility
Version
2.14.0

Definition at line 62 of file http_listener.cpp.

◆ host_allowed()

ENTROPIC_EXPORT bool entropic::http::host_allowed ( const std::string &  host,
int  port 
)

Whether a Host header names this listener's loopback endpoint.

Parameters
hostHost header value ("" when absent).
portListening port.
Returns
true for 127.0.0.1:<port> or localhost:<port>. @utility
Version
2.14.0

Definition at line 107 of file http_listener.cpp.

◆ prepare_private_dir()

ENTROPIC_EXPORT bool entropic::http::prepare_private_dir ( const std::filesystem::path &  dir,
std::string &  err 
)

Create or verify the token directory: 0700, ours, not a symlink.

Parameters
dirDirectory to prepare.
[out]errWhy it is unusable.
Returns
true when the directory is safe to hold a token. @utility
Version
2.14.0

Definition at line 146 of file http_listener.cpp.

◆ token_path_for()

ENTROPIC_EXPORT std::filesystem::path entropic::http::token_path_for ( int  port)

Where the token for a port is written.

Parameters
portListening port.
Returns
$HOME/.entropic/serve/<port>.token. @utility
Version
2.14.0

Definition at line 132 of file http_listener.cpp.

◆ tokens_equal()

ENTROPIC_EXPORT bool entropic::http::tokens_equal ( const std::string &  a,
const std::string &  b 
)

Compare two tokens in time independent of where they differ.

Parameters
aFirst value.
bSecond value.
Returns
true when equal. @utility
Version
2.14.0

Definition at line 88 of file http_listener.cpp.

◆ write_token_file()

ENTROPIC_EXPORT bool entropic::http::write_token_file ( const std::filesystem::path &  path,
const std::string &  token,
std::string &  err 
)

Write token to path atomically (temp + rename), mode 0600.

Parameters
pathDestination.
tokenToken text.
[out]errWhy the write failed.
Returns
true on success. @utility
Version
2.14.0

Definition at line 171 of file http_listener.cpp.

Variable Documentation

◆ read_fd

int read_fd = -1

Our end.

Definition at line 244 of file http_listener.cpp.

◆ worker

std::thread worker

Runs dispatch()

Definition at line 245 of file http_listener.cpp.