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.
106 lines
3.8 KiB
C
106 lines
3.8 KiB
C
#ifndef CST_HTTP_H
|
|
#define CST_HTTP_H
|
|
|
|
/* A small HTTP/1.1 server for the built-in WebUI (src/http_srv.c).
|
|
*
|
|
* Only what a local single-page app needs: exact-match routes, keep-alive,
|
|
* fixed-length bodies and chunked streaming responses (SSE). No TLS, no
|
|
* HTTP/2, no chunked *request* bodies, no CGI. Every connection is served
|
|
* by its own thread; a handler may keep the connection as long as it wants
|
|
* (that is how the event stream works). */
|
|
|
|
#include "cst_net.h"
|
|
|
|
#include <stddef.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
typedef struct cst_http_conn cst_http_conn_t;
|
|
|
|
typedef struct {
|
|
char method[8]; /* GET / POST / DELETE ... */
|
|
char path[1024]; /* percent-decoded, without the query string */
|
|
char query[1024]; /* raw, without '?' */
|
|
const char *body; /* NUL-terminated; may hold binary bytes */
|
|
size_t body_len;
|
|
/* Headers of interest, NULL when absent (pointers into the request). */
|
|
const char *authorization;
|
|
const char *cookie;
|
|
const char *origin;
|
|
const char *host;
|
|
const char *content_type;
|
|
const char *user_agent;
|
|
int keep_alive;
|
|
} cst_http_req_t;
|
|
|
|
/* 0 = handled (a response was sent), -1 = not found (the server replies). */
|
|
typedef int (*cst_http_fn)(cst_http_conn_t *c, void *ud);
|
|
|
|
typedef struct {
|
|
const char *method; /* "*" matches any */
|
|
const char *path; /* exact; "*" = fallback (try last) */
|
|
cst_http_fn fn;
|
|
void *ud;
|
|
int auth; /* ask the auth hook before the handler */
|
|
size_t max_body; /* request body cap; 0 = server default */
|
|
} cst_http_route_t;
|
|
|
|
typedef struct {
|
|
const char *host; /* NULL/"" = 127.0.0.1 */
|
|
int port; /* 0 = pick a free port */
|
|
size_t max_body; /* default 1 MiB */
|
|
int max_conn; /* default 32 */
|
|
int read_timeout_ms; /* waiting for the request headers (default 15000) */
|
|
int idle_timeout_ms; /* keep-alive pause between requests (default 60000) */
|
|
const cst_http_route_t *routes;
|
|
int nroutes;
|
|
/* 0 = allow, -1 = the server answers 401 before the handler runs. */
|
|
int (*auth)(const cst_http_req_t *req, void *ud);
|
|
void *auth_ud;
|
|
/* One line per request, e.g. "GET /api/state -> 200". */
|
|
void (*on_log)(const char *line, void *ud);
|
|
void *log_ud;
|
|
} cst_http_opts_t;
|
|
|
|
typedef struct cst_http_server cst_http_server_t;
|
|
|
|
/* Start listening. NULL on failure with a reason in err. */
|
|
cst_http_server_t *cst_http_start(const cst_http_opts_t *o, char *err,
|
|
size_t err_sz);
|
|
int cst_http_port(const cst_http_server_t *s);
|
|
/* Non-zero once stop() was called: long-lived handlers must return. */
|
|
int cst_http_stopping(const cst_http_server_t *s);
|
|
/* Close the listener and every connection, then wait for the threads. */
|
|
void cst_http_stop(cst_http_server_t *s);
|
|
void cst_http_free(cst_http_server_t *s);
|
|
|
|
/* --- inside a handler --- */
|
|
const cst_http_req_t *cst_http_request(cst_http_conn_t *c);
|
|
int cst_http_send(cst_http_conn_t *c, int status, const char *ctype,
|
|
const void *body, size_t n);
|
|
int cst_http_send_str(cst_http_conn_t *c, int status, const char *ctype,
|
|
const char *s);
|
|
/* json may be NULL (then "null" is sent); it is copied, not owned. */
|
|
int cst_http_send_json(cst_http_conn_t *c, int status, const char *json);
|
|
/* {"error":"..."} */
|
|
int cst_http_send_err(cst_http_conn_t *c, int status, const char *msg);
|
|
/* True once a response (or stream) began for this request. */
|
|
int cst_http_sent(const cst_http_conn_t *c);
|
|
|
|
/* Chunked response: begin(), any number of write()s, end(). */
|
|
int cst_http_stream_begin(cst_http_conn_t *c, const char *ctype);
|
|
int cst_http_stream_write(cst_http_conn_t *c, const void *data, size_t n);
|
|
int cst_http_stream_end(cst_http_conn_t *c);
|
|
|
|
/* Query parameter, percent-decoded ('+' = space). 0 when found. */
|
|
int cst_http_query(const cst_http_req_t *req, const char *key, char *out,
|
|
size_t out_sz);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* CST_HTTP_H */
|