matchers/matchers.h
Go to the documentation of this file.
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 #ifndef GRPC_CORE_LIB_MATCHERS_MATCHERS_H
16 #define GRPC_CORE_LIB_MATCHERS_MATCHERS_H
17 
19 
20 #include <stdint.h>
21 
22 #include <memory>
23 #include <string>
24 
25 #include "absl/status/statusor.h"
26 #include "absl/strings/string_view.h"
27 #include "absl/types/optional.h"
28 #include "re2/re2.h"
29 
30 namespace grpc_core {
31 
33  public:
34  enum class Type {
35  kExact, // value stored in string_matcher_ field
36  kPrefix, // value stored in string_matcher_ field
37  kSuffix, // value stored in string_matcher_ field
38  kSafeRegex, // pattern stored in regex_matcher_ field
39  kContains, // value stored in string_matcher_ field
40  };
41 
42  // Creates StringMatcher instance. Returns error status on failure.
43  // Note: case_sensitive is ignored for type kSafeRegex.
45  absl::string_view matcher,
46  bool case_sensitive = true);
47 
48  StringMatcher() = default;
49  StringMatcher(const StringMatcher& other);
50  StringMatcher& operator=(const StringMatcher& other);
51  StringMatcher(StringMatcher&& other) noexcept;
52  StringMatcher& operator=(StringMatcher&& other) noexcept;
53  bool operator==(const StringMatcher& other) const;
54 
55  bool Match(absl::string_view value) const;
56 
57  std::string ToString() const;
58 
59  Type type() const { return type_; }
60 
61  // Valid for kExact, kPrefix, kSuffix and kContains.
62  const std::string& string_matcher() const { return string_matcher_; }
63 
64  // Valid for kSafeRegex.
65  RE2* regex_matcher() const { return regex_matcher_.get(); }
66 
67  bool case_sensitive() const { return case_sensitive_; }
68 
69  private:
71  explicit StringMatcher(std::unique_ptr<RE2> regex_matcher);
72 
75  std::unique_ptr<RE2> regex_matcher_;
76  bool case_sensitive_ = true;
77 };
78 
80  public:
81  enum class Type {
82  kExact, // value stored in StringMatcher field
83  kPrefix, // value stored in StringMatcher field
84  kSuffix, // value stored in StringMatcher field
85  kSafeRegex, // value stored in StringMatcher field
86  kContains, // value stored in StringMatcher field
87  kRange, // uses range_start and range_end fields
88  kPresent, // uses present_match field
89  };
90 
91  // Make sure that the first five HeaderMatcher::Type enum values match up to
92  // the corresponding StringMatcher::Type enum values, so that it's safe to
93  // convert by casting when delegating to StringMatcher.
94  static_assert(static_cast<StringMatcher::Type>(Type::kExact) ==
96  "");
97  static_assert(static_cast<StringMatcher::Type>(Type::kPrefix) ==
99  "");
100  static_assert(static_cast<StringMatcher::Type>(Type::kSuffix) ==
102  "");
103  static_assert(static_cast<StringMatcher::Type>(Type::kSafeRegex) ==
105  "");
106  static_assert(static_cast<StringMatcher::Type>(Type::kContains) ==
108  "");
109 
110  // Creates HeaderMatcher instance. Returns error status on failure.
112  absl::string_view matcher,
113  int64_t range_start = 0,
114  int64_t range_end = 0,
115  bool present_match = false,
116  bool invert_match = false);
117 
118  HeaderMatcher() = default;
119  HeaderMatcher(const HeaderMatcher& other);
120  HeaderMatcher& operator=(const HeaderMatcher& other);
121  HeaderMatcher(HeaderMatcher&& other) noexcept;
122  HeaderMatcher& operator=(HeaderMatcher&& other) noexcept;
123  bool operator==(const HeaderMatcher& other) const;
124 
125  const std::string& name() const { return name_; }
126 
127  Type type() const { return type_; }
128 
129  // Valid for kExact, kPrefix, kSuffix and kContains.
130  const std::string& string_matcher() const {
131  return matcher_.string_matcher();
132  }
133 
134  // Valid for kSafeRegex.
135  RE2* regex_matcher() const { return matcher_.regex_matcher(); }
136 
137  bool Match(const absl::optional<absl::string_view>& value) const;
138 
139  std::string ToString() const;
140 
141  private:
142  // For StringMatcher.
144  bool invert_match);
145  // For RangeMatcher.
147  bool invert_match);
148  // For PresentMatcher.
149  HeaderMatcher(absl::string_view name, bool present_match, bool invert_match);
150 
157  bool invert_match_ = false;
158 };
159 
160 } // namespace grpc_core
161 
162 #endif /* GRPC_CORE_LIB_MATCHERS_MATCHERS_H */
grpc_core::HeaderMatcher::Type::kSuffix
@ kSuffix
grpc_core::HeaderMatcher::Type::kContains
@ kContains
grpc_core::StringMatcher::operator==
bool operator==(const StringMatcher &other) const
Definition: matchers/matchers.cc:96
grpc_core::HeaderMatcher::type_
Type type_
Definition: matchers/matchers.h:152
grpc_core::HeaderMatcher::type
Type type() const
Definition: matchers/matchers.h:127
grpc_core::HeaderMatcher::Match
bool Match(const absl::optional< absl::string_view > &value) const
Definition: matchers/matchers.cc:290
grpc_core::HeaderMatcher::Type::kExact
@ kExact
grpc_core::HeaderMatcher::operator==
bool operator==(const HeaderMatcher &other) const
Definition: matchers/matchers.cc:275
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::HeaderMatcher::range_end_
int64_t range_end_
Definition: matchers/matchers.h:155
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
range_end
uint32_t range_end
Definition: xds_resolver.cc:330
grpc_core::StringMatcher::Create
static absl::StatusOr< StringMatcher > Create(Type type, absl::string_view matcher, bool case_sensitive=true)
Definition: matchers/matchers.cc:34
grpc_core::HeaderMatcher::Type::kPresent
@ kPresent
grpc_core::HeaderMatcher
Definition: matchers/matchers.h:79
grpc_core::StringMatcher::case_sensitive_
bool case_sensitive_
Definition: matchers/matchers.h:76
grpc_core::StringMatcher::Type::kSuffix
@ kSuffix
grpc_core::StringMatcher::StringMatcher
StringMatcher()=default
grpc_core::StringMatcher::type
Type type() const
Definition: matchers/matchers.h:59
grpc_core::HeaderMatcher::range_start_
int64_t range_start_
Definition: matchers/matchers.h:154
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
Type
Definition: bloaty/third_party/protobuf/src/google/protobuf/type.pb.h:182
grpc_core::StringMatcher::ToString
std::string ToString() const
Definition: matchers/matchers.cc:131
grpc_core::StringMatcher::regex_matcher_
std::unique_ptr< RE2 > regex_matcher_
Definition: matchers/matchers.h:75
absl::optional< absl::string_view >
grpc_core::StringMatcher::Type
Type
Definition: matchers/matchers.h:34
grpc_core::HeaderMatcher::matcher_
StringMatcher matcher_
Definition: matchers/matchers.h:153
stdint.h
grpc_core::HeaderMatcher::regex_matcher
RE2 * regex_matcher() const
Definition: matchers/matchers.h:135
grpc_core::StringMatcher::Match
bool Match(absl::string_view value) const
Definition: matchers/matchers.cc:107
grpc_core::HeaderMatcher::Type::kPrefix
@ kPrefix
grpc_core::StringMatcher
Definition: matchers/matchers.h:32
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_core::HeaderMatcher::ToString
std::string ToString() const
Definition: matchers/matchers.cc:308
grpc_core::HeaderMatcher::invert_match_
bool invert_match_
Definition: matchers/matchers.h:157
grpc_core::HeaderMatcher::Type::kSafeRegex
@ kSafeRegex
grpc_core::HeaderMatcher::string_matcher
const std::string & string_matcher() const
Definition: matchers/matchers.h:130
grpc_core::HeaderMatcher::Create
static absl::StatusOr< HeaderMatcher > Create(absl::string_view name, Type type, absl::string_view matcher, int64_t range_start=0, int64_t range_end=0, bool present_match=false, bool invert_match=false)
Definition: matchers/matchers.cc:157
grpc_core::StringMatcher::operator=
StringMatcher & operator=(const StringMatcher &other)
Definition: matchers/matchers.cc:65
grpc_core::StringMatcher::Type::kExact
@ kExact
grpc_core::StringMatcher::Type::kSafeRegex
@ kSafeRegex
grpc_core::HeaderMatcher::name
const std::string & name() const
Definition: matchers/matchers.h:125
grpc_core::HeaderMatcher::name_
std::string name_
Definition: matchers/matchers.h:151
grpc_core::StringMatcher::string_matcher
const std::string & string_matcher() const
Definition: matchers/matchers.h:62
grpc_core::HeaderMatcher::Type::kRange
@ kRange
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
grpc_core::StringMatcher::Type::kContains
@ kContains
grpc_core::StringMatcher::case_sensitive
bool case_sensitive() const
Definition: matchers/matchers.h:67
grpc_core::StringMatcher::Type::kPrefix
@ kPrefix
grpc_core::StringMatcher::type_
Type type_
Definition: matchers/matchers.h:73
grpc_core::HeaderMatcher::present_match_
bool present_match_
Definition: matchers/matchers.h:156
grpc_core::StringMatcher::string_matcher_
std::string string_matcher_
Definition: matchers/matchers.h:74
grpc_core::HeaderMatcher::HeaderMatcher
HeaderMatcher()=default
grpc_core::StringMatcher::regex_matcher
RE2 * regex_matcher() const
Definition: matchers/matchers.h:65
grpc_core::HeaderMatcher::operator=
HeaderMatcher & operator=(const HeaderMatcher &other)
Definition: matchers/matchers.cc:222
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:22