-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathHTTPrequest.cpp
More file actions
184 lines (165 loc) · 4.58 KB
/
Copy pathHTTPrequest.cpp
File metadata and controls
184 lines (165 loc) · 4.58 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
// encode UTF-8
// @Author : Aged_cat
// @Date : 2021-05-11
#include "HTTPrequest.h"
const std::unordered_set<std::string> HTTPrequest::DEFAULT_HTML{
"/index", "/welcome", "/video", "/picture"};
void HTTPrequest::init() {
method_ = path_ = version_ = body_ = "";
state_ = REQUEST_LINE;
header_.clear();
post_.clear();
}
bool HTTPrequest::isKeepAlive() const {
if(header_.count("Connection") == 1) {
return header_.find("Connection")->second == "keep-alive" && version_ == "1.1";
}
return false;
}
bool HTTPrequest::parse(Buffer& buff) {
const char CRLF[] = "\r\n";
if(buff.readableBytes() <= 0) {
return false;
}
//std::cout<<"parse buff start:"<<std::endl;
//buff.printContent();
//std::cout<<"parse buff finish:"<<std::endl;
while(buff.readableBytes() && state_ != FINISH) {
const char* lineEnd = std::search(buff.curReadPtr(), buff.curWritePtrConst(), CRLF, CRLF + 2);
std::string line(buff.curReadPtr(), lineEnd);
switch(state_)
{
case REQUEST_LINE:
//std::cout<<"REQUEST: "<<line<<std::endl;
if(!parseRequestLine_(line)) {
return false;
}
parsePath_();
break;
case HEADERS:
parseRequestHeader_(line);
if(buff.readableBytes() <= 2) {
state_ = FINISH;
}
break;
case BODY:
parseDataBody_(line);
break;
default:
break;
}
if(lineEnd == buff.curWritePtr()) { break; }
buff.updateReadPtrUntilEnd(lineEnd + 2);
}
return true;
}
void HTTPrequest::parsePath_() {
if(path_ == "/") {
path_ = "/index.html";
}
else {
for(auto &item: DEFAULT_HTML) {
if(item == path_) {
path_ += ".html";
break;
}
}
}
}
bool HTTPrequest::parseRequestLine_(const std::string& line) {
std::regex patten("^([^ ]*) ([^ ]*) HTTP/([^ ]*)$");
std::smatch subMatch;
if(regex_match(line, subMatch, patten)) {
method_ = subMatch[1];
path_ = subMatch[2];
version_ = subMatch[3];
state_ = HEADERS;
return true;
}
return false;
}
void HTTPrequest::parseRequestHeader_(const std::string& line) {
std::regex patten("^([^:]*): ?(.*)$");
std::smatch subMatch;
if(regex_match(line, subMatch, patten)) {
header_[subMatch[1]] = subMatch[2];
}
else {
state_ = BODY;
}
}
void HTTPrequest::parseDataBody_(const std::string& line) {
body_ = line;
parsePost_();
state_ = FINISH;
}
int HTTPrequest::convertHex(char ch) {
if(ch >= 'A' && ch <= 'F') return ch -'A' + 10;
if(ch >= 'a' && ch <= 'f') return ch -'a' + 10;
return ch;
}
void HTTPrequest::parsePost_() {
if(method_ == "POST" && header_["Content-Type"] == "application/x-www-form-urlencoded") {
if(body_.size() == 0) { return; }
std::string key, value;
int num = 0;
int n = body_.size();
int i = 0, j = 0;
for(; i < n; i++) {
char ch = body_[i];
switch (ch) {
case '=':
key = body_.substr(j, i - j);
j = i + 1;
break;
case '+':
body_[i] = ' ';
break;
case '%':
num = convertHex(body_[i + 1]) * 16 + convertHex(body_[i + 2]);
body_[i + 2] = num % 10 + '0';
body_[i + 1] = num / 10 + '0';
i += 2;
break;
case '&':
value = body_.substr(j, i - j);
j = i + 1;
post_[key] = value;
break;
default:
break;
}
}
assert(j <= i);
if(post_.count(key) == 0 && j < i) {
value = body_.substr(j, i - j);
post_[key] = value;
}
}
}
std::string HTTPrequest::path() const{
return path_;
}
std::string& HTTPrequest::path(){
return path_;
}
std::string HTTPrequest::method() const {
return method_;
}
std::string HTTPrequest::version() const {
return version_;
}
std::string HTTPrequest::getPost(const std::string& key) const {
assert(key != "");
if(post_.count(key) == 1) {
return post_.find(key)->second;
}
return "";
}
std::string HTTPrequest::getPost(const char* key) const {
assert(key != nullptr);
if(post_.count(key) == 1) {
return post_.find(key)->second;
}
return "";
}