diff --git a/Makefile b/Makefile index 437028c..df3c9ae 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,12 @@ # Cocoon - 基于 coco 协程库的静态资源 Web 服务器 +# coco 库路径(可覆盖) +COCO_INCLUDE ?= ../coco/include +COCO_LIB ?= ../coco/build + CC = gcc -CFLAGS = -Wall -Wextra -O2 -std=c11 -I../coco/include -LDFLAGS = -L../coco/build -lcoco -lpthread -lm +CFLAGS = -Wall -Wextra -O2 -std=c11 -I$(COCO_INCLUDE) +LDFLAGS = -L$(COCO_LIB) -lcoco -lpthread -lm -luring # 调试模式 DEBUG ?= 0 diff --git a/http.c b/http.c index 272e1c3..4ad4e94 100644 --- a/http.c +++ b/http.c @@ -1,3 +1,5 @@ +#define _GNU_SOURCE + /** * http.c - HTTP 解析与响应构建 * diff --git a/http.h b/http.h index 7a5cd33..f32c9e6 100644 --- a/http.h +++ b/http.h @@ -11,6 +11,7 @@ #include #include +#include /* === HTTP 方法 === */ typedef enum { diff --git a/server.c b/server.c index 60bfe43..3768f1f 100644 --- a/server.c +++ b/server.c @@ -1,3 +1,5 @@ +#define _GNU_SOURCE + /** * server.c - 服务器核心实现 * @@ -25,6 +27,7 @@ #include #include #include +#include #include "../coco/include/coco.h" @@ -44,6 +47,7 @@ typedef struct { size_t buf_len; /**< 缓冲区已用长度 */ bool keep_alive; /**< 当前连接是否保持 */ bool closed; /**< 连接是否已关闭 */ + const char *root_dir; /**< 静态资源根目录(引用,不拥有) */ } connection_t; /** @@ -112,34 +116,6 @@ static ssize_t conn_read(connection_t *conn) { return n; } -/** - * conn_write - 向连接写入数据(协程安全) - * - * 使用 coco 的 I/O API 进行非阻塞写入,协程自动 yield 等待可写。 - * - * @param fd socket - * @param data 数据缓冲区 - * @param len 数据长度 - * @return 0 成功,-1 失败 - */ -static int conn_write(int fd, const char *data, size_t len) { - if (fd < 0) return -1; - - size_t sent = 0; - while (sent < len) { - ssize_t n; - if (coco_sched_get_current() != NULL) { - n = coco_write(fd, data + sent, len - sent); - } else { - n = write(fd, data + sent, len - sent); - } - if (n < 0) return -1; - if (n == 0) return -1; - sent += (size_t)n; - } - return 0; -} - /** * handle_request - 处理单个 HTTP 请求 * @@ -259,7 +235,7 @@ static void client_handler(void *arg) { } /* 尝试处理请求 */ - bool keep = handle_request(conn, conn->sched ? "" : ""); + bool keep = handle_request(conn, conn->root_dir); if (!keep) { break; } @@ -284,7 +260,7 @@ static void accept_loop(void *arg) { printf("[Cocoon] 服务器启动于端口 %d\n", ctx->config.port); if (ctx->config.threaded) { printf("[Cocoon] 多线程模式: %d 个工作线程\n", - ctx->config.num_workers > 0 ? ctx->config.num_workers : (int)sysconf(_SC_NPROCESSORS_ONLN)); + ctx->config.num_workers > 0 ? ctx->config.num_workers : (uint32_t)sysconf(_SC_NPROCESSORS_ONLN)); } printf("[Cocoon] 静态资源根目录: %s\n", ctx->config.root_dir); @@ -315,6 +291,7 @@ static void accept_loop(void *arg) { conn->fd = client_fd; conn->keep_alive = true; conn->closed = false; + conn->root_dir = ctx->config.root_dir; if (ctx->config.threaded && coco_sched_get_current()) { /* 多线程协程模式:创建协程处理连接 */ diff --git a/static.c b/static.c index 71cd47c..bd5598e 100644 --- a/static.c +++ b/static.c @@ -1,3 +1,5 @@ +#define _GNU_SOURCE + /** * static.c - 静态资源服务实现 * @@ -16,6 +18,7 @@ #include #include #include +#include #include #include