-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHttpData.cpp
More file actions
530 lines (486 loc) · 13.7 KB
/
HttpData.cpp
File metadata and controls
530 lines (486 loc) · 13.7 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
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
#include "HttpData.h"
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <iostream>
#include "Event.h"
#include "EventLoop.h"
#include "utils/SocketUtils.h"
#include "utils/Logging.h"
const __uint32_t DEFAULT_EVENT = EPOLLIN | EPOLLET | EPOLLONESHOT;
const long DEFAULT_EXPIRED_TIME = 2000000; // microsecond
const long DEFAULT_KEEP_ALIVE_TIME = 5 * 60 * 1000000; // microsecond, five minutes
HttpData::HttpData(EventLoop *loop, int connfd)
: loop_(loop),
// Event::event_ is connfd,
// also HttpData::fd_ is connfd.
event_(new Event(loop, connfd)),
fd_(connfd),
error_(false),
connectionState_(H_CONNECTED),
method_(METHOD_GET),
HTTPVersion_(HTTP_11),
nowReadPos_(0),
state_(STATE_PARSE_URI),
hState_(H_START),
keepAlive_(false) {
event_->setReadHandler(bind(&HttpData::handleRead, this));
event_->setWriteHandler(bind(&HttpData::handleWrite, this));
event_->setConnHandler(bind(&HttpData::handleConn, this));
} // end HttpData::HttpData
void HttpData::reset() {
fileName_.clear();
path_.clear();
nowReadPos_ = 0;
state_ = STATE_PARSE_URI;
hState_ = H_START;
headers_.clear();
if (timer_.lock()) {
std::shared_ptr<TimerNode> my_timer(timer_.lock());
my_timer->clearReq();
timer_.reset();
}
}
void HttpData::seperateTimer() {
// lock(): convert weak_ptr to shared_ptr
// because shared_ptr overload operator bool()
if (timer_.lock()) {
std::shared_ptr<TimerNode> my_timer(timer_.lock());
my_timer->clearReq();
timer_.reset();
}
}
void HttpData::handleRead() {
// get connection fd
//
// if connection fd has read event,
// something was send from TCP/IP,
// call this function
__uint32_t &events_ = event_->getEvents();
do {
bool zero = false;
int read_num = readn(fd_, inBuffer_, zero);
LOG << "Request: " << inBuffer_;
if (connectionState_ == H_DISCONNECTING) {
inBuffer_.clear();
break;
}
if (read_num < 0) {
perror("1");
error_ = true;
handleError(fd_, 400, "Bad Request");
break;
}
else if (zero) {
/*
* if there is a request, but can't read data,
* may be request aborted,
* or data in trans.
* most likely, opposite end close the connection.
* i see all case as oppositen end close the connection.
*/
connectionState_ = H_DISCONNECTING;
if (read_num == 0) {
break;
}
}
// read data from socket
// depend on current state_ to do one of follow:
// 1.parse uri
// 2.parse request header
// 3.analysis or recv body
if (state_ == STATE_PARSE_URI) {
URIState flag = this->parseURI();
if (flag == PARSE_URI_AGAIN) break;
else if (flag == PARSE_URI_ERROR) {
perror("2");
LOG << "FD = " << fd_ << "," << inBuffer_ << "******";
inBuffer_.clear();
error_ = true;
handleError(fd_, 400, "Bad Request");
break;
} else
// trans state_ to next state.
state_ = STATE_PARSE_HEADERS;
}
// next state
if (state_ == STATE_PARSE_HEADERS) {
HeaderState flag = this->parseHeaders();
if (flag == PARSE_HEADER_AGAIN)
break;
else if (flag == PARSE_HEADER_ERROR) {
perror("3");
error_ = true;
handleError(fd_, 400, "Bad Request");
break;
}
if (method_ == METHOD_POST) {
// trans state_ to next state
state_ = STATE_RECV_BODY;
} else {
// trans state_ to next state.
state_ = STATE_ANALYSIS;
}
}
// next state
if (state_ == STATE_RECV_BODY) {
int content_length = -1;
if (headers_.find("Content-length") != headers_.end()) {
content_length = stoi(headers_["Content-length"]);
} else {
error_ = true;
handleError(fd_, 400, "Bad Request: Lack of argument (Content-length)");
break;
}
if (static_cast<int>(inBuffer_.size()) < content_length) break;
// trans state_ to next state.
state_ = STATE_ANALYSIS;
}
// next state
if (state_ == STATE_ANALYSIS) {
AnalysisState flag = this->analysisRequest();
if (flag == ANALYSIS_SUCCESS) {
// finish
state_ = STATE_FINISH;
break;
} else {
error_ = true;
break;
}
}
} while (false);
if (!error_) {
if (outBuffer_.size() > 0) {
handleWrite();
}
if (!error_ && state_ == STATE_FINISH) {
this->reset();
if (inBuffer_.size() > 0) {
if (connectionState_ != H_DISCONNECTING) handleRead();
}
} else if (!error_ && connectionState_ != H_DISCONNECTED)
events_ |= EPOLLIN;
}
}
void HttpData::handleWrite() {
// call by handleRead.
// after parse request, respond this request
if (!error_ && connectionState_ != H_DISCONNECTED) {
__uint32_t &events_ = event_->getEvents();
if (writen(fd_, outBuffer_) < 0) {
perror("writen");
events_ = 0;
error_ = true;
}
if (outBuffer_.size() > 0){
events_ |= EPOLLOUT;
}
}
}
void HttpData::handleConn() {
// after hanlde read (see Event::handleEvents)
// do some follow-up work
seperateTimer();
__uint32_t &events_ = event_->getEvents();
if (!error_ && connectionState_ == H_CONNECTED) {
if (events_ != 0) {
// some error! reset
int timeout = DEFAULT_EXPIRED_TIME;
if (keepAlive_) timeout = DEFAULT_KEEP_ALIVE_TIME;
if ((events_ & EPOLLIN) && (events_ & EPOLLOUT)) {
events_ = __uint32_t(0);
events_ |= EPOLLOUT;
}
events_ |= EPOLLET;
loop_->updateEpoller(event_, timeout);
} else if (keepAlive_) {
events_ |= (EPOLLIN | EPOLLET);
long timeout = DEFAULT_KEEP_ALIVE_TIME;
loop_->updateEpoller(event_, timeout);
} else {
// non keep-alive
// shutdown: half close
loop_->shutdown(event_);
loop_->runInLoop(bind(&HttpData::handleClose, shared_from_this()));
}
} else if (!error_ && connectionState_ == H_DISCONNECTING &&
(events_ & EPOLLOUT)) {
events_ = (EPOLLOUT | EPOLLET);
} else {
loop_->runInLoop(bind(&HttpData::handleClose, shared_from_this()));
}
}
URIState HttpData::parseURI() {
// parse URI from inBuffer_
// HTTP header first line
// :method URL version
std::string &str = inBuffer_;
std::string cop = str;
// from nowReadPos_ index (default 0), find '\r'
size_t pos = str.find('\r', nowReadPos_);
if (pos < 0) {
return PARSE_URI_AGAIN;
}
// get request first line, URI
std::string request_line = str.substr(0, pos);
if (str.size() > pos + 1)
str = str.substr(pos + 1);
else
str.clear();
// parse method
int posGet = request_line.find("GET");
int posPost = request_line.find("POST");
int posHead = request_line.find("HEAD");
if (posGet >= 0) {
pos = posGet;
method_ = METHOD_GET;
} else if (posPost >= 0) {
pos = posPost;
method_ = METHOD_POST;
} else if (posHead >= 0) {
pos = posHead;
method_ = METHOD_HEAD;
} else {
return PARSE_URI_ERROR;
}
// from method, find "/"
// parse URL
// and get request filename
pos = request_line.find("/", pos);
if (pos < 0) {
fileName_ = "index.html";
HTTPVersion_ = HTTP_11;
return PARSE_URI_SUCCESS;
} else {
size_t _pos = request_line.find(' ', pos);
if (_pos < 0)
return PARSE_URI_ERROR;
else {
if (_pos - pos > 1) {
fileName_ = request_line.substr(pos + 1, _pos - pos - 1);
size_t __pos = fileName_.find('?');
if (__pos >= 0) {
fileName_ = fileName_.substr(0, __pos);
}
}
else
fileName_ = "index.html";
}
pos = _pos;
}
// from URL find HTTP version(e.g.,HTTP/1.1).
pos = request_line.find("/", pos);
if (pos < 0) return PARSE_URI_ERROR;
else {
if (request_line.size() - pos <= 3)
return PARSE_URI_ERROR;
else {
std::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;
}
}
return PARSE_URI_SUCCESS;
}
HeaderState HttpData::parseHeaders() {
/*
* parse HTTP Header
*
* for the sake of coding,
* we do nothing(set some value or something)
* after parse field successfully.
* but still simulate this process.
*
*/
std::string &str = inBuffer_;
int key_start = -1, key_end = -1, value_start = -1, value_end = -1;
int now_read_line_begin = 0;
bool notFinish = true;
size_t i = 0;
// parse diff field
for (; i < str.size() && notFinish; ++i) {
switch (hState_) {
case H_START: {
if (str[i] == '\n' || str[i] == '\r') break;
// parse next field
hState_ = 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;
hState_ = H_COLON;
} else if (str[i] == '\n' || str[i] == '\r')
return PARSE_HEADER_ERROR;
break;
}
case H_COLON: {
if (str[i] == ' ') {
hState_ = H_SPACES_AFTER_COLON;
} else
return PARSE_HEADER_ERROR;
break;
}
case H_SPACES_AFTER_COLON: {
hState_ = H_VALUE;
value_start = i;
break;
}
case H_VALUE: {
if (str[i] == '\r') {
hState_ = H_CR;
value_end = i;
if (value_end - value_start <= 0) return PARSE_HEADER_ERROR;
} else if (i - value_start > 512)
return PARSE_HEADER_ERROR;
break;
}
case H_CR: {
if (str[i] == '\n') {
hState_ = H_LF;
std::string key(str.begin() + key_start, str.begin() + key_end);
std::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') {
hState_ = H_END_CR;
} else {
key_start = i;
hState_ = H_KEY;
}
break;
}
case H_END_CR: {
if (str[i] == '\n') {
hState_ = 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 (hState_ == H_END_LF) {
str = str.substr(i);
return PARSE_HEADER_SUCCESS;
}
str = str.substr(now_read_line_begin);
return PARSE_HEADER_AGAIN;
}
AnalysisState HttpData::analysisRequest() {
if (method_ == METHOD_POST) {
// this server does not allow client to POST data,
// do nothing on this request
} else if (method_ == METHOD_GET || method_ == METHOD_HEAD){
std::string response_header;
response_header += "HTTP/1.1 200 OK\r\n";
if (headers_.find("Connection") != headers_.end() &&
(headers_["Connection"] == "Keep-Alive" ||
headers_["Connection"] == "keep-alive")) {
keepAlive_ = true;
response_header += std::string("Connection: Keep-Alive\r\n") + "Keep-Alive: timeout=" + std::to_string(DEFAULT_KEEP_ALIVE_TIME) + "\r\n";
}
// parse URL
// request file type
int dot_pos = fileName_.find('.');
std::string filetype;
if (dot_pos < 0)
filetype = MimeType::getInstance().getMime("default");
else
// get request file type from '.' (dot_pos)
filetype = MimeType::getInstance().getMime(fileName_.substr(dot_pos));
// echo test
if (fileName_ == "hello") {
outBuffer_ =
"HTTP/1.1 200 OK\r\nContent-type: text/plain\r\n\r\nHello World";
return ANALYSIS_SUCCESS;
}
struct stat sbuf;
//get fileName's state in filesys
//return 0 if successfully
//
//
if (stat((MimeType::getInstance().getPath() + fileName_).c_str(), &sbuf) < 0) {
// if error(many reason), return 404
response_header.clear();
handleError(fd_, 404, "Not Found!");
return ANALYSIS_ERROR;
}
response_header += "Content-Type: " + filetype + "\r\n";
response_header += "Content-Length: " + std::to_string(sbuf.st_size) + "\r\n";
response_header += "Server: LinHuang's Web Server\r\n";
// header end
response_header += "\r\n";
outBuffer_ += response_header;
if (method_ == METHOD_HEAD) return ANALYSIS_SUCCESS;
// if method_ == METHOD_GET, continue
//
// write the request data in outBuffer
int src_fd = open((MimeType::getInstance().getPath() + fileName_).c_str(), O_RDONLY, 0);
if (src_fd < 0) {
// if file not exist, return 404
outBuffer_.clear();
handleError(fd_, 404, "Not Found!");
return ANALYSIS_ERROR;
}
void *mmapRet = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, src_fd, 0);
close(src_fd);
if (mmapRet == (void *)-1) {
munmap(mmapRet, sbuf.st_size);
outBuffer_.clear();
handleError(fd_, 404, "Not Found!");
return ANALYSIS_ERROR;
}
char *src_addr = static_cast<char *>(mmapRet);
outBuffer_ += std::string(src_addr, src_addr + sbuf.st_size);
munmap(mmapRet, sbuf.st_size);
return ANALYSIS_SUCCESS;
}
return ANALYSIS_ERROR;
}
void HttpData::handleError(int fd, int err_num, std::string short_msg) {
short_msg = " " + short_msg;
char send_buff[4096];
std::string body_buff, header_buff;
body_buff += "<html><title>emmm ~ something wrong</title>";
body_buff += "<body bgcolor=\"ffffff\">";
body_buff += std::to_string(err_num) + short_msg;
body_buff += "<hr><em> LinHuang's Web Server</em>\n</body></html>";
header_buff += "HTTP/1.1 " + std::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: " + std::to_string(body_buff.size()) + "\r\n";
header_buff += "Server: LinHuang's Web Server\r\n";
header_buff += "\r\n";
// it is not possible for "error" to write overflow.
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));
}
void HttpData::handleClose() {
connectionState_ = H_DISCONNECTED;
std::shared_ptr<HttpData> guard(shared_from_this());
loop_->removeFromEpoller(event_);
}
void HttpData::newEvent() {
event_->setEvents(DEFAULT_EVENT);
loop_->addToEpoller(event_, DEFAULT_EXPIRED_TIME);
}