-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathH2Server_Request.h
333 lines (305 loc) · 9.01 KB
/
H2Server_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
#ifndef H2SERVER_REQUEST_MATCH_H
#define H2SERVER_REQUEST_MATCH_H
#include <vector>
#include <list>
#include <map>
#include <set>
#include <regex>
#include <rapidjson/pointer.h>
#include <rapidjson/document.h>
#include "H2Server_Config_Schema.h"
#include "H2Server_Response.h"
#include "H2Server_Request_Message.h"
struct ci_less
{
// case-independent (ci) compare_less binary function
struct nocase_compare
{
bool operator()(const unsigned char& c1, const unsigned char& c2) const
{
return tolower(c1) < tolower(c2);
}
};
bool operator()(const std::string& s1, const std::string& s2) const
{
return std::lexicographical_compare
(s1.begin(), s1.end(), // source range
s2.begin(), s2.end(), // dest range
nocase_compare()); // comparison
}
};
class Match_Rule
{
public:
enum Match_Type
{
EQUALS_TO = 0,
START_WITH,
END_WITH,
CONTAINS,
REGEX_MATCH
};
Match_Type match_type;
std::string header_name;
std::string json_pointer;
std::string object;
mutable uint64_t unique_id;
std::regex reg_exp;
Match_Rule(const Schema_Header_Match& header_match)
{
object = header_match.input;
header_name = header_match.header;
json_pointer = "";
match_type = string_to_match_type[header_match.matchType];
try
{
if (REGEX_MATCH == match_type)
{
reg_exp.assign(object, std::regex_constants::ECMAScript|std::regex_constants::optimize);
}
}
catch (std::regex_error e)
{
std::cerr<<"invalid reg exp: "<<object<<" reason: "<<e.what()<<std::endl;
}
}
Match_Rule(const Schema_Payload_Match& payload_match)
{
object = payload_match.input;
header_name = "";
json_pointer = payload_match.jsonPointer;
match_type = string_to_match_type[payload_match.matchType];
try
{
if (REGEX_MATCH == match_type)
{
reg_exp.assign(object, std::regex_constants::ECMAScript|std::regex_constants::optimize);
}
}
catch (std::regex_error e)
{
std::cerr<<"invalid reg exp: "<<object<<" "<<e.what()<<std::endl;
}
}
std::map<std::string, Match_Type> string_to_match_type {{"EqualsTo", EQUALS_TO}, {"StartsWith", START_WITH}, {"EndsWith", END_WITH}, {"Contains", CONTAINS}, {"RegexMatch", REGEX_MATCH}};
bool match(const std::string& subject, Match_Type verb, const std::string& object) const
{
if (debug_mode)
{
std::cout<<"subject: "<<subject<<", match type: "<<verb<<", object: "<<object<<std::endl;
}
switch (verb)
{
case EQUALS_TO:
{
return (subject == object);
}
case START_WITH:
{
return (subject.find(object) == 0);
}
case END_WITH:
{
return (subject.size() >= object.size() && 0 == subject.compare(subject.size() - object.size(), object.size(), object));
}
case CONTAINS:
{
return (subject.find(object) != std::string::npos);
}
case REGEX_MATCH:
{
return std::regex_match(subject, reg_exp);
}
}
return false;
}
bool match(const std::string& subject) const
{
return match(subject, match_type, object);
}
bool match(const rapidjson::Document& d) const
{
return match(getValueFromJsonPtr(d, json_pointer), match_type, object);
}
bool match(H2Server_Request_Message& request) const
{
if (request.match_result.count(unique_id))
{
return request.match_result[unique_id];
}
else
{
bool matched = false;
if (header_name.size())
{
auto range = request.headers.equal_range(header_name);
for (auto iter = range.first; iter != range.second; ++iter)
{
auto& header_val = iter->second;
matched = match(header_val);
if (matched)
{
break;
}
}
}
else
{
if (json_pointer.size())
{
request.decode_json_if_not_yet();
matched = match(request.json_payload);
}
else
{
matched = match(*request.raw_payload);
}
}
request.match_result[unique_id] = matched;
return matched;
}
}
bool match_header(const std::map<std::string, std::string, ci_less>& response_headers) const
{
auto it = response_headers.find(header_name);
if (it != response_headers.end())
{
return match(it->second, match_type, object);
}
return false;
}
bool match_header(const std::vector<std::map<std::string, std::string, ci_less>>& response_headers) const
{
for (auto& response_header_frame: response_headers)
{
auto it = response_header_frame.find(header_name);
if (it != response_header_frame.end())
{
return match(it->second, match_type, object);
}
}
return false;
}
bool match_json_doc(const rapidjson::Document& d) const
{
return match(getValueFromJsonPtr(d, json_pointer), match_type, object);
}
bool match(const std::map<std::string, std::string, ci_less>& response_headers, const rapidjson::Document& d)
{
if (header_name.size())
{
return match_header(response_headers);
}
else
{
return match_json_doc(d);
}
}
bool match(const std::vector<std::map<std::string, std::string, ci_less>>& response_headers, const rapidjson::Document& d)
{
if (header_name.size())
{
return match_header(response_headers);
}
else
{
return match_json_doc(d);
}
}
bool operator<(const Match_Rule& rhs) const
{
if (!header_name.empty() && rhs.header_name.empty())
{
return false;
}
else if (header_name.empty() && !rhs.header_name.empty())
{
return true;
}
else if ((match_type == EQUALS_TO) && (rhs.match_type != EQUALS_TO))
{
return false;
}
else if ((rhs.match_type == EQUALS_TO) && (match_type != EQUALS_TO))
{
return true;
}
else
{
std::string mine = std::to_string(match_type);
mine.append(header_name);
mine.append(json_pointer);
mine.append(object);
std::string other = std::to_string(rhs.match_type);
other.append(rhs.header_name);
other.append(rhs.json_pointer);
other.append(rhs.object);
return (mine < other);
}
}
};
class H2Server_Request
{
public:
std::set<Match_Rule> match_rules;
std::string name;
size_t request_index;
H2Server_Request(const Schema_Request_Match& request_match, size_t index)
{
for (auto& schema_header_match : request_match.header_match)
{
match_rules.emplace(Match_Rule(schema_header_match));
}
for (auto& schema_payload_match : request_match.payload_match)
{
match_rules.emplace(Match_Rule(schema_payload_match));
}
name = request_match.name;
request_index = index;
}
bool match(H2Server_Request_Message& request) const
{
for (auto match_rule = match_rules.rbegin(); match_rule != match_rules.rend(); match_rule++)
{
if (!match_rule->match(request))
{
return false;
}
}
return true;
}
bool operator<(const H2Server_Request& rhs) const
{
if (match_rules.size() < rhs.match_rules.size())
{
return true;
}
else if (match_rules.size() > rhs.match_rules.size())
{
return false;
}
else
{
auto match = match_rules.rbegin();
auto other_match = rhs.match_rules.rbegin();
while (match != match_rules.rend() && other_match != rhs.match_rules.rend())
{
if (*match < *other_match)
{
return true;
}
else if (*other_match < *match)
{
return false;
}
else
{
match++;
other_match++;
}
}
}
return false;
}
};
#endif