cocoon/config.h
xfy911 dd41464b9c feat(middleware): 实现中间件框架(CORS / Basic Auth / Rate Limit)
- 新增 middleware.c/middleware.h:注册表 + 链式执行机制
  - 内置 CORS 中间件:支持预检请求、跨域头
  - 内置 Basic Auth 中间件:HTTP 基础认证
  - 内置 Rate Limit 中间件:基于 IP 的秒级限流
- 修复 config_merge 参数不匹配(新增 has_cors_enabled/has_auth_user/has_auth_pass/has_rate_limit)
- 修复 server_destroy 与 main() 双重释放字符串指针
- 修复 main.c 启动段错误(access_log_path 未初始化)
- 修复 rate_limit 哈希函数包含端口导致同一 IP 不同 bucket 的问题
- 修复 rate_limit 中间件 user_data 悬空指针(改为 ctx->mw_config)
- 修复 401/429 响应 Content-Length 与 body 长度不匹配导致 curl 挂起
- 修复集成测试服务器启动循环:使用 nc 代替 curl,避免触发限流计数
- 修复集成测试 kill_server:使用 kill -9 确保进程退出,并检查端口释放
- 单元测试 18/18 通过,集成测试 66/66 通过
2026-06-06 00:32:22 +08:00

53 lines
1.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* config.h - JSON 配置文件解析模块
*
* 轻量级配置加载,支持从 JSON 文件读取服务器配置。
* 保持零依赖,只解析 cocoon 需要的字段。
*
* @author xfy
*/
#ifndef COCOON_CONFIG_H
#define COCOON_CONFIG_H
#include "cocoon.h"
#include <stdbool.h>
/**
* config_load_from_file - 从 JSON 配置文件加载配置
*
* 解析 JSON 文件,填充 cocoon_config_t 结构体。
* 只解析存在的字段,不存在的保持默认值。
*
* @param path JSON 文件路径
* @param config 输出配置结构体
* @return true 成功false 失败(文件不存在或格式错误)
*/
bool config_load_from_file(const char *path, cocoon_config_t *config);
/**
* config_merge - 用命令行配置覆盖文件配置
*
* 优先级:命令行 > 配置文件 > 硬编码默认值
* 命令行参数中显式指定的值(非默认值)会覆盖配置文件。
*
* @param base 从文件加载的基础配置
* @param cmdline 命令行解析出的配置
* @param has_root_dir 命令行是否指定了 -r
* @param has_port 命令行是否指定了 -p
* @param has_workers 命令行是否指定了 -w
* @param has_max_conn 命令行是否指定了 -m
* @param has_timeout 命令行是否指定了 -o
* @param has_log_level 命令行是否指定了 -l
*/
void config_merge(cocoon_config_t *base, const cocoon_config_t *cmdline,
bool has_root_dir, bool has_port, bool has_workers,
bool has_max_conn, bool has_timeout, bool has_log_level,
bool has_gzip_enabled, bool has_brotli_enabled,
bool has_tls_cert, bool has_tls_key, bool has_tls_enabled,
bool has_access_log,
bool has_cors_enabled, bool has_auth_user, bool has_auth_pass,
bool has_rate_limit);
#endif /* COCOON_CONFIG_H */