-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpd.cpp
More file actions
408 lines (367 loc) · 8.86 KB
/
httpd.cpp
File metadata and controls
408 lines (367 loc) · 8.86 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include "httpd.h"
#include <sys/sendfile.h>
#include <ctype.h>
#include <sys/stat.h>
//打印日志
void print_log(char* error, int level, char* file, int line)
{
time_t now; //当前时间戳
time(&now);
struct tm* timenow = localtime(&now);
//openprint_log sysprint_log closeprint_log
const char* log_level[5] = {"SUCCESS", "NOTICE", "WARNING", "ERROR", "FATAL"};
int fd = open("log/httpd.log", O_CREAT|O_WRONLY|O_APPEND, S_IWUSR|S_IRUSR);
char buf[1024];
sprintf(buf, "[%s] %s\t\t%s(%d): %s\n", log_level[level], asctime(timenow), file, line, error);
write(fd, buf, strlen(buf));
close(fd);
}
//返回状态码
static void status_code(int sock, int status)
{
const char* reason;//状态码描述
switch(status)
{
case 400:
reason = "Bad Request";
break;
case 403:
reason = "Forbidden";
break;
case 404:
reason = "Not Found";
break;
case 500:
reason = "Internal Server Error";
break;
case 503:
reason = "Server Unavailable";
break;
default:
print_log("status code error", WARNING, __FILE__, __LINE__);
reason = "Internal Server Error";
break;
}
char buf[SIZE/8];
sprintf(buf, "HTTP/1.0 %d %s\r\n", status, reason);
if(send(sock, buf, strlen(buf), 0) < 0)
{
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
return;
}
const char* head = "Content-Type: text/html\r\n";
if(send(sock, head, strlen(head), 0) <0 )
{
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
return;
}
if(send(sock, "\r\n", 2, 0) < 0)
{
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
return;
}
sprintf(buf, "<html><head><title>%d %s</title></head><body><h2><center>%d %s</center></h2><hr/></body></html>",
status, reason, status, reason);
if(send(sock, buf, strlen(buf), 0) < 0)
{
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
return;
}
}
static ssize_t read_line(int sock, char* buf, size_t size);
//清除消息报头
static void clear_header(int sock)
{
char buf[SIZE];
ssize_t size = 0;
do{
size = read_line(sock, buf, SIZE);
}while(size > 0 && strcmp("\n", buf));
//while(size > 1 || strcmp(buf, "\n")); // bug:没读到\n就一直读有bug,可能请求头里没\n
}
//启动服务
int startup(int port)
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
exit(1);
}
int opt = 1;
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
print_log(strerror(errno), WARNING, __FILE__, __LINE__);
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_addr.s_addr = inet_addr("0");
local.sin_port = htons(port);
if(bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0)
{
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
exit(2);
}
if(listen(sock, 10) < 0)
{
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
exit(3);
}
print_log("listen success", SUCCESS, __FILE__, __LINE__);
return sock;
}
//读取请求行报头行
static ssize_t read_line(int sock, char *buf, size_t len)
{
ssize_t i = 0;
char c = '\0';
ssize_t n;
while ((i < len - 1) && (c != '\n'))
{
n = recv(sock, &c, 1, 0);
if (n > 0)
{
// Window:\r\n Linux:\n Mac:\r
if (c == '\r')
{
n = recv(sock, &c, 1, MSG_PEEK);
if ((n > 0) && (c == '\n'))
recv(sock, &c, 1, 0);
else
c = '\n';
}
buf[i++] = c;
}
else if(n == 0)
{
break; //接收失败,直接跳出循环
}
else
{
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
return -1;
}
}
buf[i] = '\0';
return i;
}
//处理cgi请求
static int exec_cgi(int sock, const char* method, const char* path, const char* query)
{
char method_env[SIZE/16];
char query_env[SIZE/2];
char content_len_env[SIZE/8];
//处理http请求头
int content_len = 0;//请求正文长度
char buf[SIZE/8];
if(strcasecmp(method, "GET") == 0)
clear_header(sock);
else //POST
while(read_line(sock, buf, sizeof(buf)) > 1)
if(strncasecmp(buf, "Content-Length: ", 16) == 0)
{
content_len = atoi(buf+16);
clear_header(sock);
break;
}
//发送应答头部
const char* respond_line = "HTTP/1.0 200 OK\r\n";
if(send(sock, respond_line, strlen(respond_line), 0) <= 0)
{
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
return 1;
}
const char* content_type = "Content-Type: text/html;charset=UTF-8\r\n";
if(send(sock, content_type, strlen(content_type), 0) <= 0)
{
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
return 2;
}
if(send(sock, "\r\n", 2, 0) <= 0)
{
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
return 3;
}
//socketpair进行双向通信
int sv[2];
if(socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) < 0)
{
status_code(sock, 500);
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
return 4;
}
//fork子进程execl
pid_t pid = fork();
if(pid < 0)
{
status_code(sock, 500);
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
return 5;
}
else if(pid == 0) //child
{
//关闭不用的fd
close(sock);
close(sv[0]);
//文件描述符重定向
dup2(sv[1], 0);
dup2(sv[1], 1);
dup2(sv[1], 2); //处理错误信息要用
//环境变量传递参数
//1.方便解析字符串2.传送数据量少3.exec后的进程仍然可以看到
sprintf(method_env, "METHOD=%s", method);
putenv(method_env);
if(strcasecmp(method, "GET") == 0)
{
sprintf(query_env, "QUERY=%s", query);
putenv(query_env);
}
else //POST
{
sprintf(content_len_env, "CONTENTLENGTH=%d", content_len);
putenv(content_len_env);
}
//程序替换
execl(path, path, NULL);
print_log("execl is error", FATAL, __FILE__, __LINE__);
exit(6);
}
else //father
{
close(sv[1]);
//读取POST方法正文内容通过管道发送给子进程
char buf[content_len+1];
if(strcasecmp(method, "POST") == 0)
{
//从消息正文读 可能buf存不下
if(recv(sock, buf, content_len, 0) < 0)
print_log("recv failed", FATAL, __FILE__, __LINE__);
//写到管道
if(write(sv[0], buf, content_len) < 0)
print_log("write failed", FATAL, __FILE__, __LINE__);
}
//从管道读取内容发给sock
ssize_t s;
while((s = read(sv[0], buf, sizeof(buf))) > 0)
{
if(send(sock, buf, s, 0) < 0)
print_log(strerror(errno), FATAL, __FILE__, __LINE__);
}
waitpid(-1, NULL, 0);
}
return 0;
}
//发送普通文件
static int send_file(int sock, const char* path, ssize_t size)
{
int fd = open(path, O_RDONLY);
if(fd < 0)
{
print_log(strerror(errno), ERROR, __FILE__, __LINE__);
status_code(sock, 404);
close(fd);
return -1;
}
if(send(sock, "HTTP/1.0 200 OK\r\n", 17, 0) <= 0)
{
print_log(strerror(errno), ERROR, __FILE__, __LINE__);
close(fd);
return -2;
}
if(send(sock, "\r\n", 2, 0) <= 0) //bug:挂在第二次send
{
print_log(strerror(errno), ERROR, __FILE__, __LINE__);
close(fd);
return -3;
}
if(sendfile(sock, fd, NULL, size) <= 0)
{
print_log(strerror(errno), ERROR, __FILE__, __LINE__);
close(fd);
return -4;
}
close(fd);
return 0;
}
int request_handle(int sock)
{
char line[SIZE]; //存储一行内容
char* c = line;
char method[8]; //存储请求方法
char url[SIZE];
char path[SIZE]; //请求路径
char* query_str = NULL; //指向GET方法的参数
int cgi = 0; //是否为cgi模式
int ret = 0;
ssize_t size = read_line(sock, line, sizeof(line));//读一行
if(size <= 0)
{
print_log("request error", ERROR, __FILE__, __LINE__);
close(sock);
return 1;
}
//读取到第一行,获取请求方法
int i = 0;
while(i < 8 && !isblank(*c))
method[i++] = *c++;
method[i] = '\0';
//获取url
c++;
i = 0;
while(i < SIZE && !isblank(*c))
url[i++] = *c++;
url[i] = '\0';
//设置cgi模式
//1. POST方法肯定是cgi模式
//2. GET方法携带参数是cgi模式
if(strcasecmp("POST", method) == 0)
cgi = 1;
else if(strcasecmp("GET", method) == 0)
{
query_str = url;
while(*query_str && *query_str != '?')
++query_str;
if(*query_str == '?')
{
*query_str = '\0';
++query_str;
cgi = 1;
}
}
else //既不是GET也不是POST
{
clear_header(sock);
status_code(sock, 400);
print_log("Unknown request method", WARNING, __FILE__, __LINE__);
ret = 2;
goto END;
}
//处理请求路径
sprintf(path, "wwwroot%s", url);
if(path[strlen(path)-1] == '/') //请求path以/结尾是目录
strcat(path, "index.html");
struct stat st;
//int stat(const char *path, struct stat *buf);
if(stat(path, &st) < 0) //获取文件信息保存在stat结构体中
{
clear_header(sock);
status_code(sock, 404);
ret = 3;
goto END;
}
//获取path文件信息成功
if(S_ISDIR(st.st_mode)) //文件是否是目录
strcat(path, "/index.html");
//文件是否为可执行文件
else if(st.st_mode & S_IXUSR || st.st_mode & S_IXGRP || st.st_mode & S_IXOTH)
cgi = 1;
//处理cgi和非cgi模式
if(cgi)
ret = exec_cgi(sock, method, path, query_str);
else
{
clear_header(sock);
ret = send_file(sock, path, st.st_size);
}
END:
close(sock);
return ret;
}