cocoon/static.h
xfy911 eb38c9d942
Some checks failed
CI / build (push) Failing after 4m42s
fix: 修复 CI 构建错误和单元测试问题
- 添加缺失的 parser 辅助函数(parser_expect_string, parser_skip_value, parser_number_str)
- 修复 Makefile:添加 test_cache 构建规则,补充 cache.c 依赖
- 修复 test_cache.c:使用 cache_stats() API 替代直接访问内部字段
- 修复 test_config.c:更新 config_merge() 调用参数以匹配新签名
- 修复 CI 工作流:使用 make build-all 构建依赖
2026-06-15 19:16:02 +08:00

80 lines
2.4 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.

/**
* static.h - 静态资源服务模块接口
*
* @author xfy
*/
#ifndef COCOON_STATIC_H
#define COCOON_STATIC_H
#include "http.h"
#include <stdbool.h>
#include "cache.h"
/**
* send_all - 确保缓冲区全部发送
*
* 使用 write 循环发送,直到全部数据发送完毕或遇到不可恢复错误。
* 内部辅助函数,也可被其他模块调用。
*
* @param fd socket 文件描述符
* @param buf 数据缓冲区
* @param len 数据长度
* @return 0 成功,-1 失败
*/
int send_all(int fd, const char *buf, size_t len);
/**
* static_serve_file - 服务单个静态文件
*
* 打开文件根据请求判断是否需要发送部分内容Range
* 然后通过 sendfile 或循环读取发送文件内容。
*
* 如果 cache 不为 NULL且文件大小不超过缓存上限
* 则尝试读取完整文件内容到内存缓存,避免重复磁盘 I/O。
*
* @param fd 客户端 socket 文件描述符
* @param req HTTP 请求
* @param root_dir 静态资源根目录
* @param gzip_enabled 是否启用 gzip 压缩
* @param brotli_enabled 是否启用 brotli 压缩
* @param cache 内存缓存实例(可为 NULL
* @return COCOON_OK 成功,负值错误码
*/
int static_serve_file(int fd, const http_request_t *req, const char *root_dir, bool gzip_enabled, bool brotli_enabled, cocoon_cache_t *cache);
/**
* static_serve_directory - 生成目录列表 HTML
*
* 当请求路径是目录且不含默认 index.html 时,生成美观的目录浏览页面。
*
* @param fd 客户端 socket 文件描述符
* @param req HTTP 请求
* @param root_dir 静态资源根目录
* @param real_path 文件系统上的真实路径
* @return COCOON_OK 成功,负值错误码
*/
int static_serve_directory(int fd, const http_request_t *req,
const char *root_dir, const char *real_path);
/**
* static_send_error - 发送 HTTP 错误响应
*
* @param fd 客户端 socket
* @param status_code HTTP 状态码(如 404、403
* @param keep_alive 是否保持连接
* @return COCOON_OK 成功
*/
int static_send_error(int fd, int status_code, bool keep_alive);
/**
* static_set_cors - 设置是否启用 CORS 响应头
*
* 全局开关,启用后所有 HTTP 响应自动添加 Access-Control-Allow-Origin: *。
*
* @param enabled true 启用false 禁用
*/
void static_set_cors(bool enabled);
#endif /* COCOON_STATIC_H */