-
Notifications
You must be signed in to change notification settings - Fork 13
/
RequestParser.cpp
227 lines (200 loc) · 7.21 KB
/
RequestParser.cpp
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
/*
Copyright (c) 2011, Chris Pearce
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of Chris Pearce nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "RequestParser.h"
static int gCount = 0;
RequestParser::RequestParser()
: start(0),
complete(false),
method(UNKNOWN),
rangeStart(-1),
rangeEnd(-1),
hasRange(false),
id(gCount++)
{
}
void RequestParser::Add(const char* buf, unsigned len) {
assert(!complete);
string str(buf, len);
request += str;
const char* linebr = "\r\n";
assert(linebr[0] == 13);
assert(linebr[1] == 10);
// Continue to parse request.
while (start < request.size()) {
size_t end = request.find(linebr, start, 2);
if (end == string::npos)
break;
if (start == end)
complete = true;
string l = string(request, start, end - start);
Parse(l);
start = end + 2;
}
cout << "Request " << id << std::endl << buf;
}
#ifdef _DEBUG
void RequestParser::Test() {
assert(ExtractMethod("GET / HTTP1.1") == GET);
assert(ExtractMethod("HEAD / HTTP1.1") == HEAD);
assert(ExtractMethod("POST / HTTP1.1") == POST);
assert(ExtractMethod("Error / HTTP1.1") == UNKNOWN);
assert(ExtractTarget("GET / HTTP1.1") == "");
assert(ExtractTarget("GET // HTTP1.1") == "");
assert(ExtractTarget("GET /// HTTP1.1") == "");
assert(ExtractTarget("GET /dir/file.txt HTTP1.1") == "dir/file.txt");
assert(ExtractTarget("GET /dir/file.txt?params HTTP1.1") == "dir/file.txt");
assert(ExtractTarget("GET /?params HTTP1.1") == "");
assert(ExtractTarget("GET //?params HTTP1.1") == "");
assert(Flatten(ExtractQueryParams("GET / HTTP1.1")) == "");
assert(Flatten(ExtractQueryParams("GET // HTTP1.1")) == "");
assert(Flatten(ExtractQueryParams("GET /// HTTP1.1")) == "");
assert(Flatten(ExtractQueryParams("GET /dir/file.txt HTTP1.1")) == "");
assert(Flatten(ExtractQueryParams("GET /dir/file.txt? HTTP1.1")) == "");
assert(Flatten(ExtractQueryParams("GET /dir/file.txt?params HTTP1.1")) == "params=''");
assert(Flatten(ExtractQueryParams("GET /dir/file.txt?param1¶m2¶m3 HTTP1.1")) == "param1='' param2='' param3=''");
assert(Flatten(ExtractQueryParams("GET /dir/file.txt?param1=val1¶m2=val2¶m3=val3 HTTP1.1")) == "param1='val1' param2='val2' param3='val3'");
assert(Flatten(ExtractQueryParams("GET /?params HTTP1.1")) == "params=''");
assert(Flatten(ExtractQueryParams("GET /? HTTP1.1")) == "");
assert(Flatten(ExtractQueryParams("GET //?params HTTP1.1")) == "params=''");
assert(TestRange(true, "Range: bytes=0-1024", 0, 1024));
assert(TestRange(false, "Range: time=0-1024", 0, 0));
assert(TestRange(true, "Range: bytes=0-", 0, -1));
assert(TestRange(true, "Range: bytes=1024-", 1024, -1));
assert(TestRange(true, "Range: bytes=232128512-", 232128512, -1));
}
#endif
void RequestParser::GetRange(int64_t& start, int64_t& end) const {
start = rangeStart;
end = rangeEnd;
}
bool RequestParser::TestRange(bool expected,
const string& s,
int64_t expStart,
int64_t expEnd)
{
int64_t start=-2, end=-2;
bool r = ParseRange(s, start, end);
return r == expected &&
(!expected || start == expStart && end == expEnd);
}
void RequestParser::Parse(const string& s) {
if (start == 0) {
// Request line.
ParseRequestLine(s);
} else if (s.find("Range") == 0) {
int64_t start,end;
if (ParseRange(s, start, end)) {
rangeStart = start;
rangeEnd = end;
hasRange = true;
}
}
}
bool RequestParser::ParseRange(const string& s,
int64_t& start,
int64_t& end)
{
// Range: bytes=start,end
size_t sp = s.find(" ");
size_t eq = s.find("=");
if (sp == string::npos || eq == string::npos)
return false;
string bytes(s, sp + 1, eq - sp - 1);
if (bytes != "bytes") {
// The space is not followed by "bytes=", this is an invalid range param.
return false;
}
string range(s, eq + 1);
size_t dash = range.find("-");
if (dash == string::npos)
return false;
string startStr(range, 0, dash);
string endStr(range, dash + 1);
start = atoll(startStr.c_str());
if (endStr != "") {
end = atoll(endStr.c_str());
} else {
end = -1;
}
return true;
}
eMethod RequestParser::ExtractMethod(const string& request) {
size_t sp = request.find(" ");
string m(request, 0, sp);
if (m == "GET")
return GET;
else if (m == "HEAD")
return HEAD;
else if (m == "POST")
return POST;
return UNKNOWN;
}
string RequestParser::ExtractTarget(const string& request) {
size_t start = request.find("/") + 1;
size_t query = request.find("?", start); // Location of query params.
size_t end = (query != string::npos) ? query : request.find(" ", start);
// Remove trailing slashes.
while (end > start && request.at(end - 1) == '/') {
end--;
}
return string(request, start, end - start);
}
map<string, string> RequestParser::ExtractQueryParams(const string& request) {
map<string,string> m;
size_t query = request.find("?");
if (query == string::npos)
return m;
size_t start = query + 1;
size_t end = request.find(" ", start);
vector<string> v;
Tokenize(string(request, start, end - start), v, "&");
for (unsigned i=0; i<v.size(); i++) {
size_t eq = v[i].find("=");
string key;
string value;
if (eq == string::npos) {
key = v[i];
value = "";
} else {
key = string(v[i], 0, eq);
value = string(v[i], eq + 1, v[i].size() - eq - 1);
}
m[key] = value;
}
return m;
}
void RequestParser::ParseRequestLine(const string& request) {
// Extract method.
method = ExtractMethod(request);
target = ExtractTarget(request);
params = ExtractQueryParams(request);
//printf("Target: '%s'\n", target.c_str());
//printf("Params: '%s'\n", Flatten(params).c_str());
}