Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
Loading...
Searching...
No Matches
config.c File Reference

YAML configuration loader and validator implementation. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <yaml.h>
#include "csilk/csilk.h"
Include dependency graph for config.c:

Functions

static csilk_log_level_t string_to_log_level (const char *s)
 Convert a case-insensitive log level string to its enum value.
 
int csilk_load_config (const char *yaml_path, csilk_config_t *config)
 Load and parse server configuration from a YAML file.
 
void csilk_config_free (csilk_config_t *config)
 Free all dynamically allocated strings within a configuration.
 
int csilk_config_validate (const csilk_config_t *config, const char **error_msg)
 Validate configuration values for semantic correctness.
 

Detailed Description

YAML configuration loader and validator implementation.

Parses a YAML configuration file (using libyaml) into a csilk_config_t struct. The parser is a simple state machine that tracks the current YAML section and key, then populates the corresponding config field.

Supported sections: port (top-level), server, logger, cors, rate_limit, static_files, middleware, ai, cipher.

The companion csilk_config_validate() checks semantic correctness (port range, non-negative timeouts, required sub-fields for enabled features). csilk_config_free() releases all dynamically allocated strings.

Function Documentation

◆ csilk_config_free()

void csilk_config_free ( csilk_config_t config)

Free all dynamically allocated strings within a configuration.

Free all heap-allocated strings inside a configuration.

Releases memory for logger.file_path, cors allow_origin/methods/headers, static_files root_dir/prefix, and middleware.auth_token. Each pointer is set to NULL after being freed, making repeated calls safe.

Parameters
configConfiguration structure whose strings will be freed.
Note
Does NOT free the config struct itself — only its internal heap allocations. Safe to call with NULL.

◆ csilk_config_validate()

int csilk_config_validate ( const csilk_config_t config,
const char **  error_msg 
)

Validate configuration values for semantic correctness.

Checks that port is in range [1, 65535], timeouts are non-negative, max_body_size and max_header_size are positive, listen_backlog and worker_threads are >= 1, and that optional features (rate_limit, static_files, auth middleware) have their required sub-fields set when enabled. Returns the first validation error found.

Parameters
configConfiguration to validate.
error_msg[out] Optional pointer to receive a human-readable error string. The error string is a static literal; do not free it.
Returns
0 if valid, -1 if validation fails (error_msg is set on failure).

◆ csilk_load_config()

int csilk_load_config ( const char *  yaml_path,
csilk_config_t config 
)

Load and parse server configuration from a YAML file.

Load and parse a YAML configuration file.

Opens the specified YAML file, parses its contents, and populates the provided csilk_config_t structure. The parser recognizes top-level keys ("port") and section keys ("server", "logger", "cors", "rate_limit", "static_files", "middleware"). All fields not present in the YAML retain their default values (initialized at the start of this function).

Parameters
yaml_pathAbsolute or relative path to the YAML configuration file.
config[out] Pre-allocated config structure to populate.
Returns
0 on success, -1 on I/O error, parse error, or NULL input.
Note
Dynamically allocated strings (file_path, allow_origin, etc.) are strdup'd and must be freed later with csilk_config_free().
The config structure is memset to zero first, then defaults are set. Callers that have already populated fields will lose them.

◆ string_to_log_level()

static csilk_log_level_t string_to_log_level ( const char *  s)
static

Convert a case-insensitive log level string to its enum value.

Matches the input string against known level names ("TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"). Comparison is case-insensitive.

Parameters
sLog level string (e.g., "DEBUG", "debug", "Debug").
Returns
Corresponding csilk_log_level_t value.
Note
Returns CSILK_LOG_INFO for unrecognized strings — no error is raised.