-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.cpp
More file actions
178 lines (154 loc) · 4.86 KB
/
HttpRequest.cpp
File metadata and controls
178 lines (154 loc) · 4.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
#include "HttpRequest.h"
#include <algorithm>
#include <iostream>
using namespace std;
HttpRequest::HttpRequest() : checkState_(CheckState::kCheckRequestLine) {}
void HttpRequest::reset(HttpRequest& other) {
std::swap(checkState_, other.checkState_);
std::swap(method_, other.method_);
std::swap(version_, other.version_);
url_.swap(other.url_);
headers_.swap(other.headers_);
}
bool HttpRequest::parse(Buffer& buf) {
bool ok = true;
bool hasMore = true;
while (hasMore) {
if (checkState_ == CheckState::kCheckRequestLine) {
const char* crlf = buf.findCRLF();
if (crlf) {
ok = parseRequestLine_(buf.peek(), crlf);
if (ok) {
buf.retrieveUtil(crlf + 2);
checkState_ = CheckState::kCheckRequestHead;
} else {
hasMore = false;
}
} else {
hasMore = false;
}
}
else if (checkState_ == CheckState::kCheckRequestHead) {
const char* crlf = buf.findCRLF();
if (crlf) {
const char* colon = std::find(buf.peek(), crlf, ':');
if (colon != crlf) {
addHeader(buf.peek(), colon, crlf);
} else {
if (headers_.find("Content-Length") != headers_.end()) {
int len = stoi(headers_["Content-Length"]);
if (len > 0) {
checkState_ = CheckState::kCheckRequestBody;
} else {
checkState_ = CheckState::kFinished;
hasMore = false;
}
} else {
checkState_ = CheckState::kFinished;
hasMore = false;
}
}
buf.retrieveUtil(crlf + 2);
} else {
hasMore = false;
}
}
else if (checkState_ == CheckState::kCheckRequestBody) {
int len = stoi(headers_["Content-Length"]);
if (len > buf.readableBytes()) {
hasMore = false;
} else {
checkState_ = CheckState::kFinished;
hasMore = false;
}
}
}
return ok;
}
bool HttpRequest::parseRequestLine_(const char* begin, const char* end) {
bool succeed = false;
const char* start = begin;
const char* space = find(start, end, ' ');
if (space != end) {
setMethod(start, space);
start = space + 1;
space = find(start, end, ' ');
if (space != end) {
const char* question = find(start, space, '?');
if (question != space) {
} else {
setUrl(start, space);
}
}
start = space + 1;
succeed = ((end-start == 8) && equal(start, end-1, "HTTP/1."));
if (succeed) {
if (*(end-1) == '1') {
setVersion(Version::kHttp11);
} else if (*(end-1) == '0') {
setVersion(Version::kHttp10);
} else {
succeed = false;
}
}
}
return succeed;
}
void HttpRequest::addHeader(const char* begin, const char* colon, const char* end) {
// examples:
// host: www.xxx.com
string field(begin, colon);
colon++;
while (colon < end && isspace(*colon)) { // 去掉头部空白
colon++;
}
string value(colon, end);
while (!value.empty() && isspace(value[value.size()-1])) { // 去掉尾部空白
value.resize(value.size()-1);
}
headers_[field] = value;
}
string HttpRequest::getHeader(const string& field) const {
string res;
auto it = headers_.find(field);
if (it != headers_.end()) {
res = it->second;
}
return res;
}
bool HttpRequest::gotAll() const {
return checkState_ == CheckState::kFinished;
}
void HttpRequest::setMethod(const char* begin, const char* end) {
string m(begin, end);
if (m == "GET") {
method_ = Method::kGet;
} else if (m == "POST") {
method_ = Method::kPost;
} else { // 其他方法未实现
}
}
void HttpRequest::setVersion(Version v) {
version_ = v;
}
HttpRequest::Version HttpRequest::getVersion() const {
return version_;
}
string HttpRequest::getVersionString() const {
if (version_ == Version::kHttp10) {
return string("HTTP/1.0");
} else if (version_ == Version::kHttp11) {
return string("HTTP/1.1");
}
}
void HttpRequest::setUrl(const char* begin, const char* end) {
url_.assign(begin, end);
}
string HttpRequest::getUrl() const {
return url_;
}
void HttpRequest::printHeaders() const {
for (const auto& header : headers_) {
printf("%s=%s\n", header.first.c_str(), header.second.c_str());
}
}