-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_parser.cc
More file actions
227 lines (217 loc) · 6.3 KB
/
Copy pathrequest_parser.cc
File metadata and controls
227 lines (217 loc) · 6.3 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
#include "request_parser.h"
#include "request.h"
namespace http {
namespace server {
request_parser::request_parser()
: state_ (method_start)
{
}
void request_parser::reset()
{
state_ = method_start;
}
boost::tribool request_parser::consume(request& req, char input)
{
switch (state_) {
case method_start:
if (!is_char(input) || is_ctl(input) || is_tspecial(input) ) {
return false;
} else {
state_ = method;
req.method.push_back(input);
return boost::indeterminate;
}
case method:
if (' ' == input) {
state_ = uri;
return boost::indeterminate;
} else if (!is_char(input) || is_ctl(input) || is_tspecial(input) ) {
return false;
} else {
req.method.push_back(input);
return boost::indeterminate;
}
case uri:
if (' ' == input) {
state_ = http_version_h;
return boost::indeterminate;
} else if (is_ctl(input)) {
return false;
} else {
req.uri.push_back(input);
return boost::indeterminate;
}
case http_version_h:
if ('H' == input) {
state_ = http_version_t_1;
return boost::indeterminate;
} else {
return false;
}
case http_version_t_1:
if ('T' == input) {
state_ = http_version_t_2;
return boost::indeterminate;
} else {
return false;
}
case http_version_t_2:
if ('T' == input) {
state_ = http_version_p;
return boost::indeterminate;
} else {
return false;
}
case http_version_p:
if ('P' == input) {
state_ = http_version_slash;
return boost::indeterminate;
} else {
return false;
}
case http_version_slash:
if ('/' == input) {
req.http_version_major = 0;
req.http_version_minor = 0;
state_ = http_version_major_start;
return boost::indeterminate;
} else {
return false;
}
case http_version_major_start:
if (is_digit(input)) {
req.http_version_major = req.http_version_major * 10 + input - '0';
state_ = http_version_major;
return boost::indeterminate;
} else {
return false;
}
case http_version_major:
if ('.' == input) {
state_ = http_version_minor_start;
return boost::indeterminate;
} else if (is_digit(input)) {
req.http_version_major = req.http_version_major * 10 + input - '0';
return boost::indeterminate;
} else {
return false;
}
case http_version_minor_start:
if (is_digit(input)) {
req.http_version_minor = req.http_version_minor * 10 + input - '0';
state_ = http_version_minor;
return boost::indeterminate;
} else {
return false;
}
case http_version_minor:
if ('\r' == input) {
state_ = expecting_newline_1;
return boost::indeterminate;
} else if (is_digit(input)) {
req.http_version_minor = req.http_version_minor * 10 + input - '0';
return boost::indeterminate;
} else {
return false;
}
case expecting_newline_1:
if ('\n' == input) {
state_ = header_line_start;
return boost::indeterminate;
} else {
return false;
}
case header_line_start:
if ('\r' == input) {
state_ = expecting_newline_3;
return boost::indeterminate;
} else if (!req.headers.empty() && (' ' == input || '\t' == input)) {
state_ =header_lws;
return boost::indeterminate;
} else if (!is_char(input) || is_ctl(input) || is_tspecial(input)) {
return false;
} else {
req.headers.push_back(header());
req.headers.back().name.push_back(input);
state_ = header_name;
return boost::indeterminate;
}
case header_lws:
if ('\r' == input) {
state_ = expecting_newline_2;
return boost::indeterminate;
} else if (' ' == input || '\t' == input) {
return boost::indeterminate;
} else if (is_ctl(input)) {
return false;
} else {
state_ = header_value;
req.headers.back().value.push_back(input);
return boost::indeterminate;
}
case header_name:
if (':' == input) {
state_ = space_before_header_value;
return boost::indeterminate;
} else if (!is_char(input) || is_ctl(input) || is_tspecial(input)) {
return false;
} else {
req.headers.back().name.push_back(input);
return boost::indeterminate;
}
case space_before_header_value:
if (' ' == input) {
state_ = header_value;
return boost::indeterminate;
} else {
return false;
}
case header_value:
if ('\r' == input) {
state_ = expecting_newline_2;
return boost::indeterminate;
} else if (is_ctl(input)) {
return false;
} else {
req.headers.back().value.push_back(input);
return boost::indeterminate;
}
case expecting_newline_2:
if ('\n' == input) {
state_ = header_line_start;
return boost::indeterminate;
} else {
return false;
}
case expecting_newline_3:
return ('\n' == input);
default:
return false;
}
}
bool request_parser::is_char(int c)
{
return c >= 0 && c <= 127;
}
bool request_parser::is_ctl(int c)
{
return (c >= 0 && c <= 31) || (127 == c);
}
bool request_parser::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;
}
}
bool request_parser::is_digit(int c)
{
return c >= '0' && c <= '9';
}
};
};