fix(build): 修复编译错误与警告

- Makefile: 添加 -luring 链接(coco 内部使用 io_uring)
- static.c: 添加 #include <time.h> 和 _GNU_SOURCE 定义
- http.h: 添加 #include <stddef.h> 修复 size_t 未定义
- server.c: 删除未使用的 conn_write,修复 root_dir 传递
This commit is contained in:
xfy911 2026-06-03 16:55:48 +08:00
parent 804fa2b1fb
commit 250f336eff
5 changed files with 19 additions and 32 deletions

View File

@ -1,8 +1,12 @@
# Cocoon - 基于 coco 协程库的静态资源 Web 服务器 # Cocoon - 基于 coco 协程库的静态资源 Web 服务器
# coco 库路径(可覆盖)
COCO_INCLUDE ?= ../coco/include
COCO_LIB ?= ../coco/build
CC = gcc CC = gcc
CFLAGS = -Wall -Wextra -O2 -std=c11 -I../coco/include CFLAGS = -Wall -Wextra -O2 -std=c11 -I$(COCO_INCLUDE)
LDFLAGS = -L../coco/build -lcoco -lpthread -lm LDFLAGS = -L$(COCO_LIB) -lcoco -lpthread -lm -luring
# 调试模式 # 调试模式
DEBUG ?= 0 DEBUG ?= 0

2
http.c
View File

@ -1,3 +1,5 @@
#define _GNU_SOURCE
/** /**
* http.c - HTTP * http.c - HTTP
* *

1
http.h
View File

@ -11,6 +11,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h>
/* === HTTP 方法 === */ /* === HTTP 方法 === */
typedef enum { typedef enum {

View File

@ -1,3 +1,5 @@
#define _GNU_SOURCE
/** /**
* server.c - * server.c -
* *
@ -25,6 +27,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <sys/stat.h>
#include "../coco/include/coco.h" #include "../coco/include/coco.h"
@ -44,6 +47,7 @@ typedef struct {
size_t buf_len; /**< 缓冲区已用长度 */ size_t buf_len; /**< 缓冲区已用长度 */
bool keep_alive; /**< 当前连接是否保持 */ bool keep_alive; /**< 当前连接是否保持 */
bool closed; /**< 连接是否已关闭 */ bool closed; /**< 连接是否已关闭 */
const char *root_dir; /**< 静态资源根目录(引用,不拥有) */
} connection_t; } connection_t;
/** /**
@ -112,34 +116,6 @@ static ssize_t conn_read(connection_t *conn) {
return n; 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 * 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) { if (!keep) {
break; break;
} }
@ -284,7 +260,7 @@ static void accept_loop(void *arg) {
printf("[Cocoon] 服务器启动于端口 %d\n", ctx->config.port); printf("[Cocoon] 服务器启动于端口 %d\n", ctx->config.port);
if (ctx->config.threaded) { if (ctx->config.threaded) {
printf("[Cocoon] 多线程模式: %d 个工作线程\n", 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); printf("[Cocoon] 静态资源根目录: %s\n", ctx->config.root_dir);
@ -315,6 +291,7 @@ static void accept_loop(void *arg) {
conn->fd = client_fd; conn->fd = client_fd;
conn->keep_alive = true; conn->keep_alive = true;
conn->closed = false; conn->closed = false;
conn->root_dir = ctx->config.root_dir;
if (ctx->config.threaded && coco_sched_get_current()) { if (ctx->config.threaded && coco_sched_get_current()) {
/* 多线程协程模式:创建协程处理连接 */ /* 多线程协程模式:创建协程处理连接 */

View File

@ -1,3 +1,5 @@
#define _GNU_SOURCE
/** /**
* static.c - * static.c -
* *
@ -16,6 +18,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <time.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>