Build: <fcntl.h> in tui.c, format-truncation in http_srv.c and /web. WebUI: - refuse cross-site requests (Origin must be the page itself) in the default no-token mode: any page could POST approval=yolo and a prompt - session/workspace/undo/compact/attach/model changes are 409 while a turn runs (use-after-free, messages landing in the wrong session) - ask_user returned "dismissed" at once instead of waiting - cancel and shutdown wake a turn parked on a permission or question; SIGTERM mid-turn no longer hangs or crashes - a page opened while a question is pending still gets it; Last-Event-ID resumes the stream; event seq matches the SSE id - token mode: page assets load without the token, images via cookie - --web-open works, --web-port/--web-token are validated - frontend: gate keys ignored while typing, clear-images button, reasoning no longer wiped while streaming HTTP server: auth and size cap before reading the body, strict Content-Length, NUL in url_decode, 413 on keep-alive, idle timeout, absolute-form only without a leading '/', case-insensitive Connection, header deadline, 400 for long request lines, unlink before close, poll() instead of select(), SO_EXCLUSIVEADDRUSE on Windows. Markdown sanitizer escapes quotes in attribute values. /web: passes this session's provider/model/base/effort/approval (key via the environment), keeps output off the TUI, shows the real URL and keeps CURRENT on the TUI's session. Tests: scripts/e2e_webui_ask.py, scripts/e2e_webui_http.py (run from e2e_webui.sh), sanitizer quote test.
109 lines
4.0 KiB
C
109 lines
4.0 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;
|
|
const char *last_event_id; /* an EventSource reconnecting */
|
|
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 (30 s at most) for the
|
|
* handler threads. Returns how many still run: their ud must stay alive. */
|
|
int cst_http_stop(cst_http_server_t *s);
|
|
/* Does nothing while handlers still run on 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 */
|