cocoon/static.h
xfy911 d7b9c93e78 feat(static): 实现静态资源服务模块
- static_serve_file: 文件服务(sendfile 零拷贝 + Range 支持)
- static_serve_directory: 目录浏览(HTML 列表 + 文件大小/时间)
- static_send_error: HTTP 错误响应(400/403/404/416/500)
- safe_path_join: 路径遍历防护
2026-06-03 16:45:48 +08:00

51 lines
1.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>
/**
* static_serve_file - 服务单个静态文件
*
* 打开文件根据请求判断是否需要发送部分内容Range
* 然后通过 sendfile 或循环读取发送文件内容。
*
* @param fd 客户端 socket 文件描述符
* @param req HTTP 请求
* @param root_dir 静态资源根目录
* @return COCOON_OK 成功,负值错误码
*/
int static_serve_file(int fd, const http_request_t *req, const char *root_dir);
/**
* 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);
#endif /* COCOON_STATIC_H */