# Cocoon [](https://github.com/xfy911/cocoon/actions/workflows/ci.yml) > 一只从 **coco** 协程库中孵化出的轻量 Web 服务器。 **Cocoon**(茧)—— 用协程的轻盈,包裹静态资源的安稳。 [中文](#特性) | [Quick Start](#quick-start) --- ## 特性 | 特性 | 描述 | |------|------| | 🚀 协程驱动 | 基于 coco 有栈协程,每个连接一个协程,上下文切换 < 100ns | | 📁 静态托管 | 目录浏览、MIME 自动识别(25+ 种)、Range 请求、index.html 自动补全 | | 🗜️ 动态压缩 | 对文本/JSON/CSS/JS 启用 Brotli / Gzip 压缩,图片等二进制文件跳过,Brotli 优先 | | 📦 缓存协商 | ETag + Last-Modified + If-None-Match + If-Modified-Since,自动 304 | | 🧵 多核扩展 | M:N 调度器 + Work-stealing,自动负载均衡至多核 | | ⚡ 零拷贝 | 优先使用 `sendfile`,减少用户态/内核态数据拷贝 | | 🔐 路径安全 | 自动防护路径遍历攻击 (`../`) | | 🛡️ 资源限制 | 空闲连接超时(默认 30s)+ 最大并发连接数限制 | | 📝 分级日志 | error / warn / info / debug 四级输出,命令行可调 | | 📊 访问日志 | Nginx combined 格式,记录 User-Agent / Referer / 状态码 | | 🌐 POST 回显 | 支持 JSON / form-urlencoded / multipart 文件上传 | | 🔧 配置文件 | JSON 配置文件 + 命令行参数覆盖,生产部署友好 | | 🔒 HTTPS | TLS 1.2/1.3,OpenSSL Memory BIO 集成,ALPN 协商 HTTP/2 | | 🚀 HTTP/2 | 完整 HTTP/2 支持(TLS + h2c 明文升级),多路复用 | | 🔌 WebSocket | RFC 6455,支持广播 / 频道路由 / 定向发送 | | 🧩 中间件 | 内置 CORS / Basic Auth / Rate Limit,可自定义注册 | | 🔌 插件系统 | 动态加载 .so 插件,SIGUSR1 热重载 | | 🏥 健康检查 | `/_health` 端点返回 JSON 服务器状态 | | 🔄 反向代理 | HTTP/1.1 + HTTPS 后端转发,路径前缀匹配,X-Forwarded-* 头透传 | | 🪟 跨平台 | Linux / macOS / Windows(MinGW/MSVC)| ## Quick Start ### 依赖 - GCC / Clang(C11 标准) - [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 # Brotli 压缩(优先) curl -I -H "Accept-Encoding: br" 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 # 单元测试(Unity 框架) make unit-test # 一键运行所有测试 make test-all # 性能基准(wrk) make bench ``` ## 配置文件示例 ```json { "root_dir": "./examples/www", "port": 8080, "threaded": true, "workers": 4, "max_connections": 10000, "timeout_ms": 30000, "log_level": "info", "access_log": "-", "cors_enabled": true, "rate_limit": 100, "plugins": ["plugins/hello.so"], "proxies": [ { "prefix": "/api", "target": "http://localhost:3000" }, { "prefix": "/secure", "target": "https://api.example.com" } ] } ``` 运行:`./cocoon -c cocoon.json` ## 命令行参数 ``` Usage: ./cocoon [options] Options: -r