viewer: cpp-httplib + Mbed TLS instead of libcurl for the broker client

The viewer's live-broker client goes back to cpp-httplib, which the broker
already fetches; libcurl was only there for Kerberos, which is no longer
wanted. HTTPS comes from Mbed TLS 4.2.0, fetched and statically linked for
viewer builds only, so one TLS backend now serves Linux, Windows and macOS
(the macOS viewer previously had no TLS at all, and Linux needed host
OpenSSL >= 3.0). cpp-httplib verifies servers against the OS trust store:
the Windows certificate store, the macOS Keychain, the CA bundle on Linux.

CPPHTTPLIB_MBEDTLS_SUPPORT is set on jfjoch_viewer only, not on
httplib::httplib, so the broker stays plain HTTP. JFJochHttpReader.cpp is
the only viewer source that includes httplib.h, and the header keeps
httplib::Client opaque so <windows.h> stays out of the other viewer units.

Checked on Linux: a viewer-only build links Mbed TLS statically, with no
curl and no system libssl; a standalone client built against the same
httplib + Mbed TLS fetches from real HTTPS hosts and rejects expired,
self-signed and wrong-host certificates. Windows and macOS are untested.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-10-07 21:16:04 +02:00
1 parent 36366295f2
commit 89207fe2bd
13 files changed
+1210 -165

No files matched your search

+37 -66
View File
@@ -15,79 +15,51 @@
#include "../common/JFJochMath.h"
#include "../image_analysis/bragg_integration/CalcISigma.h"
// Included last, after all std/project headers: on Windows <curl/curl.h> pulls in <windows.h>,
// Included last, after all std/project headers: on Windows <httplib.h> pulls in <windows.h>,
// whose min/max (and other) macros must not precede the headers that use std::min / std::max.
#include <curl/curl.h>
namespace {
// libcurl write callback: append the received bytes to a std::string (which holds binary fine).
size_t AppendToString(char *ptr, size_t size, size_t nmemb, void *userdata) {
auto *out = static_cast<std::string *>(userdata);
out->append(ptr, size * nmemb);
return size * nmemb;
}
// libcurl needs a one-time global init before any easy handle is used; do it once and
// thread-safely, as the viewer drives the reader from a worker thread.
void EnsureCurlGlobalInit() {
static std::once_flag once;
std::call_once(once, [] { curl_global_init(CURL_GLOBAL_DEFAULT); });
}
}
#include <httplib.h>
JFJochHttpReader::HttpResult JFJochHttpReader::Request(const std::string &method, const std::string &path,
const std::string &body,
const std::string &content_type) const {
// One easy handle is reused across requests so the connection stays alive. curl_easy_reset
// clears the previous request's options but deliberately keeps the live connection and the DNS
// / TLS-session caches, so a same-host request reuses the open socket; if that socket has since
// been closed, libcurl reconnects (and retries the request) on its own.
std::lock_guard cl(curl_mutex);
// One client is reused across requests so the connection stays alive; if the socket has since
// been closed, httplib reconnects on its own.
std::lock_guard cl(client_mutex);
if (curl_handle == nullptr) {
EnsureCurlGlobalInit();
curl_handle = curl_easy_init();
if (curl_handle == nullptr)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Could not initialize CURL");
} else {
curl_easy_reset(curl_handle);
if (!client) {
// addr is scheme://host[:port][/prefix]; the client takes everything before the prefix.
const auto host_start = addr.find("://");
const auto prefix_start = addr.find('/', host_start == std::string::npos ? 0 : host_start + 3);
path_prefix = (prefix_start == std::string::npos) ? "" : addr.substr(prefix_start);
auto new_client = std::make_unique<httplib::Client>(addr.substr(0, prefix_start));
if (!new_client->is_valid())
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Invalid broker address " + addr);
client = std::move(new_client);
client->set_keep_alive(true);
// Cap connect and the whole request at 5 s so a stalled/black-holed broker fails the request
// instead of blocking forever (which would wedge the reader thread and Close() on
// client_mutex). All viewer requests are small and against a nearby broker, so 5 s is ample.
client->set_connection_timeout(5);
client->set_max_timeout(std::chrono::seconds(5));
}
CURL *curl = curl_handle;
httplib::Headers headers;
if (!bearer_token.empty())
headers.emplace("Authorization", "Bearer " + bearer_token);
const std::string url = path_prefix + path;
auto res = (method == "PUT")
? client->Put(url, headers, body, content_type)
: client->Get(url, headers);
HttpResult result;
const std::string url = addr + path;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, AppendToString);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result.body);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); // safe to use from worker threads
// Cap connect and total transfer at 5 s so a stalled/black-holed broker fails the request instead
// of blocking curl_easy_perform forever (which would wedge the reader thread and Close() on
// curl_mutex). All viewer requests are small and against a nearby broker, so 5 s is ample.
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L);
curl_slist *headers = nullptr;
if (!bearer_token.empty())
headers = curl_slist_append(headers, ("Authorization: Bearer " + bearer_token).c_str());
if (method == "PUT") {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.data());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(body.size()));
if (!content_type.empty())
headers = curl_slist_append(headers, ("Content-Type: " + content_type).c_str());
}
if (headers != nullptr)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
if (curl_easy_perform(curl) == CURLE_OK) {
if (res) {
result.ok = true;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &result.status);
result.status = res->status;
result.body = std::move(res->body);
}
if (headers != nullptr)
curl_slist_free_all(headers);
// A protected dataset is reported the same way from every endpoint, so no caller has to know
// which ones the broker guards.
if (result.ok && (result.status == 401 || result.status == 403))
@@ -100,16 +72,15 @@ void JFJochHttpReader::Token(const std::string &token) {
bearer_token = token;
}
JFJochHttpReader::JFJochHttpReader() = default;
JFJochHttpReader::~JFJochHttpReader() {
ResetConnection();
}
void JFJochHttpReader::ResetConnection() const {
std::lock_guard cl(curl_mutex);
if (curl_handle != nullptr) {
curl_easy_cleanup(curl_handle);
curl_handle = nullptr;
}
std::lock_guard cl(client_mutex);
client.reset();
}
void JFJochHttpReader::Close() {