-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.cc
389 lines (349 loc) · 10.9 KB
/
request.cc
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
#include "request.h"
#include <memory>
#include <sstream>
#include <thread>
// These are local variables per thread
thread_local Request::Result result = Request::indeterminate;
thread_local Request local_request;
// Default constructor
Request::Request() : state(_method_start) {
}
// Reset the request to fresh state
void Request::Reset() {
raw_request_ = std::string();
method_ = std::string();
uri_ = std::string();
path_ = std::string();
version_ = std::string();
headers_ = Headers();
body_ = std::string();
state = _method_start;
}
// Parses data from the client. The return value is valid when a
// complete request has been parsed, nullptr if the data is invalid or
// indeterminate
std::unique_ptr<Request> Request::Parse(const std::string& raw_request) {
result = indeterminate;
std::string::const_iterator begin = raw_request.begin();
std::string::const_iterator end = raw_request.end();
while (begin != end) {
result = local_request.Consume(*begin++);
if (result == good) {
auto r = std::unique_ptr<Request>(new Request(local_request));
local_request.Reset();
return r;
} else if (result == bad) {
local_request.Reset();
return std::unique_ptr<Request>(nullptr);
}
}
// Must be indeterminate request
return std::unique_ptr<Request>(nullptr);
}
// Return the result of the request Parse function
Request::Result Request::GetParseResult() {
return result;
}
// Returns the value for the given header or the empty string
std::string Request::GetHeaderValue(const std::string& name) const {
for (std::size_t i = 0; i < headers_.size(); i++) {
if (headers_[i].first == name) {
return headers_[i].second;
}
}
return std::string();
}
// Gets the unparsed string that represents the request
std::string Request::raw_request() const {
return raw_request_;
}
// Gets the method for the request (indicates what is to be performed)
std::string Request::method() const {
return method_;
}
// Gets the resource identifier for the request
std::string Request::uri() const {
return uri_;
}
// Sets the uri for the request
void Request::SetUri(const std::string& uri) {
uri_ = uri;
size_t uri_pos = raw_request_.find(" ") + 1;
size_t uri_end = raw_request_.find(" ", uri_pos);
size_t uri_len = uri_end - uri_pos;
raw_request_.replace(uri_pos, uri_len, uri);
}
// Returns the path represented by the URI or the empty string if invalid
std::string Request::path() const {
return path_;
}
// Gets the HTTP version the request was made with
std::string Request::version() const {
return version_;
}
// Gets the headers of the request
using Headers = std::vector<std::pair<std::string, std::string> >;
Headers Request::headers() const {
return headers_;
}
// Gets the body of the request
std::string Request::body() const {
return body_;
}
// Helper function for the parser that checks if c is a character
static bool is_char(int c) {
return c >= 0 && c <= 127;
}
// Helper function for the parser that checks if c is a control character
static bool is_ctl(int c) {
return (c >= 0 && c <= 31) || (c == 127);
}
// Helper function for the parser that checks if c is a HTTP tspecial
static bool is_tspecial(int c) {
switch (c) {
case '(': case ')': case '<': case '>': case '@':
case ',': case ';': case ':': case '\\': case '"':
case '/': case '[': case ']': case '?': case '=':
case '{': case '}': case ' ': case '\t':
return true;
default:
return false;
}
}
// Helper function for the parser that checks if c is a digit
static bool is_digit(int c) {
return c >= '0' && c <= '9';
}
Request::Result Request::Consume(char input) {
raw_request_ += input;
switch (state) {
case _method_start:
if (!is_char(input) || is_ctl(input) || is_tspecial(input)) {
return bad;
} else {
state = _method;
method_.push_back(input);
return indeterminate;
}
case _method:
if (input == ' ') {
state = _uri;
return indeterminate;
} else if (!is_char(input) || is_ctl(input) || is_tspecial(input)) {
return bad;
} else {
method_.push_back(input);
return indeterminate;
}
case _uri:
if (input == ' ') {
state = _http_version_h;
// Now that we have the URI, infer the path from it
path_.reserve(uri_.size());
for (std::size_t i = 0; i < uri_.size(); ++i) {
if (uri_[i] == '%') {
if (i + 3 <= uri_.size()) {
int value = 0;
std::istringstream is(uri_.substr(i + 1, 2));
if (is >> std::hex >> value) {
path_ += static_cast<char>(value);
i += 2;
} else {
path_ = std::string();
return indeterminate;
}
} else {
path_ = std::string();
return indeterminate;
}
} else if (uri_[i] == '+') {
path_ += ' ';
} else {
path_ += uri_[i];
}
}
// Ensure that the path does not go backwards in the directory
if (path_.find("..") != std::string::npos) path_ = std::string();
// If path ends in slash (i.e. is a directory) then add "index.html"
else if (path_[path_.size() - 1] == '/') path_ += "index.html";
return indeterminate;
} else if (is_ctl(input)) {
return bad;
} else {
uri_.push_back(input);
return indeterminate;
}
case _http_version_h:
if (input == 'H') {
state = _http_version_t_1;
version_.push_back('H');
return indeterminate;
} else {
return bad;
}
case _http_version_t_1:
if (input == 'T') {
state = _http_version_t_2;
version_.push_back('T');
return indeterminate;
} else {
return bad;
}
case _http_version_t_2:
if (input == 'T') {
state = _http_version_p;
version_.push_back('T');
return indeterminate;
} else {
return bad;
}
case _http_version_p:
if (input == 'P') {
state = _http_version_slash;
version_.push_back('P');
return indeterminate;
} else {
return bad;
}
case _http_version_slash:
if (input == '/') {
state = _http_version_major_start;
version_.push_back('/');
return indeterminate;
} else {
return bad;
}
case _http_version_major_start:
if (is_digit(input)) {
state = _http_version_major;
version_.push_back(input);
return indeterminate;
} else {
return bad;
}
case _http_version_major:
if (input == '.') {
state = _http_version_minor_start;
version_.push_back('.');
return indeterminate;
} else if (is_digit(input)) {
version_.push_back(input);
return indeterminate;
} else {
return bad;
}
case _http_version_minor_start:
if (is_digit(input)) {
state = _http_version_minor;
version_.push_back(input);
return indeterminate;
} else {
return bad;
}
case _http_version_minor:
if (input == '\r') {
state = _expecting_newline_1;
return indeterminate;
} else if (is_digit(input)) {
version_.push_back(input);
return indeterminate;
} else {
return bad;
}
case _expecting_newline_1:
if (input == '\n') {
state = _header_line_start;
return indeterminate;
} else {
return bad;
}
case _header_line_start:
if (input == '\r') {
state = _expecting_newline_3;
return indeterminate;
} else if (!headers_.empty() && (input == ' ' || input == '\t')) {
state = _header_lws;
return indeterminate;
} else if (!is_char(input) || is_ctl(input) || is_tspecial(input)) {
return bad;
} else {
headers_.push_back(std::pair<std::string, std::string>());
headers_.back().first.push_back(input);
state = _header_name;
return indeterminate;
}
case _header_lws:
if (input == '\r') {
state = _expecting_newline_2;
return indeterminate;
} else if (input == ' ' || input == '\t') {
return indeterminate;
} else if (is_ctl(input)) {
return bad;
} else {
state = _header_value;
headers_.back().second.push_back(input);
return indeterminate;
}
case _header_name:
if (input == ':') {
state = _space_before_header_value;
return indeterminate;
} else if (!is_char(input) || is_ctl(input) || is_tspecial(input)) {
return bad;
} else {
headers_.back().first.push_back(input);
return indeterminate;
}
case _space_before_header_value:
if (input == ' ') {
state = _header_value;
return indeterminate;
} else {
return bad;
}
case _header_value:
if (input == '\r') {
state = _expecting_newline_2;
return indeterminate;
} else if (is_ctl(input)) {
return bad;
} else {
headers_.back().second.push_back(input);
return indeterminate;
}
case _expecting_newline_2:
if (input == '\n') {
state = _header_line_start;
return indeterminate;
} else {
return bad;
}
case _expecting_newline_3:
if (input == '\n') {
try {
remaining = std::stoull(GetHeaderValue("Content-Length"));
if (remaining > 0) {
state = _body;
return indeterminate;
} else {
return good;
}
} catch (...) {
return good;
}
} else {
return bad;
}
case _body:
body_.push_back(input);
remaining--;
if (remaining > 0) {
return indeterminate;
} else {
return good;
}
default:
return bad;
}
}