Add the streamer-tools API client, with a real HTTP backend per platform
Implements the two read-key-scoped calls in apps/server/src/obs/plugin.routes.ts: GET /api/obs/:slug/slots and POST /api/obs/:slug/token. Three pieces, all in core/ with no OBS dependency: - stplugin::json -- a small, strict JSON reader. Hand-rolled rather than vendoring nlohmann because the only JSON this plugin ever sees is two fixed-shape responses from its own server, and the parser has to build on three platforms with no package-manager step in CI. It never throws, bounds its recursion (kMaxDepth=32) so a hostile response cannot overflow the stack inside OBS, rejects trailing garbage, and returns the caller's fallback for wrong-typed access instead of aborting. - stplugin::HttpClient -- a two-method injectable interface, with libcurl behind it on Linux/macOS and WinHTTP on Windows. WinHTTP rather than curl on Windows because it ships with the OS and does TLS through SChannel: the self-hosted winvm-builder runner has no package manager, and per the scaffold README does not even have cmake preinstalled. Both backends cap the response body at 4 MiB, keep TLS verification on (the read key is a credential), and honour a whole-request timeout. - stplugin::ApiClient -- maps the responses onto an ApiStatus enum that distinguishes NotFound (404), Unavailable (503), NetworkError, MalformedResponse and InvalidConfig. It deliberately does not claim to know whether a 404 was a wrong key or an unknown slug, because the server deliberately does not say. Server URLs are normalised the way an operator actually pastes them, defaulting to https so the read key is never sent in the clear by accident, and redactedUrl() exists so a URL can be logged without the key. Tests (279 checks across two new suites) run at two levels: a fake HttpClient covering every response and error branch, and a real loopback HTTP server on 127.0.0.1 driving the actual platform backend -- so libcurl on Linux/macOS and WinHTTP on Windows are each exercised in CI rather than assumed. The loopback cases deliberately include the ones that must not hang OBS: a truncated JSON body, a connection accepted and closed without a reply, non-HTTP garbage, a dead port, and a stalled server that has to be cut off by the client's own timeout. Verified locally on Ubuntu 24.04: ctest --test-dir build --output-on-failure -> 4/4 passed test_json: 158 checks passed test_api_client: 121 checks passed Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RL8abRmgFXkVASHkkqiJbE
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
streamer-tools OBS Camera Plugin - libcurl HTTP backend (Linux/macOS)
|
||||
Copyright (C) 2026 CyberCoveLLC <jknapp85@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program. If not, see <https://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
#include "stplugin/http.h"
|
||||
|
||||
#include <curl/curl.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <mutex>
|
||||
|
||||
namespace stplugin {
|
||||
|
||||
namespace {
|
||||
|
||||
/// Hard cap on a response body. The two endpoints this client talks to return
|
||||
/// a few hundred bytes; anything larger is a misconfigured proxy or a wrong
|
||||
/// URL, and must not be allowed to grow OBS's heap without bound.
|
||||
constexpr std::size_t kMaxResponseBytes = 4u * 1024u * 1024u;
|
||||
|
||||
struct WriteContext {
|
||||
std::string body;
|
||||
bool overflowed = false;
|
||||
};
|
||||
|
||||
std::size_t writeCallback(char *ptr, std::size_t size, std::size_t nmemb, void *userdata)
|
||||
{
|
||||
auto *ctx = static_cast<WriteContext *>(userdata);
|
||||
const std::size_t bytes = size * nmemb;
|
||||
if (ctx->body.size() + bytes > kMaxResponseBytes) {
|
||||
ctx->overflowed = true;
|
||||
return 0; // aborts the transfer with CURLE_WRITE_ERROR
|
||||
}
|
||||
ctx->body.append(ptr, bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/// curl_global_init is not thread-safe and must run once per process before
|
||||
/// any easy handle is created. OBS may create several sources concurrently.
|
||||
void ensureCurlGlobalInit()
|
||||
{
|
||||
static std::once_flag once;
|
||||
std::call_once(once, [] { curl_global_init(CURL_GLOBAL_DEFAULT); });
|
||||
}
|
||||
|
||||
class CurlHttpClient : public HttpClient {
|
||||
public:
|
||||
HttpResponse send(const HttpRequest &request) override
|
||||
{
|
||||
ensureCurlGlobalInit();
|
||||
|
||||
HttpResponse response;
|
||||
CURL *curl = curl_easy_init();
|
||||
if (!curl) {
|
||||
response.network_error = "curl_easy_init failed";
|
||||
return response;
|
||||
}
|
||||
|
||||
WriteContext ctx;
|
||||
struct curl_slist *headers = nullptr;
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_URL, request.url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ctx);
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, static_cast<long>(request.timeout_ms));
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, static_cast<long>(request.timeout_ms));
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3L);
|
||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, "streamer-tools-obs-plugin/1.0");
|
||||
// NOSIGNAL is required whenever curl is used off the main thread:
|
||||
// without it curl installs a SIGALRM handler for DNS timeouts, which
|
||||
// is process-global and would be a rude thing to do inside OBS.
|
||||
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
|
||||
// TLS verification stays on. The read key is a credential; sending it
|
||||
// to an unverified host is exactly the failure this must not have.
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
|
||||
|
||||
if (request.method == "POST") {
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(request.body.size()));
|
||||
} else if (request.method != "GET") {
|
||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, request.method.c_str());
|
||||
}
|
||||
|
||||
if (!request.content_type.empty()) {
|
||||
const std::string header = "Content-Type: " + request.content_type;
|
||||
headers = curl_slist_append(headers, header.c_str());
|
||||
}
|
||||
// Fastify answers a bare POST with no body fine, but some proxies
|
||||
// insert an Expect: 100-continue round trip; suppress it.
|
||||
headers = curl_slist_append(headers, "Expect:");
|
||||
if (headers)
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
|
||||
const CURLcode rc = curl_easy_perform(curl);
|
||||
if (rc == CURLE_OK) {
|
||||
long status = 0;
|
||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
|
||||
response.status = status;
|
||||
response.body = std::move(ctx.body);
|
||||
} else if (ctx.overflowed) {
|
||||
response.network_error = "response body exceeded 4 MiB";
|
||||
} else {
|
||||
response.network_error = curl_easy_strerror(rc);
|
||||
}
|
||||
|
||||
if (headers)
|
||||
curl_slist_free_all(headers);
|
||||
curl_easy_cleanup(curl);
|
||||
return response;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
HttpClient *createPlatformHttpClient()
|
||||
{
|
||||
return new CurlHttpClient();
|
||||
}
|
||||
|
||||
} // namespace stplugin
|
||||
Reference in New Issue
Block a user