-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_conn.cpp
More file actions
450 lines (401 loc) · 13.3 KB
/
Copy pathhttp_conn.cpp
File metadata and controls
450 lines (401 loc) · 13.3 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//
// Created by qwskyz on 2022/3/19.
//
#include "http_conn.h"
// HTTP response header
const char *ok_200_title = "OK";
const char *error_400_title = "Bad Request";
const char *error_400_form = "Your request has bad syntax or is inherently impossible to satisfy.\n";
const char *error_403_title = "Forbidden";
const char *error_403_form = "You do not have permission to get file from this server.\n";
const char *error_404_title = "Not Found";
const char *error_404_form = "The requested file was not found on this server.\n";
const char *error_500_title = "Internal Error";
const char *error_500_form = "There was an unusual problem serving the requested file.\n";
// webserver root directory
const char *doc_root = "/home/qwskyz/Code/WebServer/resource";
int http_conn::m_user_count = 0;
int http_conn::m_epoll_fd = -1;
void http_conn::init(int sock_fd, const sockaddr_in &addr) {
m_sock_fd = sock_fd;
m_address = addr;
// reuse port
int reuse = 1;
setsockopt(m_sock_fd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse));
// add sock_fd to epoll
addFd(m_epoll_fd, sock_fd, true);
m_user_count++;
init();
}
void http_conn::init() {
// initial state is check the first line
m_check_state = CHECK_STATE_REQUEST_LINE;
m_checked_index = 0;
m_start_line = 0;
m_read_index = 0;
m_write_index = 0;
m_method = GET;
m_url = 0;
m_version = 0;
m_content_length = 0;
m_host = 0;
m_linger = false;
bzero(m_read_buf, READ_BUFFER_SIZE);
bzero(m_write_buf, WRITE_BUFFER_SIZE);
bzero(m_real_file, FILE_NAME_LEN);
}
bool http_conn::read() {
if (m_read_index >= READ_BUFFER_SIZE) {
return false;
}
int bytes_read = 0;
while (true) {
bytes_read = recv(m_sock_fd, m_read_buf + m_read_index, READ_BUFFER_SIZE - m_read_index, 0);
if (bytes_read == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// it means no data
break;
}
return false;
} else if (bytes_read == 0) {
// the client close the connection
return false;
}
m_read_index += bytes_read;
}
std::cout << "===== read all data in one time =====" << std::endl;
std::cout << m_read_buf << std::endl;
return true;
}
bool http_conn::write() {
std::cout << "===== write all data in one time =====" << std::endl;
int temp = 0;
int bytes_have_sent = 0;
int bytes_to_sent = m_write_index;
if (bytes_to_sent == 0) {
// finish send response message
modifyFd(m_epoll_fd, m_sock_fd, EPOLLIN);
init();
return true;
}
while (true) {
// core readv 分散读, writev可以将多块分散的内存以并写入文件描述符中,即集中写。
// core http应答,包括一个状态行,多个头部字段,1个空行和文档的内容,其中前3部分内容可能被Web服务器
// core 放到一块内存中,而文档的内容通常被读入到另外一块单独的内存中(通过read函数或mmap函数)
temp = writev(m_sock_fd, m_iv, m_iv_count);
// 如果TCP写缓冲没有空间,则等待下一轮EPOLLOUT事件,虽然在此期间,
// 服务器无法立即接收到同一客户的下一个请求,但可以保证连接的完整性。
if (temp <= -1) {
if (errno == EAGAIN) {
modifyFd(m_epoll_fd, m_sock_fd, EPOLLOUT);
return true;
}
unmap();
return false;
}
bytes_to_sent -= temp;
bytes_have_sent += temp;
if (bytes_to_sent <= bytes_have_sent) {
// send response success
unmap();
if (m_linger) {
// core if keep-alive, http_conn has to init again
init();
modifyFd(m_epoll_fd, m_sock_fd, EPOLLIN);
return true;
} else {
modifyFd(m_epoll_fd, m_sock_fd, EPOLLIN);
return false;
}
}
}
}
void http_conn::close_conn() {
if (m_sock_fd != -1) {
removeFd(m_epoll_fd, m_sock_fd);
m_sock_fd = -1;
m_user_count--;
}
}
void http_conn::process() {
// 1. parse the http request
std::cout << "====== parse the request ======" << std::endl;
HTTP_CODE read_ret = process_read();
// TODO more read_ret need to be handled
if (read_ret == NO_REQUEST) {
modifyFd(m_epoll_fd, m_sock_fd, EPOLLIN);
return;
}
// 2. create the response
std::cout << "====== create the response ======" << std::endl;
bool write_ret = process_write(read_ret);
if (!write_ret) {
close_conn();
}
modifyFd(m_epoll_fd, m_sock_fd, EPOLLOUT);
}
// main state machine, parse the request
http_conn::HTTP_CODE http_conn::process_read() {
LINE_STATUS line_state = LINE_OK;
HTTP_CODE http_code = NO_REQUEST;
char *curr_line = 0;
while ((m_check_state == CHECK_STATE_CONTENT && line_state == LINE_OK) ||
(line_state = parse_line()) == LINE_OK) {
curr_line = get_line();
m_start_line = m_checked_index;
std::cout << "get 1 http request line: " << curr_line << std::endl;
switch (m_check_state) {
case CHECK_STATE_REQUEST_LINE: {
http_code = parse_request_line(curr_line);
// TODO need to think more condition
if (http_code == BAD_REQUEST) {
return BAD_REQUEST;
}
break;
}
case CHECK_STATE_HEADER: {
http_code = parse_request_headers(curr_line);
if (http_code == BAD_REQUEST) {
return BAD_REQUEST;
} else if (http_code == GET_REQUEST) {
return do_get_request();
}
break;
}
case CHECK_STATE_CONTENT: {
http_code = parse_request_content(curr_line);
if (http_code == GET_REQUEST) {
return do_get_request();
}
line_state = LINE_OPEN;
break;
}
default: {
return INTERNAL_ERROR;
}
}
}
return NO_REQUEST;
}
bool http_conn::process_write(HTTP_CODE read_ret) {
switch (read_ret) {
case INTERNAL_ERROR: {
add_status_line(500, error_500_title);
add_headers(strlen(error_400_form));
if (!add_content(error_500_form)) {
return false;
}
break;
}
case BAD_REQUEST: {
add_status_line(400, error_400_title);
add_headers(strlen(error_400_form));
if (!add_content(error_400_form)) {
return false;
}
break;
}
case NO_RESOURCE: {
add_status_line(404, error_404_title);
add_headers(strlen(error_400_form));
if (!add_content(error_404_form)) {
return false;
}
break;
}
case FORBIDDEN_REQUEST: {
add_status_line(403, error_403_title);
add_headers(strlen(error_403_form));
if (!add_content(error_403_form)) {
return false;
}
break;
}
case FILE_REQUEST: {
add_status_line(200, ok_200_title);
add_headers(m_file_stat.st_size);
m_iv[0].iov_base = m_write_buf;
m_iv[0].iov_len = m_write_index;
m_iv[1].iov_base = m_file_address;
m_iv[1].iov_len = m_file_stat.st_size;
m_iv_count = 2;
return true;
}
default: {
return false;
}
}
m_iv[0].iov_base = m_write_buf;
m_iv[0].iov_len = m_write_index;
m_iv_count = 1;
return true;
}
// parse request line, get the http method, target url and http version
http_conn::HTTP_CODE http_conn::parse_request_line(char *request) {
// GET /index.html HTTP/1.1
m_url = strpbrk(request, " \t");
if (!m_url) {
return BAD_REQUEST;
}
*m_url++ = '\0';
char *method = request;
// TODO need to support more method
// GET\0/index.html HTTP/1.1
if (strcasecmp(method, "GET") == 0) {
m_method = GET;
} else {
return BAD_REQUEST;
}
// /index.html HTTP/1.1
m_version = strpbrk(m_url, " \t");
if (!m_version) {
return BAD_REQUEST;
}
*m_version++ = '\0';
if (strcasecmp(m_version, "HTTP/1.1") != 0) {
return BAD_REQUEST;
}
// http://192.168.0.1:8888/index.html
if (strncasecmp(m_url, "http://", 7) == 0) {
m_url += 7;
m_url = strchr(m_url, '/');
}
if (!m_url || m_url[0] != '/') {
return BAD_REQUEST;
}
// core turn the m_check_state
m_check_state = CHECK_STATE_HEADER;
return NO_REQUEST;
}
http_conn::HTTP_CODE http_conn::parse_request_headers(char *request) {
if (request[0] == '\0') {
if (m_content_length != 0) {
// TODO turn m_check_state
m_check_state = CHECK_STATE_CONTENT;
return NO_REQUEST;
}
// TODO need to support other method
return GET_REQUEST;
} else if (strncasecmp(request, "Connection:", 11) == 0) {
// Connection: keep-alive
request += 11;
request += strspn(request, " \t");
if (strcasecmp(request, "keep-alive") == 0) {
// core keep-alive flag
m_linger = true;
}
} else if (strncasecmp(request, "Content-Length:", 15) == 0) {
// Content-Length头部字段
request += 15;
request += strspn(request, " \t");
m_content_length = atol(request);
} else if (strncasecmp(request, "Host:", 5) == 0) {
// Host头部字段
request += 5;
request += strspn(request, " \t");
m_host = request;
} else {
// TODO support more http header
std::cout << "oop! unknown header " << request << std::endl;
}
return NO_REQUEST;
}
http_conn::HTTP_CODE http_conn::parse_request_content(char *request) {
if (m_read_index >= (m_content_length + m_checked_index)) {
request[m_content_length] = '\0';
return GET_REQUEST;
}
return NO_REQUEST;
}
http_conn::LINE_STATUS http_conn::parse_line() {
char temp;
for (; m_checked_index < m_read_index; m_checked_index++) {
temp = m_read_buf[m_checked_index];
if (temp == '\r') {
if ((m_checked_index + 1) == m_read_index) {
return LINE_OPEN;
} else if (m_read_buf[m_checked_index + 1] == '\n') {
// assign '\r\n' to '\0\0'
m_read_buf[m_checked_index++] = '\0';
m_read_buf[m_checked_index++] = '\0';
return LINE_OK;
}
return LINE_BAD;
} else if (temp == '\n') {
if (m_checked_index > 1 && m_read_buf[m_checked_index - 1] == '\r') {
m_read_buf[m_checked_index - 1] = '\0';
m_read_buf[m_checked_index++] = '\0';
return LINE_OK;
}
return LINE_BAD;
}
}
return LINE_OPEN;
}
http_conn::HTTP_CODE http_conn::do_get_request() {
// /home/qwskyz/webserver/resources
strcpy(m_real_file, doc_root);
int len = strlen(doc_root);
strncpy(m_real_file + len, m_url, FILE_NAME_LEN - len - 1);
if (stat(m_real_file, &m_file_stat) < 0) {
return NO_RESOURCE;
}
if (!(m_file_stat.st_mode & S_IROTH)) {
return FORBIDDEN_REQUEST;
}
if (S_ISDIR(m_file_stat.st_mode)) {
return BAD_REQUEST;
}
// open file with read-only mode
int fd = open(m_real_file, O_RDONLY);
// TODO why
m_file_address = (char *) mmap(0, m_file_stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
return FILE_REQUEST;
}
bool http_conn::add_status_line(int status, const char *title) {
// TODO need to support HTTP/2.0
return add_response("%s %d %s\r\n", "HTTP/1.1", status, title);
}
bool http_conn::add_headers(int content_length) {
add_content_length(content_length);
add_content_type();
add_linger();
add_blank_line();
}
bool http_conn::add_content_length(int content_length) {
return add_response("Content-Length: %d\r\n", content_length);
}
bool http_conn::add_linger() {
return add_response("Connection: %s\r\n", m_linger ? "keep-alive" : "close");
}
bool http_conn::add_blank_line() {
return add_response("%s", "\r\n");
}
bool http_conn::add_content_type() {
// TODO need to support more content_type
return add_response("Content-Type:%s\r\n", "text/html");
}
bool http_conn::add_content(const char *content) {
return add_response("%s", content);
}
bool http_conn::add_response(const char *format, ...) {
if (m_write_index >= WRITE_BUFFER_SIZE) {
return false;
}
va_list arg_list;
va_start(arg_list, format);
int len = vsnprintf(m_write_buf + m_write_index, WRITE_BUFFER_SIZE - 1 - m_write_index, format, arg_list);
if (len > WRITE_BUFFER_SIZE - 1 - m_write_index) {
return false;
}
m_write_index += len;
va_end(arg_list);
return true;
}
void http_conn::unmap() {
if (m_file_address) {
munmap(m_file_address, m_file_stat.st_size);
m_file_address = 0;
}
}