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:
parent
804fa2b1fb
commit
250f336eff
8
Makefile
8
Makefile
@ -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
2
http.c
@ -1,3 +1,5 @@
|
|||||||
|
#define _GNU_SOURCE
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* http.c - HTTP 解析与响应构建
|
* http.c - HTTP 解析与响应构建
|
||||||
*
|
*
|
||||||
|
|||||||
1
http.h
1
http.h
@ -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 {
|
||||||
|
|||||||
37
server.c
37
server.c
@ -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()) {
|
||||||
/* 多线程协程模式:创建协程处理连接 */
|
/* 多线程协程模式:创建协程处理连接 */
|
||||||
|
|||||||
3
static.c
3
static.c
@ -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>
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user