cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
app.c File Reference
#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.
 

Macro Definition Documentation

◆ _DEFAULT_SOURCE

#define _DEFAULT_SOURCE

Definition at line 6 of file app.c.

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 2 of file app.c.

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 4 of file app.c.

◆ CWIST_DEFAULT_FD_LIMIT_TARGET

#define CWIST_DEFAULT_FD_LIMIT_TARGET   ((rlim_t)1050000)

Definition at line 99 of file app.c.

◆ CWIST_H2C_THREAD_STACK_SIZE

#define CWIST_H2C_THREAD_STACK_SIZE   (256 * 1024)

Definition at line 3318 of file app.c.

◆ CWIST_HTTP_REQUESTS_PER_TURN_DEFAULT

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

Definition at line 3276 of file app.c.

◆ CWIST_HTTP_REQUESTS_PER_TURN_MAX

#define CWIST_HTTP_REQUESTS_PER_TURN_MAX   1024

Definition at line 3277 of file app.c.

◆ CWIST_ROUTE_BUCKETS

#define CWIST_ROUTE_BUCKETS   127

Definition at line 90 of file app.c.

◆ CWIST_STATIC_RETIRE_NS

#define CWIST_STATIC_RETIRE_NS   TT_SECOND(5)

Definition at line 91 of file app.c.

◆ CWIST_STREAM_REQ_EAGER_MAX

#define CWIST_STREAM_REQ_EAGER_MAX   ((size_t)1 << 20)

Definition at line 2556 of file app.c.

◆ PATH_MAX

#define PATH_MAX   4096

Definition at line 62 of file app.c.

Typedef Documentation

◆ cwist_route_entry

typedef struct cwist_route_entry cwist_route_entry

Enumeration Type Documentation

◆ app_serve_result_t

Route and respond to one fully parsed HTTP/1.1 request. Shared by the blocking keep-alive loop and the event-driven async path.

Enumerator
APP_SERVE_CLOSE 
APP_SERVE_KEEPALIVE 
APP_SERVE_DEFERRED 
APP_SERVE_DETACH 

Definition at line 3085 of file app.c.

Function Documentation

◆ cwist_app_configure_bdr()

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.

Parameters
appApplication whose BDR context should be tuned.
max_bytesMaximum total cache footprint.
max_entry_age_secMaximum age for cached entries.
revalidate_hitsHit count that forces revalidation.

Definition at line 1454 of file app.c.

References cwist_app::bdr_ctx, and cwist_bdr_set_limits().

◆ cwist_app_create()

◆ cwist_app_delete()

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

Definition at line 2195 of file app.c.

References CWIST_HTTP_DELETE.

◆ cwist_app_delete_named()

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.

◆ cwist_app_delete_opt()

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.

◆ cwist_app_destroy()

◆ cwist_app_dispatch()

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

◆ cwist_app_dispatch_memory()

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

Returns
0 on success; -1 when the request is malformed or the response cannot be serialized (e.g. a file-streaming body).

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.

◆ cwist_app_dispatch_stream()

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.

Parameters
appApplication to dispatch against.
req_bufSerialized HTTP/1.1 request (head + body, as the wire form).
req_lenLength of req_buf.
write_fnChunk sink; must not be NULL.
write_ctxOpaque argument passed to every write_fn call.
Returns
0 when the full response was delivered; -1 on dispatch/serialize failure; -2 when the sink aborted.

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

◆ cwist_app_enable_healthz()

void cwist_app_enable_healthz ( struct cwist_app *  app)

Register built-in health check routes on an application.

Adds /healthz, /live, and /ready GET routes.

Parameters
appApplication to register routes on.

Definition at line 2249 of file app.c.

References cwist_app_get().

◆ cwist_app_enable_metrics()

void cwist_app_enable_metrics ( cwist_app *  app)

Definition at line 2227 of file app.c.

References cwist_app_get().

◆ cwist_app_enable_swagger()

void cwist_app_enable_swagger ( cwist_app *  app,
const char *  mount_path,
const char *  openapi_json_path 
)

Enables embedded Swagger UI interactive documentation page.

Parameters
appPointer to the app.
mount_pathURL route path to serve Swagger UI (e.g. "/swagger" or "/docs").
openapi_json_pathPath to the generated openapi.json file on disk.

Definition at line 5051 of file app.c.

References cap, and cwist_app_get().

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

Parameters
appApplication being configured.
pathExact route path.
handlerHTTP 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().

◆ cwist_app_get_db()

cwist_db * cwist_app_get_db ( cwist_app *  app)

Return the active shared database wrapper for the application.

Parameters
appApplication whose database handle should be queried.
Returns
Database wrapper, or NULL when no database is configured.

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_app_get_db_pool()

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_app_get_ex()

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:

  • when the app is destroyed;
  • when the same method and path (one without :param segments) is registered again, by any routing function, before the new registration takes over (unless it passes the same non-NULL user_ctx);
  • immediately, if the registration fails. Multiport sub-apps share the context without owning it. Pass NULL for destroy to keep ownership with the caller.
Parameters
appApplication being configured.
pathRoute path; may contain :param segments.
handlerHandler invoked for matching requests.
user_ctxOpaque pointer passed to handler (may be NULL).
destroyOptional destructor for user_ctx.
Returns
INT16 0 on success; INT16 -1 when 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().

◆ cwist_app_get_named()

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.

◆ cwist_app_get_opt()

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.

Parameters
appApplication being configured.
pathExact route path.
handlerHTTP handler invoked for matching requests.
optsEndpoint flags controlling cache and transport behavior.

Definition at line 2363 of file app.c.

References CWIST_HTTP_GET.

◆ cwist_app_get_redis_pool()

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_app_get_scheduler()

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.

◆ cwist_app_http_handler()

◆ cwist_app_http_handler_async()

◆ cwist_app_listen()

int cwist_app_listen ( cwist_app *  app,
int  port 
)

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.

Parameters
appApplication instance to run.
portTCP port to bind.
Returns
0 on success, or -1 when initialization, bind, or worker shutdown fails.

Definition at line 4628 of file app.c.

References cwist_app_listen_ex().

◆ cwist_app_listen_ex()

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.

Parameters
workers_overrideWorker processes (1 serves without forking); 0 or less reads CWIST_WORKERS.
c1m_override1 for the reactor server, which joins its handler threads before returning; 0 for the classic pool; negative reads CWIST_C1M_MODE. Ignored on WASI.
Returns
0 after a graceful shutdown, -1 if the server could not start.

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

◆ cwist_app_patch()

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

Definition at line 2200 of file app.c.

References CWIST_HTTP_PATCH.

◆ cwist_app_patch_named()

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.

◆ cwist_app_patch_opt()

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.

◆ cwist_app_post()

void cwist_app_post ( cwist_app *  app,
const char *  path,
cwist_handler_func  handler 
)

Register a POST handler with default endpoint options.

Parameters
appApplication being configured.
pathExact route path.
handlerHTTP handler invoked for matching requests.

Definition at line 2267 of file app.c.

References CWIST_ENDPOINT_DEFAULT, and CWIST_HTTP_POST.

◆ cwist_app_post_ex()

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.

◆ cwist_app_post_named()

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.

◆ cwist_app_post_opt()

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.

Parameters
appApplication being configured.
pathExact route path.
handlerHTTP handler invoked for matching requests.
optsEndpoint flags controlling cache and transport behavior.

Definition at line 2375 of file app.c.

References CWIST_HTTP_POST.

◆ cwist_app_put()

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

Definition at line 2190 of file app.c.

References CWIST_HTTP_PUT.

◆ cwist_app_put_named()

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.

◆ cwist_app_put_opt()

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.

◆ cwist_app_register_error_handler()

◆ cwist_app_reload_tls()

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.

◆ cwist_app_set_error_handler()

void cwist_app_set_error_handler ( cwist_app *  app,
cwist_error_handler_func  handler 
)

Install a custom HTTP error callback.

Parameters
appApplication being configured.
handlerHandler invoked for framework-generated errors such as 404 responses.

Definition at line 1411 of file app.c.

References cwist_app::error_handler.

◆ cwist_app_set_max_memspace()

void cwist_app_set_max_memspace ( cwist_app *  app,
size_t  size 
)

Override the static file memory budget used by cwist_mem_init().

Sets the maximum memory space for the static file pool.

Parameters
appApplication being configured.
sizeMaximum bytes reserved for static payload caching.

Definition at line 1402 of file app.c.

References cwist_app::max_mem_space.

◆ cwist_app_set_tls_groups()

void cwist_app_set_tls_groups ( cwist_app *  app,
const char *  groups 
)

Override TLS groups explicitly.

Parameters
appApplication context.
groupsColon-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.

◆ cwist_app_static()

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.

Serves a directory of static files at a URL prefix. Files are loaded into the fixed memory pool for Zero-Copy serving.

Parameters
appApplication being configured.
url_prefixRequest-path prefix such as "/static".
directoryFilesystem directory that backs the mapping.
Returns
Tagged CWIST error describing success or failure.

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_app_static_with_cache()

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.

Parameters
appApplication being configured.
url_prefixRequest-path prefix such as "/static".
directoryFilesystem directory that backs the mapping.
cache_controlValue for the Cache-Control header (e.g. "public, max-age=86400").
Returns
Tagged CWIST error describing success or failure.

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.

◆ cwist_app_use()

void cwist_app_use ( cwist_app *  app,
cwist_middleware_func  mw 
)

Append a middleware callback to the application's execution chain.

Parameters
appApplication being configured.
mwMiddleware callback to append.

Definition at line 1376 of file app.c.

References cap.

◆ cwist_app_use_db()

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.

Parameters
appApplication being configured.
db_pathFilesystem path or SQLite URI to open.
Returns
Tagged CWIST error describing success or failure.

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_app_use_db_pool()

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.

Parameters
appApplication context.
db_pathSQLite database path (or ":memory:").
max_connsMaximum 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_app_use_ex()

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.

Parameters
appApplication being configured.
mwLegacy middleware function (may be NULL if mw_ex is set).
mw_exExtended middleware function with user context (may be NULL if mw is set).
user_ctxOpaque context for mw_ex.
destroyDestructor for user_ctx.
Returns
INT16 0 on success, -1 on failure (the context is already released).

Definition at line 1389 of file app.c.

References cap, CWIST_ERR_INT16, and make_error().

◆ cwist_app_use_http2()

cwist_error_t cwist_app_use_http2 ( cwist_app *  app,
bool  enabled 
)

Enable HTTP/2 cleartext (h2c) over standard TCP.

Parameters
appApplication context.
enabledTrue to enable h2c.
Returns
cwist_error_t Status.

Definition at line 1806 of file app.c.

References cap, CWIST_ERR_INT16, make_error(), and cwist_app::use_http2.

◆ cwist_app_use_http3()

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.

Parameters
appApplication context.
enabledTrue to enable zero-config HTTP/3.
Returns
cwist_error_t Status.

Definition at line 1818 of file app.c.

References cap, CWIST_ERR_INT16, make_error(), and cwist_app::use_http3.

◆ cwist_app_use_https()

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.

Parameters
appApplication being configured.
cert_pathPEM certificate chain path.
key_pathPEM private key path.
Returns
Tagged CWIST error describing success or failure.

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_app_use_https2()

cwist_error_t cwist_app_use_https2 ( cwist_app *  app,
bool  enabled 
)

◆ cwist_app_use_https3()

cwist_error_t cwist_app_use_https3 ( cwist_app *  app,
bool  enabled 
)

◆ cwist_app_use_nuke_db()

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.

Parameters
appApplication being configured.
db_pathOn-disk database file to mirror into memory.
sync_interval_msSynchronization interval forwarded to NUKE DB.
Returns
Tagged CWIST error describing success or failure.

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.

◆ cwist_app_use_pqc_layer()

void cwist_app_use_pqc_layer ( cwist_app *  app,
bool  enabled 
)

Enable Post-Quantum Cryptography (PQC) hybrid key exchange layer.

Parameters
appApplication context.
enabledTrue 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_app_use_redis()

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.

Parameters
appApplication context.
hostRedis server host.
portRedis server port.
max_connsMaximum 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_app_use_scheduler()

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.

Parameters
appApplication context.
worker_countNumber of background worker threads (>= 1).
queue_capacityCapacity 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.

◆ cwist_app_use_webtransport()

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.

Parameters
appApplication context.
handlerWebTransport 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.

◆ cwist_app_ws()

void cwist_app_ws ( cwist_app *  app,
const char *  path,
cwist_ws_handler_func  handler 
)

Register a WebSocket upgrade endpoint with default options.

Parameters
appApplication being configured.
pathExact GET route that should upgrade to WebSocket.
handlerWebSocket 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.

◆ cwist_app_ws_async()

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

Parameters
appApplication being configured.
pathExact GET route that should upgrade to WebSocket.
on_messageCallback invoked per complete message on the reactor path.
user_dataOpaque 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.

◆ cwist_app_ws_opt()

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.

Parameters
appApplication being configured.
pathExact GET route that should upgrade to WebSocket.
handlerWebSocket handler invoked after a successful upgrade.
optsEndpoint 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.

◆ cwist_apply_profile()

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.

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.

◆ cwist_endpoint_has_extern()

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.

Returns
true when any bit of flag is set in opts.

Definition at line 2352 of file app.c.

References cap.

◆ cwist_https_upgrade_handler()

bool cwist_https_upgrade_handler ( cwist_https_connection *  conn,
cwist_http_request *  req,
cwist_http_response *  res 
)

◆ cwist_stream_req_begin()

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

Returns
Handle on success, NULL on allocation failure.

Definition at line 2600 of file app.c.

References cap, cwist_alloc(), cwist_free(), CWIST_STREAM_REQ_EAGER_MAX, and head.

◆ cwist_stream_req_dispatch()

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.

Returns
As cwist_app_dispatch_stream(); -1 also when cwist_stream_req_end() has not succeeded (the fed body is incomplete).

Definition at line 2653 of file app.c.

References cap, cwist_app_dispatch_stream(), and cwist_free().

◆ cwist_stream_req_end()

int cwist_stream_req_end ( cwist_stream_req_t *  req)

Declare the body complete. The fed byte count must equal the declared Content-Length (or both be zero for a bodyless request).

Returns
0 on success, -1 on a short body.

Definition at line 2646 of file app.c.

◆ cwist_stream_req_feed()

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.

Returns
0 on success, -1 on overflow/malformed state.

Definition at line 2626 of file app.c.

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

◆ cwist_url_for()