cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
http.h
Go to the documentation of this file.
1
6#ifndef __CWIST_HTTP_H__
7#define __CWIST_HTTP_H__
8
12#include <cwist/core/db/sql.h>
16#include <stdint.h>
17#include <cwist/sys/platform.h>
18#ifndef CWIST_BAREMETAL
19#include <netinet/in.h>
20#include <sys/socket.h>
21#else
22struct sockaddr;
23struct sockaddr_in;
24#endif
25#include <sys/types.h>
26#include <stdio.h>
27#include <stdlib.h>
28
29long get_cpu_cores(void);
31
32struct cwist_app;
34
48
49typedef enum cwist_http_status_t {
50 /* 1xx Informational */
55 /* 2xx Success */
66 /* 3xx Redirection */
75 /* 4xx Client Error */
106 /* 5xx Server Error */
120
126const char *cwist_http_status_reason(int status);
127
132 CWIST_HTTP_PARSE_OK = 0, /* No error. */
133 CWIST_HTTP_PARSE_EOF, /* Orderly close / no data: close quietly. */
134 CWIST_HTTP_PARSE_MALFORMED, /* 400: bad request-line, Host rules, CL/TE rules. */
135 CWIST_HTTP_PARSE_HEADER_OVERFLOW, /* 431: header block exceeds the read buffer. */
136 CWIST_HTTP_PARSE_BODY_TOO_LARGE, /* 413: Content-Length exceeds the body cap. */
137 CWIST_HTTP_PARSE_TE_UNSUPPORTED, /* 501: unsupported transfer coding. */
138 CWIST_HTTP_PARSE_EXPECT_FAILED /* 417: unsupported Expect value. */
140
142#define CWIST_HTTP_MAX_HEADER_SIZE (8 * 1024)
143#define CWIST_HTTP_MAX_BODY_SIZE (10 * 1024 * 1024)
144#define CWIST_HTTP_READ_BUFFER_SIZE (16 * 1024)
145#define CWIST_HTTP_TIMEOUT_MS 30000
146#define CWIST_HTTPS_HANDSHAKE_TIMEOUT_MS 45000 /* Total TLS handshake budget */
147#define CWIST_HTTP_HEADERS_TIMEOUT_MS 120000 /* Total header read budget */
148#define CWIST_HTTP_BODY_IDLE_TIMEOUT_MS 60000 /* Abort body read after this much silence */
149#define CWIST_HTTP2_IDLE_TIMEOUT_MS 300000 /* Default h2 idle budget (env overridable) */
150#define CWIST_HTTP_KEEP_ALIVE_TIMEOUT_SEC \
151 15 /* Default keep-alive idle connection timeout in seconds */
152
156typedef struct cwist_http_header_node {
162
163typedef struct cwist_http_request {
172 bool keep_alive;
175 int client_fd;
176 struct cwist_app *app;
178 bool upgraded;
179 uint32_t stream_id;
180 void *private_data;
182 size_t content_length;
185 void *session;
187 void *arena;
191 void *h2_queue;
193
194typedef void (*cwist_http_body_cleanup_fn)(const void *ptr, size_t len, void *ctx);
195
200typedef struct cwist_http_response {
207
210 const void *ptr_body;
214
221
230 int (*stream_sink)(void *ctx, const char *data, size_t len);
233
234 bool keep_alive;
235
237 char *alt_svc;
238
239 void *arena;
242
244 bool deferred;
245 void *async;
247
254cwist_http_request *cwist_http_parse_request(const char *raw_request);
259cwist_http_request *cwist_http_parse_request_len(const char *buf, size_t len);
266int cwist_http_response_serialize(cwist_http_response *res, char **out, size_t *out_len);
267
282int cwist_http_response_stream_write(cwist_http_response *res, const char *data, size_t len);
285cwist_http_request *cwist_http_receive_request(int client_fd, char *read_buf, size_t buf_size,
286 size_t *buf_len, cwist_http_parse_error_t *err_out);
292void cwist_http_send_error_response(int fd, int status, const char *msg);
298
302void cwist_http_format_date(time_t t, char *buf, size_t len);
303
307time_t cwist_http_parse_date(const char *str);
321
327void cwist_http_response_set_body_ptr(cwist_http_response *res, const void *ptr, size_t len);
328void cwist_http_response_set_body_ptr_managed(cwist_http_response *res, const void *ptr, size_t len,
329 cwist_http_body_cleanup_fn cleanup, void *ctx);
330void cwist_http_response_set_alt_svc(cwist_http_response *res, const char *alt_svc);
331
345bool cwist_tcp_cork_enabled(void);
351size_t cwist_http_serialize_headers(cwist_http_response *res, char *buf, size_t buf_size);
353 const char *content_type_hint, size_t *out_size);
365 const char *value);
370char *cwist_http_header_get(cwist_http_header_node *head, const char *key);
376size_t cwist_http_header_remove(cwist_http_header_node **head, const char *key);
378
386
401cwist_http_method_t cwist_http_string_to_method_len(const char *str, size_t len);
407int cwist_make_socket_ipv4(struct sockaddr_in *sockv4, const char *address, uint16_t port,
408 uint16_t backlog);
410cwist_error_t cwist_accept_socket(int server_fd, struct sockaddr *sockv4,
411 void (*handler_func)(int client_fd, void *ctx), void *ctx);
414typedef struct cwist_server_config {
415 bool use_forking;
416 bool use_threading;
417 bool use_epoll;
419
421 void (*handler)(int, void *), void *ctx);
423
425int cwist_http_pool_init(void);
428int cwist_http_pool_init_mode(bool use_c1m);
429void cwist_http_pool_limit_core(unsigned int limit);
430void cwist_http_pool_submit(int client_fd, void (*handler)(int, void *), void *ctx);
431bool cwist_http_pool_rearm_current(int client_fd, void (*handler)(int, void *), void *ctx);
432void cwist_http_pool_destroy(void);
433
434/* --- Event-driven (one-shot) connection path for the C1M reactor ---------
435 * The classic pool handler parks a worker thread on each keep-alive
436 * connection, capping concurrent connections at the thread count. The async
437 * path below never blocks on a read: the callback drains whatever arrived,
438 * serves every complete request, and rearms the fd, so one thread can hold
439 * hundreds of thousands of mostly-idle connections.
440 *
441 * Writes on the deferred-completion path are resumable: when a slow client
442 * saturates the socket, cwist_http_async_send_response deep-copies the unsent
443 * remainder and parks it on a one-shot POLLOUT slot instead of blocking the
444 * reactor thread in a poll() wait. Synchronous (non-deferred) responses
445 * still use the bounded poll wait inside cwist_http_send_response
446 * (CWIST_HTTP_TIMEOUT_MS). */
447typedef enum {
448 CWIST_ASYNC_CLOSE = 0, /* Close fd and release the connection. */
449 CWIST_ASYNC_REARM, /* Keep the connection; wait for more reads. */
450 CWIST_ASYNC_DETACH, /* Handler took ownership of fd (h2c, upgrades). */
451 CWIST_ASYNC_DEFER /* Completion/continuation owns fd and conn; leave untouched. */
453
455
456typedef struct cwist_http_async_conn {
457 int fd;
458 void *user_ctx; /* Owning app context. */
459 char *rbuf; /* Lazy recv stash; freed while empty. */
460 size_t cap;
461 size_t len;
462 char *obuf; /* Coalesced output stash (C1M async batch); reused across turns. */
463 size_t ocap;
464 size_t olen;
465 bool virgin; /* No bytes seen yet (h2c preface sniff). */
466 bool expect_continue_sent; /* 100 Continue already emitted for the pending request. */
467 uint32_t last_active_sec; /* Monotonic timestamp of last activity (for idle reaping). */
468 uint32_t worker_id; /* Assigned worker thread index for load tracking. */
469 cwist_reactor_t *reactor; /* Owning reactor (deferred-response completion target). */
470 cwist_async_handler_t handler; /* Connection handler, reused for re-arm after a defer. */
471 bool peer_eof; /* Read side closed; drain complete buffered requests. */
472 /* RX receive path (Linux reactors only: a RECV SQE on an io_uring ring,
473 * or the reactor's own recv() on an epoll one; unused elsewhere).
474 * Wait-state discipline, all transitions on the
475 * reactor owner thread: at most one of {RECV SQE in flight, one-shot
476 * POLL armed} at any time, or NONE while buffered work is served.
477 * While rx_recv_inflight is true the stash buffer must not move or be
478 * consumed: the in-flight SQE references rbuf + len. */
479 bool rx_recv_inflight; /* RECV SQE armed on the reactor ring. */
480 bool rx_data_ready; /* RECV completion already staged bytes into the stash. */
481 /* Learned mode: set when an armed RECV completed -EAGAIN (the client
482 * sends one request per idle period, so arming RECV first just burns an
483 * SQE before the POLL fallback). Cleared whenever a RECV stages bytes,
484 * so pipelining clients keep the SQE path. While set, waits go straight
485 * to the legacy POLL, making the steady-state op count identical to the
486 * legacy path for non-pipelining clients. */
488 uint64_t rx_armed_ns; /* Latency-probe arm timestamp of the in-flight RECV. */
489 /* Env-gated kernel-arrival probe (CWIST_KERN_TS=1, Linux only, experiment
490 * instrumentation for issue #153): monotonic time of the recvmsg that
491 * last delivered request bytes. Paired with SCM_TIMESTAMPNS at fill time. */
494 /* Last BDR hit on this connection: repeated routes skip the SipHash and
495 * bucket walk via a content-compare (validated under the EBR epoch on
496 * every use). Zero-initialized at connection setup. */
498 /* Hysteresis anchor for the idle stash shrink (CWIST_ASYNC_STASH_IDLE_SEC):
499 * monotonic time the rbuf/obuf capacity was last grown. While requests
500 * keep needing capacity the stash is never shrunk at re-arm; one idle
501 * window after the last growth it converges to the small floor. */
504
505/* Re-arm a connection after a deferred response completed on the reactor
506 * thread (keep-alive), reusing the same one-shot event slot model as
507 * http_async_event_cb. Buffered bytes resume through a posted continuation,
508 * not recursively inline. On failure the fd is closed and conn released. */
509bool cwist_http_async_rearm(int client_fd, cwist_reactor_t *reactor, cwist_http_async_conn_t *conn);
510/* Pipelined continuations dropped (and their connections closed) because the
511 * reactor post queue was full. Monotonic; useful for shed-rate alerting. */
513/* Live C1M connections (accepted, not yet released). Shutdown drain polls
514 * this for an early exit; also useful for metrics. */
516
517/* Send a deferred completion's response on the reactor path without ever
518 * blocking the reactor thread: speculative non-blocking write first; on a
519 * partial write the unsent remainder is deep-copied into an owned buffer and
520 * parked on a one-shot POLLOUT slot, which resumes the send and then re-arms
521 * (keep_alive) or closes exactly like the synchronous completion. File-stream
522 * bodies keep the existing bounded-blocking send path. Takes over rearm/close
523 * of (client_fd, conn) in all outcomes. */
526 bool keep_alive, bool head_only);
527
533
536 bool keep_alive, bool head_only);
537
538/* Response write coalescing on the cleartext C1M async batch path: responses
539 * served within one reactor turn accumulate in conn->obuf (capped at
540 * CWIST_HTTP_COALESCE_MAX bytes) and flush with a single speculative write
541 * at the yield/rearm point; a partial flush hands the remainder to the
542 * parked-write mechanism. cwist_http_coalesce_flush_blocking drains the
543 * stash through the bounded poll wait and is reserved for ordering-critical
544 * points (deferred completions, file streams, 100 Continue, cap overflow).
545 * cwist_http_send_response_coalesced mirrors cwist_http_send_response_async
546 * but appends to the stash instead of writing to the fd. */
547#define CWIST_HTTP_COALESCE_MAX (256 * 1024)
548
549typedef enum {
550 CWIST_COALESCE_FLUSH_DONE = 0, /* Stash fully written (or empty). */
551 CWIST_COALESCE_FLUSH_PARKED, /* Remainder parked on POLLOUT; slot owns fd/conn. */
552 CWIST_COALESCE_FLUSH_ERROR /* Fatal; caller closes. */
554
555int cwist_http_coalesce_append(cwist_http_async_conn_t *conn, const void *data, size_t len);
557cwist_http_coalesce_flush(int client_fd, cwist_http_async_conn_t *conn, bool keep_alive);
563 bool keep_alive, bool head_only);
564
565typedef enum {
567 CWIST_RECV_NEED_MORE, /* Partial request; rearm and wait. */
568 CWIST_RECV_FATAL /* Protocol error / overflow; close. */
570
571bool cwist_http_pool_submit_async(int client_fd, cwist_async_handler_t handler, void *ctx);
572/* Returns 0 on data/EAGAIN/EOF, -1 on fatal error. EOF sets peer_eof;
573 * drain complete buffered requests, then close instead of waiting for input. */
576 cwist_http_request **out,
577 cwist_http_parse_error_t *err_out);
578
579extern const int CWIST_CREATE_SOCKET_FAILED;
580extern const int CWIST_HTTP_UNAVAILABLE_ADDRESS;
581extern const int CWIST_HTTP_BIND_FAILED;
582extern const int CWIST_HTTP_SETSOCKOPT_FAILED;
583extern const int CWIST_HTTP_LISTEN_FAILED;
584
585#endif
Auto-Caching Layer for repetitive requests.
Database operations wrapper (SQLite3).
Safe string type with helper methods.
Endpoint-level behavior flags for CWIST apps.
uint32_t cwist_endpoint_opt_t
Bitmask type controlling endpoint behavior.
cwist_http_request * cwist_http_receive_request(int client_fd, char *read_buf, size_t buf_size, size_t *buf_len, cwist_http_parse_error_t *err_out)
Receive and parse an HTTP request from a socket.
Definition http.c:4722
int headers_have_content_length(cwist_http_header_node *headers)
Detect whether a header list already defines Content-Length.
Definition http.c:2394
void cwist_http_send_error_response(int fd, int status, const char *msg)
Send a minimal error response (Connection: close) for a failed request.
Definition http.c:4046
struct cwist_http_request cwist_http_request
const int CWIST_HTTP_LISTEN_FAILED
Definition http.c:1545
size_t cwist_http_serialize_headers(cwist_http_response *res, char *buf, size_t buf_size)
Serialize only the status line and headers into a caller buffer.
Definition http.c:2609
cwist_error_t cwist_http_server_loop(int server_fd, cwist_server_config *config, void(*handler)(int, void *), void *ctx)
Run the main HTTP accept loop using the configured concurrency strategy.
Definition http.c:5590
const int CWIST_CREATE_SOCKET_FAILED
Definition http.c:1541
cwist_error_t cwist_http_send_response(int client_fd, cwist_http_response *res)
Send a response over a socket.
Definition http.c:2878
struct cwist_http_response cwist_http_response
cwist_http_parse_error_t
Failure reason reported by the request receive APIs.
Definition http.h:46
@ CWIST_HTTP_PARSE_EOF
Definition http.h:48
@ CWIST_HTTP_PARSE_OK
Definition http.h:47
@ CWIST_HTTP_PARSE_BODY_TOO_LARGE
Definition http.h:51
@ CWIST_HTTP_PARSE_HEADER_OVERFLOW
Definition http.h:50
@ CWIST_HTTP_PARSE_MALFORMED
Definition http.h:49
@ CWIST_HTTP_PARSE_TE_UNSUPPORTED
Definition http.h:52
@ CWIST_HTTP_PARSE_EXPECT_FAILED
Definition http.h:53
int cwist_make_socket_ipv4(struct sockaddr_in *sockv4, const char *address, uint16_t port, uint16_t backlog)
Create an IPv4 socket and bind/listen.
Definition http.c:5361
char * cwist_http_header_get(cwist_http_header_node *head, const char *key)
Find a header value by key.
Definition http.c:1880
cwist_error_t cwist_http_send_response_head(int client_fd, cwist_http_response *res)
Send only the status line and headers (HEAD replies).
Definition http.c:2961
cwist_http_status_t
Definition http.h:27
@ CWIST_HTTP_UNAUTHORIZED
Definition http.h:32
@ CWIST_HTTP_FORBIDDEN
Definition http.h:33
@ CWIST_HTTP_INTERNAL_ERROR
Definition http.h:35
@ CWIST_HTTP_CREATED
Definition http.h:29
@ CWIST_HTTP_NOT_IMPLEMENTED
Definition http.h:36
@ CWIST_HTTP_BAD_REQUEST
Definition http.h:31
@ CWIST_HTTP_NOT_FOUND
Definition http.h:34
@ CWIST_HTTP_OK
Definition http.h:28
@ CWIST_HTTP_NO_CONTENT
Definition http.h:30
void cwist_http_request_destroy(cwist_http_request *req)
Destroy an HTTP request object.
Definition http.c:2180
cwist_http_request * cwist_http_parse_request(const char *raw_request)
Parse a raw HTTP request string.
Definition http.c:4412
void cwist_http_response_add_security_headers(cwist_http_response *res)
Add default security headers (CSP, X-Frame-Options, etc.) if missing.
Definition http.c:1935
cwist_http_method_t
Definition http.h:16
@ CWIST_HTTP_OPTIONS
Definition http.h:23
@ CWIST_HTTP_HEAD
Definition http.h:22
@ CWIST_HTTP_PUT
Definition http.h:19
@ CWIST_HTTP_PATCH
Definition http.h:21
@ CWIST_HTTP_UNKNOWN
Definition http.h:24
@ CWIST_HTTP_POST
Definition http.h:18
@ CWIST_HTTP_GET
Definition http.h:17
@ CWIST_HTTP_DELETE
Definition http.h:20
cwist_sstring * cwist_http_stringify_response(cwist_http_response *res)
Serialize a response to a string.
Definition http.c:4099
cwist_error_t cwist_http_header_add(cwist_http_header_node **head, const char *key, const char *value)
Add a header to the list.
Definition http.c:1861
void cwist_http_response_destroy(cwist_http_response *res)
Destroy an HTTP response object.
Definition http.c:2315
const int CWIST_HTTP_BIND_FAILED
Definition http.c:1543
void cwist_http_header_free_all(cwist_http_header_node *head)
Free all headers.
Definition http.c:2006
cwist_http_response * cwist_http_response_create(void)
Create a new HTTP response object.
Definition http.c:2291
struct cwist_server_config cwist_server_config
cwist_error_t cwist_accept_socket(int server_fd, struct sockaddr *sockv4, void(*handler_func)(int client_fd, void *ctx), void *ctx)
Accept sockets and invoke a handler callback.
const int CWIST_HTTP_UNAVAILABLE_ADDRESS
Definition http.c:1542
const char * cwist_http_method_to_string(cwist_http_method_t method)
Convert method enum to string.
Definition http.c:1554
cwist_http_method_t cwist_http_string_to_method(const char *method_str)
Convert method string to enum.
Definition http.c:1629
const int CWIST_HTTP_SETSOCKOPT_FAILED
Definition http.c:1544
struct cwist_http_header_node cwist_http_header_node
Linked list for headers to handle multiple headers easily.
cwist_http_request * cwist_http_request_create(void)
Create a new HTTP request object.
Definition http.c:2056
bool cwist_http_pool_rearm_current(int client_fd, void(*handler)(int, void *), void *ctx)
Requeue the current connection's next handler turn on the pool.
Definition http.c:824
cwist_async_send_status_t cwist_http_send_response_coalesced(int client_fd, cwist_http_response *res, cwist_http_async_conn_t *conn, bool keep_alive, bool head_only)
Coalescing variant of the async send: serialize small responses directly into the connection's output...
Definition http.c:3779
int cwist_http_async_conn_fill(cwist_http_async_conn_t *conn)
Drain the socket into the connection stash. Stops at EAGAIN, and also after a short read: poll is lev...
Definition http.c:4956
cwist_async_action_t(* cwist_async_handler_t)(int fd, struct cwist_http_async_conn *conn)
Definition http.h:454
bool cwist_http_async_rearm(int client_fd, cwist_reactor_t *reactor, cwist_http_async_conn_t *conn)
Re-arm an async connection for its next event.
Definition http.c:1357
cwist_async_send_status_t cwist_http_send_response_async(int client_fd, cwist_http_response *res, cwist_http_async_conn_t *conn, bool keep_alive, bool head_only)
Async send variant that reports the outcome to the caller instead of re-arming or closing the connect...
Definition http.c:3534
long get_cpu_cores(void)
Return the number of CPUs available to this process.
Definition http.c:99
long cwist_http_inflight_count(void)
Definition http.c:301
cwist_async_send_status_t
Definition http.h:528
@ CWIST_ASYNC_SEND_DEFERRED
Definition http.h:531
@ CWIST_ASYNC_SEND_CLOSE
Definition http.h:530
@ CWIST_ASYNC_SEND_KEEPALIVE
Definition http.h:529
time_t cwist_http_parse_date(const char *str)
Parse an HTTP-date string into a time_t.
Definition http.c:2119
void cwist_http_async_send_response(int client_fd, cwist_http_response *res, cwist_reactor_t *reactor, cwist_http_async_conn_t *conn, bool keep_alive, bool head_only)
Send a response on the async path, parking the unsent remainder on EAGAIN instead of blocking the rea...
Definition http.c:3413
cwist_http_method_t cwist_http_string_to_method_len(const char *str, size_t len)
Parse a method token (with explicit length) into a method enum.
Definition http.c:1584
int cwist_http_response_stream_end(cwist_http_response *res)
Terminate a streaming response with the final zero-length chunk and flush.
Definition http.c:4549
void cwist_http_response_set_body_ptr(cwist_http_response *res, const void *ptr, size_t len)
Sets a direct pointer for the response body (Zero Copy). Use this when serving large files from memor...
Definition http.c:2346
int cwist_http_pool_init(void)
Initialize the handler pool in the mode CWIST_C1M_MODE selects.
Definition http.c:678
size_t cwist_http_header_remove(cwist_http_header_node **head, const char *key)
Remove all headers matching a name, honoring per-node ownership (heap vs arena vs borrowed buffers).
Definition http.c:1907
cwist_sstring * cwist_get_client_ip_from_fd(int fd)
Resolve the peer IP address for a connected client socket.
Definition http.c:2141
bool cwist_tcp_cork_enabled(void)
Whether the TCP_CORK coalescing layer is active for cleartext HTTP/1.1 responses. Enabled at runtime ...
Definition http.c:2684
void cwist_http_pool_limit_core(unsigned int limit)
Fix the HTTP pool worker count to a specific value.
Definition http.c:145
void cwist_http_format_date(time_t t, char *buf, size_t len)
Format a time_t as an HTTP-date (RFC 7231).
Definition http.c:2108
cwist_error_t cwist_http_response_send_file(cwist_http_response *res, const char *file_path, const char *content_type_hint, size_t *out_size)
Prepare a response to serve a file either by buffering or direct streaming.
Definition http.c:5223
long get_optimal_thread_count(void)
Compute the HTTP worker/event-loop thread count.
Definition http.c:159
int cwist_http_coalesce_flush_blocking(int client_fd, cwist_http_async_conn_t *conn)
Flush the coalesced-output stash, blocking in poll() until drained.
Definition http.c:3737
cwist_recv_status_t cwist_http_receive_request_nb(cwist_http_async_conn_t *conn, cwist_http_request **out, cwist_http_parse_error_t *err_out)
Try to assemble one complete request from the stash without any blocking IO. On CWIST_RECV_OK the con...
Definition http.c:5035
@ CWIST_HTTP_MULTI_STATUS
Definition http.h:63
@ CWIST_HTTP_NETWORK_AUTHENTICATION_REQUIRED
Definition http.h:118
@ CWIST_HTTP_LOCKED
Definition http.h:98
@ CWIST_HTTP_VARIANT_ALSO_NEGOTIATES
Definition http.h:114
@ CWIST_HTTP_PAYMENT_REQUIRED
Definition http.h:78
@ CWIST_HTTP_MOVED_PERMANENTLY
Definition http.h:68
@ CWIST_HTTP_INSUFFICIENT_STORAGE
Definition http.h:115
@ CWIST_HTTP_PROCESSING
Definition http.h:53
@ CWIST_HTTP_PROXY_AUTHENTICATION_REQUIRED
Definition http.h:83
@ CWIST_HTTP_UPGRADE_REQUIRED
Definition http.h:101
@ CWIST_HTTP_VERSION_NOT_SUPPORTED
Definition http.h:113
@ CWIST_HTTP_CONFLICT
Definition http.h:85
@ CWIST_HTTP_NOT_EXTENDED
Definition http.h:117
@ CWIST_HTTP_MISDIRECTED_REQUEST
Definition http.h:96
@ CWIST_HTTP_NOT_MODIFIED
Definition http.h:71
@ CWIST_HTTP_RESET_CONTENT
Definition http.h:61
@ CWIST_HTTP_UNPROCESSABLE_CONTENT
Definition http.h:97
@ CWIST_HTTP_METHOD_NOT_ALLOWED
Definition http.h:81
@ CWIST_HTTP_PAYLOAD_TOO_LARGE
Definition http.h:90
@ CWIST_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS
Definition http.h:105
@ CWIST_HTTP_SEE_OTHER
Definition http.h:70
@ CWIST_HTTP_PARTIAL_CONTENT
Definition http.h:62
@ CWIST_HTTP_GATEWAY_TIMEOUT
Definition http.h:112
@ CWIST_HTTP_LOOP_DETECTED
Definition http.h:116
@ CWIST_HTTP_EXPECTATION_FAILED
Definition http.h:94
@ CWIST_HTTP_TEMPORARY_REDIRECT
Definition http.h:73
@ CWIST_HTTP_MULTIPLE_CHOICES
Definition http.h:67
@ CWIST_HTTP_CONTINUE
Definition http.h:51
@ CWIST_HTTP_TOO_MANY_REQUESTS
Definition http.h:103
@ CWIST_HTTP_NON_AUTHORITATIVE_INFORMATION
Definition http.h:59
@ CWIST_HTTP_PRECONDITION_REQUIRED
Definition http.h:102
@ CWIST_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE
Definition http.h:104
@ CWIST_HTTP_UNSUPPORTED_MEDIA_TYPE
Definition http.h:92
@ CWIST_HTTP_TOO_EARLY
Definition http.h:100
@ CWIST_HTTP_FAILED_DEPENDENCY
Definition http.h:99
@ CWIST_HTTP_IM_A_TEAPOT
Definition http.h:95
@ CWIST_HTTP_BAD_GATEWAY
Definition http.h:110
@ CWIST_HTTP_EARLY_HINTS
Definition http.h:54
@ CWIST_HTTP_ALREADY_REPORTED
Definition http.h:64
@ CWIST_HTTP_NOT_ACCEPTABLE
Definition http.h:82
@ CWIST_HTTP_PERMANENT_REDIRECT
Definition http.h:74
@ CWIST_HTTP_IM_USED
Definition http.h:65
@ CWIST_HTTP_GONE
Definition http.h:86
@ CWIST_HTTP_LENGTH_REQUIRED
Definition http.h:87
@ CWIST_HTTP_REQUEST_TIMEOUT
Definition http.h:84
@ CWIST_HTTP_RANGE_NOT_SATISFIABLE
Definition http.h:93
@ CWIST_HTTP_FOUND
Definition http.h:69
@ CWIST_HTTP_URI_TOO_LONG
Definition http.h:91
@ CWIST_HTTP_PRECONDITION_FAILED
Definition http.h:88
@ CWIST_HTTP_SWITCHING_PROTOCOLS
Definition http.h:52
@ CWIST_HTTP_USE_PROXY
Definition http.h:72
@ CWIST_HTTP_INTERNAL_SERVER_ERROR
Definition http.h:108
@ CWIST_HTTP_CONTENT_TOO_LARGE
Definition http.h:89
@ CWIST_HTTP_ACCEPTED
Definition http.h:58
@ CWIST_HTTP_SERVICE_UNAVAILABLE
Definition http.h:111
void cwist_http_response_add_hsts(cwist_http_response *res)
Add Strict-Transport-Security to a TLS response (HTTPS only).
Definition http.c:1993
int cwist_http_coalesce_error_response(cwist_http_async_conn_t *conn, int status)
Build a minimal text error response into the coalesce stash.
Definition http.c:3756
void cwist_http_response_set_body_ptr_managed(cwist_http_response *res, const void *ptr, size_t len, cwist_http_body_cleanup_fn cleanup, void *ctx)
Attach a managed zero-copy body pointer and optional cleanup hook to a response.
Definition http.c:2358
cwist_coalesce_flush_status_t
Definition http.h:549
@ CWIST_COALESCE_FLUSH_DONE
Definition http.h:550
@ CWIST_COALESCE_FLUSH_ERROR
Definition http.h:552
@ CWIST_COALESCE_FLUSH_PARKED
Definition http.h:551
cwist_coalesce_flush_status_t cwist_http_coalesce_flush(int client_fd, cwist_http_async_conn_t *conn, bool keep_alive)
Flush the coalesced-output stash with one speculative write.
Definition http.c:3682
@ CWIST_HTTP_CONNECT
Definition http.h:45
cwist_http_request * cwist_http_parse_request_len(const char *buf, size_t len)
Parse an HTTP/1.x request held in a memory buffer (length-based; the buffer may contain a binary body...
Definition http.c:4424
void cwist_http_pool_submit(int client_fd, void(*handler)(int, void *), void *ctx)
Queue a connection for handling on the classic dynamic pool.
Definition http.c:772
long cwist_http_continuation_shed_count(void)
Definition http.c:293
bool cwist_http_pool_submit_async(int client_fd, cwist_async_handler_t handler, void *ctx)
Submit a connection to the C1M async reactor pool.
Definition http.c:1468
int cwist_http_response_stream_write(cwist_http_response *res, const char *data, size_t len)
Append one data chunk to a streaming response.
Definition http.c:4519
cwist_async_action_t
Definition http.h:447
@ CWIST_ASYNC_REARM
Definition http.h:449
@ CWIST_ASYNC_CLOSE
Definition http.h:448
@ CWIST_ASYNC_DEFER
Definition http.h:451
@ CWIST_ASYNC_DETACH
Definition http.h:450
void cwist_http_pool_destroy(void)
Tear down both pool variants.
Definition http.c:836
cwist_http_response * cwist_http_response_create_in_arena(void *arena)
Create a response inside an existing (request) arena. The response is bump-allocated from arena and s...
Definition http.c:2306
void(* cwist_http_body_cleanup_fn)(const void *ptr, size_t len, void *ctx)
Definition http.h:194
cwist_recv_status_t
Definition http.h:565
@ CWIST_RECV_FATAL
Definition http.h:568
@ CWIST_RECV_NEED_MORE
Definition http.h:567
@ CWIST_RECV_OK
Definition http.h:566
int cwist_http_response_serialize(cwist_http_response *res, char **out, size_t *out_len)
Serialize a response into a freshly allocated HTTP/1.x buffer (status line + headers + body)....
Definition http.c:4439
int cwist_http_response_stream_begin(cwist_http_response *res)
Switch a response to chunked streaming mode.
Definition http.c:4503
int cwist_http_pool_init_mode(bool use_c1m)
Initialize the handler pool as reactor workers (use_c1m) or the classic thread pool,...
Definition http.c:696
struct cwist_http_async_conn cwist_http_async_conn_t
void cwist_http_response_set_alt_svc(cwist_http_response *res, const char *alt_svc)
Set the Alt-Svc header value for HTTP/3 upgrade advertisement.
Definition http.c:2376
const char * cwist_http_status_reason(int status)
Return the standard reason phrase for an HTTP status code (e.g. 404 -> "Not Found")....
Definition http.c:3970
int cwist_http_coalesce_append(cwist_http_async_conn_t *conn, const void *data, size_t len)
Append bytes to a connection's coalesced-output stash.
Definition http.c:3653
Query string parser and map.
size_t head
Definition otel.c:57
Target capability macros shared by all CWIST sources.
struct cwist_reactor cwist_reactor_t
Definition reactor.h:14
Main Application Context.
Definition app.h:155
Wrapper for Database Operations. Currently uses SQLite3.
Definition sql.h:21
Integrated error structure used throughout the system.
Definition cwist_err.h:72
uint32_t last_active_sec
Definition http.h:467
bool expect_continue_sent
Definition http.h:466
uint32_t stash_grown_sec
Definition http.h:502
uint64_t rx_armed_ns
Definition http.h:488
cwist_reactor_t * reactor
Definition http.h:469
cwist_async_handler_t handler
Definition http.h:470
uint64_t kern_last_mono_ns
Definition http.h:492
cwist_bdr_cursor_t bdr_cursor
Definition http.h:497
uint32_t worker_id
Definition http.h:468
Linked list for headers to handle multiple headers easily.
Definition http.h:59
bool arena_owned
True when the node lives in a request arena (freed with the arena).
Definition http.h:160
cwist_sstring * value
Definition http.h:61
cwist_sstring * key
Definition http.h:60
struct cwist_http_header_node * next
Definition http.h:62
cwist_query_map * query_params
Parsed query parameters.
Definition http.h:69
bool te_chunked_seen
Parser saw and validated Transfer-Encoding: chunked (HTTP/1.1).
Definition http.h:173
char * csrf_token
CSRF token populated by csrf middleware.
Definition http.h:186
size_t content_length
Definition http.h:79
cwist_query_map * flash
Flash messages for this request (one-time read).
Definition http.h:184
cwist_sstring * path
e.g., "/users/1"
Definition http.h:67
void * async_conn
C1M connection shell when on the reactor path (internal).
Definition http.h:188
cwist_sstring * query
e.g., "active=true" (raw)
Definition http.h:68
bool expect_100_seen
Parser saw and validated Expect: 100-continue (HTTP/1.1).
Definition http.h:174
void * route_middleware_state
Router middleware chain state (internal).
Definition http.h:181
void * h2_queue
HTTP/2 async queue when on H2 path (internal).
Definition http.h:191
struct cwist_app * app
Owning app context (if any).
Definition http.h:176
uint32_t stream_id
HTTP/2 or HTTP/3 stream ID (0 for HTTP/1.1).
Definition http.h:77
bool ws_async_handoff
WebSocket async path took fd ownership; C1M layer must detach.
Definition http.h:189
cwist_sstring * body
Definition http.h:73
void * arena
Per-request arena for bump-allocated structs (internal).
Definition http.h:187
void * session
cwist_session_t pointer set by session middleware.
Definition http.h:185
cwist_sstring * version
e.g., "HTTP/1.1"
Definition http.h:71
void * private_data
Internal framework use (protocol-specific context).
Definition http.h:78
cwist_query_map * path_params
Parsed path parameters (e.g. :id)
Definition http.h:70
void * https_conn
HTTPS connection wrapper when on TLS path (internal).
Definition http.h:190
cwist_http_header_node * headers
Definition http.h:72
bool keep_alive
Definition http.h:74
cwist_endpoint_opt_t endpoint_opts
Behavior hints for the active endpoint.
Definition http.h:183
cwist_db * db
Shared database handle from cwist_app.
Definition http.h:177
cwist_http_method_t method
Definition http.h:66
HTTP Response Object. Supports standard string body or Zero-Copy pointer body.
Definition http.h:82
cwist_endpoint_opt_t endpoint_opts
Mirrors req->endpoint_opts.
Definition http.h:206
cwist_sstring * version
e.g., "HTTP/1.1"
Definition http.h:83
cwist_sstring * status_text
e.g., "OK"
Definition http.h:85
void * ptr_body_cleanup_ctx
User data for release hook.
Definition http.h:213
int file_stream_fd
Open descriptor for sendfile.
Definition http.h:217
bool use_file_stream
Fast File Streaming.
Definition http.h:216
cwist_sstring * body
Definition http.h:87
bool arena_borrowed
Definition http.h:240
const void * ptr_body
Pointer to external data (e.g., mmap region)
Definition http.h:210
void * stream_sink_ctx
Definition http.h:231
cwist_http_status_t status_code
Definition http.h:84
bool file_stream_auto_close
Close fd after streaming.
Definition http.h:220
off_t file_stream_offset
Current offset for sendfile loop.
Definition http.h:219
char * alt_svc
Alt-Svc header for HTTP/3 upgrade advertisement.
Definition http.h:237
cwist_http_header_node * headers
Definition http.h:86
bool is_ptr_body
Zero-Copy Pointer Body.
Definition http.h:209
cwist_http_body_cleanup_fn ptr_body_cleanup
Optional release hook.
Definition http.h:212
size_t stream_flushed
stream_buf prefix already pushed to stream_sink.
Definition http.h:229
bool stream_head_sent
Head serialized and pushed to the sink.
Definition http.h:232
int(* stream_sink)(void *ctx, const char *data, size_t len)
Live sink (streaming dispatch only).
Definition http.h:230
void * arena
Per-response arena for bump-allocated structs (internal).
Definition http.h:239
bool stream_mode
cwist_http_response_stream_begin() ran.
Definition http.h:225
cwist_sstring * stream_buf
Framed chunk payload (hex\r\ndata\r ...).
Definition http.h:228
void * async
Owning cwist_async while deferred (read-only).
Definition http.h:245
size_t ptr_body_len
Length of external data.
Definition http.h:211
bool stream_failed
A live sink rejected a chunk/head.
Definition http.h:227
size_t file_stream_len
Total bytes to stream.
Definition http.h:218
bool deferred
Deferred-response handoff (internal; see net/http/async.h)
Definition http.h:244
bool stream_ended
cwist_http_response_stream_end() ran.
Definition http.h:226
bool use_forking
Process per request.
Definition http.h:220
bool use_threading
Thread per request.
Definition http.h:221
bool use_epoll
Use epoll for accepting.
Definition http.h:222
Unified error code and type definitions for the CWIST system layer.