- http_request_t 新增 body / body_len / content_type 字段 - http.c 解析 Content-Type 头,新增 http_request_free 释放 body - server.c 新增 conn_read_body 从 socket 读取请求体(8MB 上限) - server.c 新增 handle_post_request 返回 JSON 回显 - 支持 application/json 直接回显 - 支持 application/x-www-form-urlencoded 字符串回显 - 其他类型返回基本信息 - 8MB 请求体上限(HTTP_MAX_BODY),超限返回 413 - 集成测试新增 3 项 POST 测试:JSON 回显、表单回显、PUT 405 - 30 项测试全部通过 - README 更新:添加 POST 特性、curl 示例、路线图更新 - send_all 从 static 改为全局可见,供 server.c 复用 Phase 2 全部完成:缓存/超时/日志/gzip/测试/压测/POST
64 lines
1.8 KiB
C
64 lines
1.8 KiB
C
/**
|
||
* static.h - 静态资源服务模块接口
|
||
*
|
||
* @author xfy
|
||
*/
|
||
|
||
#ifndef COCOON_STATIC_H
|
||
#define COCOON_STATIC_H
|
||
|
||
#include "http.h"
|
||
#include <stdbool.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 或循环读取发送文件内容。
|
||
*
|
||
* @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 */
|