v0.7.8: WebUI (--web): HTTP/1.1 + SSE front end, engine API v1, tests
A second implementation of cst_agent_hooks_t next to the TUI: the turn runs on a worker thread, the hooks publish events into a ring buffer that SSE clients read, so the engine itself needs no changes. - src/net.c, include/cst_net.h: POSIX/Winsock sockets, non-blocking connect - src/http_srv.c, include/cst_http.h: HTTP/1.1, keep-alive, chunked streaming, body caps (413), Expect: 100-continue, connection cap, exact routing - src/web_md.c, include/cst_web_md.h: streaming markdown -> sanitized HTML (tag/attribute allowlist, javascript: URLs and off-site images dropped) - src/webui.c, include/cst_webui.h: event ring + SSE broadcast, all routes, token (Bearer/Cookie/query) and loopback Host checks, 3 s auto-deny when no client is attached - src/webui_stub.c, CST_WEBUI=0 / CST_BUILD_WEBUI=OFF: build without the WebUI - web/: no-build front end (transcript, approvals, todo, sessions, image upload, status bar) - docs/webui.md: engine API v1 - events, requests, security model - tests/test_webui.c, scripts/e2e_webui.sh: unit tests plus an end-to-end run driven by a recorded SSE stream (no API key needed) - src/main.c: --web --web-port --web-host --web-token --web-dev --web-open and CST_WEB_PORT/HOST/TOKEN/ROOT An empty POST body is now accepted without Content-Length (RFC 9112 6.3) so curl-driven clients can reach cancel/undo/compact/quit; CMakeList's core tests learned about sashidown and test_webui, and CST_WEBUI=0 defines CST_NO_WEBUI.
This commit is contained in:
+684
@@ -0,0 +1,684 @@
|
||||
#ifndef _DEFAULT_SOURCE
|
||||
#define _DEFAULT_SOURCE
|
||||
#endif
|
||||
|
||||
#include "cst_http.h"
|
||||
#include "cst_util.h"
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define HTTP_HDR_MAX 32768 /* one header block */
|
||||
#define HTTP_LINE_MAX 8192 /* one header line */
|
||||
#define HTTP_DEFAULT_BODY (1024 * 1024)
|
||||
#define HTTP_DEFAULT_CONN 32
|
||||
#define HTTP_IDLE_TIMEOUT 60000
|
||||
#define HTTP_READ_TIMEOUT 15000
|
||||
|
||||
struct cst_http_server {
|
||||
os_socket_t listener;
|
||||
int port;
|
||||
pthread_t accept_th;
|
||||
int accept_started;
|
||||
pthread_mutex_t mu;
|
||||
pthread_cond_t cv;
|
||||
int stopping;
|
||||
int nconn;
|
||||
cst_http_conn_t *conns_first;
|
||||
cst_http_opts_t o;
|
||||
cst_http_route_t *routes;
|
||||
int nroutes;
|
||||
};
|
||||
|
||||
struct cst_http_conn {
|
||||
cst_http_server_t *srv;
|
||||
os_socket_t sock;
|
||||
cst_http_req_t req;
|
||||
char rbuf[8192];
|
||||
size_t rlen, rpos;
|
||||
char *body; /* owned while the request is served */
|
||||
char hdrs[HTTP_HDR_MAX]; /* header values are copied here */
|
||||
size_t hdrs_used;
|
||||
int sent;
|
||||
int streaming;
|
||||
int failed;
|
||||
cst_http_conn_t *prev, *next;
|
||||
};
|
||||
|
||||
static const char *status_text(int code) {
|
||||
switch (code) {
|
||||
case 200: return "OK";
|
||||
case 201: return "Created";
|
||||
case 202: return "Accepted";
|
||||
case 204: return "No Content";
|
||||
case 302: return "Found";
|
||||
case 400: return "Bad Request";
|
||||
case 401: return "Unauthorized";
|
||||
case 403: return "Forbidden";
|
||||
case 404: return "Not Found";
|
||||
case 405: return "Method Not Allowed";
|
||||
case 409: return "Conflict";
|
||||
case 411: return "Length Required";
|
||||
case 413: return "Payload Too Large";
|
||||
case 415: return "Unsupported Media Type";
|
||||
case 429: return "Too Many Requests";
|
||||
case 500: return "Internal Server Error";
|
||||
case 503: return "Service Unavailable";
|
||||
default: return "Error";
|
||||
}
|
||||
}
|
||||
|
||||
/* --- reading -------------------------------------------------------------- */
|
||||
|
||||
static int fill(cst_http_conn_t *c, int timeout_ms) {
|
||||
if (c->rpos < c->rlen)
|
||||
return 1;
|
||||
long r = os_net_recv(c->sock, c->rbuf, sizeof(c->rbuf), timeout_ms);
|
||||
if (r <= 0)
|
||||
return (int)r; /* 0 eof, -1 error, -2 timeout */
|
||||
c->rpos = 0;
|
||||
c->rlen = (size_t)r;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int rd_line(cst_http_conn_t *c, char *out, size_t cap, int timeout_ms) {
|
||||
size_t n = 0;
|
||||
for (;;) {
|
||||
int rc = fill(c, timeout_ms);
|
||||
if (rc <= 0)
|
||||
return rc;
|
||||
while (c->rpos < c->rlen) {
|
||||
char ch = c->rbuf[c->rpos++];
|
||||
if (ch == '\n') {
|
||||
if (n && out[n - 1] == '\r')
|
||||
n--;
|
||||
out[n] = 0;
|
||||
return 1;
|
||||
}
|
||||
if (n + 2 >= cap)
|
||||
return -1;
|
||||
out[n++] = ch;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Exactly n bytes, using what the read buffer already holds. malloc'd. */
|
||||
static char *rd_body(cst_http_conn_t *c, size_t n, int timeout_ms) {
|
||||
char *buf = malloc(n + 1);
|
||||
if (!buf)
|
||||
return NULL;
|
||||
size_t got = 0;
|
||||
while (got < n) {
|
||||
if (c->rpos < c->rlen) {
|
||||
size_t take = c->rlen - c->rpos;
|
||||
if (take > n - got)
|
||||
take = n - got;
|
||||
memcpy(buf + got, c->rbuf + c->rpos, take);
|
||||
c->rpos += take;
|
||||
got += take;
|
||||
continue;
|
||||
}
|
||||
long r = os_net_recv(c->sock, c->rbuf, sizeof(c->rbuf), timeout_ms);
|
||||
if (r <= 0) {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
c->rpos = 0;
|
||||
c->rlen = (size_t)r;
|
||||
}
|
||||
buf[n] = 0;
|
||||
return buf;
|
||||
}
|
||||
|
||||
static void url_decode(const char *in, char *out, size_t cap, int plus_space) {
|
||||
size_t o = 0;
|
||||
for (const char *p = in; *p && o + 1 < cap; p++) {
|
||||
if (*p == '%' && p[1] && p[2]) {
|
||||
int hi = p[1] >= '0' && p[1] <= '9' ? p[1] - '0'
|
||||
: p[1] >= 'a' && p[1] <= 'f' ? p[1] - 'a' + 10
|
||||
: p[1] >= 'A' && p[1] <= 'F' ? p[1] - 'A' + 10
|
||||
: -1;
|
||||
int lo = p[2] >= '0' && p[2] <= '9' ? p[2] - '0'
|
||||
: p[2] >= 'a' && p[2] <= 'f' ? p[2] - 'a' + 10
|
||||
: p[2] >= 'A' && p[2] <= 'F' ? p[2] - 'A' + 10
|
||||
: -1;
|
||||
if (hi >= 0 && lo >= 0) {
|
||||
char ch = (char)((hi << 4) | lo);
|
||||
if (ch == 0)
|
||||
return; /* a decoded NUL would truncate the path */
|
||||
out[o++] = ch;
|
||||
p += 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
out[o++] = (*p == '+' && plus_space) ? ' ' : *p;
|
||||
}
|
||||
out[o] = 0;
|
||||
}
|
||||
|
||||
static int key_is(const char *line, const char *key) {
|
||||
size_t n = strlen(key);
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
char a = line[i], b = key[i];
|
||||
if (a >= 'A' && a <= 'Z')
|
||||
a = (char)(a - 'A' + 'a');
|
||||
if (b >= 'A' && b <= 'Z')
|
||||
b = (char)(b - 'A' + 'a');
|
||||
if (a != b || !a)
|
||||
return 0;
|
||||
}
|
||||
return line[n] == ':';
|
||||
}
|
||||
|
||||
/* Copy a header value into the connection (they must not point into the
|
||||
* line buffer, which is reused for the next header). */
|
||||
static const char *store_str(cst_http_conn_t *c, const char *val) {
|
||||
size_t n = strlen(val);
|
||||
if (c->hdrs_used + n + 1 > sizeof(c->hdrs))
|
||||
return NULL;
|
||||
char *dst = c->hdrs + c->hdrs_used;
|
||||
memcpy(dst, val, n + 1);
|
||||
c->hdrs_used += n + 1;
|
||||
return dst;
|
||||
}
|
||||
|
||||
/* 1 = parsed, 0 = the client went away or waited too long, -1 = malformed. */
|
||||
static int read_request(cst_http_conn_t *c, size_t *want_body) {
|
||||
cst_http_server_t *s = c->srv;
|
||||
char line[HTTP_LINE_MAX];
|
||||
cst_http_req_t *r = &c->req;
|
||||
int first = c->rpos >= c->rlen; /* nothing buffered: this is a new client */
|
||||
memset(r, 0, sizeof(*r));
|
||||
c->hdrs_used = 0;
|
||||
*want_body = 0;
|
||||
int rc = rd_line(c, line, sizeof(line),
|
||||
first ? s->o.read_timeout_ms : s->o.idle_timeout_ms);
|
||||
if (rc <= 0)
|
||||
return 0;
|
||||
char *sp1 = strchr(line, ' ');
|
||||
if (!sp1)
|
||||
return -1;
|
||||
*sp1 = 0;
|
||||
char *target = sp1 + 1;
|
||||
char *version = strchr(target, ' ');
|
||||
if (version) {
|
||||
*version++ = 0;
|
||||
} else {
|
||||
version = (char *)"HTTP/1.0";
|
||||
}
|
||||
if (!line[0] || strlen(line) >= sizeof(r->method))
|
||||
return -1;
|
||||
snprintf(r->method, sizeof(r->method), "%s", line);
|
||||
r->keep_alive = strncmp(version, "HTTP/1.", 7) == 0 &&
|
||||
version[7] >= '1' && version[7] <= '9';
|
||||
/* absolute-form ("http://host/path") is legal: keep the path */
|
||||
char *scheme = strstr(target, "://");
|
||||
if (scheme)
|
||||
target = strchr(scheme + 3, '/');
|
||||
if (!target || target[0] != '/')
|
||||
return -1;
|
||||
{
|
||||
char raw[sizeof(r->path)];
|
||||
snprintf(raw, sizeof(raw), "%s", target);
|
||||
char *q = strchr(raw, '?');
|
||||
if (q) {
|
||||
*q = 0;
|
||||
snprintf(r->query, sizeof(r->query), "%s", q + 1);
|
||||
}
|
||||
url_decode(raw, r->path, sizeof(r->path), 0);
|
||||
size_t plen = strlen(r->path);
|
||||
if (plen == 0 || strstr(r->path, ".."))
|
||||
return -1;
|
||||
}
|
||||
size_t hdr_bytes = 0;
|
||||
size_t content_length = 0;
|
||||
int have_length = 0;
|
||||
for (;;) {
|
||||
rc = rd_line(c, line, sizeof(line), s->o.read_timeout_ms);
|
||||
if (rc <= 0)
|
||||
return -1;
|
||||
if (!line[0])
|
||||
break;
|
||||
hdr_bytes += strlen(line) + 2;
|
||||
if (hdr_bytes > HTTP_HDR_MAX)
|
||||
return -1;
|
||||
const char *colon = strchr(line, ':');
|
||||
if (!colon)
|
||||
return -1;
|
||||
const char *val = colon + 1;
|
||||
while (*val == ' ' || *val == '\t')
|
||||
val++;
|
||||
if (key_is(line, "content-length")) {
|
||||
content_length = (size_t)strtoul(val, NULL, 10);
|
||||
have_length = 1;
|
||||
} else if (key_is(line, "transfer-encoding")) {
|
||||
return -1; /* chunked request bodies are not supported */
|
||||
} else if (key_is(line, "connection")) {
|
||||
if (strstr(val, "close"))
|
||||
r->keep_alive = 0;
|
||||
else if (strstr(val, "keep-alive"))
|
||||
r->keep_alive = 1;
|
||||
} else if (key_is(line, "expect")) {
|
||||
const char *cont = "HTTP/1.1 100 Continue\r\n\r\n";
|
||||
os_net_send_all(c->sock, cont, strlen(cont), 5000);
|
||||
} else if (key_is(line, "authorization")) {
|
||||
r->authorization = store_str(c, val);
|
||||
} else if (key_is(line, "cookie")) {
|
||||
r->cookie = store_str(c, val);
|
||||
} else if (key_is(line, "origin")) {
|
||||
r->origin = store_str(c, val);
|
||||
} else if (key_is(line, "host")) {
|
||||
r->host = store_str(c, val);
|
||||
} else if (key_is(line, "content-type")) {
|
||||
r->content_type = store_str(c, val);
|
||||
} else if (key_is(line, "user-agent")) {
|
||||
r->user_agent = store_str(c, val);
|
||||
}
|
||||
}
|
||||
if (have_length)
|
||||
*want_body = content_length;
|
||||
else if (strcmp(r->method, "POST") == 0)
|
||||
/* No Content-Length means no body (RFC 9112 6.3): curl -X POST sends
|
||||
* exactly that, and the zero-body endpoints (cancel, undo, quit) would
|
||||
* otherwise be unreachable from a hand-written client. Bytes that
|
||||
* follow are not ours to guess at, so the connection ends here. */
|
||||
r->keep_alive = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* --- writing -------------------------------------------------------------- */
|
||||
|
||||
static int send_head(cst_http_conn_t *c, int status, const char *ctype,
|
||||
size_t len, int chunked) {
|
||||
cst_buf_t out = {0};
|
||||
cst_buf_printf(&out, "HTTP/1.1 %d %s\r\nContent-Type: %s\r\n",
|
||||
status, status_text(status), ctype ? ctype : "text/plain");
|
||||
cst_buf_puts(&out, "Cache-Control: no-store\r\n");
|
||||
cst_buf_puts(&out, c->req.keep_alive ? "Connection: keep-alive\r\n"
|
||||
: "Connection: close\r\n");
|
||||
if (chunked) {
|
||||
cst_buf_puts(&out, "Transfer-Encoding: chunked\r\n");
|
||||
} else {
|
||||
cst_buf_printf(&out, "Content-Length: %zu\r\n", len);
|
||||
}
|
||||
cst_buf_puts(&out, "\r\n");
|
||||
int rc = out.buf ? os_net_send_all(c->sock, out.buf, out.len, 0) : -1;
|
||||
cst_buf_free(&out);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int cst_http_send(cst_http_conn_t *c, int status, const char *ctype,
|
||||
const void *body, size_t n) {
|
||||
if (!c || c->sent)
|
||||
return -1;
|
||||
c->sent = 1;
|
||||
if (send_head(c, status, ctype, n, 0) != 0) {
|
||||
c->failed = 1;
|
||||
return -1;
|
||||
}
|
||||
if (n && os_net_send_all(c->sock, body, n, 0) != 0) {
|
||||
c->failed = 1;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cst_http_send_str(cst_http_conn_t *c, int status, const char *ctype,
|
||||
const char *s) {
|
||||
return cst_http_send(c, status, ctype, s ? s : "", s ? strlen(s) : 0);
|
||||
}
|
||||
|
||||
int cst_http_send_json(cst_http_conn_t *c, int status, const char *json) {
|
||||
return cst_http_send_str(c, status, "application/json; charset=utf-8",
|
||||
json ? json : "null");
|
||||
}
|
||||
|
||||
int cst_http_send_err(cst_http_conn_t *c, int status, const char *msg) {
|
||||
cst_buf_t b = {0};
|
||||
cst_buf_puts(&b, "{\"error\":\"");
|
||||
for (const char *p = msg ? msg : "error"; *p; p++) {
|
||||
if (*p == '"' || *p == '\\')
|
||||
cst_buf_printf(&b, "\\%c", *p);
|
||||
else if ((unsigned char)*p < 0x20)
|
||||
cst_buf_printf(&b, "\\u%04x", (unsigned char)*p);
|
||||
else
|
||||
cst_buf_append(&b, p, 1);
|
||||
}
|
||||
cst_buf_puts(&b, "\"}");
|
||||
int rc = cst_http_send_json(c, status, b.buf);
|
||||
cst_buf_free(&b);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int cst_http_sent(const cst_http_conn_t *c) { return c ? c->sent : 0; }
|
||||
|
||||
int cst_http_stream_begin(cst_http_conn_t *c, const char *ctype) {
|
||||
if (!c || c->sent)
|
||||
return -1;
|
||||
c->sent = 1;
|
||||
c->streaming = 1;
|
||||
if (send_head(c, 200, ctype, 0, 1) != 0) {
|
||||
c->failed = 1;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cst_http_stream_write(cst_http_conn_t *c, const void *data, size_t n) {
|
||||
if (!c || !c->streaming || c->failed)
|
||||
return -1;
|
||||
if (n == 0)
|
||||
return 0;
|
||||
cst_buf_t b = {0};
|
||||
cst_buf_printf(&b, "%zx\r\n", n);
|
||||
cst_buf_append(&b, data, n);
|
||||
cst_buf_puts(&b, "\r\n");
|
||||
int rc = b.buf ? os_net_send_all(c->sock, b.buf, b.len, 0) : -1;
|
||||
cst_buf_free(&b);
|
||||
if (rc != 0)
|
||||
c->failed = 1;
|
||||
return rc;
|
||||
}
|
||||
|
||||
int cst_http_stream_end(cst_http_conn_t *c) {
|
||||
if (!c || !c->streaming)
|
||||
return -1;
|
||||
c->streaming = 0;
|
||||
if (c->failed)
|
||||
return -1;
|
||||
const char *end = "0\r\n\r\n";
|
||||
if (os_net_send_all(c->sock, end, strlen(end), 0) != 0) {
|
||||
c->failed = 1;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const cst_http_req_t *cst_http_request(cst_http_conn_t *c) {
|
||||
return c ? &c->req : NULL;
|
||||
}
|
||||
|
||||
int cst_http_query(const cst_http_req_t *req, const char *key, char *out,
|
||||
size_t out_sz) {
|
||||
if (!req || !key || !out || out_sz == 0)
|
||||
return -1;
|
||||
size_t klen = strlen(key);
|
||||
const char *p = req->query;
|
||||
while (p && *p) {
|
||||
const char *amp = strchr(p, '&');
|
||||
size_t seg = amp ? (size_t)(amp - p) : strlen(p);
|
||||
const char *eq = memchr(p, '=', seg);
|
||||
size_t name_len = eq ? (size_t)(eq - p) : seg;
|
||||
if (name_len == klen && strncmp(p, key, klen) == 0) {
|
||||
char raw[1024];
|
||||
size_t vlen = eq ? seg - name_len - 1 : 0;
|
||||
if (vlen >= sizeof(raw))
|
||||
vlen = sizeof(raw) - 1;
|
||||
if (eq)
|
||||
memcpy(raw, eq + 1, vlen);
|
||||
raw[vlen] = 0;
|
||||
url_decode(raw, out, out_sz, 1);
|
||||
return 0;
|
||||
}
|
||||
p = amp ? amp + 1 : NULL;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* --- connections ---------------------------------------------------------- */
|
||||
|
||||
static void dispatch(cst_http_server_t *s, cst_http_conn_t *c) {
|
||||
const cst_http_route_t *route = NULL;
|
||||
const cst_http_route_t *fallback = NULL;
|
||||
for (int i = 0; i < s->nroutes; i++) {
|
||||
const cst_http_route_t *r = &s->routes[i];
|
||||
if (strcmp(r->method, "*") != 0 && strcmp(r->method, c->req.method) != 0)
|
||||
continue;
|
||||
if (strcmp(r->path, "*") == 0) {
|
||||
fallback = r;
|
||||
continue;
|
||||
}
|
||||
if (strcmp(r->path, c->req.path) == 0) {
|
||||
route = r;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!route)
|
||||
route = fallback;
|
||||
if (!route) {
|
||||
cst_http_send_err(c, 404, "not found");
|
||||
return;
|
||||
}
|
||||
if (route->auth && s->o.auth && s->o.auth(&c->req, s->o.auth_ud) != 0) {
|
||||
cst_http_send_err(c, 401, "unauthorized");
|
||||
return;
|
||||
}
|
||||
int rc = route->fn(c, route->ud);
|
||||
if (!c->sent)
|
||||
cst_http_send_err(c, rc == 0 ? 500 : 404,
|
||||
rc == 0 ? "handler sent no response" : "not found");
|
||||
}
|
||||
|
||||
static void *conn_main(void *arg) {
|
||||
cst_http_conn_t *c = arg;
|
||||
cst_http_server_t *s = c->srv;
|
||||
for (;;) {
|
||||
size_t want = 0;
|
||||
int rc = read_request(c, &want);
|
||||
if (rc < 0) {
|
||||
/* malformed (bad request line, traversal, unsupported transfer
|
||||
* encoding): say so instead of closing silently */
|
||||
c->sent = 0;
|
||||
c->req.keep_alive = 0;
|
||||
cst_http_send_err(c, 400, "malformed request");
|
||||
break;
|
||||
}
|
||||
if (rc == 0)
|
||||
break;
|
||||
size_t cap = s->o.max_body;
|
||||
for (int i = 0; i < s->nroutes; i++) {
|
||||
const cst_http_route_t *r = &s->routes[i];
|
||||
if (r->max_body && strcmp(r->method, c->req.method) == 0 &&
|
||||
strcmp(r->path, c->req.path) == 0)
|
||||
cap = r->max_body;
|
||||
}
|
||||
if (want > cap) {
|
||||
c->req.keep_alive = 0;
|
||||
cst_http_send_err(c, 413, "request body too large");
|
||||
break;
|
||||
}
|
||||
if (want > 0) {
|
||||
char *body = rd_body(c, want, s->o.read_timeout_ms);
|
||||
if (!body)
|
||||
break;
|
||||
c->body = body;
|
||||
c->req.body = body;
|
||||
c->req.body_len = want;
|
||||
}
|
||||
c->sent = 0;
|
||||
c->streaming = 0;
|
||||
dispatch(s, c);
|
||||
if (s->o.on_log) {
|
||||
char line[512];
|
||||
snprintf(line, sizeof(line), "%s %s -> %d", c->req.method, c->req.path,
|
||||
c->failed ? 499 : (c->sent ? 200 : 500));
|
||||
s->o.on_log(line, s->o.log_ud);
|
||||
}
|
||||
free(c->body);
|
||||
c->body = NULL;
|
||||
c->req.body = NULL;
|
||||
c->req.body_len = 0;
|
||||
c->req.authorization = c->req.cookie = c->req.origin = NULL;
|
||||
c->req.host = c->req.content_type = c->req.user_agent = NULL;
|
||||
if (!c->req.keep_alive || s->stopping || c->failed)
|
||||
break;
|
||||
}
|
||||
os_net_shutdown(c->sock);
|
||||
os_net_close(c->sock);
|
||||
pthread_mutex_lock(&s->mu);
|
||||
if (c->prev)
|
||||
c->prev->next = c->next;
|
||||
else
|
||||
s->conns_first = c->next;
|
||||
if (c->next)
|
||||
c->next->prev = c->prev;
|
||||
s->nconn--;
|
||||
pthread_cond_broadcast(&s->cv);
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
free(c);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void *accept_main(void *arg) {
|
||||
cst_http_server_t *s = arg;
|
||||
while (!s->stopping) {
|
||||
int timed_out = 0;
|
||||
os_socket_t sock = os_net_accept(s->listener, 500, &timed_out);
|
||||
if (sock == OS_SOCKET_BAD)
|
||||
continue;
|
||||
if (s->stopping) {
|
||||
os_net_close(sock);
|
||||
break;
|
||||
}
|
||||
pthread_mutex_lock(&s->mu);
|
||||
int over = s->nconn >= s->o.max_conn;
|
||||
if (!over)
|
||||
s->nconn++;
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
if (over) {
|
||||
const char *busy = "HTTP/1.1 503 Service Unavailable\r\n"
|
||||
"Content-Length: 0\r\nConnection: close\r\n\r\n";
|
||||
os_net_send_all(sock, busy, strlen(busy), 2000);
|
||||
os_net_close(sock);
|
||||
continue;
|
||||
}
|
||||
cst_http_conn_t *c = calloc(1, sizeof(*c));
|
||||
if (!c) {
|
||||
pthread_mutex_lock(&s->mu);
|
||||
s->nconn--;
|
||||
pthread_cond_broadcast(&s->cv);
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
os_net_close(sock);
|
||||
continue;
|
||||
}
|
||||
c->srv = s;
|
||||
c->sock = sock;
|
||||
pthread_mutex_lock(&s->mu);
|
||||
c->next = s->conns_first;
|
||||
if (c->next)
|
||||
c->next->prev = c;
|
||||
s->conns_first = c;
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
pthread_t th;
|
||||
if (pthread_create(&th, NULL, conn_main, c) != 0) {
|
||||
pthread_mutex_lock(&s->mu);
|
||||
s->conns_first = c->next;
|
||||
if (c->next)
|
||||
c->next->prev = NULL;
|
||||
s->nconn--;
|
||||
pthread_cond_broadcast(&s->cv);
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
os_net_close(sock);
|
||||
free(c);
|
||||
continue;
|
||||
}
|
||||
pthread_detach(th);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cst_http_server_t *cst_http_start(const cst_http_opts_t *o, char *err,
|
||||
size_t err_sz) {
|
||||
if (!o)
|
||||
return NULL;
|
||||
if (os_net_init() != 0) {
|
||||
if (err)
|
||||
snprintf(err, err_sz, "network init failed");
|
||||
return NULL;
|
||||
}
|
||||
cst_http_server_t *s = calloc(1, sizeof(*s));
|
||||
if (!s)
|
||||
return NULL;
|
||||
s->o = *o;
|
||||
s->o.max_body = o->max_body ? o->max_body : HTTP_DEFAULT_BODY;
|
||||
s->o.max_conn = o->max_conn ? o->max_conn : HTTP_DEFAULT_CONN;
|
||||
if (!s->o.read_timeout_ms)
|
||||
s->o.read_timeout_ms = HTTP_READ_TIMEOUT;
|
||||
if (!s->o.idle_timeout_ms)
|
||||
s->o.idle_timeout_ms = HTTP_IDLE_TIMEOUT;
|
||||
if (o->nroutes > 0) {
|
||||
s->routes = calloc((size_t)o->nroutes, sizeof(*s->routes));
|
||||
if (!s->routes) {
|
||||
free(s);
|
||||
return NULL;
|
||||
}
|
||||
memcpy(s->routes, o->routes, (size_t)o->nroutes * sizeof(*s->routes));
|
||||
s->nroutes = o->nroutes;
|
||||
}
|
||||
pthread_mutex_init(&s->mu, NULL);
|
||||
pthread_cond_init(&s->cv, NULL);
|
||||
s->listener = os_net_listen(o->host, o->port, err, err_sz);
|
||||
if (s->listener == OS_SOCKET_BAD) {
|
||||
free(s->routes);
|
||||
free(s);
|
||||
return NULL;
|
||||
}
|
||||
s->port = os_net_port(s->listener);
|
||||
if (pthread_create(&s->accept_th, NULL, accept_main, s) != 0) {
|
||||
os_net_close(s->listener);
|
||||
free(s->routes);
|
||||
free(s);
|
||||
if (err)
|
||||
snprintf(err, err_sz, "cannot start the accept thread");
|
||||
return NULL;
|
||||
}
|
||||
s->accept_started = 1;
|
||||
return s;
|
||||
}
|
||||
|
||||
int cst_http_port(const cst_http_server_t *s) { return s ? s->port : 0; }
|
||||
|
||||
int cst_http_stopping(const cst_http_server_t *s) {
|
||||
return s ? s->stopping : 1;
|
||||
}
|
||||
|
||||
void cst_http_stop(cst_http_server_t *s) {
|
||||
if (!s)
|
||||
return;
|
||||
pthread_mutex_lock(&s->mu);
|
||||
s->stopping = 1;
|
||||
for (cst_http_conn_t *c = s->conns_first; c; c = c->next)
|
||||
os_net_shutdown(c->sock);
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
os_net_shutdown(s->listener);
|
||||
if (s->accept_started) {
|
||||
pthread_join(s->accept_th, NULL);
|
||||
s->accept_started = 0;
|
||||
}
|
||||
/* Handler threads own their connection until they return; the shutdown
|
||||
* above is what wakes them. Bounded wait so free() stays safe. */
|
||||
pthread_mutex_lock(&s->mu);
|
||||
for (int i = 0; i < 300 && s->nconn > 0; i++) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
ts.tv_nsec += 100000000; /* 100 ms */
|
||||
if (ts.tv_nsec >= 1000000000) {
|
||||
ts.tv_sec++;
|
||||
ts.tv_nsec -= 1000000000;
|
||||
}
|
||||
pthread_cond_timedwait(&s->cv, &s->mu, &ts);
|
||||
}
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
os_net_close(s->listener);
|
||||
}
|
||||
|
||||
void cst_http_free(cst_http_server_t *s) {
|
||||
if (!s)
|
||||
return;
|
||||
pthread_mutex_destroy(&s->mu);
|
||||
pthread_cond_destroy(&s->cv);
|
||||
free(s->routes);
|
||||
free(s);
|
||||
}
|
||||
+53
-3
@@ -3,6 +3,7 @@
|
||||
#include "cst_models.h"
|
||||
#include "cst_tools.h"
|
||||
#include "cst_version.h"
|
||||
#include "cst_webui.h"
|
||||
#include "os_compat.h"
|
||||
|
||||
#include <curl/curl.h>
|
||||
@@ -41,6 +42,13 @@ static void usage(const char *argv0) {
|
||||
" -T, --tools PROFILE default (all tools) | minimal (paper set)\n"
|
||||
" -n, --max-steps N model calls per turn (30)\n"
|
||||
" -H, --headless -p TEXT run one prompt without the TUI\n"
|
||||
" --web serve the UI over HTTP on 127.0.0.1:8787\n"
|
||||
" --web-port N port (0 = pick a free one)\n"
|
||||
" --web-host ADDR bind address (default 127.0.0.1; a\n"
|
||||
" non-loopback address requires a token)\n"
|
||||
" --web-token T require ?token=T / Authorization: Bearer T\n"
|
||||
" --web-dev DIR serve the UI from DIR (development)\n"
|
||||
" --web-open open the browser at startup\n"
|
||||
" -a, --ask-key enter and save the provider's API key\n"
|
||||
" -k, --api-key KEY use KEY for this run only (not saved)\n"
|
||||
" -b, --api-base URL OpenAI-compatible base URL (…/v1)\n"
|
||||
@@ -320,6 +328,16 @@ int main(int argc, char **argv) {
|
||||
const char *prompt = NULL;
|
||||
int new_session = 0, do_list = 0;
|
||||
const char *resume_id = NULL;
|
||||
/* WebUI (--web): a second front end on the same engine. */
|
||||
int web_mode = 0;
|
||||
cst_webui_opts_t web;
|
||||
memset(&web, 0, sizeof(web));
|
||||
const char *env = getenv("CST_WEB_PORT");
|
||||
if (env && env[0])
|
||||
web.port = atoi(env);
|
||||
web.host = getenv("CST_WEB_HOST");
|
||||
web.token = getenv("CST_WEB_TOKEN");
|
||||
web.root = getenv("CST_WEB_ROOT");
|
||||
|
||||
static struct option opts[] = {
|
||||
{"workspace", required_argument, 0, 'w'},
|
||||
@@ -346,6 +364,12 @@ int main(int argc, char **argv) {
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"version", no_argument, 0, 'V'},
|
||||
{"list-models", optional_argument, 0, 0x100},
|
||||
{"web", no_argument, 0, 0x101},
|
||||
{"web-port", required_argument, 0, 0x102},
|
||||
{"web-host", required_argument, 0, 0x103},
|
||||
{"web-token", required_argument, 0, 0x104},
|
||||
{"web-dev", required_argument, 0, 0x105},
|
||||
{"web-open", no_argument, 0, 0x106},
|
||||
{0, 0, 0, 0}};
|
||||
int c;
|
||||
while ((c = getopt_long(argc, argv, "w:s:b:k:m:P:r:n:T:Hp:dahVNcR:LyA:C:e:", opts, NULL)) !=
|
||||
@@ -453,6 +477,29 @@ int main(int argc, char **argv) {
|
||||
curl_global_cleanup();
|
||||
return rc;
|
||||
}
|
||||
case 0x101:
|
||||
web_mode = 1;
|
||||
break;
|
||||
case 0x102:
|
||||
web.port = atoi(optarg);
|
||||
web_mode = 1;
|
||||
break;
|
||||
case 0x103:
|
||||
web.host = optarg;
|
||||
web_mode = 1;
|
||||
break;
|
||||
case 0x104:
|
||||
web.token = optarg;
|
||||
web_mode = 1;
|
||||
break;
|
||||
case 0x105:
|
||||
web.root = optarg;
|
||||
web_mode = 1;
|
||||
break;
|
||||
case 0x106:
|
||||
web.open_browser = 1;
|
||||
web_mode = 1;
|
||||
break;
|
||||
case 'V':
|
||||
printf("cst-agent %s\n", CST_VERSION);
|
||||
curl_global_cleanup();
|
||||
@@ -600,7 +647,7 @@ int main(int argc, char **argv) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (!cfg.api_key[0] && cfg.headless) {
|
||||
if (!cfg.api_key[0] && (cfg.headless || web_mode)) {
|
||||
fprintf(stderr,
|
||||
"no API key for %s: start cst-agent once to enter it, or run "
|
||||
"cst-agent --ask-key --provider %s\n",
|
||||
@@ -610,7 +657,7 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
/* Router presets have no fixed model: take the first one the API lists
|
||||
* (the TUI does the same after its /models probe). */
|
||||
if (!cfg.model[0] && cfg.headless) {
|
||||
if (!cfg.model[0] && (cfg.headless || web_mode)) {
|
||||
static cst_model_meta_t list[512];
|
||||
char err[256] = "";
|
||||
long age = 0;
|
||||
@@ -654,7 +701,10 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
int rc = 0;
|
||||
if (cfg.headless) {
|
||||
if (web_mode) {
|
||||
web.prompt = prompt; /* --web -p "...": run one turn at startup */
|
||||
rc = cst_webui_run(agent, &web);
|
||||
} else if (cfg.headless) {
|
||||
if (!prompt) {
|
||||
fprintf(stderr, "--headless requires --prompt\n");
|
||||
rc = 1;
|
||||
|
||||
@@ -0,0 +1,319 @@
|
||||
#ifndef _DEFAULT_SOURCE
|
||||
#define _DEFAULT_SOURCE
|
||||
#endif
|
||||
|
||||
#include "cst_net.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <signal.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifndef MSG_NOSIGNAL
|
||||
#define MSG_NOSIGNAL 0
|
||||
#endif
|
||||
|
||||
#define NET_SEND_TIMEOUT_MS 20000
|
||||
|
||||
static void set_err(char *err, size_t err_sz, const char *what) {
|
||||
if (err && err_sz)
|
||||
snprintf(err, err_sz, "%s", what);
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
static void set_err_win(char *err, size_t err_sz, const char *what) {
|
||||
if (err && err_sz)
|
||||
snprintf(err, err_sz, "%s (winsock error %d)", what, WSAGetLastError());
|
||||
}
|
||||
#define SET_ERR(e, n, w) set_err_win(e, n, w)
|
||||
#define NET_FD(s) ((SOCKET)(s))
|
||||
#define NET_NFDS(s) (0) /* Winsock ignores nfds */
|
||||
static void net_close_raw(os_socket_t s) { closesocket((SOCKET)s); }
|
||||
static int net_last_would_block(void) {
|
||||
int e = WSAGetLastError();
|
||||
return e == WSAEWOULDBLOCK || e == WSAEINTR;
|
||||
}
|
||||
static int net_last_conn_reset(void) {
|
||||
int e = WSAGetLastError();
|
||||
return e == WSAECONNRESET || e == WSAECONNABORTED || e == WSAENOTSOCK;
|
||||
}
|
||||
#else
|
||||
#define SET_ERR(e, n, w) set_err(e, n, w)
|
||||
#define NET_FD(s) ((int)(s))
|
||||
#define NET_NFDS(s) ((int)((s) + 1))
|
||||
static void net_close_raw(os_socket_t s) { close(s); }
|
||||
static int net_last_would_block(void) { return errno == EINTR; }
|
||||
static int net_last_conn_reset(void) {
|
||||
return errno == ECONNRESET || errno == EPIPE || errno == EBADF;
|
||||
}
|
||||
#endif
|
||||
|
||||
int os_net_init(void) {
|
||||
#ifdef _WIN32
|
||||
WSADATA w;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &w) != 0)
|
||||
return -1;
|
||||
#else
|
||||
/* A hung-up client must not kill the process with SIGPIPE. */
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
void os_net_cleanup(void) {
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
}
|
||||
|
||||
static int wait_io(os_socket_t s, int want_write, int timeout_ms) {
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(NET_FD(s), &fds);
|
||||
struct timeval tv;
|
||||
tv.tv_sec = timeout_ms / 1000;
|
||||
tv.tv_usec = (timeout_ms % 1000) * 1000;
|
||||
int rc = select(NET_NFDS(s), want_write ? NULL : &fds,
|
||||
want_write ? &fds : NULL, NULL, timeout_ms < 0 ? NULL : &tv);
|
||||
if (rc > 0)
|
||||
return 1;
|
||||
if (rc == 0)
|
||||
return 0; /* timed out */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Host to bind: AF_INET unless the name is an IPv6 literal, so "localhost"
|
||||
* and "127.0.0.1" always mean the IPv4 loopback a browser will reach. */
|
||||
static int resolve_bind(const char *host, int port, struct sockaddr_storage *sa,
|
||||
socklen_t *sa_len) {
|
||||
memset(sa, 0, sizeof(*sa));
|
||||
int family = AF_INET;
|
||||
const char *h = host && host[0] ? host : "127.0.0.1";
|
||||
if (strchr(h, ':'))
|
||||
family = AF_INET6;
|
||||
struct addrinfo hints, *res = NULL;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = family;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
char portstr[16];
|
||||
snprintf(portstr, sizeof(portstr), "%d", port);
|
||||
if (getaddrinfo(h, portstr, &hints, &res) != 0 || !res)
|
||||
return -1;
|
||||
memcpy(sa, res->ai_addr, res->ai_addrlen);
|
||||
*sa_len = (socklen_t)res->ai_addrlen;
|
||||
freeaddrinfo(res);
|
||||
return 0;
|
||||
}
|
||||
|
||||
os_socket_t os_net_listen(const char *host, int port, char *err, size_t err_sz) {
|
||||
struct sockaddr_storage sa;
|
||||
socklen_t sa_len;
|
||||
if (resolve_bind(host, port, &sa, &sa_len) != 0) {
|
||||
SET_ERR(err, err_sz, "cannot resolve the bind address");
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
os_socket_t s = (os_socket_t)socket(sa.ss_family, SOCK_STREAM, 0);
|
||||
if (s == OS_SOCKET_BAD) {
|
||||
SET_ERR(err, err_sz, "cannot create a socket");
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
int one = 1;
|
||||
setsockopt(NET_FD(s), SOL_SOCKET, SO_REUSEADDR, (const char *)&one,
|
||||
sizeof(one));
|
||||
#ifdef IPV6_V6ONLY
|
||||
if (sa.ss_family == AF_INET6)
|
||||
setsockopt(NET_FD(s), IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&one,
|
||||
sizeof(one));
|
||||
#endif
|
||||
if (bind(NET_FD(s), (const struct sockaddr *)&sa, sa_len) != 0) {
|
||||
SET_ERR(err, err_sz, "the address is already in use");
|
||||
net_close_raw(s);
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
if (listen(NET_FD(s), 32) != 0) {
|
||||
SET_ERR(err, err_sz, "listen failed");
|
||||
net_close_raw(s);
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
int os_net_port(os_socket_t s) {
|
||||
struct sockaddr_storage sa;
|
||||
socklen_t len = sizeof(sa);
|
||||
if (getsockname(NET_FD(s), (struct sockaddr *)&sa, &len) != 0)
|
||||
return 0;
|
||||
if (sa.ss_family == AF_INET)
|
||||
return ntohs(((struct sockaddr_in *)&sa)->sin_port);
|
||||
if (sa.ss_family == AF_INET6)
|
||||
return ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void os_net_peer(os_socket_t s, char *out, size_t out_sz) {
|
||||
if (!out || out_sz == 0)
|
||||
return;
|
||||
out[0] = 0;
|
||||
struct sockaddr_storage sa;
|
||||
socklen_t len = sizeof(sa);
|
||||
if (getpeername(NET_FD(s), (struct sockaddr *)&sa, &len) != 0)
|
||||
return;
|
||||
char ip[64] = "";
|
||||
int port = 0;
|
||||
if (sa.ss_family == AF_INET) {
|
||||
if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&sa)->sin_addr, ip,
|
||||
sizeof(ip)))
|
||||
return;
|
||||
port = ntohs(((struct sockaddr_in *)&sa)->sin_port);
|
||||
} else if (sa.ss_family == AF_INET6) {
|
||||
if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&sa)->sin6_addr, ip,
|
||||
sizeof(ip)))
|
||||
return;
|
||||
port = ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
if (strchr(ip, ':'))
|
||||
snprintf(out, out_sz, "[%s]:%d", ip, port);
|
||||
else
|
||||
snprintf(out, out_sz, "%s:%d", ip, port);
|
||||
}
|
||||
|
||||
os_socket_t os_net_connect(const char *host, int port, int timeout_ms) {
|
||||
const char *h = host && host[0] ? host : "127.0.0.1";
|
||||
struct addrinfo hints, *res = NULL;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = strchr(h, ':') ? AF_INET6 : AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
char portstr[16];
|
||||
snprintf(portstr, sizeof(portstr), "%d", port);
|
||||
if (getaddrinfo(h, portstr, &hints, &res) != 0 || !res)
|
||||
return OS_SOCKET_BAD;
|
||||
os_socket_t s = (os_socket_t)socket(res->ai_family, SOCK_STREAM, 0);
|
||||
if (s == OS_SOCKET_BAD) {
|
||||
freeaddrinfo(res);
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
/* Bounded wait: connect() non-blocking, poll, then read SO_ERROR. A dead
|
||||
* server must not hang the caller. */
|
||||
#ifdef _WIN32
|
||||
u_long nb = 1;
|
||||
ioctlsocket(NET_FD(s), FIONBIO, &nb);
|
||||
#else
|
||||
int fl = fcntl(s, F_GETFL, 0);
|
||||
fcntl(s, F_SETFL, fl | O_NONBLOCK);
|
||||
#endif
|
||||
int rc = connect(NET_FD(s), res->ai_addr, (int)res->ai_addrlen);
|
||||
freeaddrinfo(res);
|
||||
if (rc != 0) {
|
||||
#ifdef _WIN32
|
||||
int in_progress = WSAGetLastError() == WSAEWOULDBLOCK;
|
||||
#else
|
||||
int in_progress = errno == EINPROGRESS || errno == EINTR;
|
||||
#endif
|
||||
if (!in_progress || wait_io(s, 1, timeout_ms) <= 0) {
|
||||
net_close_raw(s);
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
int soerr = 0;
|
||||
socklen_t elen = sizeof(soerr);
|
||||
if (getsockopt(NET_FD(s), SOL_SOCKET, SO_ERROR, (char *)&soerr, &elen) != 0 ||
|
||||
soerr != 0) {
|
||||
net_close_raw(s);
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
}
|
||||
#ifdef _WIN32
|
||||
nb = 0;
|
||||
ioctlsocket(NET_FD(s), FIONBIO, &nb);
|
||||
#else
|
||||
fcntl(s, F_SETFL, fl);
|
||||
#endif
|
||||
return s;
|
||||
}
|
||||
|
||||
os_socket_t os_net_accept(os_socket_t s, int timeout_ms, int *timed_out) {
|
||||
if (timed_out)
|
||||
*timed_out = 0;
|
||||
int w = wait_io(s, 0, timeout_ms);
|
||||
if (w == 0) {
|
||||
if (timed_out)
|
||||
*timed_out = 1;
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
if (w < 0)
|
||||
return OS_SOCKET_BAD;
|
||||
os_socket_t c = (os_socket_t)accept(NET_FD(s), NULL, NULL);
|
||||
if (c == OS_SOCKET_BAD)
|
||||
return OS_SOCKET_BAD;
|
||||
int one = 1;
|
||||
setsockopt(NET_FD(c), IPPROTO_TCP, TCP_NODELAY, (const char *)&one,
|
||||
sizeof(one));
|
||||
return c;
|
||||
}
|
||||
|
||||
long os_net_recv(os_socket_t s, void *buf, size_t n, int timeout_ms) {
|
||||
int w = wait_io(s, 0, timeout_ms);
|
||||
if (w == 0)
|
||||
return -2;
|
||||
if (w < 0)
|
||||
return -1;
|
||||
size_t want = n > (size_t)(1 << 20) ? (size_t)(1 << 20) : n;
|
||||
int r = (int)recv(NET_FD(s), (char *)buf, (int)want, 0);
|
||||
if (r == 0)
|
||||
return 0;
|
||||
if (r < 0)
|
||||
return net_last_conn_reset() ? 0 : (net_last_would_block() ? -2 : -1);
|
||||
return r;
|
||||
}
|
||||
|
||||
int os_net_send_all(os_socket_t s, const void *buf, size_t n, int timeout_ms) {
|
||||
const char *p = buf;
|
||||
size_t off = 0;
|
||||
while (off < n) {
|
||||
int w = wait_io(s, 1, timeout_ms ? timeout_ms : NET_SEND_TIMEOUT_MS);
|
||||
if (w <= 0)
|
||||
return -1;
|
||||
long r = (long)send(NET_FD(s), p + off, (int)(n - off), MSG_NOSIGNAL);
|
||||
if (r <= 0) {
|
||||
if (net_last_would_block())
|
||||
continue;
|
||||
return -1;
|
||||
}
|
||||
off += (size_t)r;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void os_net_shutdown(os_socket_t s) {
|
||||
#ifdef _WIN32
|
||||
shutdown((SOCKET)s, SD_BOTH);
|
||||
#else
|
||||
shutdown(s, SHUT_RDWR);
|
||||
#endif
|
||||
}
|
||||
|
||||
void os_net_close(os_socket_t s) {
|
||||
if (s == OS_SOCKET_BAD)
|
||||
return;
|
||||
net_close_raw(s);
|
||||
}
|
||||
+319
@@ -0,0 +1,319 @@
|
||||
#ifndef _DEFAULT_SOURCE
|
||||
#define _DEFAULT_SOURCE
|
||||
#endif
|
||||
|
||||
#include "cst_web_md.h"
|
||||
|
||||
#include "cst_util.h"
|
||||
#include "sashidown.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct cst_web_md {
|
||||
sashidown_stream_t *st;
|
||||
cst_web_md_emit_fn emit;
|
||||
void *ud;
|
||||
cst_buf_t in; /* HTML from sashidown, waiting for a complete tag */
|
||||
int failed;
|
||||
};
|
||||
|
||||
/* --- sanitizer ------------------------------------------------------------ */
|
||||
|
||||
static const char *ALLOWED_TAGS[] = {
|
||||
"p", "br", "hr", "h1", "h2", "h3", "h4",
|
||||
"h5", "h6", "strong", "em", "b", "i", "u",
|
||||
"s", "del", "code", "pre", "blockquote", "ul", "ol",
|
||||
"li", "table", "thead", "tbody", "tr", "th", "td",
|
||||
"a", "img", "span", "div", NULL};
|
||||
|
||||
static int tag_allowed(const char *name, size_t n) {
|
||||
for (int i = 0; ALLOWED_TAGS[i]; i++) {
|
||||
if (strlen(ALLOWED_TAGS[i]) == n && strncmp(name, ALLOWED_TAGS[i], n) == 0)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* A URL we are willing to put in href: http(s), mailto, or a same-site
|
||||
* path. Anything else (javascript:, data:, file:, //evil) is dropped. */
|
||||
static int href_ok(const char *v, size_t n) {
|
||||
if (n == 0)
|
||||
return 0;
|
||||
if (!strncmp(v, "http://", 7) || !strncmp(v, "https://", 8) ||
|
||||
!strncmp(v, "mailto:", 7))
|
||||
return 1;
|
||||
if (v[0] == '/' && !(n > 1 && (v[1] == '/' || v[1] == '\\')))
|
||||
return 1;
|
||||
if (v[0] == '#')
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int src_ok(const char *v, size_t n) {
|
||||
if (n > 11 && !strncmp(v, "data:image/", 11))
|
||||
return 1;
|
||||
if (n > 12 && !strncmp(v, "/api/attach/", 12))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void attr_append(cst_buf_t *b, const char *name, size_t nlen,
|
||||
const char *val, size_t vlen) {
|
||||
cst_buf_puts(b, " ");
|
||||
cst_buf_append(b, name, nlen);
|
||||
cst_buf_puts(b, "=\"");
|
||||
cst_buf_append(b, val, vlen);
|
||||
cst_buf_puts(b, "\"");
|
||||
}
|
||||
|
||||
/* Rewrite one tag (open, close or self-closing) into out; may emit nothing
|
||||
* when the tag is not allowed. Returns 0, or -1 on OOM. */
|
||||
static int sanitize_tag(const char *tag, size_t len, cst_buf_t *out) {
|
||||
/* <name ...> or </name> or <!-- ... --> */
|
||||
size_t i = 0;
|
||||
int closing = 0;
|
||||
if (len < 3 || tag[0] != '<')
|
||||
return 0;
|
||||
i = 1;
|
||||
if (i < len && tag[i] == '/') {
|
||||
closing = 1;
|
||||
i++;
|
||||
}
|
||||
size_t name_start = i;
|
||||
while (i < len && ((tag[i] >= 'a' && tag[i] <= 'z') ||
|
||||
(tag[i] >= 'A' && tag[i] <= 'Z') ||
|
||||
(tag[i] >= '0' && tag[i] <= '9')))
|
||||
i++;
|
||||
size_t name_len = i - name_start;
|
||||
if (name_len == 0 || !tag_allowed(tag + name_start, name_len))
|
||||
return 0; /* comments, <script>, <iframe>: dropped entirely */
|
||||
int is_img = name_len == 3 && strncmp(tag + name_start, "img", 3) == 0;
|
||||
int is_a = name_len == 1 && tag[name_start] == 'a';
|
||||
int self_closing = len > 2 && tag[len - 2] == '/';
|
||||
size_t start = out->len;
|
||||
cst_buf_puts(out, "<");
|
||||
if (closing)
|
||||
cst_buf_puts(out, "/");
|
||||
cst_buf_append(out, tag + name_start, name_len);
|
||||
if (!closing) {
|
||||
/* copy the allowed attributes */
|
||||
int keep_target = 0; /* a link is only useful with a usable href */
|
||||
size_t p = name_start + name_len;
|
||||
while (p < len && tag[p] != '>' && !(tag[p] == '/' && p + 1 < len &&
|
||||
tag[p + 1] == '>')) {
|
||||
while (p < len && (tag[p] == ' ' || tag[p] == '\t' || tag[p] == '\n'))
|
||||
p++;
|
||||
size_t an = p;
|
||||
while (p < len && tag[p] != '=' && tag[p] != ' ' && tag[p] != '\t' &&
|
||||
tag[p] != '\n' && tag[p] != '>' && tag[p] != '/')
|
||||
p++;
|
||||
size_t an_len = p - an;
|
||||
if (an_len == 0) {
|
||||
p++;
|
||||
continue;
|
||||
}
|
||||
const char *aval = "";
|
||||
size_t av_len = 0;
|
||||
if (p < len && tag[p] == '=') {
|
||||
p++;
|
||||
char quote = 0;
|
||||
if (p < len && (tag[p] == '"' || tag[p] == '\''))
|
||||
quote = tag[p++];
|
||||
size_t as = p;
|
||||
while (p < len &&
|
||||
(quote ? tag[p] != quote : (tag[p] != ' ' && tag[p] != '>')))
|
||||
p++;
|
||||
aval = tag + as;
|
||||
av_len = p - as;
|
||||
if (quote && p < len)
|
||||
p++; /* closing quote */
|
||||
}
|
||||
int keep = 0;
|
||||
if (is_a && an_len == 4 && strncmp(tag + an, "href", 4) == 0) {
|
||||
keep = href_ok(aval, av_len);
|
||||
keep_target = keep;
|
||||
} else if (is_img && an_len == 3 && strncmp(tag + an, "src", 3) == 0) {
|
||||
keep = src_ok(aval, av_len);
|
||||
keep_target = keep;
|
||||
} else if (is_img && an_len == 3 && strncmp(tag + an, "alt", 3) == 0)
|
||||
keep = 1;
|
||||
else if (an_len == 5 && strncmp(tag + an, "class", 5) == 0 &&
|
||||
av_len > 9 && !strncmp(aval, "language-", 9))
|
||||
keep = 1;
|
||||
if (keep)
|
||||
attr_append(out, tag + an, an_len, aval, av_len);
|
||||
}
|
||||
if (is_a && keep_target) {
|
||||
cst_buf_puts(out, " target=\"_blank\" rel=\"noopener noreferrer\"");
|
||||
}
|
||||
if ((is_a && !keep_target) || (is_img && !keep_target)) {
|
||||
/* a link with an unusable href, or an image from nowhere: drop the
|
||||
* whole tag and keep only its text */
|
||||
out->len = start;
|
||||
if (out->buf)
|
||||
out->buf[start] = 0;
|
||||
return 0;
|
||||
}
|
||||
cst_buf_puts(out, self_closing ? " /" : "");
|
||||
}
|
||||
cst_buf_puts(out, ">");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Emit the text before every '<' and every tag through the sanitizer. */
|
||||
static void scan_chunk(cst_web_md_t *s, int final) {
|
||||
if (!s->emit || s->failed)
|
||||
return;
|
||||
size_t i = 0, emitted = 0;
|
||||
while (i < s->in.len) {
|
||||
if (s->in.buf[i] != '<') {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
size_t close = i;
|
||||
while (close < s->in.len && s->in.buf[close] != '>')
|
||||
close++;
|
||||
if (close >= s->in.len && !final)
|
||||
break; /* the tag may still be growing: keep it buffered */
|
||||
if (i > emitted && s->emit(s->in.buf + emitted, i - emitted, s->ud) != 0)
|
||||
s->failed = 1;
|
||||
size_t tag_len = close >= s->in.len ? s->in.len - i : close - i + 1;
|
||||
cst_buf_t tag = {0};
|
||||
if (sanitize_tag(s->in.buf + i, tag_len, &tag) != 0) {
|
||||
cst_buf_free(&tag);
|
||||
s->failed = 1;
|
||||
return;
|
||||
}
|
||||
if (tag.len && s->emit(tag.buf, tag.len, s->ud) != 0)
|
||||
s->failed = 1;
|
||||
cst_buf_free(&tag);
|
||||
i = i + tag_len;
|
||||
emitted = i;
|
||||
if (s->failed)
|
||||
break;
|
||||
}
|
||||
if (emitted > 0) {
|
||||
memmove(s->in.buf, s->in.buf + emitted, s->in.len - emitted);
|
||||
s->in.len -= emitted;
|
||||
s->in.buf[s->in.len] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Collector used by the one-shot sanitizer. */
|
||||
typedef struct {
|
||||
cst_buf_t *b;
|
||||
int failed;
|
||||
} md_collect_t;
|
||||
|
||||
static int collect_emit(const char *html, size_t len, void *ud) {
|
||||
md_collect_t *c = ud;
|
||||
if (cst_buf_append(c->b, html, len) != 0) {
|
||||
c->failed = 1;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *cst_web_md_sanitize(const char *html, size_t len) {
|
||||
cst_web_md_t tmp;
|
||||
memset(&tmp, 0, sizeof(tmp));
|
||||
cst_buf_t out = {0};
|
||||
md_collect_t col = {&out, 0};
|
||||
if (cst_buf_append(&tmp.in, html ? html : "", len) != 0)
|
||||
return NULL;
|
||||
tmp.emit = collect_emit;
|
||||
tmp.ud = &col;
|
||||
scan_chunk(&tmp, 1);
|
||||
cst_buf_free(&tmp.in);
|
||||
if (col.failed) {
|
||||
cst_buf_free(&out);
|
||||
return NULL;
|
||||
}
|
||||
return cst_buf_steal(&out);
|
||||
}
|
||||
|
||||
/* --- sashidown glue ------------------------------------------------------- */
|
||||
|
||||
static int on_sashidown_html(const char *html, size_t len, void *user) {
|
||||
cst_web_md_t *s = user;
|
||||
if (cst_buf_append(&s->in, html, len) != 0) {
|
||||
s->failed = 1;
|
||||
return 1;
|
||||
}
|
||||
scan_chunk(s, 0);
|
||||
return s->failed ? 1 : 0;
|
||||
}
|
||||
|
||||
cst_web_md_t *cst_web_md_new(cst_web_md_emit_fn emit, void *ud) {
|
||||
cst_web_md_t *s = calloc(1, sizeof(*s));
|
||||
if (!s)
|
||||
return NULL;
|
||||
sashidown_stream_opts_t opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.on_html = on_sashidown_html;
|
||||
opts.user = s;
|
||||
s->st = sashidown_stream_create(&opts);
|
||||
if (!s->st) {
|
||||
free(s);
|
||||
return NULL;
|
||||
}
|
||||
s->emit = emit;
|
||||
s->ud = ud;
|
||||
return s;
|
||||
}
|
||||
|
||||
int cst_web_md_feed(cst_web_md_t *s, const char *data, size_t len) {
|
||||
if (!s || !s->st)
|
||||
return -1;
|
||||
if (len && sashidown_stream_feed(s->st, data, len) != 0) {
|
||||
s->failed = 1;
|
||||
return -1;
|
||||
}
|
||||
return s->failed ? -1 : 0;
|
||||
}
|
||||
|
||||
int cst_web_md_finish(cst_web_md_t *s) {
|
||||
if (!s || !s->st)
|
||||
return -1;
|
||||
if (sashidown_stream_finish(s->st) != 0) {
|
||||
s->failed = 1;
|
||||
return -1;
|
||||
}
|
||||
scan_chunk(s, 1);
|
||||
return s->failed ? -1 : 0;
|
||||
}
|
||||
|
||||
void cst_web_md_free(cst_web_md_t *s) {
|
||||
if (!s)
|
||||
return;
|
||||
if (s->st)
|
||||
sashidown_stream_destroy(s->st);
|
||||
cst_buf_free(&s->in);
|
||||
free(s);
|
||||
}
|
||||
|
||||
char *cst_web_md_html(const char *md, size_t len) {
|
||||
if (!md)
|
||||
return NULL;
|
||||
size_t cap = 65536;
|
||||
char *out = NULL;
|
||||
for (int attempt = 0; attempt < 8; attempt++) {
|
||||
char *buf = malloc(cap);
|
||||
if (!buf)
|
||||
return NULL;
|
||||
size_t out_len = 0;
|
||||
if (sashidown_convert(md, len, buf, cap, &out_len, SASHIDOWN_FMT_HTML) ==
|
||||
0) {
|
||||
out = cst_web_md_sanitize(buf, out_len);
|
||||
free(buf);
|
||||
return out;
|
||||
}
|
||||
free(buf);
|
||||
if (cap >= (size_t)8 * 1024 * 1024)
|
||||
break;
|
||||
cap *= 2;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
+1257
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,15 @@
|
||||
#include "cst_agent.h"
|
||||
#include "cst_webui.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef CST_NO_WEBUI
|
||||
int cst_webui_run(cst_agent_t *agent, const cst_webui_opts_t *opts) {
|
||||
(void)agent;
|
||||
(void)opts;
|
||||
fprintf(stderr,
|
||||
"the WebUI was not built into this binary (CST_BUILD_WEBUI=OFF).\n"
|
||||
"Use --headless --prompt '...' or build with make CST_WEBUI=1\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user