-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy pathHTTPHandlerRequestFilter.cpp
More file actions
296 lines (244 loc) · 11.7 KB
/
Copy pathHTTPHandlerRequestFilter.cpp
File metadata and controls
296 lines (244 loc) · 11.7 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
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
#include <Server/HTTPHandlerRequestFilter.h>
#include <Common/Exception.h>
#include <Common/StringUtils.h>
#include <Common/re2.h>
#include <base/find_symbols.h>
#include <Poco/String.h>
#include <Poco/StringTokenizer.h>
#include <absl/container/inlined_vector.h>
#include <fmt/format.h>
#include <algorithm>
#include <cstring>
#include <memory>
#include <string_view>
#include <unordered_map>
#include <vector>
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_COMPILE_REGEXP;
extern const int UNKNOWN_ELEMENT_IN_CONFIG;
}
namespace
{
/// A parsed filter expression.
struct FilterExpression
{
/// The value to match against (without marker "regex:" if any).
String value;
/// Non-null if the value is a regular expression.
CompiledRegexPtr regex = nullptr;
/// Whether to match `value` as a base path: the path itself or anything below it on a path-segment boundary.
bool match_prefix = false;
/// Whether to ignore the query string before matching (for `url`/`full_url` filters).
bool ignore_query_string = false;
};
bool checkRegexExpression(std::string_view match_str, const CompiledRegexPtr & compiled_regex)
{
int num_captures = compiled_regex->NumberOfCapturingGroups() + 1;
absl::InlinedVector<std::string_view, 5> matches(num_captures);
return compiled_regex->Match(
{match_str.data(), match_str.size()}, 0, match_str.size(), re2::RE2::Anchor::ANCHOR_BOTH, matches.data(), num_captures);
}
bool checkExpression(std::string_view match_str, const FilterExpression & expression)
{
if (expression.ignore_query_string)
{
const auto * query_string = find_first_symbols<'?'>(match_str.data(), match_str.data() + match_str.size());
match_str = match_str.substr(0, query_string - match_str.data());
}
if (expression.regex)
return checkRegexExpression(match_str, expression.regex);
if (expression.match_prefix)
{
/// Match the base path itself or any path below it, on a path-segment ('/') boundary. E.g. the
/// base "/api/v1" matches "/api/v1", "/api/v1/" and "/api/v1/write", but not "/api/v1beta".
const std::string_view prefix = expression.value;
return match_str.starts_with(prefix)
&& (match_str.size() == prefix.size() || match_str[prefix.size()] == '/');
}
return match_str == expression.value;
}
CompiledRegexPtr compileRegex(const std::string & regex)
{
auto compiled_regex = std::make_shared<const re2::RE2>(regex);
if (!compiled_regex->ok())
throw Exception(ErrorCodes::CANNOT_COMPILE_REGEXP, "cannot compile re2: {} for http handling rule, error: {}. "
"Look at https://github.com/google/re2/wiki/Syntax for reference.",
regex, compiled_regex->error());
return compiled_regex;
}
/// Whether `compiled_regex` has at least one named capturing group whose name is in `group_names`.
bool hasAnyOfCapturingGroups(const CompiledRegexPtr & compiled_regex, const NameSet & group_names)
{
const auto & actual_groups = compiled_regex->NamedCapturingGroups();
return std::any_of(actual_groups.begin(), actual_groups.end(),
[&](const auto & group) { return group_names.contains(group.first); });
}
FilterExpression getExpression(const std::string & expression, HTTPRequestFilterMatchType match_type, bool ignore_query_string)
{
if (match_type == HTTPRequestFilterMatchType::Prefix)
{
/// Match the value as a base path on a path-segment boundary (see checkExpression). A trailing '/'
/// is stripped here, so "/api/v1/" and "/api/v1" behave the same.
std::string_view value = expression;
if (value.ends_with('/'))
value.remove_suffix(1);
return {.value = String{value}, .match_prefix = true, .ignore_query_string = ignore_query_string};
}
/// `Regexp` treats the whole value as a regular expression, while `Full` does so only when the value
/// starts with the "regex:" marker.
if (match_type == HTTPRequestFilterMatchType::Regexp)
return {.value = expression, .regex = compileRegex(expression), .ignore_query_string = ignore_query_string};
if (startsWith(expression, "regex:"))
{
auto regex = expression.substr(strlen("regex:"));
return {.value = regex, .regex = compileRegex(regex), .ignore_query_string = ignore_query_string};
}
return {.value = expression, .ignore_query_string = ignore_query_string};
}
}
HTTPRequestFilter methodsFilter(const Poco::Util::AbstractConfiguration & config, const std::string & config_path)
{
std::vector<String> methods;
Poco::StringTokenizer tokenizer(config.getString(config_path), ",");
for (const auto & iterator : tokenizer)
methods.emplace_back(Poco::toUpper(Poco::trim(iterator)));
return [methods](const HTTPServerRequest & request) { return std::count(methods.begin(), methods.end(), request.getMethod()); };
}
HTTPRequestFilter urlFilter(const Poco::Util::AbstractConfiguration & config, const std::string & config_path, HTTPRequestFilterMatchType match_type)
{
return [expression = getExpression(config.getString(config_path), match_type, /* ignore_query_string= */ true)](const HTTPServerRequest & request)
{
return checkExpression(request.getURI(), expression);
};
}
HTTPRequestFilter fullUrlFilter(const Poco::Util::AbstractConfiguration & config, const std::string & config_path, HTTPRequestFilterMatchType match_type)
{
return [expression = getExpression(config.getString(config_path), match_type, /* ignore_query_string= */ true)](const HTTPServerRequest & request)
{
const auto & server_address = request.serverAddress();
std::string url = fmt::format("{}://{}{}",
request.isSecure() ? "https" : "http",
server_address.toString(),
request.getURI());
return checkExpression(url, expression);
};
}
HTTPRequestFilter emptyQueryStringFilter()
{
return [](const HTTPServerRequest & request)
{
const auto & uri = request.getURI();
return !uri.contains('?');
};
}
HTTPRequestFilter headersFilter(const Poco::Util::AbstractConfiguration & config, const std::string & prefix, HTTPRequestFilterMatchType match_type)
{
std::unordered_map<String, FilterExpression> headers_expression;
Poco::Util::AbstractConfiguration::Keys headers_name;
config.keys(prefix, headers_name);
for (const auto & header_name : headers_name)
{
const auto & expression = getExpression(
config.getString(prefix + "." + header_name), match_type, /* ignore_query_string= */ false);
checkExpression("", expression); /// Check expression syntax is correct
headers_expression.emplace(std::make_pair(header_name, expression));
}
return [headers_expression](const HTTPServerRequest & request)
{
for (const auto & [header_name, header_expression] : headers_expression)
{
const auto header_value = request.get(header_name, "");
if (!checkExpression(std::string_view(header_value.data(), header_value.size()), header_expression))
return false;
}
return true;
};
}
std::vector<HTTPRequestFilter> extractHTTPRequestFiltersFromConfig(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix)
{
std::vector<HTTPRequestFilter> filters;
Poco::Util::AbstractConfiguration::Keys filter_types;
config.keys(config_prefix, filter_types);
for (const auto & filter_type : filter_types)
{
if (filter_type == "handler")
continue;
/// URL path (the query string is ignored)
if (filter_type == "url")
filters.push_back(urlFilter(config, config_prefix + ".url", HTTPRequestFilterMatchType::Full));
/// URL path matched as a base path
else if (filter_type == "url_prefix")
filters.push_back(urlFilter(config, config_prefix + ".url_prefix", HTTPRequestFilterMatchType::Prefix));
/// URL path matched as a regular expression
else if (filter_type == "url_regexp")
filters.push_back(urlFilter(config, config_prefix + ".url_regexp", HTTPRequestFilterMatchType::Regexp));
/// Complete URL (scheme://host:port/path); the query string is ignored
else if (filter_type == "full_url")
filters.push_back(fullUrlFilter(config, config_prefix + ".full_url", HTTPRequestFilterMatchType::Full));
/// Complete URL matched as a base path
else if (filter_type == "full_url_prefix")
filters.push_back(fullUrlFilter(config, config_prefix + ".full_url_prefix", HTTPRequestFilterMatchType::Prefix));
/// Complete URL matched as a regular expression
else if (filter_type == "full_url_regexp")
filters.push_back(fullUrlFilter(config, config_prefix + ".full_url_regexp", HTTPRequestFilterMatchType::Regexp));
else if (filter_type == "empty_query_string")
filters.push_back(emptyQueryStringFilter());
else if (filter_type == "headers")
filters.push_back(headersFilter(config, config_prefix + ".headers", HTTPRequestFilterMatchType::Full));
/// Each header value matched as a regular expression
else if (filter_type == "headers_regexp")
filters.push_back(headersFilter(config, config_prefix + ".headers_regexp", HTTPRequestFilterMatchType::Regexp));
else if (filter_type == "methods")
filters.push_back(methodsFilter(config, config_prefix + ".methods"));
else
throw Exception(ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG, "Unknown element in config: {}.{}", config_prefix, filter_type);
}
return filters;
}
HTTPHandlerRegexpsWithNamedGroups HTTPHandlerRegexpsWithNamedGroups::fromConfig(
const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix, const NameSet & group_names)
{
HTTPHandlerRegexpsWithNamedGroups result;
/// The URL is a regular expression when configured via the dedicated `url_regexp` tag,
/// or via `url` carrying the obsolete "regex:" marker.
CompiledRegexPtr url_regexp;
if (config.has(config_prefix + ".url_regexp"))
{
url_regexp = compileRegex(config.getString(config_prefix + ".url_regexp"));
}
else if (config.has(config_prefix + ".url"))
{
const auto url = config.getString(config_prefix + ".url");
if (startsWith(url, "regex:"))
url_regexp = compileRegex(url.substr(strlen("regex:")));
}
if (url_regexp && hasAnyOfCapturingGroups(url_regexp, group_names))
result.url_regexp = url_regexp;
/// Headers configured as regular expressions: `headers` entries carrying the "regex:" marker, and
/// `headers_regexp` entries (whose value is the regular expression itself).
const auto collect_header_regexes = [&](const std::string & headers_key, bool has_regex_marker)
{
Poco::Util::AbstractConfiguration::Keys header_names;
config.keys(config_prefix + "." + headers_key, header_names);
for (const auto & header_name : header_names)
{
auto expression = config.getString(config_prefix + "." + headers_key + "." + header_name);
if (has_regex_marker)
{
if (!startsWith(expression, "regex:"))
continue;
expression = expression.substr(strlen("regex:"));
}
auto regex = compileRegex(expression);
if (hasAnyOfCapturingGroups(regex, group_names))
result.headers_name_with_regexp.emplace(header_name, regex);
}
};
collect_header_regexes("headers", /* has_regex_marker= */ true);
collect_header_regexes("headers_regexp", /* has_regex_marker= */ false);
return result;
}
}