cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
http.h File Reference

HTTP Protocol Definitions and Helpers. More...

#include <cwist/core/sstring/sstring.h>
#include <cwist/sys/err/cwist_err.h>
#include <cwist/net/http/query.h>
#include <cwist/core/db/sql.h>
#include <cwist/sys/app/endpoint_opts.h>
#include <cwist/sys/app/big_dumb_reply.h>
#include <cwist/sys/io/reactor.h>
#include <stdint.h>
#include <cwist/sys/platform.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for http.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  cwist_http_header_node
 Linked list for headers to handle multiple headers easily. More...
 
struct  cwist_http_request
 
struct  cwist_http_response
 HTTP Response Object. Supports standard string body or Zero-Copy pointer body. More...
 
struct  cwist_server_config
 
struct  cwist_http_async_conn
 

Macros

#define CWIST_HTTP_MAX_HEADER_SIZE   (8 * 1024)
 
#define CWIST_HTTP_MAX_BODY_SIZE   (10 * 1024 * 1024)
 
#define CWIST_HTTP_READ_BUFFER_SIZE   (16 * 1024)
 
#define CWIST_HTTP_TIMEOUT_MS   30000
 
#define CWIST_HTTPS_HANDSHAKE_TIMEOUT_MS   45000 /* Total TLS handshake budget */
 
#define CWIST_HTTP_HEADERS_TIMEOUT_MS   120000 /* Total header read budget */
 
#define CWIST_HTTP_BODY_IDLE_TIMEOUT_MS   60000 /* Abort body read after this much silence */
 
#define CWIST_HTTP2_IDLE_TIMEOUT_MS   300000 /* Default h2 idle budget (env overridable) */
 
#define CWIST_HTTP_KEEP_ALIVE_TIMEOUT_SEC    15 /* Default keep-alive idle connection timeout in seconds */
 
#define CWIST_HTTP_COALESCE_MAX   (256 * 1024)
 

Typedefs

typedef enum cwist_http_method_t cwist_http_method_t
 
typedef enum cwist_http_status_t cwist_http_status_t
 
typedef enum cwist_http_parse_error_t cwist_http_parse_error_t
 Failure reason reported by the request receive APIs. Distinguishes protocol errors (which deserve an error response before close) from an orderly client disconnect (close quietly).
 
typedef struct cwist_http_header_node cwist_http_header_node
 Linked list node for request/response headers.
 
typedef struct cwist_http_request cwist_http_request
 
typedef void(* cwist_http_body_cleanup_fn) (const void *ptr, size_t len, void *ctx)
 
typedef struct cwist_http_response cwist_http_response
 HTTP Response Object. Supports standard string body or Zero-Copy pointer body.
 
typedef struct cwist_server_config cwist_server_config
 
typedef cwist_async_action_t(* cwist_async_handler_t) (int fd, struct cwist_http_async_conn *conn)
 
typedef struct cwist_http_async_conn cwist_http_async_conn_t
 

Enumerations

enum  cwist_http_method_t {
  CWIST_HTTP_GET , CWIST_HTTP_POST , CWIST_HTTP_PUT , CWIST_HTTP_DELETE ,
  CWIST_HTTP_PATCH , CWIST_HTTP_HEAD , CWIST_HTTP_OPTIONS , CWIST_HTTP_CONNECT ,
  CWIST_HTTP_UNKNOWN
}
 
enum  cwist_http_status_t {
  CWIST_HTTP_CONTINUE = 100 , CWIST_HTTP_SWITCHING_PROTOCOLS = 101 , CWIST_HTTP_PROCESSING = 102 , CWIST_HTTP_EARLY_HINTS = 103 ,
  CWIST_HTTP_OK = 200 , CWIST_HTTP_CREATED = 201 , CWIST_HTTP_ACCEPTED = 202 , CWIST_HTTP_NON_AUTHORITATIVE_INFORMATION = 203 ,
  CWIST_HTTP_NO_CONTENT = 204 , CWIST_HTTP_RESET_CONTENT = 205 , CWIST_HTTP_PARTIAL_CONTENT = 206 , CWIST_HTTP_MULTI_STATUS = 207 ,
  CWIST_HTTP_ALREADY_REPORTED = 208 , CWIST_HTTP_IM_USED = 226 , CWIST_HTTP_MULTIPLE_CHOICES = 300 , CWIST_HTTP_MOVED_PERMANENTLY = 301 ,
  CWIST_HTTP_FOUND = 302 , CWIST_HTTP_SEE_OTHER = 303 , CWIST_HTTP_NOT_MODIFIED = 304 , CWIST_HTTP_USE_PROXY = 305 ,
  CWIST_HTTP_TEMPORARY_REDIRECT = 307 , CWIST_HTTP_PERMANENT_REDIRECT = 308 , CWIST_HTTP_BAD_REQUEST = 400 , CWIST_HTTP_UNAUTHORIZED = 401 ,
  CWIST_HTTP_PAYMENT_REQUIRED = 402 , CWIST_HTTP_FORBIDDEN = 403 , CWIST_HTTP_NOT_FOUND = 404 , CWIST_HTTP_METHOD_NOT_ALLOWED = 405 ,
  CWIST_HTTP_NOT_ACCEPTABLE = 406 , CWIST_HTTP_PROXY_AUTHENTICATION_REQUIRED = 407 , CWIST_HTTP_REQUEST_TIMEOUT = 408 , CWIST_HTTP_CONFLICT = 409 ,
  CWIST_HTTP_GONE = 410 , CWIST_HTTP_LENGTH_REQUIRED = 411 , CWIST_HTTP_PRECONDITION_FAILED = 412 , CWIST_HTTP_CONTENT_TOO_LARGE = 413 ,
  CWIST_HTTP_PAYLOAD_TOO_LARGE = 413 , CWIST_HTTP_URI_TOO_LONG = 414 , CWIST_HTTP_UNSUPPORTED_MEDIA_TYPE = 415 , CWIST_HTTP_RANGE_NOT_SATISFIABLE = 416 ,
  CWIST_HTTP_EXPECTATION_FAILED = 417 , CWIST_HTTP_IM_A_TEAPOT = 418 , CWIST_HTTP_MISDIRECTED_REQUEST = 421 , CWIST_HTTP_UNPROCESSABLE_CONTENT = 422 ,
  CWIST_HTTP_LOCKED = 423 , CWIST_HTTP_FAILED_DEPENDENCY = 424 , CWIST_HTTP_TOO_EARLY = 425 , CWIST_HTTP_UPGRADE_REQUIRED = 426 ,
  CWIST_HTTP_PRECONDITION_REQUIRED = 428 , CWIST_HTTP_TOO_MANY_REQUESTS = 429 , CWIST_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431 , CWIST_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451 ,
  CWIST_HTTP_INTERNAL_ERROR = 500 , CWIST_HTTP_INTERNAL_SERVER_ERROR = 500 , CWIST_HTTP_NOT_IMPLEMENTED = 501 , CWIST_HTTP_BAD_GATEWAY = 502 ,
  CWIST_HTTP_SERVICE_UNAVAILABLE = 503 , CWIST_HTTP_GATEWAY_TIMEOUT = 504 , CWIST_HTTP_VERSION_NOT_SUPPORTED = 505 , CWIST_HTTP_VARIANT_ALSO_NEGOTIATES = 506 ,
  CWIST_HTTP_INSUFFICIENT_STORAGE = 507 , CWIST_HTTP_LOOP_DETECTED = 508 , CWIST_HTTP_NOT_EXTENDED = 510 , CWIST_HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511
}
 
enum  cwist_http_parse_error_t {
  CWIST_HTTP_PARSE_OK = 0 , CWIST_HTTP_PARSE_EOF , CWIST_HTTP_PARSE_MALFORMED , CWIST_HTTP_PARSE_HEADER_OVERFLOW ,
  CWIST_HTTP_PARSE_BODY_TOO_LARGE , CWIST_HTTP_PARSE_TE_UNSUPPORTED , CWIST_HTTP_PARSE_EXPECT_FAILED
}
 Failure reason reported by the request receive APIs. Distinguishes protocol errors (which deserve an error response before close) from an orderly client disconnect (close quietly). More...
 
enum  cwist_async_action_t { CWIST_ASYNC_CLOSE = 0 , CWIST_ASYNC_REARM , CWIST_ASYNC_DETACH , CWIST_ASYNC_DEFER }
 
enum  cwist_async_send_status_t { CWIST_ASYNC_SEND_KEEPALIVE = 0 , CWIST_ASYNC_SEND_CLOSE , CWIST_ASYNC_SEND_DEFERRED }
 
enum  cwist_coalesce_flush_status_t { CWIST_COALESCE_FLUSH_DONE = 0 , CWIST_COALESCE_FLUSH_PARKED , CWIST_COALESCE_FLUSH_ERROR }
 
enum  cwist_recv_status_t { CWIST_RECV_OK = 0 , CWIST_RECV_NEED_MORE , CWIST_RECV_FATAL }
 

Functions

long get_cpu_cores (void)
 Return the number of CPUs available to this process.
 
long get_optimal_thread_count (void)
 Compute the HTTP worker/event-loop thread count.
 
const char * cwist_http_status_reason (int status)
 Return the standard reason phrase for an HTTP status code (e.g. 404 -> "Not Found"). Unknown codes return NULL; applications may also assign any int to a response status_code for non-standard codes.
 
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)
 Blocking read of one full request from a classic-pool connection.
 
void cwist_http_send_error_response (int fd, int status, const char *msg)
 Send a minimal HTTP/1.x error response (Connection: close) on a socket, used to answer malformed requests before dropping them.
 
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.
 
int headers_have_content_length (cwist_http_header_node *headers)
 Detect whether a header list already defines Content-Length.
 
int cwist_http_pool_init (void)
 Initialize the handler pool in the mode CWIST_C1M_MODE selects.
 
int cwist_http_pool_init_mode (bool use_c1m)
 Initialize the handler pool as reactor workers (use_c1m) or the classic thread pool, without reading CWIST_C1M_MODE.
 
void cwist_http_pool_limit_core (unsigned int limit)
 Fix the HTTP pool worker count to a specific value.
 
void cwist_http_pool_submit (int client_fd, void(*handler)(int, void *), void *ctx)
 Queue a connection for handling on the classic dynamic pool.
 
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.
 
void cwist_http_pool_destroy (void)
 Tear down both pool variants.
 
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.
 
long cwist_http_continuation_shed_count (void)
 
long cwist_http_inflight_count (void)
 
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 reactor thread.
 
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 connection itself (deferred-completion handoff).
 
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.
 
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.
 
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.
 
int cwist_http_coalesce_error_response (cwist_http_async_conn_t *conn, int status)
 Build a minimal text error response into the coalesce stash.
 
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 stash so one reactor turn flushes many responses with a single write.
 
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.
 
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 level-triggered, so if bytes remain after a short recv the one-shot re-arm fires again immediately. This skips the guaranteed-EAGAIN second recv that otherwise costs one wasted syscall per request on non-pipelined keep-alive traffic.
 
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 consumed bytes are removed from the stash and *out holds a fully parsed request.
 
Request Lifecycle

— API Functions —

cwist_http_request * cwist_http_request_create (void)
 Allocate and initialize a default HTTP request object.
 
void cwist_http_request_destroy (cwist_http_request *req)
 Destroy a parsed HTTP request and all nested allocations it owns.
 
cwist_http_request * cwist_http_parse_request (const char *raw_request)
 Parse a NUL-terminated raw HTTP request buffer.
 
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 and need not be NUL-terminated).
 
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). Caller frees out with cwist_free(). File-streaming bodies cannot be serialized; the call fails for them.
 
Streaming producer (chunked response bodies from handlers)

A handler switches the response to chunked mode, writes the body incrementally, and ends the stream. Under cwist_app_dispatch_stream() each write is pushed to the host sink immediately (head first, lazily on the first write), so SSE-style handlers deliver chunks while still running; under cwist_app_dispatch_memory() the framed bytes buffer and serialize like any other body. A body assigned before begin is discarded; writes after end fail. When the handler returns without end, dispatch finalizes the stream implicitly.

int cwist_http_response_stream_begin (cwist_http_response *res)
 Switch a response to chunked streaming mode.
 
int cwist_http_response_stream_write (cwist_http_response *res, const char *data, size_t len)
 Append one data chunk to a streaming response.
 
int cwist_http_response_stream_end (cwist_http_response *res)
 Terminate a streaming response with the final zero-length chunk and flush.
 
Request Data Processing
cwist_sstring * cwist_get_client_ip_from_fd (int fd)
 Resolve the peer IP address for a connected client socket.
 
void cwist_http_format_date (time_t t, char *buf, size_t len)
 Format a time_t as an HTTP-date (RFC 7231).
 
time_t cwist_http_parse_date (const char *str)
 Parse an HTTP-date string into a time_t.
 
Response Lifecycle
cwist_http_response * cwist_http_response_create (void)
 Allocate a response backed by its own arena.
 
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 shares its lifetime: cwist_http_response_destroy releases body/header resources but leaves the arena itself to the owner. arena must outlive the response.
 
void cwist_http_response_destroy (cwist_http_response *res)
 Destroy an HTTP response and release any attached body resources.
 
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 memory mapped regions. The pointer must remain valid until the response is sent.
 
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.
 
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.
 
cwist_sstring * cwist_http_stringify_response (cwist_http_response *res)
 Materialize an HTTP response into a contiguous string for debugging or TLS writes.
 
cwist_error_t cwist_http_send_response (int client_fd, cwist_http_response *res)
 Serialize and send an HTTP response to a connected client socket.
 
cwist_error_t cwist_http_send_response_head (int client_fd, cwist_http_response *res)
 Send only the status line and headers of a response (HEAD replies). Content-Length still reflects the would-be body; body resources are released without being transmitted.
 
bool cwist_tcp_cork_enabled (void)
 Whether the TCP_CORK coalescing layer is active for cleartext HTTP/1.1 responses. Enabled at runtime with CWIST_USE_TCP_CORK=1 (burst size via CWIST_TCP_CORK_BURST, default 256 KiB); always false off-Linux.
 
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. Used by the TLS send path to stream header block and body separately.
 
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.
 
Header Manipulation
cwist_error_t cwist_http_header_add (cwist_http_header_node **head, const char *key, const char *value)
 Prepend a header to the list.
 
char * cwist_http_header_get (cwist_http_header_node *head, const char *key)
 Finds a header value by key.
 
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).
 
void cwist_http_header_free_all (cwist_http_header_node *head)
 Destroy every node in a request or response header list.
 
void cwist_http_response_add_security_headers (cwist_http_response *res)
 Add default security headers (CSP, X-Frame-Options, Permissions-Policy, etc.) if missing.
 
void cwist_http_response_add_hsts (cwist_http_response *res)
 Add Strict-Transport-Security to a TLS response (HTTPS only).
 
Helpers
const char * cwist_http_method_to_string (cwist_http_method_t method)
 Convert a HTTP method enum into its wire-format token.
 
cwist_http_method_t cwist_http_string_to_method (const char *method_str)
 Parse a NUL-terminated method string into a method enum.
 
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.
 
TCP Socket Helpers
int cwist_make_socket_ipv4 (struct sockaddr_in *sockv4, const char *address, uint16_t port, uint16_t backlog)
 Create an IPv4 socket and perform bind/listen.
 
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.
 

Variables

const int CWIST_CREATE_SOCKET_FAILED
 
const int CWIST_HTTP_UNAVAILABLE_ADDRESS
 
const int CWIST_HTTP_BIND_FAILED
 
const int CWIST_HTTP_SETSOCKOPT_FAILED
 
const int CWIST_HTTP_LISTEN_FAILED
 

Detailed Description

HTTP Protocol Definitions and Helpers.

Definition in file http.h.

Macro Definition Documentation

◆ CWIST_HTTP2_IDLE_TIMEOUT_MS

#define CWIST_HTTP2_IDLE_TIMEOUT_MS   300000 /* Default h2 idle budget (env overridable) */

Definition at line 149 of file http.h.

◆ CWIST_HTTP_BODY_IDLE_TIMEOUT_MS

#define CWIST_HTTP_BODY_IDLE_TIMEOUT_MS   60000 /* Abort body read after this much silence */

Definition at line 148 of file http.h.

◆ CWIST_HTTP_COALESCE_MAX

#define CWIST_HTTP_COALESCE_MAX   (256 * 1024)

Definition at line 547 of file http.h.

◆ CWIST_HTTP_HEADERS_TIMEOUT_MS

#define CWIST_HTTP_HEADERS_TIMEOUT_MS   120000 /* Total header read budget */

Definition at line 147 of file http.h.

◆ CWIST_HTTP_KEEP_ALIVE_TIMEOUT_SEC

#define CWIST_HTTP_KEEP_ALIVE_TIMEOUT_SEC    15 /* Default keep-alive idle connection timeout in seconds */

Definition at line 150 of file http.h.

◆ CWIST_HTTP_MAX_BODY_SIZE

#define CWIST_HTTP_MAX_BODY_SIZE   (10 * 1024 * 1024)

Definition at line 143 of file http.h.

◆ CWIST_HTTP_MAX_HEADER_SIZE

#define CWIST_HTTP_MAX_HEADER_SIZE   (8 * 1024)

— Constants and Limits —

Definition at line 142 of file http.h.

◆ CWIST_HTTP_READ_BUFFER_SIZE

#define CWIST_HTTP_READ_BUFFER_SIZE   (16 * 1024)

Definition at line 144 of file http.h.

◆ CWIST_HTTP_TIMEOUT_MS

#define CWIST_HTTP_TIMEOUT_MS   30000

Definition at line 145 of file http.h.

◆ CWIST_HTTPS_HANDSHAKE_TIMEOUT_MS

#define CWIST_HTTPS_HANDSHAKE_TIMEOUT_MS   45000 /* Total TLS handshake budget */

Definition at line 146 of file http.h.

Typedef Documentation

◆ cwist_async_handler_t

typedef cwist_async_action_t(* cwist_async_handler_t) (int fd, struct cwist_http_async_conn *conn)

Definition at line 454 of file http.h.

◆ cwist_http_async_conn_t

◆ cwist_http_body_cleanup_fn

typedef void(* cwist_http_body_cleanup_fn) (const void *ptr, size_t len, void *ctx)

Definition at line 194 of file http.h.

◆ cwist_http_header_node

Linked list node for request/response headers.

— Structures —

◆ cwist_http_method_t

◆ cwist_http_parse_error_t

Failure reason reported by the request receive APIs. Distinguishes protocol errors (which deserve an error response before close) from an orderly client disconnect (close quietly).

◆ cwist_http_request

◆ cwist_http_response

HTTP Response Object. Supports standard string body or Zero-Copy pointer body.

◆ cwist_http_status_t

◆ cwist_server_config

Enumeration Type Documentation

◆ cwist_async_action_t

Enumerator
CWIST_ASYNC_CLOSE 
CWIST_ASYNC_REARM 
CWIST_ASYNC_DETACH 
CWIST_ASYNC_DEFER 

Definition at line 447 of file http.h.

◆ cwist_async_send_status_t

Enumerator
CWIST_ASYNC_SEND_KEEPALIVE 
CWIST_ASYNC_SEND_CLOSE 
CWIST_ASYNC_SEND_DEFERRED 

Definition at line 528 of file http.h.

◆ cwist_coalesce_flush_status_t

Enumerator
CWIST_COALESCE_FLUSH_DONE 
CWIST_COALESCE_FLUSH_PARKED 
CWIST_COALESCE_FLUSH_ERROR 

Definition at line 549 of file http.h.

◆ cwist_http_method_t

— Enums —

Enumerator
CWIST_HTTP_GET 
CWIST_HTTP_POST 
CWIST_HTTP_PUT 
CWIST_HTTP_DELETE 
CWIST_HTTP_PATCH 
CWIST_HTTP_HEAD 
CWIST_HTTP_OPTIONS 
CWIST_HTTP_CONNECT 
CWIST_HTTP_UNKNOWN 

Definition at line 37 of file http.h.

◆ cwist_http_parse_error_t

Failure reason reported by the request receive APIs. Distinguishes protocol errors (which deserve an error response before close) from an orderly client disconnect (close quietly).

Enumerator
CWIST_HTTP_PARSE_OK 
CWIST_HTTP_PARSE_EOF 
CWIST_HTTP_PARSE_MALFORMED 
CWIST_HTTP_PARSE_HEADER_OVERFLOW 
CWIST_HTTP_PARSE_BODY_TOO_LARGE 
CWIST_HTTP_PARSE_TE_UNSUPPORTED 
CWIST_HTTP_PARSE_EXPECT_FAILED 

Definition at line 131 of file http.h.

◆ cwist_http_status_t

Enumerator
CWIST_HTTP_CONTINUE 
CWIST_HTTP_SWITCHING_PROTOCOLS 
CWIST_HTTP_PROCESSING 
CWIST_HTTP_EARLY_HINTS 
CWIST_HTTP_OK 
CWIST_HTTP_CREATED 
CWIST_HTTP_ACCEPTED 
CWIST_HTTP_NON_AUTHORITATIVE_INFORMATION 
CWIST_HTTP_NO_CONTENT 
CWIST_HTTP_RESET_CONTENT 
CWIST_HTTP_PARTIAL_CONTENT 
CWIST_HTTP_MULTI_STATUS 
CWIST_HTTP_ALREADY_REPORTED 
CWIST_HTTP_IM_USED 
CWIST_HTTP_MULTIPLE_CHOICES 
CWIST_HTTP_MOVED_PERMANENTLY 
CWIST_HTTP_FOUND 
CWIST_HTTP_SEE_OTHER 
CWIST_HTTP_NOT_MODIFIED 
CWIST_HTTP_USE_PROXY 
CWIST_HTTP_TEMPORARY_REDIRECT 
CWIST_HTTP_PERMANENT_REDIRECT 
CWIST_HTTP_BAD_REQUEST 
CWIST_HTTP_UNAUTHORIZED 
CWIST_HTTP_PAYMENT_REQUIRED 
CWIST_HTTP_FORBIDDEN 
CWIST_HTTP_NOT_FOUND 
CWIST_HTTP_METHOD_NOT_ALLOWED 
CWIST_HTTP_NOT_ACCEPTABLE 
CWIST_HTTP_PROXY_AUTHENTICATION_REQUIRED 
CWIST_HTTP_REQUEST_TIMEOUT 
CWIST_HTTP_CONFLICT 
CWIST_HTTP_GONE 
CWIST_HTTP_LENGTH_REQUIRED 
CWIST_HTTP_PRECONDITION_FAILED 
CWIST_HTTP_CONTENT_TOO_LARGE 
CWIST_HTTP_PAYLOAD_TOO_LARGE 
CWIST_HTTP_URI_TOO_LONG 
CWIST_HTTP_UNSUPPORTED_MEDIA_TYPE 
CWIST_HTTP_RANGE_NOT_SATISFIABLE 
CWIST_HTTP_EXPECTATION_FAILED 
CWIST_HTTP_IM_A_TEAPOT 
CWIST_HTTP_MISDIRECTED_REQUEST 
CWIST_HTTP_UNPROCESSABLE_CONTENT 
CWIST_HTTP_LOCKED 
CWIST_HTTP_FAILED_DEPENDENCY 
CWIST_HTTP_TOO_EARLY 
CWIST_HTTP_UPGRADE_REQUIRED 
CWIST_HTTP_PRECONDITION_REQUIRED 
CWIST_HTTP_TOO_MANY_REQUESTS 
CWIST_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE 
CWIST_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS 
CWIST_HTTP_INTERNAL_ERROR 
CWIST_HTTP_INTERNAL_SERVER_ERROR 
CWIST_HTTP_NOT_IMPLEMENTED 
CWIST_HTTP_BAD_GATEWAY 
CWIST_HTTP_SERVICE_UNAVAILABLE 
CWIST_HTTP_GATEWAY_TIMEOUT 
CWIST_HTTP_VERSION_NOT_SUPPORTED 
CWIST_HTTP_VARIANT_ALSO_NEGOTIATES 
CWIST_HTTP_INSUFFICIENT_STORAGE 
CWIST_HTTP_LOOP_DETECTED 
CWIST_HTTP_NOT_EXTENDED 
CWIST_HTTP_NETWORK_AUTHENTICATION_REQUIRED 

Definition at line 49 of file http.h.

◆ cwist_recv_status_t

Enumerator
CWIST_RECV_OK 
CWIST_RECV_NEED_MORE 
CWIST_RECV_FATAL 

Definition at line 565 of file http.h.

Function Documentation

◆ cwist_accept_socket()

cwist_error_t cwist_accept_socket ( int  server_fd,
struct sockaddr *  sockv4,
void(*)(int client_fd, void *ctx)  handler_func,
void *  ctx 
)

Accept sockets and invoke a handler callback.

◆ cwist_get_client_ip_from_fd()

cwist_sstring * cwist_get_client_ip_from_fd ( int  fd)

Resolve the peer IP address for a connected client socket.

Parameters
fdConnected client socket descriptor.
Returns
New sstring with the textual IP; "127.0.0.1" when unavailable.

Definition at line 2141 of file http.c.

References cap, cwist_sstring_assign(), and cwist_sstring_create().

◆ cwist_http_async_conn_fill()

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 level-triggered, so if bytes remain after a short recv the one-shot re-arm fires again immediately. This skips the guaranteed-EAGAIN second recv that otherwise costs one wasted syscall per request on non-pipelined keep-alive traffic.

Returns
0 on data, EAGAIN, or EOF (recorded in peer_eof); -1 on fatal error.

Definition at line 4956 of file http.c.

References cwist_http_async_conn::cap, cap, cwist_http_async_conn::fd, cwist_http_async_conn::len, cwist_http_async_conn::peer_eof, cwist_http_async_conn::rbuf, cwist_http_async_conn::rx_data_ready, cwist_http_async_conn::rx_recv_inflight, and cwist_http_async_conn::virgin.

Referenced by cwist_app_http_handler_async().

◆ cwist_http_async_rearm()

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.

Handles deferred-completion shutdown races, peer EOF, pipelined bytes (posted as a continuation so other connections run between batches), and RECV-first re-arm with a POLL fallback.

Returns
false when the connection was closed or the re-arm failed.

Definition at line 1357 of file http.c.

References cap, cwist_alloc(), cwist_fast_monotonic_sec(), cwist_free(), CWIST_METRIC_HTTP_CONTINUATION_SHED, cwist_metric_inc(), cwist_metrics_registry(), cwist_http_async_conn::handler, cwist_http_async_conn::last_active_sec, cwist_http_async_conn::len, cwist_http_async_conn::peer_eof, and cwist_http_async_conn::user_ctx.

Referenced by cwist_app_http_handler_async(), and cwist_http_async_send_response().

◆ cwist_http_async_send_response()

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 reactor thread.

Pointer bodies park by reference (ownership moves to the park state); other bodies deep-copy the unsent remainder. File streams use the non-blocking sendfile path. On completion the connection is re-armed for keep-alive or closed.

Definition at line 3413 of file http.c.

References cwist_http_response::body, cap, cwist_alloc(), CWIST_FILE_BEGIN_HANDLED, cwist_free(), cwist_http_async_rearm(), CWIST_HTTP_MAX_HEADER_SIZE, cwist_http_send_response(), cwist_http_sendmsg_speculative(), cwist_reactor_add_out(), CWIST_WRITE_DONE, CWIST_WRITE_PENDING, cwist_sstring::data, g_cwist_running, cwist_http_response::is_ptr_body, cwist_http_async_conn::len, MSG_DONTWAIT, cwist_http_response::ptr_body, cwist_http_response::ptr_body_len, cwist_sstring::size, and cwist_http_response::use_file_stream.

◆ cwist_http_coalesce_append()

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.

Returns
0 on success, -1 when the data would exceed the coalesce cap or the stash could not be grown.

Definition at line 3653 of file http.c.

References cap, cwist_alloc(), cwist_fast_monotonic_sec(), cwist_free(), CWIST_HTTP_COALESCE_MAX, cwist_http_async_conn::obuf, cwist_http_async_conn::ocap, cwist_http_async_conn::olen, and cwist_http_async_conn::stash_grown_sec.

Referenced by cwist_http_coalesce_error_response().

◆ cwist_http_coalesce_error_response()

int cwist_http_coalesce_error_response ( cwist_http_async_conn_t *  conn,
int  status 
)

Build a minimal text error response into the coalesce stash.

Returns
0 on success, -1 when the response could not be appended.

Definition at line 3756 of file http.c.

References cap, cwist_http_coalesce_append(), and cwist_http_status_reason().

◆ cwist_http_coalesce_flush()

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.

On a partial write the unsent remainder is deep-copied into the parked writer, which re-arms or closes the connection when it drains.

Returns
DONE when fully written, PARKED when deferred, ERROR otherwise.

Definition at line 3682 of file http.c.

References cap, cwist_alloc(), CWIST_COALESCE_FLUSH_DONE, CWIST_COALESCE_FLUSH_ERROR, CWIST_COALESCE_FLUSH_PARKED, cwist_free(), cwist_http_sendmsg_speculative(), cwist_reactor_add_out(), CWIST_WRITE_DONE, CWIST_WRITE_PENDING, g_cwist_running, cwist_http_async_conn::len, MSG_DONTWAIT, cwist_http_async_conn::obuf, cwist_http_async_conn::olen, and cwist_http_async_conn::reactor.

Referenced by cwist_app_http_handler_async().

◆ cwist_http_coalesce_flush_blocking()

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.

Returns
0 on success, -1 on send failure.

Definition at line 3737 of file http.c.

References cap, MSG_DONTWAIT, cwist_http_async_conn::obuf, and cwist_http_async_conn::olen.

Referenced by cwist_http_receive_request_nb(), and cwist_http_send_response_coalesced().

◆ cwist_http_continuation_shed_count()

long cwist_http_continuation_shed_count ( void  )

Continuations dropped because the reactor post queue was full; each one closed its connection. Exported for metrics/observability.

Definition at line 293 of file http.c.

References cap.

Referenced by cwist_metric_load(), and cwist_metrics_render_prometheus().

◆ cwist_http_format_date()

void cwist_http_format_date ( time_t  t,
char *  buf,
size_t  len 
)

Format a time_t as an HTTP-date (RFC 7231).

Format a time_t as an HTTP-date (RFC 7231).

Parameters
fdConnected client socket descriptor.
Returns
Heap-allocated string containing the textual IP address.

Format a time_t as an HTTP-date (RFC 7231).

Parameters
tUnix timestamp.
bufOutput buffer.
lenBuffer capacity.

Definition at line 2108 of file http.c.

References cap.

◆ cwist_http_header_add()

cwist_error_t cwist_http_header_add ( cwist_http_header_node **  head,
const char *  key,
const char *  value 
)

Prepend a header to the list.

Returns
INT16 0 on success; INT16 -1 when head is NULL or key/value contains CR or LF (nothing is added); a JSON error on allocation failure. Check with cwist_error_is_ok().

Prepend a header to the list.

Add a header to the list.

Parameters
headHeader-list head pointer to update.
keyHeader name to store.
valueHeader value to store.
Returns
Tagged CWIST error: INT16 0 on success, INT16 -1 when head is NULL or key/value contains CR or LF (nothing is added), or a JSON error on allocation failure.

Definition at line 1861 of file http.c.

Referenced by cwist_http_response_send_file().

◆ cwist_http_header_free_all()

void cwist_http_header_free_all ( cwist_http_header_node *  head)

Destroy every node in a request or response header list.

Free all headers.

Parameters
headHead of the header linked list.

Definition at line 2006 of file http.c.

Referenced by cwist_http_request_destroy(), and cwist_http_response_destroy().

◆ cwist_http_header_get()

char * cwist_http_header_get ( cwist_http_header_node *  head,
const char *  key 
)

Finds a header value by key.

Returns
Raw C-string pointer (NULL if not found).

Finds a header value by key.

Find a header value by key.

Parameters
headHead of the header linked list.
keyHeader name to search for.
Returns
Raw header value string, or NULL when absent.

Definition at line 1880 of file http.c.

Referenced by cwist_http_response_add_hsts(), cwist_http_response_add_security_headers(), and cwist_http_response_send_file().

◆ cwist_http_header_remove()

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).

Returns
Number of nodes removed.

Remove all headers matching a name, honoring per-node ownership (heap vs arena vs borrowed buffers).

Parameters
headPointer to the header-list head; updated as nodes are removed.
keyHeader name (case-insensitive) to remove; all duplicates go.
Returns
Number of nodes removed.

Ownership is handled per node: heap-owned nodes are freed, arena-owned nodes are left for the arena to reclaim, and borrowed key/value buffers are never freed individually. Applications should call this instead of hand-rolling unlink loops, which have repeatedly gotten that bookkeeping wrong.

Definition at line 1907 of file http.c.

References cwist_http_header_node::arena_owned, cap, cwist_free(), cwist_sstring_destroy(), cwist_sstring::data, head, cwist_http_header_node::key, cwist_http_header_node::next, and cwist_http_header_node::value.

Referenced by cwist_http_response_html_redirect().

◆ cwist_http_inflight_count()

long cwist_http_inflight_count ( void  )

Live C1M connections (accepted, not yet closed). The shutdown drain in cwist_app_listen_ex polls this to exit early once nothing is left. Acquire pairs with the handlers' release decrement, so once the drain sees zero, everything those handlers did happens before cwist_app_destroy().

Definition at line 301 of file http.c.

References cap.

◆ cwist_http_method_to_string()

const char * cwist_http_method_to_string ( cwist_http_method_t  method)

Convert a HTTP method enum into its wire-format token.

Convert method enum to string.

Parameters
methodHTTP method enum value.
Returns
Static string name for the method.

Definition at line 1554 of file http.c.

◆ cwist_http_parse_date()

time_t cwist_http_parse_date ( const char *  str)

Parse an HTTP-date string into a time_t.

Parameters
strHTTP-date string.
Returns
Parsed timestamp, or (time_t)-1 on failure.

Definition at line 2119 of file http.c.

References cap.

◆ cwist_http_parse_request()

cwist_http_request * cwist_http_parse_request ( const char *  raw_request)

Parse a NUL-terminated raw HTTP request buffer.

Parse a raw HTTP request string.

Returns
Parsed request, or NULL when no header terminator exists.

Definition at line 4412 of file http.c.

◆ cwist_http_parse_request_len()

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 and need not be NUL-terminated).

Parse an HTTP/1.x request held in a memory buffer (length-based; the buffer may contain a binary body and need not be NUL-terminated).

Returns
Parsed request, or NULL when no header terminator exists.

Definition at line 4424 of file http.c.

References cap.

Referenced by cwist_app_dispatch_memory(), and cwist_app_dispatch_stream().

◆ cwist_http_pool_destroy()

void cwist_http_pool_destroy ( void  )

Tear down both pool variants.

Stops new work, drains queued tasks, retires the reactor generation and waits for in-flight posters, then stops, joins, and frees the C1M reactors.

Definition at line 836 of file http.c.

References cap, cwist_free(), cwist_reactor_destroy(), and cwist_reactor_stop().

Referenced by cwist_async_server_loop(), and cwist_http_server_loop().

◆ cwist_http_pool_init()

int cwist_http_pool_init ( void  )

Initialize the handler pool in the mode CWIST_C1M_MODE selects.

Initialize the handler pool in the mode CWIST_C1M_MODE selects.

Returns
0 on success, -1 on failure.

Definition at line 678 of file http.c.

References cap, and cwist_http_pool_init_mode().

◆ cwist_http_pool_init_mode()

int cwist_http_pool_init_mode ( bool  use_c1m)

Initialize the handler pool as reactor workers (use_c1m) or the classic thread pool, without reading CWIST_C1M_MODE.

Initialize the handler pool as reactor workers (use_c1m) or the classic thread pool, without reading CWIST_C1M_MODE.

Parameters
use_c1mtrue for the event-driven reactor (C1M) pool, false for the classic dynamic thread pool.
Returns
0 on success, -1 on failure (partially created state is cleaned up by cwist_http_pool_destroy()).

Definition at line 696 of file http.c.

References cap, cwist_alloc(), cwist_free(), cwist_reactor_create(), and get_optimal_thread_count().

Referenced by cwist_async_server_loop(), cwist_http_pool_init(), and cwist_http_server_loop().

◆ cwist_http_pool_limit_core()

void cwist_http_pool_limit_core ( unsigned int  limit)

Fix the HTTP pool worker count to a specific value.

Parameters
limitFixed thread count; 0 restores automatic sizing.

Definition at line 145 of file http.c.

References cap.

◆ cwist_http_pool_rearm_current()

bool cwist_http_pool_rearm_current ( int  client_fd,
void(*)(int, void *)  handler,
void *  ctx 
)

Requeue the current connection's next handler turn on the pool.

Returns
false for invalid arguments (fd < 0 or NULL handler).

Definition at line 824 of file http.c.

References cwist_http_pool_submit().

◆ cwist_http_pool_submit()

void cwist_http_pool_submit ( int  client_fd,
void(*)(int, void *)  handler,
void *  ctx 
)

Queue a connection for handling on the classic dynamic pool.

Enforces the inflight limit (over-limit connections get a bare 503 and are closed) and spawns an extra worker when queued demand exceeds idle capacity.

Definition at line 772 of file http.c.

References cap, cwist_alloc(), and MSG_DONTWAIT.

Referenced by cwist_http_pool_rearm_current(), and cwist_http_server_loop().

◆ cwist_http_pool_submit_async()

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.

Enforces the inflight limit (503 + close on overflow), sets the socket non-blocking, allocates the connection shell, and load-balances with power-of-two-choices over the per-worker load counters.

Returns
false when the connection was rejected or the initial arm failed.

Definition at line 1468 of file http.c.

References cap, cwist_alloc(), cwist_fast_monotonic_sec(), cwist_sched_p2c_select_worker(), cwist_http_async_conn::fd, cwist_http_async_conn::handler, cwist_http_async_conn::last_active_sec, MSG_DONTWAIT, cwist_http_async_conn::reactor, cwist_http_async_conn::stash_grown_sec, cwist_http_async_conn::user_ctx, cwist_http_async_conn::virgin, and cwist_http_async_conn::worker_id.

◆ cwist_http_receive_request()

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 
)

Blocking read of one full request from a classic-pool connection.

Receive and parse an HTTP request from a socket.

Bounds the header read by CWIST_HTTP_HEADERS_TIMEOUT_MS, answers a validated Expect: 100-continue, then completes the body per Content-Length or chunked framing. Leftover pipelined bytes are compacted into read_buf and *buf_len is updated.

Returns
Parsed request, or NULL with *err_out set (defaults to CWIST_HTTP_PARSE_EOF).

Definition at line 4722 of file http.c.

◆ cwist_http_receive_request_nb()

◆ cwist_http_request_create()

cwist_http_request * cwist_http_request_create ( void  )

Allocate and initialize a default HTTP request object.

Create a new HTTP request object.

Returns
Newly allocated request, or NULL on allocation failure.

Definition at line 2056 of file http.c.

◆ cwist_http_request_destroy()

void cwist_http_request_destroy ( cwist_http_request *  req)

Destroy a parsed HTTP request and all nested allocations it owns.

Destroy an HTTP request object.

Parameters
reqRequest object to destroy.

Definition at line 2180 of file http.c.

Referenced by cwist_http_receive_request(), and cwist_http_receive_request_nb().

◆ cwist_http_response_add_hsts()

void cwist_http_response_add_hsts ( cwist_http_response *  res)

Add Strict-Transport-Security to a TLS response (HTTPS only).

RFC 6797 section 7.2 prohibits HSTS over plain HTTP. Call this from HTTPS handlers after cwist_http_response_add_security_headers(). No-op when the header is already present.

RFC 6797 section 7.2 prohibits HSTS over plain HTTP. Call this only from an HTTPS handler, after cwist_http_response_add_security_headers(). No-op when the header is already present.

Parameters
resResponse object to populate.

Definition at line 1993 of file http.c.

References cwist_http_response::arena, cwist_http_header_get(), and cwist_http_response::headers.

◆ cwist_http_response_add_security_headers()

void cwist_http_response_add_security_headers ( cwist_http_response *  res)

Add default security headers (CSP, X-Frame-Options, Permissions-Policy, etc.) if missing.

Transport-agnostic: safe to call on HTTP and HTTPS responses. HSTS is intentionally excluded — use cwist_http_response_add_hsts() on TLS only.

Add default security headers (CSP, X-Frame-Options, Permissions-Policy, etc.) if missing.

Add default security headers (CSP, X-Frame-Options, etc.) if missing.

Safe to call for both HTTP and HTTPS responses. HSTS is intentionally omitted here because RFC 6797 section 7.2 forbids sending it over plain HTTP; browsers ignore it on non-TLS connections anyway. Use cwist_http_response_add_hsts() from your HTTPS handler to add it there.

Parameters
resResponse object to populate.

Definition at line 1935 of file http.c.

◆ cwist_http_response_create()

cwist_http_response * cwist_http_response_create ( void  )

Allocate a response backed by its own arena.

Create a new HTTP response object.

Returns
Newly allocated response, or NULL on failure.

Definition at line 2291 of file http.c.

Referenced by cwist_http_response_create_in_arena().

◆ cwist_http_response_create_in_arena()

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 shares its lifetime: cwist_http_response_destroy releases body/header resources but leaves the arena itself to the owner. arena must outlive the response.

Create a response inside an existing (request) arena. The response is bump-allocated from arena and shares its lifetime: cwist_http_response_destroy releases body/header resources but leaves the arena itself to the owner. arena must outlive the response.

Parameters
arenaArena to allocate from; NULL falls back to cwist_http_response_create().

Definition at line 2306 of file http.c.

References cwist_http_response_create().

Referenced by cwist_http2_push_resource(), and cwist_http2_serve_connection_ex().

◆ cwist_http_response_destroy()

void cwist_http_response_destroy ( cwist_http_response *  res)

Destroy an HTTP response and release any attached body resources.

Destroy an HTTP response object.

Parameters
resResponse object to destroy.

Definition at line 2315 of file http.c.

◆ cwist_http_response_send_file()

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.

Parameters
resResponse object to populate.
file_pathFilesystem path to the file that should be served.
content_type_hintOptional MIME type override.
out_sizeOptional output pointer receiving the file size in bytes.
Returns
Tagged CWIST error describing success or failure.

Definition at line 5223 of file http.c.

References cwist_http_response::body, cap, cwist_alloc(), CWIST_ENDPOINT_FILE, CWIST_ERR_INT16, cwist_free(), cwist_http_header_add(), cwist_http_header_get(), CWIST_HTTP_MAX_BODY_SIZE, CWIST_HTTP_OK, cwist_sstring_adopt_len(), cwist_sstring_borrow(), cwist_http_response::endpoint_opts, cwist_http_response::file_stream_auto_close, cwist_http_response::file_stream_fd, cwist_http_response::file_stream_len, cwist_http_response::file_stream_offset, cwist_http_response::headers, make_error(), cwist_http_response::status_code, and cwist_http_response::use_file_stream.

◆ cwist_http_response_serialize()

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). Caller frees out with cwist_free(). File-streaming bodies cannot be serialized; the call fails for them.

Returns
0 on success, -1 on failure.

Serialize a response into a freshly allocated HTTP/1.x buffer (status line + headers + body). Caller frees out with cwist_free(). File-streaming bodies cannot be serialized; the call fails for them.

Streaming responses are auto-finalized so the chunked terminator is present even when the handler skipped cwist_http_response_stream_end().

Returns
0 with out and out_len set, or -1 (invalid input, file-stream body, or allocation failure).

Definition at line 4439 of file http.c.

References cwist_http_response::body, cap, cwist_alloc(), CWIST_HTTP_MAX_HEADER_SIZE, cwist_http_response_stream_end(), cwist_sstring::data, cwist_http_response::is_ptr_body, cwist_http_response::ptr_body, cwist_http_response::ptr_body_len, cwist_sstring::size, cwist_http_response::stream_buf, cwist_http_response::stream_ended, cwist_http_response::stream_mode, and cwist_http_response::use_file_stream.

Referenced by cwist_app_dispatch_memory().

◆ cwist_http_response_set_alt_svc()

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.

Parameters
resResponse object to modify.
alt_svcAlt-Svc header value (e.g., h3=":443"; ma=86400). Pass NULL to clear any previously set value.

Definition at line 2376 of file http.c.

References cwist_http_response::alt_svc, cap, cwist_free(), and cwist_strdup().

◆ cwist_http_response_set_body_ptr()

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 memory mapped regions. The pointer must remain valid until the response is sent.

Sets a direct pointer for the response body (Zero Copy). Use this when serving large files from memory mapped regions. The pointer must remain valid until the response is sent.

Parameters
resResponse object to modify.
ptrExternal body pointer.
lenLength of the external body in bytes.

Definition at line 2346 of file http.c.

References cap, and cwist_http_response_set_body_ptr_managed().

Referenced by cwist_assets_respond().

◆ cwist_http_response_set_body_ptr_managed()

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.

Parameters
resResponse object to modify.
ptrExternal body pointer.
lenLength of the external body in bytes.
cleanupOptional cleanup callback for the body pointer.
ctxOpaque context forwarded to the cleanup callback.

Definition at line 2358 of file http.c.

References cwist_http_response::is_ptr_body, cwist_http_response::ptr_body, cwist_http_response::ptr_body_cleanup, cwist_http_response::ptr_body_cleanup_ctx, and cwist_http_response::ptr_body_len.

Referenced by cwist_http_response_set_body_ptr().

◆ cwist_http_response_stream_begin()

int cwist_http_response_stream_begin ( cwist_http_response *  res)

Switch a response to chunked streaming mode.

Discards any body assigned before the switch. Incompatible with pointer or file-stream bodies.

Returns
0 on success, -1 otherwise.

Definition at line 4503 of file http.c.

References cwist_http_response::body, cwist_http_response::is_ptr_body, cwist_sstring::size, cwist_http_response::stream_mode, and cwist_http_response::use_file_stream.

◆ cwist_http_response_stream_end()

int cwist_http_response_stream_end ( cwist_http_response *  res)

Terminate a streaming response with the final zero-length chunk and flush.

Returns
0 on success, -1 on error.

Definition at line 4549 of file http.c.

References cwist_http_response::arena, cap, cwist_sstring_append_len(), cwist_http_response::stream_buf, cwist_http_response::stream_ended, cwist_http_response::stream_failed, and cwist_http_response::stream_mode.

Referenced by cwist_app_dispatch_memory(), cwist_app_dispatch_stream(), and cwist_http_response_serialize().

◆ cwist_http_response_stream_write()

int cwist_http_response_stream_write ( cwist_http_response *  res,
const char *  data,
size_t  len 
)

Append one data chunk to a streaming response.

Frames the chunk (hex length prefix / CRLF suffix) and flushes through the sink. A sink failure marks the stream failed.

Returns
0 on success, -1 on error.

Definition at line 4519 of file http.c.

References cwist_http_response::arena, cap, cwist_sstring_append_len(), cwist_http_response::stream_buf, cwist_http_response::stream_ended, cwist_http_response::stream_failed, and cwist_http_response::stream_mode.

◆ cwist_http_send_error_response()

void cwist_http_send_error_response ( int  fd,
int  status,
const char *  msg 
)

Send a minimal HTTP/1.x error response (Connection: close) on a socket, used to answer malformed requests before dropping them.

Parameters
msgPlain-text body; NULL uses the status reason phrase.

Send a minimal HTTP/1.x error response (Connection: close) on a socket, used to answer malformed requests before dropping them.

Send a minimal error response (Connection: close) for a failed request.

Parameters
fdConnected client socket descriptor.
statusHTTP status code (reason phrase is derived from it).
msgPlain-text body; NULL falls back to the reason phrase.

Definition at line 4046 of file http.c.

◆ cwist_http_send_response()

cwist_error_t cwist_http_send_response ( int  client_fd,
cwist_http_response *  res 
)

Serialize and send an HTTP response to a connected client socket.

Send a response over a socket.

Parameters
client_fdConnected client socket descriptor.
resResponse object to send.
Returns
Tagged CWIST error describing success or transmission failure.

Definition at line 2878 of file http.c.

Referenced by cwist_http_async_send_response(), cwist_http_send_response_async(), and cwist_http_send_response_coalesced().

◆ cwist_http_send_response_async()

◆ cwist_http_send_response_coalesced()

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 
)

◆ cwist_http_send_response_head()

cwist_error_t cwist_http_send_response_head ( int  client_fd,
cwist_http_response *  res 
)

Send only the status line and headers of a response (HEAD replies). Content-Length still reflects the would-be body; body resources are released without being transmitted.

Send only the status line and headers of a response (HEAD replies). Content-Length still reflects the would-be body; body resources are released without being transmitted.

Send only the status line and headers (HEAD replies).

Definition at line 2961 of file http.c.

◆ cwist_http_serialize_headers()

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. Used by the TLS send path to stream header block and body separately.

Returns
Number of bytes written.

Serialize only the status line and headers into a caller buffer. Used by the TLS send path to stream header block and body separately.

Serialize only the status line and headers into a caller buffer.

Non-static wrapper around serialize_headers() so the TLS send path can stream headers and body separately instead of materializing one blob.

Parameters
resResponse object to serialize.
bufDestination buffer for the header block.
buf_sizeTotal capacity of buf in bytes.
Returns
Number of bytes written into the buffer.

Definition at line 2609 of file http.c.

◆ cwist_http_server_loop()

cwist_error_t cwist_http_server_loop ( int  server_fd,
cwist_server_config *  config,
void(*)(int, void *)  handler,
void *  ctx 
)

Run the main HTTP accept loop using the configured concurrency strategy.

Parameters
server_fdListening server socket.
configServer concurrency configuration flags.
handlerCallback that handles one accepted client.
ctxOpaque callback context.
Returns
Tagged CWIST error describing success or failure.

Definition at line 5590 of file http.c.

◆ cwist_http_status_reason()

const char * cwist_http_status_reason ( int  status)

Return the standard reason phrase for an HTTP status code (e.g. 404 -> "Not Found"). Unknown codes return NULL; applications may also assign any int to a response status_code for non-standard codes.

Return the standard reason phrase for an HTTP status code (e.g. 404 -> "Not Found"). Unknown codes return NULL; applications may also assign any int to a response status_code for non-standard codes.

Returns
Static reason phrase, or NULL for unknown codes.

Definition at line 3970 of file http.c.

References cap.

Referenced by cwist_http_coalesce_error_response(), and cwist_http_send_error_response().

◆ cwist_http_string_to_method()

cwist_http_method_t cwist_http_string_to_method ( const char *  method_str)

Parse a NUL-terminated method string into a method enum.

Convert method string to enum.

Returns
Matching method, or CWIST_HTTP_UNKNOWN for NULL/unrecognized input.

Definition at line 1629 of file http.c.

◆ cwist_http_string_to_method_len()

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.

Uses a SWAR 4-byte magic fast path for GET/POST/PUT/DELETE/HEAD, then exact-length comparisons.

Returns
Matching method, or CWIST_HTTP_UNKNOWN.

Definition at line 1584 of file http.c.

References cap, CWIST_HTTP_DELETE, CWIST_HTTP_GET, CWIST_HTTP_HEAD, CWIST_HTTP_OPTIONS, CWIST_HTTP_PATCH, CWIST_HTTP_POST, CWIST_HTTP_PUT, CWIST_HTTP_UNKNOWN, MAGIC_DELE, MAGIC_GET, MAGIC_HEAD, MAGIC_POST, and MAGIC_PUT.

Referenced by cwist_http_string_to_method().

◆ cwist_http_stringify_response()

cwist_sstring * cwist_http_stringify_response ( cwist_http_response *  res)

Materialize an HTTP response into a contiguous string for debugging or TLS writes.

Serialize a response to a string.

Parameters
resResponse object to stringify.
Returns
Heap-allocated response string, or NULL on invalid input.

Definition at line 4099 of file http.c.

◆ cwist_make_socket_ipv4()

int cwist_make_socket_ipv4 ( struct sockaddr_in *  sockv4,
const char *  address,
uint16_t  port,
uint16_t  backlog 
)

Create an IPv4 socket and perform bind/listen.

Create an IPv4 socket and perform bind/listen.

Create an IPv4 socket and bind/listen.

Parameters
sockv4Output sockaddr structure populated for the bind call.
addressIPv4 address string to bind.
portTCP port to listen on.
backlogListen backlog passed to listen(2).
Returns
Listening socket fd on success, or a negative CWIST socket error code.

Definition at line 5361 of file http.c.

◆ cwist_tcp_cork_enabled()

bool cwist_tcp_cork_enabled ( void  )

Whether the TCP_CORK coalescing layer is active for cleartext HTTP/1.1 responses. Enabled at runtime with CWIST_USE_TCP_CORK=1 (burst size via CWIST_TCP_CORK_BURST, default 256 KiB); always false off-Linux.

Whether the TCP_CORK coalescing layer is active for cleartext HTTP/1.1 responses. Enabled at runtime with CWIST_USE_TCP_CORK=1 (burst size via CWIST_TCP_CORK_BURST, default 256 KiB); always false off-Linux.

Returns
Always false.

Definition at line 2684 of file http.c.

Referenced by cwist_http_send_response().

◆ get_cpu_cores()

long get_cpu_cores ( void  )

Return the number of CPUs available to this process.

Honors the process CPU affinity mask where the platform supports it (sched_getaffinity on Linux); falls back to sysconf(3)/sysctl and ultimately 1 when the count cannot be determined.

Returns
Number of usable CPUs, at least 1.

Definition at line 99 of file http.c.

References cap, and count.

Referenced by get_optimal_thread_count().

◆ get_optimal_thread_count()

long get_optimal_thread_count ( void  )

Compute the HTTP worker/event-loop thread count.

Resolution order: a value fixed with cwist_http_pool_limit_core(), then the CWIST_WORKER_THREADS environment override, then CPU-core based sizing modified by CWIST_WORKERS (worker-process count) and CWIST_C1M_MODE. C1M mode divides the core budget across worker processes (one event loop per CPU); classic mode multiplies it to cover blocking keep-alive handlers.

Returns
Computed thread count, clamped to the mode-specific range.

Definition at line 159 of file http.c.

References cap, count, and get_cpu_cores().

Referenced by cwist_http_pool_init_mode(), https_pool_destroy(), and https_pool_init().

◆ headers_have_content_length()

int headers_have_content_length ( cwist_http_header_node *  headers)

Detect whether a header list already defines Content-Length.

Parameters
headersHeader linked list to scan.
Returns
1 when a Content-Length header is present, otherwise 0.

Definition at line 2394 of file http.c.

Variable Documentation

◆ CWIST_CREATE_SOCKET_FAILED

const int CWIST_CREATE_SOCKET_FAILED
extern

Definition at line 1541 of file http.c.

Referenced by cwist_make_socket_ipv4().

◆ CWIST_HTTP_BIND_FAILED

const int CWIST_HTTP_BIND_FAILED
extern

Definition at line 1543 of file http.c.

Referenced by cwist_make_socket_ipv4().

◆ CWIST_HTTP_LISTEN_FAILED

const int CWIST_HTTP_LISTEN_FAILED
extern

Definition at line 1545 of file http.c.

Referenced by cwist_make_socket_ipv4().

◆ CWIST_HTTP_SETSOCKOPT_FAILED

const int CWIST_HTTP_SETSOCKOPT_FAILED
extern

Definition at line 1544 of file http.c.

Referenced by cwist_make_socket_ipv4().

◆ CWIST_HTTP_UNAVAILABLE_ADDRESS

const int CWIST_HTTP_UNAVAILABLE_ADDRESS
extern

Definition at line 1542 of file http.c.

Referenced by cwist_make_socket_ipv4().