-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestData.cpp
603 lines (573 loc) · 16.4 KB
/
requestData.cpp
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#include "requestData.h"
#include "util.h"
#include "epoll.h"
#include <sys/epoll.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <queue>
#include <cstdlib>
#include <iostream>
using namespace std;
pthread_once_t MimeType::once_control = PTHREAD_ONCE_INIT;
std::unordered_map<std::string, std::string> MimeType::mime;
void MimeType::init()
{
mime[".html"] = "text/html";
mime[".avi"] = "video/x-msvideo";
mime[".bmp"] = "image/bmp";
mime[".c"] = "text/plain";
mime[".doc"] = "application/msword";
mime[".gif"] = "image/gif";
mime[".gz"] = "application/x-gzip";
mime[".htm"] = "text/html";
mime[".ico"] = "application/x-ico";
mime[".jpg"] = "image/jpeg";
mime[".png"] = "image/png";
mime[".txt"] = "text/plain";
mime[".mp3"] = "audio/mp3";
mime["default"] = "text/html";
}
std::string MimeType::getMime(const std::string &suffix)
{
pthread_once(&once_control, MimeType::init);
/*
int pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
本函数使用初值为PTHREAD_ONCE_INIT的once_control变量保证init_routine()函数在本进程执行序列中仅执行一次
*/
if (mime.find(suffix) == mime.end())
return mime["default"];
else
return mime[suffix];
}
RequestData::RequestData():
now_read_pos(0),
state(STATE_PARSE_URI),
h_state(h_start),
keep_alive(false)
{
//cout << "RequestData()" << endl;
}
RequestData::RequestData(int _epollfd, int _fd, std::string _path):
fd(_fd),
epollfd(_epollfd),
now_read_pos(0),
state(STATE_PARSE_URI),
h_state(h_start),
keep_alive(false),
path(_path)
{
//cout << "RequestData()" << endl;
}
RequestData::~RequestData()
{
//cout << "~RequestData()" << endl;
close(fd);
}
void RequestData::linkTimer(shared_ptr<TimerNode> mtimer)
{
if (!timer.lock())
{
timer = mtimer;
}
//cout<<"--linkTimer use_count:-- "<<timer.use_count()<<endl;
}
void RequestData::getCurrentTime(char *Time)
{
time_t now ;
struct tm *tm_now ;
time(&now);
tm_now = localtime(&now) ;
sprintf(Time," %d-%d-%d %d:%d:%d", tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday,
tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec) ;
}
int RequestData::getFd()
{
return fd;
}
void RequestData::setFd(int _fd)
{
fd = _fd;
}
void RequestData::reset()
{
content.clear();
file_name.clear();
path.clear();
now_read_pos = 0;
state = STATE_PARSE_URI;
h_state = h_start;
headers.clear();
keep_alive = false;
if (timer.lock())
{
shared_ptr<TimerNode> my_timer(timer.lock());
my_timer->clearReq();
timer.reset();
}
}
void RequestData::seperateTimer()
{
if (timer.lock())
{
shared_ptr<TimerNode> my_timer(timer.lock());
my_timer->clearReq();
timer.reset();
}
}
void RequestData::handleRequest()
{
char buff[MAX_BUFF];
bool isError = false;
while (true)
{
int read_num = readn(fd, buff, MAX_BUFF);
if (read_num < 0)
{
perror("1");
isError = true;
break;
}
else if (read_num == 0)
{
// 有请求出现但是读不到数据,可能是Request Aborted,或者来自网络的数据没有达到等原因
perror("read_num == 0");
isError = true;
break;
}
string now_read(buff, buff + read_num);
content += now_read;
cout<<content;
if (state == STATE_PARSE_URI)
{
int flag = this->parse_URI();
if (flag == PARSE_URI_AGAIN)
{
break;
}
else if (flag == PARSE_URI_ERROR)
{
perror("2");
isError = true;
break;
}
}
if (state == STATE_PARSE_HEADERS)
{
int flag = this->parse_Headers();
if (flag == PARSE_HEADER_AGAIN)
{
break;
}
else if (flag == PARSE_HEADER_ERROR)
{
perror("3");
isError = true;
break;
}
if(method == METHOD_POST)
{
state = STATE_RECV_BODY;
}
else
{
state = STATE_ANALYSIS;
}
}
if (state == STATE_RECV_BODY)
{
size_t content_length;
if (headers.find("Content-length") != headers.end())
{
content_length = stoi(headers["Content-length"]);
}
else
{
isError = true;
break;
}
if (content.size() < content_length)
continue;
state = STATE_ANALYSIS;
}
if (state == STATE_ANALYSIS)
{
int flag = this->analysisRequest();
if (flag < 0)
{
isError = true;
break;
}
else if (flag == ANALYSIS_SUCCESS)
{
state = STATE_FINISH;
break;
}
else
{
isError = true;
break;
}
}
}
if (isError)
{
//delete this;
return;
}
// 加入epoll继续
if (state == STATE_FINISH)
{
if (keep_alive)
{
this->reset();
}
else
{
//delete this;
return;
}
}
// 一定要先加时间信息,否则可能会出现刚加进去,下个in触发来了,然后分离失败后,又加入队列,最后超时被删,然后正在线程中进行的任务出错,double free错误。
// 新增时间信息
Epoll::add_timer(shared_from_this(), 500);
__uint32_t _epo_event = EPOLLIN | EPOLLET | EPOLLONESHOT;
int ret = Epoll::epoll_mod(fd, shared_from_this(), _epo_event);
//cout << "shared_from_this().use_count() ==" << shared_from_this().use_count() << endl;
if (ret < 0)
{
// 返回错误处理
//delete this;
return;
}
}
int RequestData::parse_URI()
{
string &str = content;
// 读到完整的请求行再开始解析请求
size_t pos = str.find('\r', now_read_pos);
if (pos < 0)
{
return PARSE_URI_AGAIN;
}
// 去掉请求行所占的空间,节省空间
string request_line = str.substr(0, pos);
if (str.size() > pos + 1)
str = str.substr(pos + 1);
else
str.clear();
// Method
pos = request_line.find("GET");
if (pos < 0)
{
pos = request_line.find("POST");
if (pos < 0)
{
return PARSE_URI_ERROR;
}
else
{
method = METHOD_POST;
}
}
else
{
method = METHOD_GET;
}
//printf("method = %d\n", method);
// filename
pos = request_line.find("/", pos);
if (pos < 0)
{
return PARSE_URI_ERROR;
}
else
{
int _pos = request_line.find(' ', pos);
if (_pos < 0)
return PARSE_URI_ERROR;
else
{
if (_pos - pos > 1)
{
file_name = request_line.substr(pos + 1, _pos - pos - 1);
int __pos = file_name.find('?');
if (__pos >= 0)
{
file_name = file_name.substr(0, __pos);
}
}
else
file_name = "index.html";
}
pos = _pos;
}
// HTTP 版本号
pos = request_line.find("/", pos);
if (pos < 0)
{
return PARSE_URI_ERROR;
}
else
{
if (request_line.size() - pos <= 3)
{
return PARSE_URI_ERROR;
}
else
{
string ver = request_line.substr(pos + 1, 3);
if (ver == "1.0")
HTTPversion = HTTP_10;
else if (ver == "1.1")
HTTPversion = HTTP_11;
else
return PARSE_URI_ERROR;
}
}
state = STATE_PARSE_HEADERS;
return PARSE_URI_SUCCESS;
}
int RequestData::parse_Headers()
{
string &str = content;
/*
int pos=str.find("Connection",0);
if(pos<0)
{
return 0;
}
int _pos=str.find('\r',pos+10);
if(_pos<0)
{
return 0;
}
string Connection_line=str.substr(pos,_pos-pos);
//cout<<"Connection_line: "<<Connection_line<<endl;
pos=Connection_line.find(' ',0);
if(pos<0)
{
return 0;
}
string Connection_Type=Connection_line.substr(pos+1);
//cout<<"Connection_Type:"<<Connection_Type<<endl;
if(Connection_Type=="keep-alive")
{
ConnectionType=1;
}
*/
int key_start = -1, key_end = -1, value_start = -1, value_end = -1;
int now_read_line_begin = 0;
bool notFinish = true;
for (size_t i = 0; i != str.size() && notFinish; ++i)
{
switch(h_state)
{
case h_start:
{
if (str[i] == '\n' || str[i] == '\r')
break;
h_state = h_key;
key_start = i;
now_read_line_begin = i;
break;
}
case h_key:
{
if (str[i] == ':')
{
key_end = i;
if (key_end - key_start <= 0)
return PARSE_HEADER_ERROR;
h_state = h_colon;
}
else if (str[i] == '\n' || str[i] == '\r')
return PARSE_HEADER_ERROR;
break;
}
case h_colon:
{
if (str[i] == ' ')
{
h_state = h_spaces_after_colon;
}
else
return PARSE_HEADER_ERROR;
break;
}
case h_spaces_after_colon:
{
h_state = h_value;
value_start = i;
break;
}
case h_value:
{
if (str[i] == '\r')
{
h_state = h_CR;
value_end = i;
if (value_end - value_start <= 0)
return PARSE_HEADER_ERROR;
}
else if (i - value_start > 255)
return PARSE_HEADER_ERROR;
break;
}
case h_CR:
{
if (str[i] == '\n')
{
h_state = h_LF;
string key(str.begin() + key_start, str.begin() + key_end);
string value(str.begin() + value_start, str.begin() + value_end);
headers[key] = value;
now_read_line_begin = i;
}
else
return PARSE_HEADER_ERROR;
break;
}
case h_LF:
{
if (str[i] == '\r')
{
h_state = h_end_CR;
}
else
{
key_start = i;
h_state = h_key;
}
break;
}
case h_end_CR:
{
if (str[i] == '\n')
{
h_state = h_end_LF;
}
else
return PARSE_HEADER_ERROR;
break;
}
case h_end_LF:
{
notFinish = false;
key_start = i;
now_read_line_begin = i;
break;
}
}
}
if (h_state == h_end_LF)
{
str = str.substr(now_read_line_begin);
return PARSE_HEADER_SUCCESS;
}
str = str.substr(now_read_line_begin);
return PARSE_HEADER_AGAIN;
}
int RequestData::analysisRequest()
{
if (method == METHOD_POST)
{
//get content
char header[MAX_BUFF];
sprintf(header, "HTTP/1.1 %d %s\r\n", 200, "OK");
if(headers.find("Connection") != headers.end() && headers["Connection"] == "keep-alive")
{
keep_alive = true;
sprintf(header, "%sConnection: keep-alive\r\n", header);
sprintf(header, "%sKeep-Alive: timeout=%d\r\n", header, EPOLL_WAIT_TIME);
}
//cout << "content=" << content << endl;
// test char*
string send_content = "I have receiced this.";
sprintf(header, "%sContent-length: %zu\r\n", header, send_content.size());
sprintf(header, "%s\r\n", header);
size_t send_len = (size_t)writen(fd, header, strlen(header));
if(send_len != strlen(header))
{
perror("Send header failed");
return ANALYSIS_ERROR;
}
send_len = (size_t)writen(fd,const_cast<char *>(send_content.c_str()), send_content.size());
if(send_len != send_content.size())
{
perror("Send content failed");
return ANALYSIS_ERROR;
}
return ANALYSIS_SUCCESS;
}
else if (method == METHOD_GET)
{
char header[MAX_BUFF],time[MAX_BUFF];
getCurrentTime(time);
sprintf(header, "HTTP/1.1 %d %s\r\n", 200, "OK");
if(headers.find("Connection") != headers.end() && headers["Connection"] == "keep-alive")
{
keep_alive = true;
sprintf(header, "%sResponse time: %s\r\n", header,time);
sprintf(header, "%sConnection: keep-alive\r\n", header);
sprintf(header, "%sKeep-Alive: timeout=%d\r\n", header, EPOLL_WAIT_TIME);
}
int dot_pos = file_name.find('.');
string filetype;
if (dot_pos < 0)
filetype = MimeType::getMime("default");
else
filetype = MimeType::getMime(file_name.substr(dot_pos));
struct stat sbuf;
if (stat(file_name.c_str(), &sbuf) < 0)
{
handleError(fd, 404, "Not Found!");
return ANALYSIS_ERROR;
}
sprintf(header, "%sContent-type: %s\r\n", header, filetype.c_str());
// 通过Content-length返回文件大小
sprintf(header, "%sContent-length: %ld\r\n", header, sbuf.st_size);
sprintf(header, "%s\r\n", header);
size_t send_len = (size_t)writen(fd, header, strlen(header));
if(send_len != strlen(header))
{
perror("Send header failed");
return ANALYSIS_ERROR;
}
cout<<header;
int src_fd = open(file_name.c_str(), O_RDONLY, 0);
char *src_addr = static_cast<char*>(mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, src_fd, 0));
close(src_fd);
// 发送文件并校验完整性
send_len = writen(fd, src_addr, sbuf.st_size);
if(send_len != static_cast<size_t>(sbuf.st_size))
{
perror("Send file failed");
return ANALYSIS_ERROR;
}
munmap(src_addr, sbuf.st_size);
return ANALYSIS_SUCCESS;
}
else
return ANALYSIS_ERROR;
}
void RequestData::handleError(int fd, int err_num, string short_msg)
{
short_msg = " " + short_msg;
char send_buff[MAX_BUFF];
string body_buff, header_buff;
body_buff += "<html><title>WebServer Error</title>";
body_buff += "<body bgcolor=\"ffffff\">";
body_buff += to_string(err_num) + short_msg;
body_buff += "<hr><em> Realself's Web Server</em>\n</body></html>";
header_buff += "HTTP/1.1 " + to_string(err_num) + short_msg + "\r\n";
header_buff += "Content-type: text/html\r\n";
header_buff += "Connection: close\r\n";
header_buff += "Content-length: " + to_string(body_buff.size()) + "\r\n";
header_buff += "\r\n";
sprintf(send_buff, "%s", header_buff.c_str());
writen(fd, send_buff, strlen(send_buff));
sprintf(send_buff, "%s", body_buff.c_str());
writen(fd, send_buff, strlen(send_buff));
}