This commit is contained in:
Alexander Tokmakov 2020-04-28 10:19:37 +03:00
parent 5a1d22a363
commit aeac8cb621
4 changed files with 32 additions and 40 deletions

View File

@ -783,9 +783,9 @@ void PredefinedQueryHandler::customizeContext(Poco::Net::HTTPServerRequest & req
{
int num_captures = compiled_regex->NumberOfCapturingGroups() + 1;
re2_st::StringPiece matches[num_captures];
re2_st::StringPiece input(begin, end - begin);
if (compiled_regex->Match(input, 0, end - begin, re2_st::RE2::Anchor::ANCHOR_BOTH, matches, num_captures))
re2::StringPiece matches[num_captures];
re2::StringPiece input(begin, end - begin);
if (compiled_regex->Match(input, 0, end - begin, re2::RE2::Anchor::ANCHOR_BOTH, matches, num_captures))
{
for (const auto & [capturing_name, capturing_index] : compiled_regex->NamedCapturingGroups())
{
@ -840,7 +840,7 @@ static inline bool capturingNamedQueryParam(NameSet receive_params, const Compil
static inline CompiledRegexPtr getCompiledRegex(const std::string & expression)
{
auto compiled_regex = std::make_shared<const re2_st::RE2>(expression);
auto compiled_regex = std::make_shared<const re2::RE2>(expression);
if (!compiled_regex->ok())
throw Exception("Cannot compile re2: " + expression + " for http handling rule, error: " +

View File

@ -8,11 +8,6 @@
#include <Common/HTMLForm.h>
#include <re2/re2.h>
#if USE_RE2_ST
#include <re2_st/re2.h>
#else
#define re2_st re2
#endif
namespace CurrentMetrics
{
@ -26,7 +21,7 @@ namespace DB
class WriteBufferFromHTTPServerResponse;
typedef std::shared_ptr<const re2_st::RE2> CompiledRegexPtr;
typedef std::shared_ptr<const re2::RE2> CompiledRegexPtr;
class HTTPHandler : public Poco::Net::HTTPRequestHandler
{

View File

@ -12,12 +12,6 @@
#include "InterserverIOHTTPHandler.h"
#include "PrometheusRequestHandler.h"
#if USE_RE2_ST
#include <re2_st/re2.h>
#else
#define re2_st re2
#endif
namespace DB
{

View File

@ -9,11 +9,6 @@
#include <common/find_symbols.h>
#if USE_RE2_ST
#include <re2_st/re2.h>
#else
#define re2_st re2
#endif
namespace DB
{
@ -24,29 +19,24 @@ namespace ErrorCodes
extern const int UNKNOWN_ELEMENT_IN_CONFIG;
}
static inline bool checkRegexExpression(const StringRef & match_str, const StringRef & expression)
typedef std::shared_ptr<const re2::RE2> CompiledRegexPtr;
static inline bool checkRegexExpression(const StringRef & match_str, const CompiledRegexPtr & compiled_regex)
{
re2_st::StringPiece regex(expression.data, expression.size);
auto compiled_regex = std::make_shared<re2_st::RE2>(regex);
if (!compiled_regex->ok())
throw Exception("cannot compile re2: " + expression.toString() + " for http handling rule, error: " + compiled_regex->error() +
". Look at https://github.com/google/re2/wiki/Syntax for reference.", ErrorCodes::CANNOT_COMPILE_REGEXP);
int num_captures = compiled_regex->NumberOfCapturingGroups() + 1;
re2_st::StringPiece matches[num_captures];
re2_st::StringPiece match_input(match_str.data, match_str.size);
return compiled_regex->Match(match_input, 0, match_str.size, re2_st::RE2::Anchor::ANCHOR_BOTH, matches, num_captures);
re2::StringPiece matches[num_captures];
re2::StringPiece match_input(match_str.data, match_str.size);
return compiled_regex->Match(match_input, 0, match_str.size, re2::RE2::Anchor::ANCHOR_BOTH, matches, num_captures);
}
static inline bool checkExpression(const StringRef & match_str, const std::string & expression)
static inline bool checkExpression(const StringRef & match_str, const std::pair<String, CompiledRegexPtr> & expression)
{
if (startsWith(expression, "regex:"))
return checkRegexExpression(match_str, expression.substr(6));
if (expression.second)
return checkRegexExpression(match_str, expression.second);
return match_str == expression;
return match_str == expression.first;
}
static inline auto methodsFilter(Poco::Util::AbstractConfiguration & config, const std::string & config_path)
@ -60,9 +50,22 @@ static inline auto methodsFilter(Poco::Util::AbstractConfiguration & config, con
return [methods](const Poco::Net::HTTPServerRequest & request) { return std::count(methods.begin(), methods.end(), request.getMethod()); };
}
static inline auto getExpression(const std::string & expression)
{
if (!startsWith(expression, "regex:"))
return std::make_pair(expression, CompiledRegexPtr{});
auto compiled_regex = std::make_shared<const re2::RE2>(expression.substr(6));
if (!compiled_regex->ok())
throw Exception("cannot compile re2: " + expression + " for http handling rule, error: " + compiled_regex->error() +
". Look at https://github.com/google/re2/wiki/Syntax for reference.", ErrorCodes::CANNOT_COMPILE_REGEXP);
return std::make_pair(expression, compiled_regex);
}
static inline auto urlFilter(Poco::Util::AbstractConfiguration & config, const std::string & config_path)
{
return [expression = config.getString(config_path)](const Poco::Net::HTTPServerRequest & request)
return [expression = getExpression(config.getString(config_path))](const Poco::Net::HTTPServerRequest & request)
{
const auto & uri = request.getURI();
const auto & end = find_first_symbols<'?'>(uri.data(), uri.data() + uri.size());
@ -73,13 +76,13 @@ static inline auto urlFilter(Poco::Util::AbstractConfiguration & config, const s
static inline auto headersFilter(Poco::Util::AbstractConfiguration & config, const std::string & prefix)
{
std::unordered_map<String, String> headers_expression;
std::unordered_map<String, std::pair<String, CompiledRegexPtr>> headers_expression;
Poco::Util::AbstractConfiguration::Keys headers_name;
config.keys(prefix, headers_name);
for (const auto & header_name : headers_name)
{
const auto & expression = config.getString(prefix + "." + header_name);
const auto & expression = getExpression(config.getString(prefix + "." + header_name));
checkExpression("", expression); /// Check expression syntax is correct
headers_expression.emplace(std::make_pair(header_name, expression));
}