-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathH2Server.h
269 lines (240 loc) · 8.56 KB
/
H2Server.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
#ifndef H2SERVER_H
#define H2SERVER_H
#include <map>
#include <boost/asio.hpp>
#include "H2Server_Request.h"
#include "H2Server_Request_Message.h"
using Request_Processor = std::function<bool(boost::asio::io_service*,
uint64_t,
int32_t,
const std::multimap<std::string, std::string>& req_headers,
const std::string&)>;
class H2Server_Response_Group
{
public:
std::vector<H2Server_Response> responses;
void init_distribution_map_and_total_weight(const std::vector<Schema_Response_To_Return>& responses_schema)
{
uint64_t totalWeight = 0;
for (size_t index = 0; index < responses.size(); index++)
{
if (responses[index].weight)
{
auto& resp = responses[index];
totalWeight += responses[index].weight;
distribution_map[totalWeight] = index;
}
}
total_weight = distribution_map.size() ? distribution_map.rbegin()->first : 1;
}
H2Server_Response_Group(const std::vector<Schema_Response_To_Return>& responses_schema)
:generator((std::random_device())())
{
for (auto i = 0; i < responses_schema.size(); i++)
{
responses.emplace_back(H2Server_Response(responses_schema[i], i));
}
init_distribution_map_and_total_weight(responses_schema);
distr.param(std::uniform_int_distribution<>::param_type(0, total_weight - 1));
}
size_t select_response()
{
size_t response_index = distribution_map.size() ? distribution_map.begin()->second : 0;
if (distribution_map.size() > 1)
{
uint64_t randomNumber = distr(generator);
auto iter = distribution_map.upper_bound(randomNumber);
if (iter != distribution_map.end())
{
response_index = iter->second;
}
if (debug_mode)
{
std::cout<<"randomNumber: " << randomNumber << ", response index: "<<response_index<<std::endl;
}
}
return response_index;
}
void set_request_processor(Request_Processor req_proc)
{
request_processor = req_proc;
}
void clear_request_processor()
{
auto dummy = std::move(request_processor);
}
const Request_Processor& get_request_processor() const
{
return request_processor;
}
private:
std::map<uint64_t, size_t> distribution_map;
uint64_t total_weight;
std::mt19937_64 generator;
std::uniform_int_distribution<int> distr;
Request_Processor request_processor;
};
class H2Server_Service
{
public:
H2Server_Request request;
H2Server_Response_Group response_group;
H2Server_Service(const Schema_Service& service, size_t index):
request(service.request, index),
response_group(service.responses)
{
}
};
inline std::ostream& operator<<(std::ostream& o, const H2Server_Request& request)
{
o << std::endl << "H2Server_Request:" << request.name << std::endl;
o << "{" << std::endl;
for (auto& match : request.match_rules)
{
o << "match_rule: " << match.match_type
<< ", " << match.header_name
<< ", " << match.json_pointer
<< ", " << match.object
<< ", " << match.unique_id
<< std::endl;
}
o << "}" << std::endl;
return o;
}
inline std::ostream& operator<<(std::ostream& o, const H2Server_Response& response)
{
o << "H2Server_Response:" << response.name << std::endl;
o << "{" << std::endl;
o << "status_code:" << response.status_code << std::endl;
o << "name:" << response.name << std::endl;
o << "weight:" << response.weight << std::endl;
o << "response_index:" << response.response_index << std::endl;
for (auto& header : response.additonalHeaders)
{
std::cout<<"header:"<<std::endl;
for (auto& token : header.tokenizedHeader)
{
o << token << " ";
}
o << std::endl;
o << "header_arguments: ";
for (auto& arg : header.header_arguments)
{
o << "header name: " << arg.header_name << std::endl;
o << "regex: " << arg.regex << std::endl;
o << "substring_start: " << arg.substring_start << std::endl;
o << "substring_length: " << arg.substring_length << std::endl;
}
}
o << "payload: ";
for (auto& token : response.tokenizedPayload)
{
o << token << " ";
}
o << std::endl;
o << "payload_arguments: ";
for (auto& arg : response.payload_arguments)
{
o << "json_pointer: " << arg.json_pointer << std::endl;
o << "regex: " << arg.regex << std::endl;
o << "substring_start: " << arg.substring_start << std::endl;
o << "substring_length: " << arg.substring_length << std::endl;
}
o << "}" << std::endl;
return o;
}
class H2Server
{
public:
std::map<H2Server_Request, H2Server_Response_Group> services;
boost::asio::io_service* io_service = nullptr;
void build_match_rule_unique_id(std::map<H2Server_Request, H2Server_Response_Group>& services)
{
std::set<Match_Rule> all_match_rules;
for (auto& each_service : services)
{
for (auto& match_rule : each_service.first.match_rules)
{
all_match_rules.insert(match_rule);
}
}
std::map<H2Server_Request, H2Server_Response_Group> new_services;
for (auto& each_service : services)
{
for (auto& match_rule : each_service.first.match_rules)
{
match_rule.unique_id = std::distance(all_match_rules.begin(), all_match_rules.find(match_rule));
}
new_services.insert(std::make_pair(each_service.first, each_service.second));
}
services.swap(new_services);
}
H2Server(const H2Server_Config_Schema& config_schema)
{
for (size_t index = 0; index < config_schema.service.size(); index++)
{
H2Server_Service service(config_schema.service[index], index);
services.insert(std::make_pair(std::move(service.request), std::move(service.response_group)));
}
build_match_rule_unique_id(services);
}
void set_io_service(boost::asio::io_service* io_serv)
{
io_service = io_serv;
}
std::map<H2Server_Request, H2Server_Response_Group>::reverse_iterator get_matched_request(H2Server_Request_Message& msg, int64_t& matched_request_index)
{
matched_request_index = -1;
for (auto iter = services.rbegin(); iter != services.rend(); iter++)
{
if (debug_mode)
{
std::cout<<"checking request: "<<iter->first<<std::endl;
}
if (iter->first.match(msg))
{
matched_request_index = iter->first.request_index;
if (debug_mode)
{
std::cout<<"matched request found, request index: "<<matched_request_index<<std::endl;
}
return iter;
}
}
return services.rend();
}
H2Server_Response* get_response_to_return(std::map<H2Server_Request, H2Server_Response_Group>::reverse_iterator service, size_t& matched_response_index)
{
size_t index = service->second.select_response();
if (debug_mode)
{
std::cout<<"response to be returned: "<<service->second.responses[index]<<std::endl;
}
matched_response_index = index;
return &service->second.responses[index];
}
H2Server_Response* get_response_to_return(H2Server_Request_Message& msg, size_t& matched_request_index, size_t& matched_response_index)
{
for (auto iter = services.rbegin(); iter != services.rend(); iter++)
{
if (debug_mode)
{
std::cout<<"checking request: "<<iter->first<<std::endl;
}
if (iter->first.match(msg))
{
size_t index = iter->second.select_response();
if (debug_mode)
{
std::cout<<"matched request found, request index: "<<iter->first.request_index<<std::endl;
std::cout<<"response to be returned: "<<iter->second.responses[index]<<std::endl;
}
matched_request_index = iter->first.request_index;
matched_response_index = index;
return &iter->second.responses[index];
}
}
return nullptr;
}
};
#endif