KEEL 1.0.0
Minimal C11 HTTP client/server library built on epoll/kqueue/io_uring/poll
Loading...
Searching...
No Matches
Data Structures | Macros | Functions
cors.h File Reference

Built-in CORS middleware. More...

#include <keel/request.h>
#include <keel/response.h>
#include <stddef.h>
Include dependency graph for cors.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  KlCorsConfig
 

Macros

#define KL_CORS_MAX_ORIGINS   16
 Maximum number of allowed origins.
 
#define KL_CORS_ORIGIN_SIZE   256
 Maximum origin string length.
 

Functions

void kl_cors_init (KlCorsConfig *config)
 Initialize CORS config with defaults. Allows all origins (*), standard methods/headers, 24h preflight cache.
 
int kl_cors_add_origin (KlCorsConfig *config, const char *origin)
 Add an allowed origin. If no origins are added, all origins are allowed (*). Once at least one origin is added, only those origins are allowed.
 
int kl_cors_parse_origins (KlCorsConfig *config, const char *origins)
 Parse comma-separated origins and add them. Useful for parsing from environment variables.
 
int kl_cors_is_allowed (const KlCorsConfig *config, const char *origin, size_t origin_len)
 Check if an origin is allowed by the config.
 
int kl_cors_middleware (KlRequest *req, KlResponse *res, void *user_data)
 CORS middleware function. Pass a KlCorsConfig* as user_data when registering.
 

Detailed Description

Built-in CORS middleware.

Usage:

kl_cors_init(&cors);
kl_cors_add_origin(&cors, "https://app.example.com");
kl_server_use(&s, "*", pattern, kl_cors_middleware, &cors);
int kl_cors_middleware(KlRequest *req, KlResponse *res, void *user_data)
CORS middleware function. Pass a KlCorsConfig* as user_data when registering.
int kl_cors_add_origin(KlCorsConfig *config, const char *origin)
Add an allowed origin. If no origins are added, all origins are allowed (*). Once at least one origin...
void kl_cors_init(KlCorsConfig *config)
Initialize CORS config with defaults. Allows all origins (*), standard methods/headers,...
int kl_server_use(KlServer *s, const char *method, const char *pattern, KlMiddleware fn, void *user_data)
Register pre-body middleware on the server.
Definition cors.h:25

Macro Definition Documentation

◆ KL_CORS_MAX_ORIGINS

#define KL_CORS_MAX_ORIGINS   16

Maximum number of allowed origins.

◆ KL_CORS_ORIGIN_SIZE

#define KL_CORS_ORIGIN_SIZE   256

Maximum origin string length.

Function Documentation

◆ kl_cors_init()

void kl_cors_init ( KlCorsConfig config)

Initialize CORS config with defaults. Allows all origins (*), standard methods/headers, 24h preflight cache.

◆ kl_cors_add_origin()

int kl_cors_add_origin ( KlCorsConfig config,
const char *  origin 
)

Add an allowed origin. If no origins are added, all origins are allowed (*). Once at least one origin is added, only those origins are allowed.

Returns
1 on success, 0 if full or invalid.

◆ kl_cors_parse_origins()

int kl_cors_parse_origins ( KlCorsConfig config,
const char *  origins 
)

Parse comma-separated origins and add them. Useful for parsing from environment variables.

Returns
Number of origins added.

◆ kl_cors_is_allowed()

int kl_cors_is_allowed ( const KlCorsConfig config,
const char *  origin,
size_t  origin_len 
)

Check if an origin is allowed by the config.

Returns
1 if allowed, 0 if not.

◆ kl_cors_middleware()

int kl_cors_middleware ( KlRequest req,
KlResponse res,
void *  user_data 
)

CORS middleware function. Pass a KlCorsConfig* as user_data when registering.

For OPTIONS requests: sends 204 with preflight headers, short-circuits. For other requests: adds Access-Control-Allow-Origin and continues.

Returns
0 to continue, 1 to short-circuit (OPTIONS preflight).