scorpiox-bot-plugin-websearch
←ws_http.cc
126 lines · 3 KBHEADBlameDownload
1/*
2 * ws_http.c - the one outbound HTTP call plugin_websearch makes, over the
3 * SCORPIOX BOT Connector SDK's verified-TLS transport (vendored in this repo
4 * as src/sx_http_tls.c + vendor/bearssl + src/ca_roots_pem.h).
5 *
6 * Why not plain sockets: a search API answers over TLS only, and a plugin that
7 * ships no TLS stack has to either shell out to curl (unauditable, absent on a
8 * slim container and on a stock Windows node) or refuse https on POSIX. The
9 * vendored BearSSL build verifies the chain against the compiled-in root store
10 * plus any operator bundle, checks the hostname against the certificate, and
11 * has no "skip verification" path outside one environment variable that is
12 * documented as debug-only.
13 *
14 * The contract stays deliberately small: one request in, status + body out.
15 * An HTTP 4xx/5xx is a *successful* exchange — the provider's own error text is
16 * what the user needs to read — so it is reported with ok=1 and the status.
17 *
18 * MIT License. Copyright (c) 2026 SCORPIOX INC.
19 */
20
21#if !defined(_WIN32)
22# if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L
23# undef _POSIX_C_SOURCE
24# define _POSIX_C_SOURCE 200809L
25# endif
26# ifndef _DEFAULT_SOURCE
27# define _DEFAULT_SOURCE 1
28# endif
29#endif
30
31#include "ws.h"
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
37#include "sx_http_tls.h"
38
39void ws_resp_free(ws_resp *r)
40{
41 if (!r) return;
42 free(r->body);
43 r->body = NULL;
44 r->body_len = 0;
45}
46
47int ws_http_do(const char *method, const char *url, const char *headers,
48 const char *body, size_t body_len, long timeout_ms,
49 ws_resp *out)
50{
51 sx_url parsed;
52 sx_conn c;
53 char err[512];
54 char *buf = NULL;
55 size_t len = 0;
56 int rc = -1;
57
58 if (!out) return -1;
59 memset(out, 0, sizeof *out);
60 err[0] = '\0';
61
62 if (!url || !*url) {
63 ws_copy(out->error, sizeof out->error, "no URL configured for this provider");
64 return -1;
65 }
66 if (sx_url_parse(url, &parsed, err, sizeof err) != 0) {
67 snprintf(out->error, sizeof out->error, "%s",
68 err[0] ? err : "unsupported URL");
69 return -1;
70 }
71 if (sx_conn_open(&c, &parsed, timeout_ms > 0 ? timeout_ms : 15000,
72 err, sizeof err) != 0) {
73 snprintf(out->error, sizeof out->error, "%s",
74 err[0] ? err : "cannot connect");
75 return -1;
76 }
77 if (sx_http_request(&c, method ? method : "GET", parsed.path, headers, NULL,
78 (body && body_len) ? body : NULL, body_len,
79 timeout_ms > 0 ? timeout_ms : 20000,
80 err, sizeof err) != 0) {
81 snprintf(out->error, sizeof out->error, "%s",
82 err[0] ? err : "request failed");
83 sx_conn_close(&c);
84 return -1;
85 }
86 out->status = (long)c.status;
87
88 if (sx_read_all(&c, &buf, &len, timeout_ms > 0 ? timeout_ms : 20000) != 1) {
89 /* A 204 / empty body legitimately reads as "nothing to read" in some
90 * servers; treat an empty stream as an empty body, not a failure, as
91 * long as we got a status line. */
92 if (len == 0 && buf == NULL) {
93 buf = (char *)malloc(1);
94 if (!buf) {
95 ws_copy(out->error, sizeof out->error, "out of memory");
96 sx_conn_close(&c);
97 return -1;
98 }
99 buf[0] = '\0';
100 len = 0;
101 rc = 0;
102 } else {
103 ws_copy(out->error, sizeof out->error, "cannot read the response body");
104 free(buf);
105 sx_conn_close(&c);
106 return -1;
107 }
108 } else {
109 rc = 0;
110 }
111 sx_conn_close(&c);
112
113 if (!buf) {
114 buf = (char *)malloc(1);
115 if (!buf) {
116 ws_copy(out->error, sizeof out->error, "out of memory");
117 return -1;
118 }
119 buf[0] = '\0';
120 len = 0;
121 }
122 out->body = buf;
123 out->body_len = len;
124 out->ok = 1;
125 return rc;
126}