cocoon/fcgi_handler.h
xfy911 a09452b43c FastCGI 服务器集成完成:协议核心、配置解析、请求分发、连接池管理
实现内容:
1. 新增 fcgi_handler.h / fcgi_handler.c 模块:
   - FastCGI 协议核心(BEGIN_REQUEST / PARAMS / STDIN / FCGI_END_REQUEST)
   - CGI 参数构建(REQUEST_METHOD, SCRIPT_NAME, PATH_INFO, QUERY_STRING 等)
   - HTTP 响应解析(Status 头提取、默认 200 回退)
   - 连接池(TCP / Unix Socket 复用)与路由匹配

2. 配置解析扩展(config.c):
   - 支持 --fastcgi-prefix / --fastcgi-target 等命令行参数
   - 支持配置文件的 fastcgi 规则段
   - 最大 4 条规则,可配置 pool_size、timeout_ms、unix_socket

3. 请求处理链集成(server.c):
   - handle_request 中反向代理之后、静态资源之前插入 FastCGI 分发
   - server_context 添加 fcgi_config 字段
   - server_create / server_destroy 中初始化/销毁连接池

4. Makefile 更新:
   - 加入 fcgi_handler.c 编译目标
   - 测试规则同步更新

5. .gitignore 修复:拆分 tests/fixtures/uploads/ 为独立规则

测试:458 单元测试 + 115 集成测试全部通过
关联:Phase 5 应用网关 — FastCGI 服务器集成
2026-06-15 09:28:21 +08:00

82 lines
2.1 KiB
C
Raw Permalink 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.

/**
* fcgi_handler.h - FastCGI 服务器集成模块
*
* 将 FastCGI 协议接入 Cocoon 请求处理流程,
* 支持路径前缀匹配到 FastCGI 后端PHP-FPM 等)。
*
* @author xfy
*/
#ifndef COCOON_FCGI_HANDLER_H
#define COCOON_FCGI_HANDLER_H
#include "cocoon.h"
#include "platform.h"
#include "http.h"
#include "fastcgi.h"
#include <stdbool.h>
/**
* cocoon_fcgi_rule_t - FastCGI 路由规则
*
* 路径前缀匹配 + 后端连接池。
*/
typedef struct {
char prefix[256]; /**< 路径前缀(如 "/api.php" */
fcgi_pool_t pool; /**< 后端连接池 */
fcgi_backend_t backend; /**< 后端配置(内嵌在规则中) */
} cocoon_fcgi_rule_t;
/**
* cocoon_fcgi_config_t - FastCGI 配置集合
*/
typedef struct {
cocoon_fcgi_rule_t rules[COCOON_MAX_FASTCGI_RULES];
size_t count;
} cocoon_fcgi_config_t;
/**
* fcgi_handler_init - 初始化 FastCGI 处理器
*
* 根据服务器配置创建连接池。
*
* @param cfg 输出配置
* @param config 服务器配置(含 fastcgi 数组)
* @return true 成功
*/
bool fcgi_handler_init(cocoon_fcgi_config_t *cfg, const cocoon_config_t *config);
/**
* fcgi_handler_destroy - 销毁 FastCGI 处理器
*
* 关闭所有连接池。
*
* @param cfg FastCGI 配置
*/
void fcgi_handler_destroy(cocoon_fcgi_config_t *cfg);
/**
* fcgi_handler_match - 匹配路径到 FastCGI 规则
*
* @param cfg FastCGI 配置
* @param path 请求路径
* @return 匹配的规则,未匹配返回 NULL
*/
cocoon_fcgi_rule_t *fcgi_handler_match(cocoon_fcgi_config_t *cfg, const char *path);
/**
* fcgi_handler_forward - 将 HTTP 请求转发到 FastCGI 后端
*
* 构建 CGI 环境变量,通过 FastCGI 连接池发送请求,
* 将后端响应(含 HTTP 头)回写给客户端。
*
* @param client_fd 客户端 socket
* @param req HTTP 请求
* @param rule FastCGI 规则
* @return true 保持连接false 关闭
*/
bool fcgi_handler_forward(cocoon_socket_t client_fd, const http_request_t *req,
cocoon_fcgi_rule_t *rule);
#endif /* COCOON_FCGI_HANDLER_H */