|
cwist
Modern C web framework with libttak runtime
|
#include <sched.h>#include "assets_internal.h"#include "routes_internal.h"#include <cwist/sys/app/app.h>#include <cwist/sys/app/config.h>#include <cwist/sys/app/logger.h>#include <cwist/sys/app/shutdown.h>#include <cwist/sys/app/big_dumb_reply.h>#include <cwist/sys/wasi.h>#include <cwist/sys/health/healthz.h>#include <cwist/net/http/http.h>#include <cwist/net/http/async.h>#include <cwist/net/http/https.h>#include <cwist/net/http/http2.h>#include <cwist/net/grpc/grpc.h>#include <cwist/net/http/http3.h>#include <cwist/net/http/async_server.h>#include "../../net/http/async_internal.h"#include "../../net/http/http_live.h"#include "../../net/http/sockopts.h"#include "../../net/http/simd_parser.h"#include "../../net/websocket/ws_async_internal.h"#include "../io/async_debug.h"#include <cwist/core/sstring/sstring.h>#include <cwist/core/db/nuke_db.h>#include <cwist/core/mem/alloc.h>#include <cwist/core/utils/json_builder.h>#include <ttak/mem/mem.h>#include <ttak/timing/timing.h>#include <ttak/net/lattice.h>#include <ttak/mols_control.h>#include <ttak/priority/scheduler.h>#include <ttak/async/sched.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <ctype.h>#include <strings.h>#include <limits.h>#include <unistd.h>#include <cwist/sys/platform.h>#include <errno.h>#include <fcntl.h>#include <poll.h>#include <arpa/inet.h>#include <netinet/tcp.h>#include <dirent.h>#include <sys/stat.h>#include <sys/resource.h>#include <time.h>#include <pthread.h>#include <cwist/sys/metrics/metrics.h>Go to the source code of this file.
Macros | |
| #define | _GNU_SOURCE |
| #define | _POSIX_C_SOURCE 200809L |
| #define | _DEFAULT_SOURCE |
| #define | PATH_MAX 4096 |
| #define | CWIST_ROUTE_BUCKETS 127 |
| #define | CWIST_STATIC_RETIRE_NS TT_SECOND(5) |
| #define | CWIST_DEFAULT_FD_LIMIT_TARGET ((rlim_t)1050000) |
| #define | CWIST_STREAM_REQ_EAGER_MAX ((size_t)1 << 20) |
| #define | CWIST_HTTP_REQUESTS_PER_TURN_DEFAULT 16 |
| Event-driven HTTP/1.1 handler for the C1M reactor path. Drains the socket without blocking and serves a bounded request batch. Remaining buffered work is posted so other ready connections can run. | |
| #define | CWIST_HTTP_REQUESTS_PER_TURN_MAX 1024 |
| #define | CWIST_H2C_THREAD_STACK_SIZE (256 * 1024) |
Typedefs | |
| typedef struct cwist_route_entry | cwist_route_entry |
Enumerations | |
| enum | app_serve_result_t { APP_SERVE_CLOSE = 0 , APP_SERVE_KEEPALIVE , APP_SERVE_DEFERRED , APP_SERVE_DETACH } |
| Route and respond to one fully parsed HTTP/1.1 request. Shared by the blocking keep-alive loop and the event-driven async path. More... | |
Functions | |
| cwist_app * | cwist_app_create (void) |
| Creates a new CWIST application instance. | |
| void | cwist_app_use (cwist_app *app, cwist_middleware_func mw) |
| Append a middleware callback to the application's execution chain. | |
| cwist_error_t | cwist_app_use_ex (cwist_app *app, cwist_middleware_func mw, cwist_middleware_func_ex mw_ex, void *user_ctx, cwist_middleware_ctx_destroy_func destroy) |
| Append an extended middleware callback to the application's execution chain. | |
| void | cwist_app_set_max_memspace (cwist_app *app, size_t size) |
| Override the static file memory budget used by cwist_mem_init(). | |
| void | cwist_app_set_error_handler (cwist_app *app, cwist_error_handler_func handler) |
| Install a custom HTTP error callback. | |
| void | cwist_app_register_error_handler (cwist_app *app, cwist_http_status_t status, cwist_error_handler_func handler) |
| void | cwist_app_configure_bdr (cwist_app *app, size_t max_bytes, time_t max_entry_age_sec, uint64_t revalidate_hits) |
| Configure the Big Dumb Reply cache thresholds for the application. | |
| void | cwist_app_destroy (cwist_app *app) |
| Release every resource owned by the application object. | |
| cwist_error_t | cwist_app_use_https (cwist_app *app, const char *cert_path, const char *key_path) |
| Enable TLS for the application using the supplied certificate pair. | |
| cwist_error_t | cwist_app_reload_tls (cwist_app *app, const char *cert_path, const char *key_path) |
| Hot-reload the TLS certificate pair on every live TLS context of the application. See the public contract in app.h. | |
| void | cwist_app_use_pqc_layer (cwist_app *app, bool enabled) |
| Enable Post-Quantum Cryptography (PQC) hybrid key exchange layer. | |
| void | cwist_app_set_tls_groups (cwist_app *app, const char *groups) |
| Override TLS groups explicitly. | |
| cwist_error_t | cwist_app_use_https2 (cwist_app *app, bool enabled) |
| cwist_error_t | cwist_app_use_https3 (cwist_app *app, bool enabled) |
| cwist_error_t | cwist_app_use_http2 (cwist_app *app, bool enabled) |
| Enable HTTP/2 cleartext (h2c) over standard TCP. | |
| cwist_error_t | cwist_app_use_http3 (cwist_app *app, bool enabled) |
| Enable HTTP/3 (QUIC) without manual SSL configuration. Auto-generates an ephemeral self-signed certificate internally. | |
| void | cwist_app_use_webtransport (cwist_app *app, cwist_webtransport_handler_func handler) |
| Enable WebTransport over HTTP/3 on the application. | |
| cwist_error_t | cwist_app_use_db (cwist_app *app, const char *db_path) |
| Open a SQLite database and attach it as the shared application handle. | |
| cwist_error_t | cwist_app_use_nuke_db (cwist_app *app, const char *db_path, int sync_interval_ms) |
| Enable NUKE DB and fall back to standard SQLite when RAM bootstrap fails. | |
| cwist_db * | cwist_app_get_db (cwist_app *app) |
| Return the active shared database wrapper for the application. | |
| cwist_error_t | cwist_app_use_db_pool (cwist_app *app, const char *db_path, size_t max_conns) |
| Open a SQLite connection pool and attach it to the application. | |
| cwist_db_pool_t * | cwist_app_get_db_pool (cwist_app *app) |
| Return the application's database connection pool. | |
| cwist_error_t | cwist_app_use_redis (cwist_app *app, const char *host, int port, size_t max_conns) |
| Configure a Redis connection pool for the application. | |
| cwist_redis_pool_t * | cwist_app_get_redis_pool (cwist_app *app) |
| Return the application's Redis connection pool. | |
| cwist_error_t | cwist_app_use_scheduler (cwist_app *app, size_t worker_count, size_t queue_capacity) |
| Configure a background job scheduler for the application. | |
| cwist_scheduler_t * | cwist_app_get_scheduler (cwist_app *app) |
| Return the application's background job scheduler. | |
| cwist_error_t | cwist_app_static (cwist_app *app, const char *url_prefix, const char *directory) |
| Register a filesystem directory to be served beneath a URL prefix. | |
| cwist_error_t | cwist_app_static_with_cache (cwist_app *app, const char *url_prefix, const char *directory, const char *cache_control) |
| Register a filesystem directory with a custom Cache-Control header. | |
| cwist_error_t | cwist_app_get_ex (cwist_app *app, const char *path, cwist_handler_ex_func handler, void *user_ctx, cwist_handler_ctx_destroy_func destroy) |
| Registers a GET route whose handler receives a user context. | |
| cwist_error_t | cwist_app_post_ex (cwist_app *app, const char *path, cwist_handler_ex_func handler, void *user_ctx, cwist_handler_ctx_destroy_func destroy) |
| POST counterpart of cwist_app_get_ex(). | |
| cwist_error_t | cwist_app_put_ex (cwist_app *app, const char *path, cwist_handler_ex_func handler, void *user_ctx, cwist_handler_ctx_destroy_func destroy) |
| PUT counterpart of cwist_app_get_ex(). | |
| cwist_error_t | cwist_app_delete_ex (cwist_app *app, const char *path, cwist_handler_ex_func handler, void *user_ctx, cwist_handler_ctx_destroy_func destroy) |
| DELETE counterpart of cwist_app_get_ex(). | |
| cwist_error_t | cwist_app_patch_ex (cwist_app *app, const char *path, cwist_handler_ex_func handler, void *user_ctx, cwist_handler_ctx_destroy_func destroy) |
| PATCH counterpart of cwist_app_get_ex(). | |
| void | cwist_app_get (cwist_app *app, const char *path, cwist_handler_func handler) |
| Register a GET handler with default endpoint options. | |
| void | cwist_app_enable_metrics (cwist_app *app) |
| void | cwist_app_enable_healthz (cwist_app *app) |
| Register built-in health check routes on an application. | |
| void | cwist_app_get_named (cwist_app *app, const char *path, const char *name, cwist_handler_func handler) |
| void | cwist_app_post (cwist_app *app, const char *path, cwist_handler_func handler) |
| Register a POST handler with default endpoint options. | |
| void | cwist_app_put (cwist_app *app, const char *path, cwist_handler_func handler) |
| void | cwist_app_delete (cwist_app *app, const char *path, cwist_handler_func handler) |
| void | cwist_app_patch (cwist_app *app, const char *path, cwist_handler_func handler) |
| void | cwist_app_post_named (cwist_app *app, const char *path, const char *name, cwist_handler_func handler) |
| void | cwist_app_put_named (cwist_app *app, const char *path, const char *name, cwist_handler_func handler) |
| void | cwist_app_delete_named (cwist_app *app, const char *path, const char *name, cwist_handler_func handler) |
| void | cwist_app_patch_named (cwist_app *app, const char *path, const char *name, cwist_handler_func handler) |
| void | cwist_app_ws (cwist_app *app, const char *path, cwist_ws_handler_func handler) |
| Register a WebSocket upgrade endpoint with default options. | |
| void | cwist_app_ws_async (cwist_app *app, const char *path, cwist_ws_on_message_t on_message, void *user_data) |
| Register a callback-shaped non-blocking WebSocket endpoint (C1M mode). | |
| bool | cwist_endpoint_has_extern (cwist_endpoint_opt_t opts, cwist_endpoint_opt_t flag) |
Out-of-line cwist_endpoint_has(), for callers that cannot use a static inline function, such as bindings generated from this header. | |
| void | cwist_app_get_opt (cwist_app *app, const char *path, cwist_handler_func handler, cwist_endpoint_opt_t opts) |
| Register a GET handler with explicit endpoint options. | |
| void | cwist_app_post_opt (cwist_app *app, const char *path, cwist_handler_func handler, cwist_endpoint_opt_t opts) |
| Register a POST handler with explicit endpoint options. | |
| void | cwist_app_put_opt (cwist_app *app, const char *path, cwist_handler_func handler, cwist_endpoint_opt_t opts) |
| void | cwist_app_delete_opt (cwist_app *app, const char *path, cwist_handler_func handler, cwist_endpoint_opt_t opts) |
| void | cwist_app_patch_opt (cwist_app *app, const char *path, cwist_handler_func handler, cwist_endpoint_opt_t opts) |
| void | cwist_app_ws_opt (cwist_app *app, const char *path, cwist_ws_handler_func handler, cwist_endpoint_opt_t opts) |
| Register a WebSocket route with explicit endpoint options. | |
| char * | cwist_url_for (cwist_app *app, const char *name, cwist_query_map *params) |
| void | cwist_app_dispatch (cwist_app *app, cwist_http_request *req, cwist_http_response *res) |
| Dispatch a request internally through the app's router and middleware. Used by the test client and advanced integrations. | |
| int | cwist_app_dispatch_memory (cwist_app *app, const char *req_buf, size_t req_len, char **res_buf, size_t *res_len) |
| Run the full router/middleware/handler pipeline on a raw HTTP/1.x request held in a memory buffer, with no socket or event loop involved. | |
| cwist_stream_req_t * | cwist_stream_req_begin (const char *head, size_t head_len) |
Begin an incremental request: head is the serialized request line plus headers (everything up to and including the terminating CRLF of the header block). The body length is taken from Content-Length (required for fed bodies; Transfer-Encoding: chunked is not reassembled). | |
| int | cwist_stream_req_feed (cwist_stream_req_t *r, const char *chunk, size_t len) |
| Feed one body chunk. Chunks may be any size and may split anywhere; reassembly is the implementation's job. Feeding more bytes than the declared Content-Length is an error. | |
| int | cwist_stream_req_end (cwist_stream_req_t *r) |
| Declare the body complete. The fed byte count must equal the declared Content-Length (or both be zero for a bodyless request). | |
| int | cwist_stream_req_dispatch (cwist_stream_req_t *r, cwist_app *app, cwist_stream_write_fn write_fn, void *write_ctx) |
Dispatch a completed incremental request with a streaming response. Takes ownership of req regardless of outcome. | |
| int | cwist_app_dispatch_stream (cwist_app *app, const char *req_buf, size_t req_len, cwist_stream_write_fn write_fn, void *write_ctx) |
Like cwist_app_dispatch_memory(), but streams the serialized response through write_fn instead of returning one buffer. | |
| bool | cwist_https_upgrade_handler (cwist_https_connection *conn, cwist_http_request *req, cwist_http_response *res) |
| cwist_async_action_t | cwist_app_http_handler_async (int client_fd, cwist_http_async_conn_t *conn) |
| void | cwist_app_http_handler (int client_fd, void *ctx) |
| void | cwist_apply_profile (void) |
| Apply a CWIST_PROFILE preset before the per-variable env reads in cwist_app_listen(). Each setenv() call uses overwrite=0 so any variable already present in the caller's environment takes precedence. | |
| int | cwist_app_listen (cwist_app *app, int port) |
| Initialize runtime services and enter the HTTP or HTTPS server loop. | |
| int | cwist_app_listen_ex (cwist_app *app, int port, int workers_override, int c1m_override) |
| cwist_app_listen() with the worker count and server mode given explicitly. | |
| void | cwist_app_enable_swagger (cwist_app *app, const char *mount_path, const char *openapi_json_path) |
| Enables embedded Swagger UI interactive documentation page. | |
| #define CWIST_HTTP_REQUESTS_PER_TURN_DEFAULT 16 |
| void cwist_app_configure_bdr | ( | cwist_app * | app, |
| size_t | max_bytes, | ||
| time_t | max_entry_age_sec, | ||
| uint64_t | revalidate_hits | ||
| ) |
Configure the Big Dumb Reply cache thresholds for the application.
Configures the Big Dumb Reply guardrails.
| app | Application whose BDR context should be tuned. |
| max_bytes | Maximum total cache footprint. |
| max_entry_age_sec | Maximum age for cached entries. |
| revalidate_hits | Hit count that forces revalidation. |
Definition at line 1454 of file app.c.
References cwist_app::bdr_ctx, and cwist_bdr_set_limits().
Creates a new CWIST application instance.
— API —
Definition at line 1292 of file app.c.
References cwist_app::bdr_ctx, cap, cwist_app::cert_path, cwist_app::config, cwist_alloc(), cwist_bdr_create(), cwist_config_create(), cwist_free(), cwist_logger_create(), cwist_app::db, cwist_app::db_path, cwist_app::db_pool, cwist_app::error_handler, cwist_app::error_handlers, cwist_app::grpc_routes, cwist_app::h3_ctx, cwist_app::https_request_handler, cwist_app::key_path, cwist_app::logger, cwist_app::max_mem_space, cwist_app::mem_manager, cwist_app::middlewares, cwist_app::nuke_enabled, cwist_app::port, cwist_app::pqc_layer_enabled, cwist_app::redis_pool, cwist_app::router, cwist_app::scheduler, cwist_app::session_max_age, cwist_app::session_name, cwist_app::session_secret, cwist_app::ssl_ctx, cwist_app::static_dirs, cwist_app::tls_groups, cwist_app::use_http2, cwist_app::use_http3, cwist_app::use_https2, cwist_app::use_https3, cwist_app::use_ssl, and cwist_app::wt_handler.
| void cwist_app_delete | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_func | handler | ||
| ) |
Definition at line 2275 of file app.c.
References CWIST_ENDPOINT_DEFAULT, and CWIST_HTTP_DELETE.
| cwist_error_t cwist_app_delete_ex | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_ex_func | handler, | ||
| void * | user_ctx, | ||
| cwist_handler_ctx_destroy_func | destroy | ||
| ) |
DELETE counterpart of cwist_app_get_ex().
Definition at line 2195 of file app.c.
References CWIST_HTTP_DELETE.
| void cwist_app_delete_named | ( | cwist_app * | app, |
| const char * | path, | ||
| const char * | name, | ||
| cwist_handler_func | handler | ||
| ) |
Definition at line 2293 of file app.c.
References CWIST_ENDPOINT_DEFAULT, and CWIST_HTTP_DELETE.
| void cwist_app_delete_opt | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_func | handler, | ||
| cwist_endpoint_opt_t | opts | ||
| ) |
Definition at line 2385 of file app.c.
References CWIST_HTTP_DELETE.
Release every resource owned by the application object.
Destroys the application and frees all resources.
| app | Application instance to destroy. |
Definition at line 1464 of file app.c.
References cwist_app::assets, cwist_app::bdr_ctx, cap, cwist_app::cert_path, cwist_app::config, cwist_assets_destroy(), cwist_bdr_destroy(), cwist_config_destroy(), cwist_db_close(), cwist_db_pool_destroy(), cwist_free(), cwist_grpc_routes_destroy(), cwist_http3_destroy_context(), cwist_https_destroy_context(), cwist_logger_destroy(), cwist_nuke_close(), cwist_redis_pool_destroy(), cwist_scheduler_destroy(), cwist_app::db, cwist_app::db_path, cwist_app::db_pool, cwist_middleware_node::destroy, cwist_app::error_handlers, cwist_fix_server_mem::file_count, cwist_fix_server_mem::file_tree, cwist_fix_server_mem::files, cwist_file_t::fs_path, cwist_app::h3_ctx, cwist_app::key_path, cwist_fix_server_mem::lock, cwist_app::logger, cwist_app::mem_manager, cwist_app::middlewares, cwist_error_handler_entry::next, cwist_middleware_node::next, cwist_file_t::node, cwist_app::nuke_enabled, cwist_file_t::path, cwist_app::rdbms, cwist_app::redis_pool, cwist_app::router, cwist_app::scheduler, cwist_app::session_name, cwist_app::session_secret, cwist_app::ssl_ctx, cwist_app::static_dirs, cwist_app::tls_groups, cwist_fix_server_mem::watcher_running, and cwist_fix_server_mem::watcher_thread.
| void cwist_app_dispatch | ( | cwist_app * | app, |
| cwist_http_request * | req, | ||
| cwist_http_response * | res | ||
| ) |
Dispatch a request internally through the app's router and middleware. Used by the test client and advanced integrations.
Definition at line 2516 of file app.c.
References cwist_http_request::app, cwist_http_request::db, and cwist_app::db.
Referenced by cwist_app_dispatch_memory(), and cwist_app_dispatch_stream().
| int cwist_app_dispatch_memory | ( | cwist_app * | app, |
| const char * | req_buf, | ||
| size_t | req_len, | ||
| char ** | res_buf, | ||
| size_t * | res_len | ||
| ) |
Run the full router/middleware/handler pipeline on a raw HTTP/1.x request held in a memory buffer, with no socket or event loop involved.
The response is serialized (status line + headers + body) into a freshly allocated buffer; the caller frees res_buf with cwist_free(). The exchange uses Connection: close semantics. This is the in-memory entry point for embedded transports (e.g. WASM / Service Worker hosts).
Definition at line 2523 of file app.c.
References cap, cwist_app_dispatch(), cwist_http_parse_request_len(), cwist_http_request_destroy(), cwist_http_response_create(), cwist_http_response_destroy(), cwist_http_response_serialize(), cwist_http_response_stream_end(), cwist_http_response::keep_alive, cwist_http_response::stream_ended, and cwist_http_response::stream_mode.
| int cwist_app_dispatch_stream | ( | cwist_app * | app, |
| const char * | req_buf, | ||
| size_t | req_len, | ||
| cwist_stream_write_fn | write_fn, | ||
| void * | write_ctx | ||
| ) |
Like cwist_app_dispatch_memory(), but streams the serialized response through write_fn instead of returning one buffer.
| app | Application to dispatch against. |
| req_buf | Serialized HTTP/1.1 request (head + body, as the wire form). |
| req_len | Length of req_buf. |
| write_fn | Chunk sink; must not be NULL. |
| write_ctx | Opaque argument passed to every write_fn call. |
Definition at line 2668 of file app.c.
References cwist_http_response::body, cap, cwist_app_dispatch(), CWIST_HTTP_MAX_HEADER_SIZE, cwist_http_parse_request_len(), cwist_http_request_destroy(), cwist_http_response_create(), cwist_http_response_destroy(), cwist_http_response_stream_end(), cwist_http_serialize_headers(), CWIST_STREAM_CHUNK, cwist_sstring::data, cwist_http_response::is_ptr_body, cwist_http_response::keep_alive, cwist_http_response::ptr_body, cwist_http_response::ptr_body_len, cwist_sstring::size, cwist_http_response::stream_ended, cwist_http_response::stream_failed, cwist_http_response::stream_mode, cwist_http_response::stream_sink, cwist_http_response::stream_sink_ctx, and cwist_http_response::use_file_stream.
Referenced by cwist_stream_req_dispatch().
Register built-in health check routes on an application.
Adds /healthz, /live, and /ready GET routes.
| app | Application to register routes on. |
Definition at line 2249 of file app.c.
References cwist_app_get().
Definition at line 2227 of file app.c.
References cwist_app_get().
| void cwist_app_enable_swagger | ( | cwist_app * | app, |
| const char * | mount_path, | ||
| const char * | openapi_json_path | ||
| ) |
Enables embedded Swagger UI interactive documentation page.
| app | Pointer to the app. |
| mount_path | URL route path to serve Swagger UI (e.g. "/swagger" or "/docs"). |
| openapi_json_path | Path to the generated openapi.json file on disk. |
Definition at line 5051 of file app.c.
References cap, and cwist_app_get().
| void cwist_app_get | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_func | handler | ||
| ) |
Register a GET handler with default endpoint options.
Registers a GET route handler.
| app | Application being configured. |
| path | Exact route path. |
| handler | HTTP handler invoked for matching requests. |
Definition at line 2216 of file app.c.
References CWIST_ENDPOINT_DEFAULT, and CWIST_HTTP_GET.
Referenced by cwist_app_enable_healthz(), cwist_app_enable_metrics(), and cwist_app_enable_swagger().
Return the active shared database wrapper for the application.
| app | Application whose database handle should be queried. |
Definition at line 1948 of file app.c.
References cap, cwist_db::conn, cwist_nuke_get_db(), cwist_app::db, and cwist_app::nuke_enabled.
| cwist_db_pool_t * cwist_app_get_db_pool | ( | cwist_app * | app | ) |
Return the application's database connection pool.
Definition at line 1984 of file app.c.
References cap, and cwist_app::db_pool.
| cwist_error_t cwist_app_get_ex | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_ex_func | handler, | ||
| void * | user_ctx, | ||
| cwist_handler_ctx_destroy_func | destroy | ||
| ) |
Registers a GET route whose handler receives a user context.
Behaves like cwist_app_get() (exact and :param paths, middleware, every transport), but the handler is called as handler(user_ctx, req, res). This lets a caller bind state to a route without globals, for example a closure from a language binding.
Ownership of user_ctx passes to the app on every call. When destroy is not NULL it is called exactly once with user_ctx:
:param segments) is registered again, by any routing function, before the new registration takes over (unless it passes the same non-NULL user_ctx);destroy to keep ownership with the caller.| app | Application being configured. |
| path | Route path; may contain :param segments. |
| handler | Handler invoked for matching requests. |
| user_ctx | Opaque pointer passed to handler (may be NULL). |
| destroy | Optional destructor for user_ctx. |
app, path or handler is NULL or the route could not be allocated. Definition at line 2180 of file app.c.
References CWIST_HTTP_GET.
Referenced by cwist_app_enable_openapi().
| void cwist_app_get_named | ( | cwist_app * | app, |
| const char * | path, | ||
| const char * | name, | ||
| cwist_handler_func | handler | ||
| ) |
Definition at line 2256 of file app.c.
References CWIST_ENDPOINT_DEFAULT, and CWIST_HTTP_GET.
| void cwist_app_get_opt | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_func | handler, | ||
| cwist_endpoint_opt_t | opts | ||
| ) |
Register a GET handler with explicit endpoint options.
| app | Application being configured. |
| path | Exact route path. |
| handler | HTTP handler invoked for matching requests. |
| opts | Endpoint flags controlling cache and transport behavior. |
Definition at line 2363 of file app.c.
References CWIST_HTTP_GET.
| cwist_redis_pool_t * cwist_app_get_redis_pool | ( | cwist_app * | app | ) |
Return the application's Redis connection pool.
Definition at line 2015 of file app.c.
References cap, and cwist_app::redis_pool.
| cwist_scheduler_t * cwist_app_get_scheduler | ( | cwist_app * | app | ) |
Return the application's background job scheduler.
Definition at line 2045 of file app.c.
References cap, and cwist_app::scheduler.
Definition at line 3506 of file app.c.
References cwist_http_request::app, APP_SERVE_CLOSE, APP_SERVE_DEFERRED, cwist_app::bdr_ctx, cap, cwist_http_request::client_fd, cwist_bdr_get(), cwist_grpc_http2_hooks(), cwist_http2_serve_connection_ex(), cwist_http_live_idle_enter(), cwist_http_live_idle_leave(), CWIST_HTTP_PARSE_OK, CWIST_HTTP_READ_BUFFER_SIZE, cwist_http_receive_request(), CWIST_HTTPS_PROTOCOL_HTTP2, cwist_http_request::db, cwist_app::db, cwist_http_async_conn::fd, and cwist_app::use_http2.
| cwist_async_action_t cwist_app_http_handler_async | ( | int | client_fd, |
| cwist_http_async_conn_t * | conn | ||
| ) |
Definition at line 3343 of file app.c.
References cwist_http_request::app, APP_SERVE_CLOSE, APP_SERVE_DEFERRED, APP_SERVE_DETACH, cwist_http_request::async_conn, cap, cwist_http_request::client_fd, cwist_alloc(), CWIST_ASYNC_CLOSE, CWIST_ASYNC_DEFER, CWIST_ASYNC_DETACH, CWIST_ASYNC_REARM, CWIST_COALESCE_FLUSH_ERROR, CWIST_COALESCE_FLUSH_PARKED, cwist_free(), cwist_grpc_http2_hooks(), CWIST_H2C_THREAD_STACK_SIZE, cwist_http2_serve_connection_ex(), cwist_http_async_conn_fill(), cwist_http_async_rearm(), cwist_http_coalesce_flush(), CWIST_HTTP_PARSE_OK, cwist_http_receive_request_nb(), CWIST_HTTPS_PROTOCOL_HTTP2, CWIST_RECV_FATAL, CWIST_RECV_NEED_MORE, cwist_http_request::db, cwist_app::db, cwist_https_connection::fd, cwist_http_async_conn::len, cwist_http_async_conn::peer_eof, cwist_http_async_conn::rbuf, cwist_http_async_conn::reactor, cwist_app::use_http2, and cwist_http_async_conn::user_ctx.
Initialize runtime services and enter the HTTP or HTTPS server loop.
Serve app on port until SIGTERM, SIGINT or cwist_shutdown_request(); blocks the calling thread.
| app | Application instance to run. |
| port | TCP port to bind. |
Definition at line 4628 of file app.c.
References cwist_app_listen_ex().
cwist_app_listen() with the worker count and server mode given explicitly.
| workers_override | Worker processes (1 serves without forking); 0 or less reads CWIST_WORKERS. |
| c1m_override | 1 for the reactor server, which joins its handler threads before returning; 0 for the classic pool; negative reads CWIST_C1M_MODE. Ignored on WASI. |
Definition at line 4636 of file app.c.
References cap, cwist_shutdown_install_handlers(), cwist_shutdown_restore_handlers(), and cwist_app::port.
Referenced by cwist_app_listen().
| void cwist_app_patch | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_func | handler | ||
| ) |
Definition at line 2279 of file app.c.
References CWIST_ENDPOINT_DEFAULT, and CWIST_HTTP_PATCH.
| cwist_error_t cwist_app_patch_ex | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_ex_func | handler, | ||
| void * | user_ctx, | ||
| cwist_handler_ctx_destroy_func | destroy | ||
| ) |
PATCH counterpart of cwist_app_get_ex().
Definition at line 2200 of file app.c.
References CWIST_HTTP_PATCH.
| void cwist_app_patch_named | ( | cwist_app * | app, |
| const char * | path, | ||
| const char * | name, | ||
| cwist_handler_func | handler | ||
| ) |
Definition at line 2298 of file app.c.
References CWIST_ENDPOINT_DEFAULT, and CWIST_HTTP_PATCH.
| void cwist_app_patch_opt | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_func | handler, | ||
| cwist_endpoint_opt_t | opts | ||
| ) |
Definition at line 2390 of file app.c.
References CWIST_HTTP_PATCH.
| void cwist_app_post | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_func | handler | ||
| ) |
Register a POST handler with default endpoint options.
| app | Application being configured. |
| path | Exact route path. |
| handler | HTTP handler invoked for matching requests. |
Definition at line 2267 of file app.c.
References CWIST_ENDPOINT_DEFAULT, and CWIST_HTTP_POST.
| cwist_error_t cwist_app_post_ex | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_ex_func | handler, | ||
| void * | user_ctx, | ||
| cwist_handler_ctx_destroy_func | destroy | ||
| ) |
POST counterpart of cwist_app_get_ex().
Definition at line 2185 of file app.c.
References CWIST_HTTP_POST.
| void cwist_app_post_named | ( | cwist_app * | app, |
| const char * | path, | ||
| const char * | name, | ||
| cwist_handler_func | handler | ||
| ) |
Definition at line 2283 of file app.c.
References CWIST_ENDPOINT_DEFAULT, and CWIST_HTTP_POST.
| void cwist_app_post_opt | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_func | handler, | ||
| cwist_endpoint_opt_t | opts | ||
| ) |
Register a POST handler with explicit endpoint options.
| app | Application being configured. |
| path | Exact route path. |
| handler | HTTP handler invoked for matching requests. |
| opts | Endpoint flags controlling cache and transport behavior. |
Definition at line 2375 of file app.c.
References CWIST_HTTP_POST.
| void cwist_app_put | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_func | handler | ||
| ) |
Definition at line 2271 of file app.c.
References CWIST_ENDPOINT_DEFAULT, and CWIST_HTTP_PUT.
| cwist_error_t cwist_app_put_ex | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_ex_func | handler, | ||
| void * | user_ctx, | ||
| cwist_handler_ctx_destroy_func | destroy | ||
| ) |
PUT counterpart of cwist_app_get_ex().
Definition at line 2190 of file app.c.
References CWIST_HTTP_PUT.
| void cwist_app_put_named | ( | cwist_app * | app, |
| const char * | path, | ||
| const char * | name, | ||
| cwist_handler_func | handler | ||
| ) |
Definition at line 2288 of file app.c.
References CWIST_ENDPOINT_DEFAULT, and CWIST_HTTP_PUT.
| void cwist_app_put_opt | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_handler_func | handler, | ||
| cwist_endpoint_opt_t | opts | ||
| ) |
Definition at line 2380 of file app.c.
References CWIST_HTTP_PUT.
| void cwist_app_register_error_handler | ( | cwist_app * | app, |
| cwist_http_status_t | status, | ||
| cwist_error_handler_func | handler | ||
| ) |
Definition at line 1415 of file app.c.
References cap, cwist_alloc(), cwist_app::error_handlers, cwist_error_handler_entry::handler, cwist_error_handler_entry::next, and cwist_error_handler_entry::status_code.
| cwist_error_t cwist_app_reload_tls | ( | cwist_app * | app, |
| const char * | cert_path, | ||
| const char * | key_path | ||
| ) |
Hot-reload the TLS certificate pair on every live TLS context of the application. See the public contract in app.h.
Hot-reload the TLS certificate chain and key without restarting the listener.
Definition at line 1691 of file app.c.
References cap, cwist_app::cert_path, CWIST_ERR_INT16, cwist_free(), cwist_http3_reload_context(), cwist_https_reload_context(), cwist_strdup(), cwist_app::h3_ctx, cwist_app::key_path, make_error(), cwist_app::ssl_ctx, cwist_app::use_https3, and cwist_app::use_ssl.
| void cwist_app_set_error_handler | ( | cwist_app * | app, |
| cwist_error_handler_func | handler | ||
| ) |
Install a custom HTTP error callback.
| app | Application being configured. |
| handler | Handler invoked for framework-generated errors such as 404 responses. |
Definition at line 1411 of file app.c.
References cwist_app::error_handler.
Override the static file memory budget used by cwist_mem_init().
Sets the maximum memory space for the static file pool.
| app | Application being configured. |
| size | Maximum bytes reserved for static payload caching. |
Definition at line 1402 of file app.c.
References cwist_app::max_mem_space.
Override TLS groups explicitly.
| app | Application context. |
| groups | Colon-separated group list (e.g., "X25519MLKEM768:X25519:P-256"). Pass NULL to reset to automatic selection. |
Definition at line 1749 of file app.c.
References cap, cwist_free(), cwist_strdup(), and cwist_app::tls_groups.
Register a filesystem directory to be served beneath a URL prefix.
Serves a directory of static files at a URL prefix. Files are loaded into the fixed memory pool for Zero-Copy serving.
| app | Application being configured. |
| url_prefix | Request-path prefix such as "/static". |
| directory | Filesystem directory that backs the mapping. |
Definition at line 2057 of file app.c.
References cap, cwist_alloc(), CWIST_ERR_INT16, cwist_free(), make_error(), and cwist_app::static_dirs.
| cwist_error_t cwist_app_static_with_cache | ( | cwist_app * | app, |
| const char * | url_prefix, | ||
| const char * | directory, | ||
| const char * | cache_control | ||
| ) |
Register a filesystem directory with a custom Cache-Control header.
Serves a directory of static files at a URL prefix with custom Cache-Control. Files are loaded into the fixed memory pool for Zero-Copy serving.
| app | Application being configured. |
| url_prefix | Request-path prefix such as "/static". |
| directory | Filesystem directory that backs the mapping. |
| cache_control | Value for the Cache-Control header (e.g. "public, max-age=86400"). |
Definition at line 2103 of file app.c.
References cap, cwist_alloc(), CWIST_ERR_INT16, cwist_free(), cwist_strdup(), make_error(), and cwist_app::static_dirs.
| void cwist_app_use | ( | cwist_app * | app, |
| cwist_middleware_func | mw | ||
| ) |
| cwist_error_t cwist_app_use_db | ( | cwist_app * | app, |
| const char * | db_path | ||
| ) |
Open a SQLite database and attach it as the shared application handle.
| app | Application being configured. |
| db_path | Filesystem path or SQLite URI to open. |
Definition at line 1851 of file app.c.
References cap, cwist_db_close(), cwist_db_open(), CWIST_ERR_INT16, cwist_free(), cwist_strdup(), cwist_app::db, cwist_app::db_path, make_error(), and cwist_app::nuke_enabled.
Referenced by cwist_app_use_nuke_db().
| cwist_error_t cwist_app_use_db_pool | ( | cwist_app * | app, |
| const char * | db_path, | ||
| size_t | max_conns | ||
| ) |
Open a SQLite connection pool and attach it to the application.
| app | Application context. |
| db_path | SQLite database path (or ":memory:"). |
| max_conns | Maximum number of connections in the pool (>= 1). |
Definition at line 1958 of file app.c.
References cap, cwist_db_pool_create(), cwist_db_pool_destroy(), CWIST_ERR_INT16, cwist_app::db_pool, and make_error().
| cwist_error_t cwist_app_use_ex | ( | cwist_app * | app, |
| cwist_middleware_func | mw, | ||
| cwist_middleware_func_ex | mw_ex, | ||
| void * | user_ctx, | ||
| cwist_middleware_ctx_destroy_func | destroy | ||
| ) |
Append an extended middleware callback to the application's execution chain.
Append an extended middleware to the application chain.
| app | Application being configured. |
| mw | Legacy middleware function (may be NULL if mw_ex is set). |
| mw_ex | Extended middleware function with user context (may be NULL if mw is set). |
| user_ctx | Opaque context for mw_ex. |
| destroy | Destructor for user_ctx. |
Definition at line 1389 of file app.c.
References cap, CWIST_ERR_INT16, and make_error().
| cwist_error_t cwist_app_use_http2 | ( | cwist_app * | app, |
| bool | enabled | ||
| ) |
Enable HTTP/2 cleartext (h2c) over standard TCP.
| app | Application context. |
| enabled | True to enable h2c. |
Definition at line 1806 of file app.c.
References cap, CWIST_ERR_INT16, make_error(), and cwist_app::use_http2.
| cwist_error_t cwist_app_use_http3 | ( | cwist_app * | app, |
| bool | enabled | ||
| ) |
Enable HTTP/3 (QUIC) without manual SSL configuration. Auto-generates an ephemeral self-signed certificate internally.
| app | Application context. |
| enabled | True to enable zero-config HTTP/3. |
Definition at line 1818 of file app.c.
References cap, CWIST_ERR_INT16, make_error(), and cwist_app::use_http3.
| cwist_error_t cwist_app_use_https | ( | cwist_app * | app, |
| const char * | cert_path, | ||
| const char * | key_path | ||
| ) |
Enable TLS for the application using the supplied certificate pair.
| app | Application being configured. |
| cert_path | PEM certificate chain path. |
| key_path | PEM private key path. |
Definition at line 1646 of file app.c.
References cap, cwist_app::cert_path, CWIST_ERR_INT16, cwist_free(), cwist_strdup(), cwist_app::key_path, make_error(), cwist_app::use_http3, cwist_app::use_https3, and cwist_app::use_ssl.
| cwist_error_t cwist_app_use_https2 | ( | cwist_app * | app, |
| bool | enabled | ||
| ) |
Definition at line 1760 of file app.c.
References cap, cwist_app::cert_path, CWIST_ERR_INT16, cwist_app::key_path, make_error(), cwist_app::use_https2, and cwist_app::use_ssl.
| cwist_error_t cwist_app_use_https3 | ( | cwist_app * | app, |
| bool | enabled | ||
| ) |
Definition at line 1783 of file app.c.
References cap, cwist_app::cert_path, CWIST_ERR_INT16, cwist_app::key_path, make_error(), cwist_app::use_https3, and cwist_app::use_ssl.
| cwist_error_t cwist_app_use_nuke_db | ( | cwist_app * | app, |
| const char * | db_path, | ||
| int | sync_interval_ms | ||
| ) |
Enable NUKE DB and fall back to standard SQLite when RAM bootstrap fails.
| app | Application being configured. |
| db_path | On-disk database file to mirror into memory. |
| sync_interval_ms | Synchronization interval forwarded to NUKE DB. |
Definition at line 1890 of file app.c.
References cap, cwist_db::conn, cwist_app_use_db(), cwist_db_close(), CWIST_ERR_INT16, cwist_free(), cwist_nuke_close(), CWIST_NUKE_ERR_LOW_MEMORY, cwist_nuke_get_db(), cwist_nuke_init(), CWIST_NUKE_OK, cwist_strdup(), cwist_app::db, cwist_app::db_path, make_error(), and cwist_app::nuke_enabled.
Enable Post-Quantum Cryptography (PQC) hybrid key exchange layer.
| app | Application context. |
| enabled | True to enable PQC hybrid TLS groups and force TLS 1.3. |
Definition at line 1744 of file app.c.
References cwist_app::pqc_layer_enabled.
| cwist_error_t cwist_app_use_redis | ( | cwist_app * | app, |
| const char * | host, | ||
| int | port, | ||
| size_t | max_conns | ||
| ) |
Configure a Redis connection pool for the application.
| app | Application context. |
| host | Redis server host. |
| port | Redis server port. |
| max_conns | Maximum number of pooled connections (>= 1). |
Definition at line 1989 of file app.c.
References cap, CWIST_ERR_INT16, cwist_redis_pool_create(), cwist_redis_pool_destroy(), make_error(), and cwist_app::redis_pool.
| cwist_error_t cwist_app_use_scheduler | ( | cwist_app * | app, |
| size_t | worker_count, | ||
| size_t | queue_capacity | ||
| ) |
Configure a background job scheduler for the application.
| app | Application context. |
| worker_count | Number of background worker threads (>= 1). |
| queue_capacity | Capacity hint for the underlying job queue. |
Definition at line 2020 of file app.c.
References cap, CWIST_ERR_INT16, cwist_scheduler_create(), cwist_scheduler_destroy(), make_error(), and cwist_app::scheduler.
| void cwist_app_use_webtransport | ( | cwist_app * | app, |
| cwist_webtransport_handler_func | handler | ||
| ) |
Enable WebTransport over HTTP/3 on the application.
This registers a WebTransport session handler. The application must also enable HTTP/3 (via cwist_app_use_http3 or cwist_app_use_https3) for WebTransport to function.
| app | Application context. |
| handler | WebTransport session handler. |
Definition at line 1835 of file app.c.
References cwist_http3_set_webtransport_handler(), cwist_app::h3_ctx, and cwist_app::wt_handler.
| void cwist_app_ws | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_ws_handler_func | handler | ||
| ) |
Register a WebSocket upgrade endpoint with default options.
| app | Application being configured. |
| path | Exact GET route that should upgrade to WebSocket. |
| handler | WebSocket handler invoked after a successful upgrade. |
Definition at line 2309 of file app.c.
References cap, CWIST_ENDPOINT_DEFAULT, CWIST_HTTP_GET, and cwist_app::router.
| void cwist_app_ws_async | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_ws_on_message_t | on_message, | ||
| void * | user_data | ||
| ) |
Register a callback-shaped non-blocking WebSocket endpoint (C1M mode).
| app | Application being configured. |
| path | Exact GET route that should upgrade to WebSocket. |
| on_message | Callback invoked per complete message on the reactor path. |
| user_data | Opaque pointer forwarded to the callback. |
Definition at line 2330 of file app.c.
References cap, CWIST_ENDPOINT_DEFAULT, CWIST_HTTP_GET, and cwist_app::router.
| void cwist_app_ws_opt | ( | cwist_app * | app, |
| const char * | path, | ||
| cwist_ws_handler_func | handler, | ||
| cwist_endpoint_opt_t | opts | ||
| ) |
Register a WebSocket route with explicit endpoint options.
| app | Application being configured. |
| path | Exact GET route that should upgrade to WebSocket. |
| handler | WebSocket handler invoked after a successful upgrade. |
| opts | Endpoint flags associated with the route. |
Definition at line 2402 of file app.c.
References cap, CWIST_ENDPOINT_DEFAULT, CWIST_HTTP_GET, and cwist_app::router.
Apply a CWIST_PROFILE preset before the per-variable env reads in cwist_app_listen(). Each setenv() call uses overwrite=0 so any variable already present in the caller's environment takes precedence.
Apply CWIST_PROFILE preset env-var defaults.
Profiles: performance - maximize request throughput: C1M enabled, glibc arena cap, tighter reactor drain chunk (8 events between post drains), larger per-turn HTTP batch so a hot connection yields less. lowmem - minimize resident memory: C1M enabled, glibc arena capped, worker count capped at 2 so total RSS scales with fewer processes (throughput trades off against memory). lowlat - minimize per-request latency: classic thread-pool mode, sub-millisecond median at moderate concurrency. default - C1M enabled with reactor drain chunk 8; also applied when CWIST_PROFILE is unset, so the built-in app baseline uses the tighter drain chunk instead of the reactor's 64-event fallback. Explicit env vars still win (overwrite=0).
Definition at line 4593 of file app.c.
References cap.
| bool cwist_endpoint_has_extern | ( | cwist_endpoint_opt_t | opts, |
| cwist_endpoint_opt_t | flag | ||
| ) |
| bool cwist_https_upgrade_handler | ( | cwist_https_connection * | conn, |
| cwist_http_request * | req, | ||
| cwist_http_response * | res | ||
| ) |
| cwist_stream_req_t * cwist_stream_req_begin | ( | const char * | head, |
| size_t | head_len | ||
| ) |
Begin an incremental request: head is the serialized request line plus headers (everything up to and including the terminating CRLF of the header block). The body length is taken from Content-Length (required for fed bodies; Transfer-Encoding: chunked is not reassembled).
Definition at line 2600 of file app.c.
References cap, cwist_alloc(), cwist_free(), CWIST_STREAM_REQ_EAGER_MAX, and head.
| int cwist_stream_req_dispatch | ( | cwist_stream_req_t * | req, |
| cwist_app * | app, | ||
| cwist_stream_write_fn | write_fn, | ||
| void * | write_ctx | ||
| ) |
Dispatch a completed incremental request with a streaming response. Takes ownership of req regardless of outcome.
Definition at line 2653 of file app.c.
References cap, cwist_app_dispatch_stream(), and cwist_free().
| int cwist_stream_req_end | ( | cwist_stream_req_t * | req | ) |
| int cwist_stream_req_feed | ( | cwist_stream_req_t * | req, |
| const char * | chunk, | ||
| size_t | len | ||
| ) |
Feed one body chunk. Chunks may be any size and may split anywhere; reassembly is the implementation's job. Feeding more bytes than the declared Content-Length is an error.
Definition at line 2626 of file app.c.
References cap, cwist_alloc(), and cwist_free().
| char * cwist_url_for | ( | cwist_app * | app, |
| const char * | name, | ||
| cwist_query_map * | params | ||
| ) |
Definition at line 2475 of file app.c.
References cap, cwist_query_map_get(), cwist_sstring_append(), cwist_sstring_create(), cwist_sstring_destroy(), cwist_strdup(), cwist_sstring::data, and cwist_app::router.