|
cwist
Modern C web framework with libttak runtime
|
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>
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. | |
Built-in middleware implementations for request IDs, logging, rate limits, CORS, and JWT auth.
Definition in file middleware.c.
Definition at line 1 of file middleware.c.
Definition at line 426 of file middleware.c.
| #define CWIST_RATE_LIMIT_MAX_CFGS 8 |
Definition at line 416 of file middleware.c.
| #define MAX_IP_BUCKETS 1024 |
Definition at line 347 of file middleware.c.
| typedef struct cwist_jwt_ctx cwist_jwt_ctx_t |
| cwist_middleware_func cwist_mw_access_log | ( | cwist_log_format_t | format | ) |
Return the built-in access-log middleware.
Access log middleware factory.
| format | Log format selector (COMMON, COMBINED, JSON). |
Definition at line 296 of file middleware.c.
References cap, CWIST_LOG_COMBINED, CWIST_LOG_COMMON, and CWIST_LOG_JSON.
| 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).
Definition at line 525 of file middleware.c.
References 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.
| req | Incoming HTTP request. |
| res | Outgoing HTTP response. |
| next | Next 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_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.
| secret | Borrowed signing secret used to verify bearer tokens. |
Definition at line 649 of file middleware.c.
References cap.
| 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.
| req | Request currently executing behind the JWT middleware. |
Definition at line 687 of file middleware.c.
References cap.
| cwist_middleware_func cwist_mw_metrics | ( | void | ) |
Return the built-in metrics collection middleware.
Prometheus metrics collection middleware.
Definition at line 333 of file middleware.c.
References 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.
| req | Incoming HTTP request. |
| res | Outgoing HTTP response. |
| next | Next 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_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.
| requests_per_minute | Allowed requests per minute (token bucket rate); values <= 0 fall back to 60. |
Definition at line 467 of file middleware.c.
References cap, and CWIST_RATE_LIMIT_MAX_CFGS.
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_middleware_func cwist_mw_request_id | ( | const char * | header_name | ) |
Return the built-in request ID middleware.
Request ID middleware.
Request ID middleware factory.
| header_name | Currently unused custom header override. |
Definition at line 79 of file middleware.c.
References cap, cwist_mw_request_id_handler(), and CWIST_UNUSED.
| 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.
| req | Incoming HTTP request. |
| res | Outgoing HTTP response. |
| next | Next 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().