静态服务器,解析并响应get请求

解析 GET 请求

根据路径返回不同内容

//index.html → 返回 index.html

其他路径 → 返回 404

1.index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Page Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

2.server.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/sendfile.h>

#define PORT 8080

// 解析 GET 请求,提取路径
void parse_get_path(const char *req, char *path_out)
{
// 格式:GET /xxx HTTP/1.1
const char *p = strstr(req, "GET ");
if (!p)
{
strcpy(path_out, "");
return;
}

p += 4; // 跳过 "GET "

const char *space = strchr(p, ' ');
if (!space)
{
strcpy(path_out, "");
return;
}

size_t len = space - p;
if (len > 255) len = 255;

strncpy(path_out, p, len);
path_out[len] = '\0';
}

int main() {
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
{
perror("socket");
return 1;
}

int opt = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = INADDR_ANY;

if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0)
{
perror("bind");
return 1;
}

if (listen(s, SOMAXCONN) < 0)
{
perror("listen");
return 1;
}

printf("Server running on http://0.0.0.0:%d\n", PORT);

while (1)
{
int client_fd = accept(s, NULL, NULL);
if (client_fd < 0)
{
perror("accept");
continue;
}

char buffer[2048] = {0};
recv(client_fd, buffer, sizeof(buffer), 0);

// 解析 GET 路径
char path[256] = {0};
parse_get_path(buffer, path);

printf("Request path: %s\n", path);

// 处理路径
const char *filename = NULL;

if (strcmp(path, "/") == 0 || strcmp(path, "/index.html") == 0)
{
filename = "index.html";
}

if (filename)
{
// 打开文件
int fd = open(filename, O_RDONLY);
if (fd < 0)
{
// 文件不存在 → 404
const char *err = "HTTP/1.1 404 Not Found\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: 13\r\n"
"Connection: close\r\n\r\n"
"404 Not Found";
write(client_fd, err, strlen(err));
close(client_fd);
continue;
}

// 获取文件大小
struct stat st;
fstat(fd, &st);
size_t filesize = st.st_size;

// 发送头
char header[256];
int len = snprintf(header, sizeof(header),
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Content-Length: %zu\r\n"
"Connection: close\r\n\r\n",
filesize
);
write(client_fd, header, len);

// 发送文件内容
off_t offset = 0;
while (offset < filesize)
{
ssize_t sent = sendfile(client_fd, fd, &offset, filesize - offset);
if (sent <= 0) break;
}

close(fd);
}
else
{
// 路径不匹配 → 404
const char *err = "HTTP/1.1 404 Not Found\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: 13\r\n"
"Connection: close\r\n\r\n"
"404 Not Found";
write(client_fd, err, strlen(err));
}

close(client_fd);
}

close(s);
return 0;
}

3.编译运行

1
2
gcc server.c
./a.out

浏览器访问127.0.0.1:8080