| 1 |
// Copyright 2021 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include <grpc/support/port_platform.h> |
| 16 |
|
| 17 |
#include "src/core/lib/matchers/matchers.h" |
| 18 |
|
| 19 |
#include <utility> |
| 20 |
|
| 21 |
#include "absl/status/status.h" |
| 22 |
#include "absl/strings/ascii.h" |
| 23 |
#include "absl/strings/match.h" |
| 24 |
#include "absl/strings/numbers.h" |
| 25 |
#include "absl/strings/str_format.h" |
| 26 |
|
| 27 |
namespace grpc_core { |
| 28 |
|
| 29 |
// |
| 30 |
// StringMatcher |
| 31 |
// |
| 32 |
|
| 33 |
absl::StatusOr<StringMatcher> StringMatcher::Create(Type type, |
| 34 |
absl::string_view matcher, |
| 35 |
bool case_sensitive) { |
| 36 |
if (type == Type::kSafeRegex) { |
| 37 |
auto regex_matcher = std::make_unique<RE2>(std::string(matcher)); |
| 38 |
if (!regex_matcher->ok()) { |
| 39 |
return absl::InvalidArgumentError( |
| 40 |
"Invalid regex string specified in matcher."); |
| 41 |
} |
| 42 |
return StringMatcher(std::move(regex_matcher)); |
| 43 |
} else { |
| 44 |
return StringMatcher(type, matcher, case_sensitive); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
StringMatcher::StringMatcher(Type type, absl::string_view matcher, |
| 49 |
bool case_sensitive) |
| 50 |
: type_(type), string_matcher_(matcher), case_sensitive_(case_sensitive) {} |
| 51 |
|
| 52 |
StringMatcher::StringMatcher(std::unique_ptr<RE2> regex_matcher) |
| 53 |
: type_(Type::kSafeRegex), regex_matcher_(std::move(regex_matcher)) {} |
| 54 |
|
| 55 |
StringMatcher::StringMatcher(const StringMatcher& other) |
| 56 |
: type_(other.type_), case_sensitive_(other.case_sensitive_) { |
| 57 |
if (type_ == Type::kSafeRegex) { |
| 58 |
regex_matcher_ = std::make_unique<RE2>(other.regex_matcher_->pattern()); |
| 59 |
} else { |
| 60 |
string_matcher_ = other.string_matcher_; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
StringMatcher& StringMatcher::operator=(const StringMatcher& other) { |
| 65 |
type_ = other.type_; |
| 66 |
if (type_ == Type::kSafeRegex) { |
| 67 |
regex_matcher_ = std::make_unique<RE2>(other.regex_matcher_->pattern()); |
| 68 |
} else { |
| 69 |
string_matcher_ = other.string_matcher_; |
| 70 |
} |
| 71 |
case_sensitive_ = other.case_sensitive_; |
| 72 |
return *this; |
| 73 |
} |
| 74 |
|
| 75 |
StringMatcher::StringMatcher(StringMatcher&& other) noexcept |
| 76 |
: type_(other.type_), case_sensitive_(other.case_sensitive_) { |
| 77 |
if (type_ == Type::kSafeRegex) { |
| 78 |
regex_matcher_ = std::move(other.regex_matcher_); |
| 79 |
} else { |
| 80 |
string_matcher_ = std::move(other.string_matcher_); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
StringMatcher& StringMatcher::operator=(StringMatcher&& other) noexcept { |
| 85 |
type_ = other.type_; |
| 86 |
if (type_ == Type::kSafeRegex) { |
| 87 |
regex_matcher_ = std::move(other.regex_matcher_); |
| 88 |
} else { |
| 89 |
string_matcher_ = std::move(other.string_matcher_); |
| 90 |
} |
| 91 |
case_sensitive_ = other.case_sensitive_; |
| 92 |
return *this; |
| 93 |
} |
| 94 |
|
| 95 |
bool StringMatcher::operator==(const StringMatcher& other) const { |
| 96 |
if (type_ != other.type_ || case_sensitive_ != other.case_sensitive_) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
if (type_ == Type::kSafeRegex) { |
| 100 |
return regex_matcher_->pattern() == other.regex_matcher_->pattern(); |
| 101 |
} else { |
| 102 |
return string_matcher_ == other.string_matcher_; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
bool StringMatcher::Match(absl::string_view value) const { |
| 107 |
switch (type_) { |
| 108 |
case Type::kExact: |
| 109 |
return case_sensitive_ ? value == string_matcher_ |
| 110 |
: absl::EqualsIgnoreCase(value, string_matcher_); |
| 111 |
case StringMatcher::Type::kPrefix: |
| 112 |
return case_sensitive_ |
| 113 |
? absl::StartsWith(value, string_matcher_) |
| 114 |
: absl::StartsWithIgnoreCase(value, string_matcher_); |
| 115 |
case StringMatcher::Type::kSuffix: |
| 116 |
return case_sensitive_ ? absl::EndsWith(value, string_matcher_) |
| 117 |
: absl::EndsWithIgnoreCase(value, string_matcher_); |
| 118 |
case StringMatcher::Type::kContains: |
| 119 |
return case_sensitive_ |
| 120 |
? absl::StrContains(value, string_matcher_) |
| 121 |
: absl::StrContains(absl::AsciiStrToLower(value), |
| 122 |
absl::AsciiStrToLower(string_matcher_)); |
| 123 |
case StringMatcher::Type::kSafeRegex: |
| 124 |
return RE2::FullMatch(std::string(value), *regex_matcher_); |
| 125 |
default: |
| 126 |
return false; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
std::string StringMatcher::ToString() const { |
| 131 |
switch (type_) { |
| 132 |
case Type::kExact: |
| 133 |
return absl::StrFormat("StringMatcher{exact=%s%s}", string_matcher_, |
| 134 |
case_sensitive_ ? "" : ", case_sensitive=false"); |
| 135 |
case Type::kPrefix: |
| 136 |
return absl::StrFormat("StringMatcher{prefix=%s%s}", string_matcher_, |
| 137 |
case_sensitive_ ? "" : ", case_sensitive=false"); |
| 138 |
case Type::kSuffix: |
| 139 |
return absl::StrFormat("StringMatcher{suffix=%s%s}", string_matcher_, |
| 140 |
case_sensitive_ ? "" : ", case_sensitive=false"); |
| 141 |
case Type::kContains: |
| 142 |
return absl::StrFormat("StringMatcher{contains=%s%s}", string_matcher_, |
| 143 |
case_sensitive_ ? "" : ", case_sensitive=false"); |
| 144 |
case Type::kSafeRegex: |
| 145 |
return absl::StrFormat("StringMatcher{safe_regex=%s}", |
| 146 |
regex_matcher_->pattern()); |
| 147 |
default: |
| 148 |
return ""; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// |
| 153 |
// HeaderMatcher |
| 154 |
// |
| 155 |
|
| 156 |
absl::StatusOr<HeaderMatcher> HeaderMatcher::Create( |
| 157 |
absl::string_view name, Type type, absl::string_view matcher, |
| 158 |
int64_t range_start, int64_t range_end, bool present_match, |
| 159 |
bool invert_match) { |
| 160 |
if (static_cast<int>(type) < 5) { |
| 161 |
// Only for EXACT, PREFIX, SUFFIX, SAFE_REGEX and CONTAINS. |
| 162 |
absl::StatusOr<StringMatcher> string_matcher = |
| 163 |
StringMatcher::Create(static_cast<StringMatcher::Type>(type), matcher, |
| 164 |
/*case_sensitive=*/true); |
| 165 |
if (!string_matcher.ok()) { |
| 166 |
return string_matcher.status(); |
| 167 |
} |
| 168 |
return HeaderMatcher(name, type, std::move(string_matcher.value()), |
| 169 |
invert_match); |
| 170 |
} else if (type == Type::kRange) { |
| 171 |
if (range_start > range_end) { |
| 172 |
return absl::InvalidArgumentError( |
| 173 |
"Invalid range specifier specified: end cannot be smaller than " |
| 174 |
"start."); |
| 175 |
} |
| 176 |
return HeaderMatcher(name, range_start, range_end, invert_match); |
| 177 |
} else { |
| 178 |
return HeaderMatcher(name, present_match, invert_match); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
HeaderMatcher::HeaderMatcher(absl::string_view name, Type type, |
| 183 |
StringMatcher string_matcher, bool invert_match) |
| 184 |
: name_(name), |
| 185 |
type_(type), |
| 186 |
matcher_(std::move(string_matcher)), |
| 187 |
invert_match_(invert_match) {} |
| 188 |
|
| 189 |
HeaderMatcher::HeaderMatcher(absl::string_view name, int64_t range_start, |
| 190 |
int64_t range_end, bool invert_match) |
| 191 |
: name_(name), |
| 192 |
type_(Type::kRange), |
| 193 |
range_start_(range_start), |
| 194 |
range_end_(range_end), |
| 195 |
invert_match_(invert_match) {} |
| 196 |
|
| 197 |
HeaderMatcher::HeaderMatcher(absl::string_view name, bool present_match, |
| 198 |
bool invert_match) |
| 199 |
: name_(name), |
| 200 |
type_(Type::kPresent), |
| 201 |
present_match_(present_match), |
| 202 |
invert_match_(invert_match) {} |
| 203 |
|
| 204 |
HeaderMatcher::HeaderMatcher(const HeaderMatcher& other) |
| 205 |
: name_(other.name_), |
| 206 |
type_(other.type_), |
| 207 |
invert_match_(other.invert_match_) { |
| 208 |
switch (type_) { |
| 209 |
case Type::kRange: |
| 210 |
range_start_ = other.range_start_; |
| 211 |
range_end_ = other.range_end_; |
| 212 |
break; |
| 213 |
case Type::kPresent: |
| 214 |
present_match_ = other.present_match_; |
| 215 |
break; |
| 216 |
default: |
| 217 |
matcher_ = other.matcher_; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
HeaderMatcher& HeaderMatcher::operator=(const HeaderMatcher& other) { |
| 222 |
name_ = other.name_; |
| 223 |
type_ = other.type_; |
| 224 |
invert_match_ = other.invert_match_; |
| 225 |
switch (type_) { |
| 226 |
case Type::kRange: |
| 227 |
range_start_ = other.range_start_; |
| 228 |
range_end_ = other.range_end_; |
| 229 |
break; |
| 230 |
case Type::kPresent: |
| 231 |
present_match_ = other.present_match_; |
| 232 |
break; |
| 233 |
default: |
| 234 |
matcher_ = other.matcher_; |
| 235 |
} |
| 236 |
return *this; |
| 237 |
} |
| 238 |
|
| 239 |
HeaderMatcher::HeaderMatcher(HeaderMatcher&& other) noexcept |
| 240 |
: name_(std::move(other.name_)), |
| 241 |
type_(other.type_), |
| 242 |
invert_match_(other.invert_match_) { |
| 243 |
switch (type_) { |
| 244 |
case Type::kRange: |
| 245 |
range_start_ = other.range_start_; |
| 246 |
range_end_ = other.range_end_; |
| 247 |
break; |
| 248 |
case Type::kPresent: |
| 249 |
present_match_ = other.present_match_; |
| 250 |
break; |
| 251 |
default: |
| 252 |
matcher_ = std::move(other.matcher_); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
HeaderMatcher& HeaderMatcher::operator=(HeaderMatcher&& other) noexcept { |
| 257 |
name_ = std::move(other.name_); |
| 258 |
type_ = other.type_; |
| 259 |
invert_match_ = other.invert_match_; |
| 260 |
switch (type_) { |
| 261 |
case Type::kRange: |
| 262 |
range_start_ = other.range_start_; |
| 263 |
range_end_ = other.range_end_; |
| 264 |
break; |
| 265 |
case Type::kPresent: |
| 266 |
present_match_ = other.present_match_; |
| 267 |
break; |
| 268 |
default: |
| 269 |
matcher_ = std::move(other.matcher_); |
| 270 |
} |
| 271 |
return *this; |
| 272 |
} |
| 273 |
|
| 274 |
bool HeaderMatcher::operator==(const HeaderMatcher& other) const { |
| 275 |
if (name_ != other.name_) return false; |
| 276 |
if (type_ != other.type_) return false; |
| 277 |
if (invert_match_ != other.invert_match_) return false; |
| 278 |
switch (type_) { |
| 279 |
case Type::kRange: |
| 280 |
return range_start_ == other.range_start_ && |
| 281 |
range_end_ == other.range_end_; |
| 282 |
case Type::kPresent: |
| 283 |
return present_match_ == other.present_match_; |
| 284 |
default: |
| 285 |
return matcher_ == other.matcher_; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
bool HeaderMatcher::Match( |
| 290 |
const absl::optional<absl::string_view>& value) const { |
| 291 |
bool match; |
| 292 |
if (type_ == Type::kPresent) { |
| 293 |
match = value.has_value() == present_match_; |
| 294 |
} else if (!value.has_value()) { |
| 295 |
// All other types fail to match if field is not present. |
| 296 |
return false; |
| 297 |
} else if (type_ == Type::kRange) { |
| 298 |
int64_t int_value; |
| 299 |
match = absl::SimpleAtoi(value.value(), &int_value) && |
| 300 |
int_value >= range_start_ && int_value < range_end_; |
| 301 |
} else { |
| 302 |
match = matcher_.Match(value.value()); |
| 303 |
} |
| 304 |
return match != invert_match_; |
| 305 |
} |
| 306 |
|
| 307 |
std::string HeaderMatcher::ToString() const { |
| 308 |
switch (type_) { |
| 309 |
case Type::kRange: |
| 310 |
return absl::StrFormat("HeaderMatcher{%s %srange=[%d, %d]}", name_, |
| 311 |
invert_match_ ? "not " : "", range_start_, |
| 312 |
range_end_); |
| 313 |
case Type::kPresent: |
| 314 |
return absl::StrFormat("HeaderMatcher{%s %spresent=%s}", name_, |
| 315 |
invert_match_ ? "not " : "", |
| 316 |
present_match_ ? "true" : "false"); |
| 317 |
case Type::kExact: |
| 318 |
case Type::kPrefix: |
| 319 |
case Type::kSuffix: |
| 320 |
case Type::kSafeRegex: |
| 321 |
case Type::kContains: |
| 322 |
return absl::StrFormat("HeaderMatcher{%s %s%s}", name_, |
| 323 |
invert_match_ ? "not " : "", matcher_.ToString()); |
| 324 |
default: |
| 325 |
return ""; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
} // namespace grpc_core |