|
| 1 | +#include "common/matcher/matcher.h" |
| 2 | + |
| 3 | +#include "common/common/logger.h" |
| 4 | +#include "common/common/regex.h" |
| 5 | +#include "common/router/config_impl.h" |
| 6 | + |
| 7 | +#include "absl/strings/match.h" |
| 8 | + |
| 9 | +using ::envoy::api::v2::route::RouteMatch; |
| 10 | +using Envoy::Router::ConfigUtility; |
| 11 | + |
| 12 | +namespace Envoy { |
| 13 | +namespace Matcher { |
| 14 | +namespace { |
| 15 | + |
| 16 | +/** |
| 17 | + * Perform a match against any HTTP header or pseudo-header. |
| 18 | + */ |
| 19 | +class BaseMatcherImpl : public Matcher, public Logger::Loggable<Logger::Id::filter> { |
| 20 | +public: |
| 21 | + BaseMatcherImpl(const ::envoy::api::v2::route::RouteMatch& match) |
| 22 | + : case_sensitive_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(match, case_sensitive, true)), |
| 23 | + config_headers_(Http::HeaderUtility::buildHeaderDataVector(match.headers())) { |
| 24 | + for (const auto& query_parameter : match.query_parameters()) { |
| 25 | + config_query_parameters_.push_back( |
| 26 | + std::make_unique<Router::ConfigUtility::QueryParameterMatcher>(query_parameter)); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // Check match for HeaderMatcher and QueryParameterMatcher |
| 31 | + bool matchRoute(const Http::HeaderMap& headers) const { |
| 32 | + bool matches = true; |
| 33 | + // TODO(potatop): matching on RouteMatch runtime is not implemented. |
| 34 | + |
| 35 | + matches &= Http::HeaderUtility::matchHeaders(headers, config_headers_); |
| 36 | + if (!config_query_parameters_.empty()) { |
| 37 | + Http::Utility::QueryParams query_parameters = |
| 38 | + Http::Utility::parseQueryString(headers.Path()->value().getStringView()); |
| 39 | + matches &= ConfigUtility::matchQueryParams(query_parameters, config_query_parameters_); |
| 40 | + } |
| 41 | + return matches; |
| 42 | + } |
| 43 | + |
| 44 | +protected: |
| 45 | + const bool case_sensitive_; |
| 46 | + |
| 47 | +private: |
| 48 | + std::vector<Http::HeaderUtility::HeaderDataPtr> config_headers_; |
| 49 | + std::vector<Router::ConfigUtility::QueryParameterMatcherPtr> config_query_parameters_; |
| 50 | +}; |
| 51 | + |
| 52 | +/** |
| 53 | + * Perform a match against any path with prefix rule. |
| 54 | + */ |
| 55 | +class PrefixMatcherImpl : public BaseMatcherImpl { |
| 56 | +public: |
| 57 | + PrefixMatcherImpl(const ::envoy::api::v2::route::RouteMatch& match) |
| 58 | + : BaseMatcherImpl(match), prefix_(match.prefix()) {} |
| 59 | + |
| 60 | + bool matches(const Http::HeaderMap& headers) const override { |
| 61 | + if (BaseMatcherImpl::matchRoute(headers) && |
| 62 | + (case_sensitive_ |
| 63 | + ? absl::StartsWith(headers.Path()->value().getStringView(), prefix_) |
| 64 | + : absl::StartsWithIgnoreCase(headers.Path()->value().getStringView(), prefix_))) { |
| 65 | + ENVOY_LOG(debug, "Prefix requirement '{}' matched.", prefix_); |
| 66 | + return true; |
| 67 | + } |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | +private: |
| 72 | + // prefix string |
| 73 | + const std::string prefix_; |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * Perform a match against any path with a specific path rule. |
| 78 | + */ |
| 79 | +class PathMatcherImpl : public BaseMatcherImpl { |
| 80 | +public: |
| 81 | + PathMatcherImpl(const ::envoy::api::v2::route::RouteMatch& match) |
| 82 | + : BaseMatcherImpl(match), path_(match.path()) {} |
| 83 | + |
| 84 | + bool matches(const Http::HeaderMap& headers) const override { |
| 85 | + if (BaseMatcherImpl::matchRoute(headers)) { |
| 86 | + const Http::HeaderString& path = headers.Path()->value(); |
| 87 | + const size_t compare_length = |
| 88 | + path.getStringView().length() - Http::Utility::findQueryStringStart(path).length(); |
| 89 | + auto real_path = path.getStringView().substr(0, compare_length); |
| 90 | + bool match = case_sensitive_ ? real_path == path_ : StringUtil::caseCompare(real_path, path_); |
| 91 | + if (match) { |
| 92 | + ENVOY_LOG(debug, "Path requirement '{}' matched.", path_); |
| 93 | + return true; |
| 94 | + } |
| 95 | + } |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | +private: |
| 100 | + // path string. |
| 101 | + const std::string path_; |
| 102 | +}; |
| 103 | + |
| 104 | +/** |
| 105 | + * Perform a match against any path with a regex rule. |
| 106 | + * TODO(mattklein123): This code needs dedup with RegexRouteEntryImpl. |
| 107 | + */ |
| 108 | +class RegexMatcherImpl : public BaseMatcherImpl { |
| 109 | +public: |
| 110 | + RegexMatcherImpl(const ::envoy::api::v2::route::RouteMatch& match) : BaseMatcherImpl(match) { |
| 111 | + if (match.path_specifier_case() == envoy::api::v2::route::RouteMatch::kRegex) { |
| 112 | + regex_ = Regex::Utility::parseStdRegexAsCompiledMatcher(match.regex()); |
| 113 | + regex_str_ = match.regex(); |
| 114 | + } else { |
| 115 | + ASSERT(match.path_specifier_case() == envoy::api::v2::route::RouteMatch::kSafeRegex); |
| 116 | + regex_ = Regex::Utility::parseRegex(match.safe_regex()); |
| 117 | + regex_str_ = match.safe_regex().regex(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + bool matches(const Http::HeaderMap& headers) const override { |
| 122 | + if (BaseMatcherImpl::matchRoute(headers)) { |
| 123 | + const Http::HeaderString& path = headers.Path()->value(); |
| 124 | + const absl::string_view query_string = Http::Utility::findQueryStringStart(path); |
| 125 | + absl::string_view path_view = path.getStringView(); |
| 126 | + path_view.remove_suffix(query_string.length()); |
| 127 | + if (regex_->match(path_view)) { |
| 128 | + ENVOY_LOG(debug, "Regex requirement '{}' matched.", regex_str_); |
| 129 | + return true; |
| 130 | + } |
| 131 | + } |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | +private: |
| 136 | + Regex::CompiledMatcherPtr regex_; |
| 137 | + // raw regex string, for logging. |
| 138 | + std::string regex_str_; |
| 139 | +}; |
| 140 | + |
| 141 | +} // namespace |
| 142 | + |
| 143 | +MatcherConstPtr Matcher::create(const ::envoy::api::v2::route::RouteMatch& match) { |
| 144 | + switch (match.path_specifier_case()) { |
| 145 | + case RouteMatch::PathSpecifierCase::kPrefix: |
| 146 | + return std::make_shared<PrefixMatcherImpl>(match); |
| 147 | + case RouteMatch::PathSpecifierCase::kPath: |
| 148 | + return std::make_shared<PathMatcherImpl>(match); |
| 149 | + case RouteMatch::PathSpecifierCase::kRegex: |
| 150 | + case RouteMatch::PathSpecifierCase::kSafeRegex: |
| 151 | + return std::make_shared<RegexMatcherImpl>(match); |
| 152 | + // path specifier is required. |
| 153 | + case RouteMatch::PathSpecifierCase::PATH_SPECIFIER_NOT_SET: |
| 154 | + default: |
| 155 | + NOT_REACHED_GCOVR_EXCL_LINE; |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +} // namespace Matcher |
| 160 | +} // namespace Envoy |
0 commit comments