diff --git a/.cocoon-plan.md b/.cocoon-plan.md index 2d14984..b46bb28 100644 --- a/.cocoon-plan.md +++ b/.cocoon-plan.md @@ -26,8 +26,8 @@ - [x] 请求体解析(POST 支持)✅ 2026-06-03 - Content-Length 读取 - JSON / form-urlencoded 回显 - - 8MB 请求体上限 -- [x] C 语言单元测试框架 ✅ 2026-06-03(Unity 框架,66 个测试全部通过) + - **multipart/form-data 文件上传**(保存到 root_dir/uploads/)✅ 2026-06-04 +- [x] C 语言单元测试框架 ✅ 2026-06-03(Unity 框架,99 个测试全部通过) ### Phase 3 — 扩展 - [ ] 配置文件支持(JSON / YAML) @@ -44,21 +44,30 @@ ## 当前状态 - 编译通过,零警告(除 coco 子模块的 linker .note.GNU-stack 提示) -- 30 项集成测试全部通过(GET/HEAD/POST/404/Range/304/gzip/MIME/目录浏览/路径防护) -- **66 个单元测试全部通过**(Unity 框架,覆盖 http.c 和 static.c 核心逻辑) +- **32 项集成测试全部通过**(GET/HEAD/POST/404/Range/304/gzip/MIME/目录浏览/路径防护/**文件上传**) +- **99 个单元测试全部通过**(Unity 框架,覆盖 http.c / static.c / multipart.c 核心逻辑) - 压测数据:wrk -t4 -c100 -d10s → 16,179 RPS,平均延迟 59.86μs -- POST 支持:JSON 和 form-urlencoded 回显,Content-Length 解析,8MB 上限 +- POST 支持:JSON 和 form-urlencoded 回显,multipart 文件上传,Content-Length 解析,8MB 上限 - README / Makefile 已更新 ## 待办池 1. **Brotli 压缩** — 在 Gzip 基础上添加 br 支持 2. **配置文件支持** — JSON/YAML 配置替代纯命令行 -3. **multipart/form-data 文件上传** — 从 POST 回显扩展到实际文件上传 -4. **server.c 单元测试** — 需要 mock socket 和 coco 协程环境 +3. **server.c 单元测试** — 需要 mock socket 和 coco 协程环境 +4. **性能优化** — io_uring 性能调优、连接池优化 +5. **HTTPS / TLS** — 添加 SSL 支持 ## 最近行动记录 +- 2026-06-04: **本轮行动 — multipart 文件上传** + - 实现 `multipart.c` / `multipart.h`:boundary 提取、multipart 解析、part 内存管理 + - `server.c` 集成:POST 请求检测 multipart,保存文件到 `root_dir/uploads/` + - 修复 boundary 解析条件(`>= b_len` 替代 `> b_len`),修复边界扫描越界 + - 创建 `tests/unit/test_multipart.c`(12 个测试):boundary 提取、解析、边界条件 + - 修复集成测试 CRLF 问题:使用 `printf` 生成真实 CRLF 字节 + - 集成测试 32 项全部通过,单元测试 99 个全部通过 + - 推送到 main - 2026-06-03: 项目初始化,核心模块全部实现 - 2026-06-03: 添加缓存协商(ETag + Last-Modified + 304),修复编译警告 - 2026-06-03: 添加连接空闲超时管理 + 最大并发限制 + 分级日志系统 diff --git a/.gitignore b/.gitignore index 43d8dd2..c213028 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,11 @@ -# Cocoon -/build/ +# 忽略构建产物 *.o -*.a -*.so cocoon -.DS_Store -*.swp -*.swo -*~ -.vscode/ -.idea/ + +# 忽略测试二进制文件和上传目录 +tests/unit/test_config +tests/unit/test_http +tests/unit/test_log +tests/unit/test_multipart +tests/unit/test_static +tests/fixtures/uploads/ diff --git a/tests/integration_test.sh b/tests/integration_test.sh index 684a83b..a5ca4c9 100755 --- a/tests/integration_test.sh +++ b/tests/integration_test.sh @@ -219,14 +219,9 @@ assert_post_multipart() { local resp local boundary="----CocoonTestBoundary" local body - body="--${boundary}\r\n" - body+="Content-Disposition: form-data; name=\"file\"; filename=\"upload_test.txt\"\r\n" - body+="Content-Type: text/plain\r\n" - body+="\r\n" - body+="Cocoon multipart upload test content\r\n" - body+="--${boundary}--\r\n" + body=$(printf -- '--%s\r\nContent-Disposition: form-data; name="file"; filename="upload_test.txt"\r\nContent-Type: text/plain\r\n\r\nCocoon multipart upload test content\r\n--%s--\r\n' "$boundary" "$boundary") - resp=$(curl -s -X POST -H "Content-Type: multipart/form-data; boundary=${boundary}" -d "$body" "$url") + resp=$(curl -s -X POST -H "Content-Type: multipart/form-data; boundary=${boundary}" --data-binary "$body" "$url") if echo "$resp" | grep -q '"uploaded"'; then echo " ✓ $desc — 响应包含上传结果" pass diff --git a/tests/unit/test_config.c b/tests/unit/test_config.c index b0c4ac6..4e3d232 100644 --- a/tests/unit/test_config.c +++ b/tests/unit/test_config.c @@ -177,6 +177,69 @@ void test_merge_partial_override(void) { free((void *)base.root_dir); } +void test_load_invalid_field_type(void) { + /* port 为字符串而非数字,应解析失败或忽略 */ + const char *p = write_temp_config("{\"port\": \"not_a_number\"}"); + cocoon_config_t cfg = {0}; + /* 极简解析器可能忽略非法类型,不 crash 即可 */ + config_load_from_file(p, &cfg); + /* port 保持默认值 0 */ + TEST_ASSERT_EQUAL(0, cfg.port); + free((void *)cfg.root_dir); + cleanup(p); +} + +void test_load_log_level_invalid(void) { + const char *p = write_temp_config("{\"log_level\": \"verbose\"}"); + cocoon_config_t cfg = {0}; + config_load_from_file(p, &cfg); + /* 非法 log_level 保持默认值 INFO */ + TEST_ASSERT_EQUAL(LOG_LEVEL_INFO, cfg.log_level); + free((void *)cfg.root_dir); + cleanup(p); +} + +void test_load_nested_ignored(void) { + /* 极简解析器遇到嵌套对象会失败,但至少不 crash */ + const char *p = write_temp_config( + "{\n" + " \"port\": 7777,\n" + " \"nested\": {\"a\": 1}\n" + "}\n" + ); + cocoon_config_t cfg = {0}; + /* 当前极简解析器不支持嵌套对象,返回 false,但不应 segfault */ + config_load_from_file(p, &cfg); + TEST_ASSERT_TRUE(1); /* 走到这里说明没 crash */ + free((void *)cfg.root_dir); + cleanup(p); +} + +void test_load_array_ignored(void) { + /* 极简解析器遇到数组会失败,但至少不 crash */ + const char *p = write_temp_config( + "{\n" + " \"port\": 7777,\n" + " \"arr\": [1, 2, 3]\n" + "}\n" + ); + cocoon_config_t cfg = {0}; + config_load_from_file(p, &cfg); + TEST_ASSERT_TRUE(1); /* 走到这里说明没 crash */ + free((void *)cfg.root_dir); + cleanup(p); +} + +void test_merge_cmdline_null_root_dir(void) { + /* cmdline root_dir 为 NULL,不应覆盖 base */ + cocoon_config_t base = {.root_dir = strdup("/old"), .port = 8080}; + cocoon_config_t cmdline = {.root_dir = NULL, .port = 9090}; + config_merge(&base, &cmdline, true, true, false, false, false, false); + TEST_ASSERT_EQUAL_STRING("/old", base.root_dir); /* NULL 不覆盖 */ + TEST_ASSERT_EQUAL(9090, base.port); /* port 覆盖 */ + free((void *)base.root_dir); +} + void test_merge_null_safety(void) { /* 不应 crash */ config_merge(NULL, NULL, true, true, true, true, true, true); @@ -196,11 +259,16 @@ int main(void) { RUN_TEST(test_load_missing_file); RUN_TEST(test_load_empty_file); RUN_TEST(test_load_null_args); + RUN_TEST(test_load_invalid_field_type); + RUN_TEST(test_load_log_level_invalid); + RUN_TEST(test_load_nested_ignored); + RUN_TEST(test_load_array_ignored); RUN_TEST(test_merge_override_all); RUN_TEST(test_merge_no_override); RUN_TEST(test_merge_partial_override); RUN_TEST(test_merge_null_safety); + RUN_TEST(test_merge_cmdline_null_root_dir); return UNITY_END(); }