-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.h
427 lines (354 loc) · 14.1 KB
/
Request.h
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
#pragma once
#include <map>
#include <string>
#include <vector>
#include "MulPaField.h"
#include "HttpParserState.h"
#include <sstream>
#include <functional>
#include <FlowUtils/FlowCParser.h>
#include <FlowUtils/FlowParser.h>
#include <FlowUtils/FlowString.h>
#include <FlowUtils/FlowVParser.h>
#include <FlowUtils/FlowLog.h>
#include <string_view>
class Request : public std::unordered_map<std::string, std::string> {
public:
std::function<std::string(MulPaField *)> FileNamingFunction = nullptr;
std::string HttpVersion() {
return Header("HTTP_Version");
}
void HttpVersion(const std::string &version) {
this->operator[]("HTTP_Version") = version;
}
void Method(const std::string &version) {
this->operator[]("Method") = version;
}
std::string Method() {
return Header("Method");
}
std::string Protocol() {
return Header("Protocol");
}
void Protocol(const std::string &version) {
this->operator[]("Protocol") = version;
}
std::string Path() {
return Header("Path");
}
void Path(const std::string &path) {
this->operator[]("Path") = path;
}
void AddParameter(const std::string &key, const std::string &value) {
Parameter[key] = value;
}
std::string GetBody() {
return GetParameter("$Body");
}
std::string GetParameter(const std::string &key) {
auto itr = Parameter.find(key);
if (itr == Parameter.end())
return "";
return Parameter.find(key)->second;
}
std::string ContentLength() {
return Header("Content-Length");
}
void ContentLength(const std::string &contentLength) {
this->operator[]("Content-Length") = contentLength;
}
std::string ContentType() {
return Header("Content-Type");
}
void ContentType(const std::string &contentType) {
this->operator[]("Content-Type") = contentType;
}
std::string Header(const std::string &key) {
auto keyItr = this->find(key);
if (keyItr == this->end()) {
std::string key_c = key;
FlowString::toLower(key_c);
keyItr = this->find(key_c);
if (keyItr == this->end())
return "";
}
return keyItr->second;
}
bool ExpectContinue() {
return Expect() == "100-continue";
}
std::string LastModified() {
return Header("Last-Modified");
}
std::string IfModifiedSince() {
return Header("If-Modified-Since");
}
std::string Expect() {
return Header("Expect");
}
void Expect(const std::string &value) {
this->operator[]("Content-Expect") = value;
}
void InsertField(MulPaField &field) {
}
// Request &operator<<(const std::string &data) {
// ParserState = parseRequest(data, *this, ParserState);
// return *this;
// }
Request &operator<<(std::vector<unsigned char> &data) {
ParserState = parseRequest(data, *this, ParserState);
return *this;
}
std::string ToString() {
std::stringstream ss;
ss << this->Method() << " " << this->Path() << " " << this->Protocol() << "/" << this->HttpVersion();
for (auto item: *this) {
ss << std::endl << item.first << ": " << item.second;
}
return ss.str();
}
Request() {
ParserState = HttpParserState::START;
}
void CloseFiles() {
for (auto &field: fields)
field.CloseFile();
}
void CloseConnection() {
this->operator[]("Connection") = "";
}
std::map<std::string, std::string> Parameter;
std::string Boundary;
std::vector<MulPaField> fields;
HttpParserState ParserState;
bool ParseToDisk = false;
std::string WriteFolder;
private:
std::vector<unsigned char> prefBuffer;
void parseFieldHeaderPart(const std::string &req, size_t &pos, MulPaField &field) {
while (pos < req.size() && !FlowParser::isDoubleNewLine(req, pos)) {
if (pos >= req.size()) break;
const auto key = FlowParser::goTo(req, ":", pos);
FlowParser::gotoNextNonWhite(req, ++pos);
const auto value = FlowParser::goToNewLine(req, pos);
field[key] = value;
}
}
HttpParserState parseHTML2(const std::string &req, size_t &pos, Request &request) {
const std::string HTML2_PRI_CHECK = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
if (req.substr(0, HTML2_PRI_CHECK.length()) != HTML2_PRI_CHECK) {
return HttpParserState::BAD_REQUEST;
}
request.AddParameter("data", req.substr(HTML2_PRI_CHECK.length()));
return HttpParserState::END;
}
void parseHeaderPart(const std::string &req, size_t &pos, Request &request) {
while (pos < req.size() && !FlowParser::isDoubleNewLine(req, pos)) {
if (pos >= req.size()) break;
const auto key = FlowParser::goTo(req, ":", pos);
FlowParser::gotoNextNonWhite(req, ++pos);
const auto value = FlowParser::goToNewLine(req, pos);
request[key] = value;
}
}
HttpParserState
parseRequest(std::vector<unsigned char> &data, Request &request, HttpParserState state) {
using namespace FlowVParser;
if (!prefBuffer.empty()) {
data.insert(data.begin(), prefBuffer.begin(), prefBuffer.end());
prefBuffer.clear();
}
std::vector<unsigned char>::iterator pos = data.begin();
if (state == HttpParserState::START) {
if (pos == data.end() || !isOneOf(pos,
{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTION", "HEAD", "CONNECT",
"TRACE", "COPY", "LOCK", "MKCOL", "MOVE", "PROPFIND", "PROPPATCH",
"UNLOCK"})) {
state = HttpParserState::BAD_REQUEST;
return state;
}
auto method = gotoNextNonAlpha(data, pos);
FlowString::toUpper(method);
request.Method(method);
if (pos == data.end() || method.empty()) {
state = HttpParserState::END;
return state;
}
const auto test = gotoNextNonWhite(data, pos);
const auto path = goToOne(data, " ?", pos);
request.Path(path);
while (*pos == '?' || *pos == '&') {
const auto key = goToOne(data, " =", ++pos);
if (key.empty()) {
++pos;
continue;
}
const auto value = goToOne(data, " &", ++pos);
if (!key.empty() && !value.empty()) {
request.AddParameter(key, value);
}
}
gotoNextNonWhite(data, pos);
const auto protocol = goTo(data, "/", pos);
request.Protocol(protocol);
const auto httpVersion = goToNewLine(data, ++pos);
request.HttpVersion(httpVersion);
state = HttpParserState::HEADER;
}
if (state == HttpParserState::HEADER) {
state = parseHeaderPart(data, pos, request);
if (state == HttpParserState::HEADER_END) {
if (!request.ContentLength().empty() && request.ContentLength() != "0")
state = HttpParserState::CONTENT_START;
else
state = HttpParserState::END;
}
}
if (state == HttpParserState::CONTENT_START
&& request.ContentType() == "application/x-www-form-urlencoded") {
gotoNextNonWhite(data, pos);
while (pos != data.end()) {
const auto key = goTo(data, "=", pos);
const auto value = goToOne(data, " &", ++pos);
request.AddParameter(key, value);
if (pos != data.end())
++pos;
}
state = HttpParserState::END;
}
while (pos != data.end() && (state == HttpParserState::CONTENT_DATA
|| state == HttpParserState::CONTENT_START
|| state == HttpParserState::CONTENT_HEADER
|| state == HttpParserState::CONTENT_HEADERFIELD)) {
if (state == HttpParserState::CONTENT_START) {
size_t ctPos = request.ContentType().find("multipart/form-data;");
if (ctPos == std::string::npos) {
goToNextLine(data, pos);
request.AddParameter("$Body", {pos, data.end()});
if (request.GetBody().length() == std::stoull(request.ContentLength())) {
state = HttpParserState::END;
} else {
state = HttpParserState::CONTENT_START;
}
return state;
}
FlowParser::goTo(request.ContentType(), "=", ctPos);
string boundary = FlowParser::goToEnd(request.ContentType(), ++ctPos);
request.Boundary = boundary;
goToNewLine(data, pos);
goToNextLine(data, pos);
state = HttpParserState::CONTENT_HEADER;
}
if (state == HttpParserState::CONTENT_HEADER && pos != data.end()) {
goTo(data, request.Boundary, pos);
if (pos == data.end())
return state;
state = HttpParserState::CONTENT_HEADERFIELD;
}
if (state == HttpParserState::CONTENT_HEADERFIELD && pos != data.end()) {
auto savePos = pos;
goToNewLine(data, pos);
goToNextLine(data, pos);
if (!hasNextDoubleNewLine(data, pos)) {
prefBuffer.assign(savePos, data.end());
return state;
}
MulPaField &field = request.fields.emplace_back();
parseFieldHeaderPart(data, pos, field);
state = HttpParserState::CONTENT_DATA;
if (pos != data.end())
++pos;
}
if (state == HttpParserState::CONTENT_DATA && pos != data.end()) {
auto start = pos;
goTo(data, request.Boundary, pos);
MulPaField &field = request.fields.back();
if (pos == data.end()) {
pos = pos - request.Boundary.size() - 3;
goTo(data, "\n--", pos);
if (pos != data.end()) {
auto toTestPos = pos;
auto dataEnding = FlowVParser::findLastData(pos);
prefBuffer.assign(dataEnding + 1, data.end());
pos = dataEnding;
}
if (request.ParseToDisk) {
writeToDisk(request, field, start, pos);
} else {
copy(start, pos, back_inserter(field.data));
}
return state;
} else {
auto dataEnding = FlowVParser::findLastData(pos, data.begin());
if (request.ParseToDisk) {
writeToDisk(request, field, start, dataEnding);
} else {
copy(start, dataEnding, back_inserter(field.data));
}
pos += request.Boundary.size();
if (*pos == '-' && *++pos == '-' &&
((*++pos == '\r' && *++pos == '\n') || *++pos == '\n') &&
++pos == data.end())
state = HttpParserState::END;
else
state = HttpParserState::CONTENT_HEADERFIELD;
field.CloseFile();
}
}
}
return state;
}
HttpParserState
parseHeaderPart(std::vector<unsigned char> &data, std::vector<unsigned char>::iterator &pos,
Request &request) {
while (pos != data.end()) {
if (FlowVParser::isDoubleNewLine(pos)) {
return HttpParserState::HEADER_END;
}
if (pos == data.end()) break;
const auto key = FlowVParser::goTo(data, ":", pos);
FlowVParser::gotoNextNonWhite(data, ++pos);
const auto value = FlowVParser::goToNewLine(data, pos);
request[key] = value;
}
return HttpParserState::HEADER;
}
void
parseFieldHeaderPart(std::vector<unsigned char> &data, std::vector<unsigned char>::iterator &pos,
MulPaField &field) {
while (pos != data.end() && !FlowVParser::isDoubleNewLine(pos)) {
if (pos == data.end()) break;
const auto key = FlowVParser::goTo(data, ":", pos);
FlowVParser::gotoNextNonWhite(data, ++pos);
const auto value = FlowVParser::goToNewLine(data, pos);
field[key] = value;
}
}
bool
hasNextDoubleNewLine(std::vector<unsigned char> &data, std::vector<unsigned char>::iterator pos) {
const std::string wnl("\r\n\r\n");
auto toCheck = std::search(pos, data.end(), wnl.begin(), wnl.end());
if (toCheck == data.end()) {
const std::string unl("\n\n");
toCheck = std::search(pos, data.end(), unl.begin(), unl.end());
}
return toCheck != data.end();
}
void
writeToDisk(const Request &request, MulPaField &field, std::vector<unsigned char>::iterator &start,
std::vector<unsigned char>::iterator &end) {
std::ofstream *file;
if (field.FileIsOpen()) {
file = field.File();
} else {
file = field.OpenIn(request.WriteFolder, FileNamingFunction);
if (!field.data.empty()) {
std::ostream_iterator<unsigned char> ofitr(*file);
copy(field.data.begin(), field.data.end(), ofitr);
field.data.clear();
}
}
std::ostream_iterator<unsigned char> ofitr(*file);
copy(start, end, ofitr);
}
};