cocoon/README.md
xfy911 bbc45d0735 feat(post): 添加 POST 请求体解析支持
- 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
2026-06-03 21:24:03 +08:00

244 lines
7.1 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

# Cocoon
> 一只从 **coco** 协程库中孵化出的轻量 Web 服务器。
**Cocoon**(茧)—— 用协程的轻盈,包裹静态资源的安稳。
[中文](#特性) | [Quick Start](#quick-start)
---
## 特性
| 特性 | 描述 |
|------|------|
| 🚀 协程驱动 | 基于 coco 有栈协程,每个连接一个协程,上下文切换 < 100ns |
| 📁 静态托管 | 目录浏览MIME 自动识别25+ )、Range 请求index.html 自动补全 |
| 🗜 动态压缩 | 对文本/JSON/CSS/JS 启用 Gzip 压缩图片等二进制文件跳过 |
| 📦 缓存协商 | ETag + Last-Modified + If-None-Match + If-Modified-Since自动 304 |
| 🧵 多核扩展 | M:N 调度器 + Work-stealing自动负载均衡至多核 |
| 零拷贝 | 优先使用 `sendfile`减少用户态/内核态数据拷贝 |
| 🔐 路径安全 | 自动防护路径遍历攻击 (`../`) |
| 🛡 资源限制 | 空闲连接超时默认 30s+ 最大并发连接数限制 |
| 📝 分级日志 | error / warn / info / debug 四级输出命令行可调 |
| 🌐 POST 回显 | 支持 JSON / form-urlencoded 请求体回显API 测试 |
| 🔧 极简配置 | 纯命令行启动无需配置文件 |
## Quick Start
### 依赖
- GCC / ClangC11 标准
- [coco](https://github.com/xfy911/coco) 协程库通过 git submodule 自动获取
- Linux内核 5.1支持 io_uring/ macOS
- CMake 3.10+用于构建 coco 依赖
### 构建
```bash
git clone --recursive https://github.com/xfy911/cocoon.git
cd cocoon
# 如果 clone 时忘了 --recursive补初始化 submodule
git submodule update --init --recursive
# 构建 cocoon会自动检测并构建 coco 依赖)
make build-all
# 或者分开构建
make deps # 先构建 coco
make # 再构建 cocoon
# 也可以使用系统上已有的 coco
make COCO_DIR=/path/to/coco
```
### 运行
```bash
# 单线程模式(开发调试)
./cocoon -r ./examples/www -p 8080
# 多线程模式(生产环境,自动检测 CPU 核心)
./cocoon -r ./examples/www -p 8080 -t
# 指定 8 个工作线程
./cocoon -r ./examples/www -p 8080 -t -w 8
```
### 测试
```bash
# 启动服务器
./cocoon -r ./examples/www -p 8080 &
# 访问首页
curl http://localhost:8080/
# 目录浏览
curl http://localhost:8080/examples/
# Range 请求
curl -H "Range: bytes=0-99" http://localhost:8080/index.html
# 缓存协商304 Not Modified
curl -I -H "If-None-Match: \"your-etag\"" http://localhost:8080/index.html
# Gzip 压缩
curl -I -H "Accept-Encoding: gzip" http://localhost:8080/index.html
# POST JSON 回显
curl -X POST -H "Content-Type: application/json" -d '{"hello":"world"}' http://localhost:8080/api/echo
# POST 表单回显
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'name=cocoon' http://localhost:8080/api/echo
```
### 自动化测试
```bash
# 集成测试curl + bash
make test
# 性能基准wrk
make bench
```
## 命令行参数
```
Usage: ./cocoon [options]
Options:
-r <dir> 静态资源根目录(必填)
-p <port> 监听端口(默认 8080
-t 启用多线程调度
-w <num> 工作线程数(默认自动检测 CPU 核心)
-m <num> 最大并发连接数限制(默认 10000
-o <ms> 连接空闲超时毫秒数(默认 30000
-l <level> 日志级别debug, info, warn, error默认 info
-v 显示版本号
-h 显示帮助
```
## 架构
```
┌─────────────┐ ┌─────────────────┐ ┌──────────────┐
│ main.c │────▶│ server.c │────▶│ static.c │
│ (入口) │ │ (TCP 服务器) │ │ (静态文件) │
└─────────────┘ └─────────────────┘ └──────────────┘
┌─────────────────┐
│ http.c │
│ (HTTP 解析) │
└─────────────────┘
┌─────────────────┐
│ coco 协程库 │
│ (并发 + I/O) │
└─────────────────┘
```
### 模块职责
| 文件 | 职责 |
|------|------|
| `main.c` | 程序入口信号处理命令行参数解析 |
| `server.c` | TCP 服务器生命周期 + 连接超时管理 + 并发限制 |
| `http.c` | HTTP/1.1 请求解析响应头格式化MIME 类型推断 |
| `static.c` | 静态文件服务sendfile/gzip)、目录浏览 HTML错误响应 |
| `log.c` | 分级日志输出error/warn/info/debug |
| `cocoon.h` | 公共配置结构体与错误码定义 |
## 核心 API 速览
### HTTP 解析
```c
http_request_t req;
int parsed = http_parse_request(buf, buf_len, &req);
// req.method, req.path, req.content_length, req.keep_alive ...
```
### 响应格式化
```c
http_response_t resp = {
.status_code = 200,
.content_type = "text/html",
.content_length = file_size,
.keep_alive = true
};
int n = http_format_response_header(buf, sizeof(buf), &resp);
```
### 文件服务
```c
static_serve_file(fd, &req, "/var/www/html");
static_serve_directory(fd, &req, "/var/www/html", real_path);
static_send_error(fd, 404, true);
```
## 安全设计
- **路径遍历防护**自动过滤 `../`拒绝超出根目录的访问
- **目录隐藏**不显示以 `.` 开头的隐藏文件
- **HTML 转义**目录列表中的文件名自动转义防止 XSS
- **请求方法过滤**仅允许 `GET` / `HEAD`拒绝其他方法
## 性能
| 指标 | 数值 | 条件 |
|------|------|------|
| 协程上下文切换 | < 100ns | coco 基准 |
| 单线程 RPS | ~16K | wrk, 100 连接, 4 线程 |
| 平均延迟 | ~60μs | wrk, 100 连接, 4 线程 |
| 最大并发 | 数万连接 | 单线程模式 |
| 多线程扩展 | 线性至核数 | M:N 调度 + Work-stealing |
> 压测环境AMD EPYC / 4 核 / Ubuntu 22.04 / io_uring 后端
```bash
# 复现压测
make bench
```
## 示例页面
内置响应式示例首页展示 Cocoon 三大特性
![Cocoon Demo](examples/www/index.html)
> 渐变紫蓝背景 + 玻璃拟态卡片 + 移动端适配
## 路线图
- [x] ETag / Last-Modified 缓存协商 + 304 Not Modified
- [x] Gzip 动态压缩
- [x] 连接空闲超时 + 最大并发限制
- [x] 分级日志系统
- [x] POST 请求体解析JSON / form-urlencoded 回显
- [ ] HTTPS / TLS 支持
- [ ] HTTP/2 多路复用
- [ ] 配置文件支持JSON / YAML
## 构建与安装
```bash
make build-all # 完整构建(含 coco 依赖)
make test # 运行集成测试
make bench # 运行性能基准
make install # 安装到 /usr/local/bin
make clean # 清理构建产物
```
## 许可证
MIT © xfy
> 用 [coco](https://github.com/DefectingCat/coco) 孵化,为轻量而生。