/* * ws_http.c - the one outbound HTTP call plugin_websearch makes, over the * SCORPIOX BOT Connector SDK's verified-TLS transport (vendored in this repo * as src/sx_http_tls.c + vendor/bearssl + src/ca_roots_pem.h). * * Why not plain sockets: a search API answers over TLS only, and a plugin that * ships no TLS stack has to either shell out to curl (unauditable, absent on a * slim container and on a stock Windows node) or refuse https on POSIX. The * vendored BearSSL build verifies the chain against the compiled-in root store * plus any operator bundle, checks the hostname against the certificate, and * has no "skip verification" path outside one environment variable that is * documented as debug-only. * * The contract stays deliberately small: one request in, status + body out. * An HTTP 4xx/5xx is a *successful* exchange — the provider's own error text is * what the user needs to read — so it is reported with ok=1 and the status. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #if !defined(_WIN32) # if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L # undef _POSIX_C_SOURCE # define _POSIX_C_SOURCE 200809L # endif # ifndef _DEFAULT_SOURCE # define _DEFAULT_SOURCE 1 # endif #endif #include "ws.h" #include #include #include #include "sx_http_tls.h" void ws_resp_free(ws_resp *r) { if (!r) return; free(r->body); r->body = NULL; r->body_len = 0; } int ws_http_do(const char *method, const char *url, const char *headers, const char *body, size_t body_len, long timeout_ms, ws_resp *out) { sx_url parsed; sx_conn c; char err[512]; char *buf = NULL; size_t len = 0; int rc = -1; if (!out) return -1; memset(out, 0, sizeof *out); err[0] = '\0'; if (!url || !*url) { ws_copy(out->error, sizeof out->error, "no URL configured for this provider"); return -1; } if (sx_url_parse(url, &parsed, err, sizeof err) != 0) { snprintf(out->error, sizeof out->error, "%s", err[0] ? err : "unsupported URL"); return -1; } if (sx_conn_open(&c, &parsed, timeout_ms > 0 ? timeout_ms : 15000, err, sizeof err) != 0) { snprintf(out->error, sizeof out->error, "%s", err[0] ? err : "cannot connect"); return -1; } if (sx_http_request(&c, method ? method : "GET", parsed.path, headers, NULL, (body && body_len) ? body : NULL, body_len, timeout_ms > 0 ? timeout_ms : 20000, err, sizeof err) != 0) { snprintf(out->error, sizeof out->error, "%s", err[0] ? err : "request failed"); sx_conn_close(&c); return -1; } out->status = (long)c.status; if (sx_read_all(&c, &buf, &len, timeout_ms > 0 ? timeout_ms : 20000) != 1) { /* A 204 / empty body legitimately reads as "nothing to read" in some * servers; treat an empty stream as an empty body, not a failure, as * long as we got a status line. */ if (len == 0 && buf == NULL) { buf = (char *)malloc(1); if (!buf) { ws_copy(out->error, sizeof out->error, "out of memory"); sx_conn_close(&c); return -1; } buf[0] = '\0'; len = 0; rc = 0; } else { ws_copy(out->error, sizeof out->error, "cannot read the response body"); free(buf); sx_conn_close(&c); return -1; } } else { rc = 0; } sx_conn_close(&c); if (!buf) { buf = (char *)malloc(1); if (!buf) { ws_copy(out->error, sizeof out->error, "out of memory"); return -1; } buf[0] = '\0'; len = 0; } out->body = buf; out->body_len = len; out->ok = 1; return rc; }