|
cwist
Modern C web framework with libttak runtime
|
Header: <cwist/net/http/http.h>
Low-level HTTP structures, framing limits, and parsing logic.
Defined in <cwist/net/http/http.h> to guard every connection against resource exhaustion.
CWIST_HTTP_MAX_HEADER_SIZE – cap on accumulated header bytes (default 8KiB).CWIST_HTTP_MAX_BODY_SIZE – cap on POST body bytes kept in memory (default 10MiB).CWIST_HTTP_READ_BUFFER_SIZE – scratch buffer per connection for pipelined/keep‑alive traffic.CWIST_HTTP_TIMEOUT_MS – poll timeout used while waiting for header/body as well as while sending responses. Allocates a new HTTP request structure. cwist_app_listen wires each inbound request with framework context:
req->app – back-reference to the running cwist_app (useful for pulling global config).req->db – shared cwist_db handle configured via cwist_app_use_db. Parses a raw HTTP string into a cwist_http_request object.
Blocking, framed read helper for keep-alive sockets.
\r\n\r\n, enforcing CWIST_HTTP_MAX_HEADER_SIZE.Content-Length and continues polling/recving until the full body is buffered.read_buf; caller passes the same buffer back in the next iteration.NULL on timeout, malformed framing, or when limits are exceeded.Allocates a new HTTP response structure. Default status is 200 OK.
Reads a static file from disk, copies it into the response body, and sets a sensible Content-Type. Files larger than CWIST_HTTP_MAX_BODY_SIZE are rejected with -EFBIG unless the route is tagged with CWIST_ENDPOINT_FILE, which switches Linux/macOS/FreeBSD to sendfile(2) so the kernel streams the payload directly. Returns 0 on success and propagates -errno on failures (-ENOENT, -EISDIR, etc.). out_size is optional; when provided it receives the file length so handlers can emit HEAD responses without buffering the payload. The built-in table includes modern web extensions such as .wasm and .mjs.
Adds a header to the linked list.
Retrieves the value of a header by key. Returns NULL if not found. Header lookup is case-insensitive (Host and host are equivalent).
static_http_handler in src/framework/app.c now loops on cwist_http_receive_request until either side requests Connection: close. Each response generated via cwist_http_send_response() contains an explicit Content-Length, and the send path uses poll()+send(MSG_NOSIGNAL) to avoid blocking indefinitely or tripping SIGPIPE while ApacheBench is bursting 64 concurrent keep-alive posts.
tests/stress_test.c spins up a minimal /api endpoint and shells out to:
The test succeeds only when the server drains each request body, recycles the leftover bytes for the next request on the same socket, and finalizes every response without triggering apr_pollset_poll timeout or connection reset.
Starts the main server loop.
config.ctx is passed to the handler for thread-safe state management.Low-level accept loop wrapper.
Monotonic counter of pipelined HTTP/1.1 continuations dropped (and whose connections were closed) because the reactor post queue was full. Also exposed via Prometheus /metrics as cwist_http_continuation_shed_total. Useful for backpressure and shed-rate alerting.
Injects a default set of hardening headers into the response if each is not already present (first-one-wins, so a handler can pre-set a header to override the default). Safe to call on both HTTP and HTTPS responses. Does not set Strict-Transport-Security; use cwist_http_response_add_hsts() for that.
Headers set by this function:
| Header | Default value |
|---|---|
X-Frame-Options | DENY |
X-Content-Type-Options | nosniff |
Referrer-Policy | strict-origin-when-cross-origin |
Content-Security-Policy | restricted default-src/script-src/style-src/font-src/img-src policy |
Cross-Origin-Resource-Policy | same-origin |
Permissions-Policy | camera/microphone/geolocation/payment/usb/interest-cohort all denied |
Cross-Origin-Opener-Policy | same-origin |
Adds Strict-Transport-Security: max-age=31536000; includeSubDomains to a TLS response. No-op when the header is already present. Per RFC 6797 section 7.2, call this only from an HTTPS handler; never call it on a plain-HTTP response.