cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
middleware.c File Reference

Built-in middleware implementations for request IDs, logging, rate limits, CORS, and JWT auth. More...

#include <cwist/sys/app/middleware.h>
#include <cwist/net/http/http.h>
#include <cwist/core/macros.h>
#include <cwist/core/utils/json_builder.h>
#include <cwist/core/mem/alloc.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>
#include "../../net/http/async_internal.h"
#include "log_async.h"
#include <cwist/sys/metrics/metrics.h>
#include <ttak/limit/limit.h>
Include dependency graph for middleware.c:

Go to the source code of this file.

Macros

#define _POSIX_C_SOURCE   200809L
 
#define MAX_IP_BUCKETS   1024
 
#define CWIST_RATE_LIMIT_MAX_CFGS   8
 
#define CWIST_RATE_LIMIT_DEFINE_WRAPPER(N)
 

Typedefs

typedef struct cwist_jwt_ctx cwist_jwt_ctx_t
 

Functions

void cwist_mw_request_id_handler (cwist_http_request *req, cwist_http_response *res, cwist_handler_func next)
 Attach an X-Request-Id header to both the request and the response.
 
cwist_middleware_func cwist_mw_request_id (const char *header_name)
 Return the built-in request ID middleware.
 
cwist_middleware_func cwist_mw_access_log (cwist_log_format_t format)
 Return the built-in access-log middleware.
 
void cwist_mw_metrics_handler (cwist_http_request *req, cwist_http_response *res, cwist_handler_func next)
 Increment request counter and observe duration for Prometheus metrics.
 
cwist_middleware_func cwist_mw_metrics (void)
 Return the built-in metrics collection middleware.
 
void cwist_mw_rate_limit_reset (void)
 Reset all per-IP rate-limiter buckets, clearing tracked clients.
 
cwist_middleware_func cwist_mw_rate_limit_ip (int requests_per_minute)
 Return the built-in per-IP rate-limiter middleware.
 
void cwist_mw_cors_handler (cwist_http_request *req, cwist_http_response *res, cwist_handler_func next)
 Inject permissive CORS headers and short-circuit preflight requests.
 
cwist_middleware_func cwist_mw_cors (void)
 Return the built-in permissive CORS middleware.
 
cwist_middleware_func cwist_mw_jwt_auth (const char *secret)
 Register a JWT secret in a static slot and return the matching middleware wrapper.
 
const cwist_jwt_claims * cwist_mw_jwt_get_claims (const cwist_http_request *req)
 Retrieve the claims the JWT middleware decoded for a request.
 

Detailed Description

Built-in middleware implementations for request IDs, logging, rate limits, CORS, and JWT auth.

Definition in file middleware.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 1 of file middleware.c.

◆ CWIST_RATE_LIMIT_DEFINE_WRAPPER

#define CWIST_RATE_LIMIT_DEFINE_WRAPPER (   N)
Value:
cwist_mw_rate_limit_ip_handler(req, res, next, s_rate_cfgs[N].rpm); \
}
size_t cap
Definition otel.c:56
HTTP Response Object. Supports standard string body or Zero-Copy pointer body.
Definition http.h:82
void(* cwist_handler_func)(cwist_http_request *req, cwist_http_response *res)
Function pointer type for HTTP route handlers.
Definition app.h:32

Definition at line 426 of file middleware.c.

◆ CWIST_RATE_LIMIT_MAX_CFGS

#define CWIST_RATE_LIMIT_MAX_CFGS   8

Definition at line 416 of file middleware.c.

◆ MAX_IP_BUCKETS

#define MAX_IP_BUCKETS   1024

Definition at line 347 of file middleware.c.

Typedef Documentation

◆ cwist_jwt_ctx_t

Function Documentation

◆ cwist_mw_access_log()

cwist_middleware_func cwist_mw_access_log ( cwist_log_format_t  format)

Return the built-in access-log middleware.

Access log middleware factory.

Parameters
formatLog format selector (COMMON, COMBINED, JSON).
Returns
Middleware function pointer for access logging.

Definition at line 296 of file middleware.c.

References cap, CWIST_LOG_COMBINED, CWIST_LOG_COMMON, and CWIST_LOG_JSON.

◆ cwist_mw_cors()

cwist_middleware_func cwist_mw_cors ( void  )

Return the built-in permissive CORS middleware.

CORS middleware. Adds CORS headers and handles OPTIONS requests (returns 204).

Returns
Middleware function pointer for CORS handling.

Definition at line 525 of file middleware.c.

References cwist_mw_cors_handler().

◆ cwist_mw_cors_handler()

void cwist_mw_cors_handler ( cwist_http_request *  req,
cwist_http_response *  res,
cwist_handler_func  next 
)

Inject permissive CORS headers and short-circuit preflight requests.

Parameters
reqIncoming HTTP request.
resOutgoing HTTP response.
nextNext middleware or final handler in the chain.

Definition at line 500 of file middleware.c.

References cwist_http_header_add(), CWIST_HTTP_NO_CONTENT, CWIST_HTTP_OPTIONS, cwist_http_response::headers, cwist_http_request::method, and cwist_http_response::status_code.

Referenced by cwist_mw_cors().

◆ cwist_mw_jwt_auth()

cwist_middleware_func cwist_mw_jwt_auth ( const char *  secret)

Register a JWT secret in a static slot and return the matching middleware wrapper.

JWT authentication middleware factory.

Parameters
secretBorrowed signing secret used to verify bearer tokens.
Returns
Middleware wrapper bound to the supplied secret, or NULL on overflow or when the JWT module is compiled out (CWIST_JWT=0).

Definition at line 649 of file middleware.c.

References cap.

◆ cwist_mw_jwt_get_claims()

const cwist_jwt_claims * cwist_mw_jwt_get_claims ( const cwist_http_request *  req)

Retrieve the claims the JWT middleware decoded for a request.

Retrieve JWT claims stored by cwist_mw_jwt_auth from a request.

Parameters
reqRequest currently executing behind the JWT middleware.
Returns
Active decoded claims, or NULL when the request is not inside JWT auth on this thread.

Definition at line 687 of file middleware.c.

References cap.

◆ cwist_mw_metrics()

cwist_middleware_func cwist_mw_metrics ( void  )

Return the built-in metrics collection middleware.

Prometheus metrics collection middleware.

Returns
Middleware function pointer for request metrics.

Definition at line 333 of file middleware.c.

References cwist_mw_metrics_handler().

◆ cwist_mw_metrics_handler()

void cwist_mw_metrics_handler ( cwist_http_request *  req,
cwist_http_response *  res,
cwist_handler_func  next 
)

Increment request counter and observe duration for Prometheus metrics.

Parameters
reqIncoming HTTP request.
resOutgoing HTTP response.
nextNext middleware or final handler in the chain.

Definition at line 315 of file middleware.c.

References cap, cwist_metric_add(), cwist_metric_inc(), CWIST_METRIC_REQUEST_DURATION_NS, CWIST_METRIC_REQUESTS_TOTAL, and cwist_metrics_registry().

Referenced by cwist_mw_metrics().

◆ cwist_mw_rate_limit_ip()

cwist_middleware_func cwist_mw_rate_limit_ip ( int  requests_per_minute)

Return the built-in per-IP rate-limiter middleware.

Fixed-window rate limiter middleware (per-IP).

Reuses an existing wrapper when the same rate was already registered. If all CWIST_RATE_LIMIT_MAX_CFGS config slots are taken, logs a warning and falls back to the first registered wrapper.

Parameters
requests_per_minuteAllowed requests per minute (token bucket rate); values <= 0 fall back to 60.
Returns
Middleware function pointer for per-IP rate limiting.

Definition at line 467 of file middleware.c.

References cap, and CWIST_RATE_LIMIT_MAX_CFGS.

◆ cwist_mw_rate_limit_reset()

void cwist_mw_rate_limit_reset ( void  )

Reset all per-IP rate-limiter buckets, clearing tracked clients.

Reset the process-local IP table; Redis configs and keys are unchanged.

Definition at line 450 of file middleware.c.

References cap.

◆ cwist_mw_request_id()

cwist_middleware_func cwist_mw_request_id ( const char *  header_name)

Return the built-in request ID middleware.

Request ID middleware.

Request ID middleware factory.

Parameters
header_nameCurrently unused custom header override.
Returns
Middleware function pointer for request ID injection.

Definition at line 79 of file middleware.c.

References cap, cwist_mw_request_id_handler(), and CWIST_UNUSED.

◆ cwist_mw_request_id_handler()

void cwist_mw_request_id_handler ( cwist_http_request *  req,
cwist_http_response *  res,
cwist_handler_func  next 
)

Attach an X-Request-Id header to both the request and the response.

Parameters
reqIncoming HTTP request.
resOutgoing HTTP response.
nextNext middleware or final handler in the chain.

Definition at line 55 of file middleware.c.

References cap, cwist_http_header_add(), cwist_http_header_get(), cwist_http_request::headers, and cwist_http_response::headers.

Referenced by cwist_mw_request_id().