GRPC C++  1.39.1
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 <memory>
21 #include <string>
22 
23 #include "absl/status/statusor.h"
24 #include "absl/strings/string_view.h"
25 #include "absl/types/optional.h"
26 
27 #include "re2/re2.h"
28 
29 namespace grpc_core {
30 
32  public:
33  enum class Type {
34  kExact, // value stored in string_matcher_ field
35  kPrefix, // value stored in string_matcher_ field
36  kSuffix, // value stored in string_matcher_ field
37  kSafeRegex, // pattern stored in regex_matcher_ field
38  kContains, // value stored in string_matcher_ field
39  };
40 
41  // Creates StringMatcher instance. Returns error status on failure.
42  static absl::StatusOr<StringMatcher> Create(Type type,
43  absl::string_view matcher,
44  bool case_sensitive = true);
45 
46  StringMatcher() = default;
47  StringMatcher(const StringMatcher& other);
48  StringMatcher& operator=(const StringMatcher& other);
49  StringMatcher(StringMatcher&& other) noexcept;
50  StringMatcher& operator=(StringMatcher&& other) noexcept;
51  bool operator==(const StringMatcher& other) const;
52 
53  bool Match(absl::string_view value) const;
54 
55  std::string ToString() const;
56 
57  Type type() const { return type_; }
58 
59  // Valid for kExact, kPrefix, kSuffix and kContains.
60  const std::string& string_matcher() const { return string_matcher_; }
61 
62  // Valid for kSafeRegex.
63  RE2* regex_matcher() const { return regex_matcher_.get(); }
64 
65  bool case_sensitive() const { return case_sensitive_; }
66 
67  private:
68  StringMatcher(Type type, absl::string_view matcher, bool case_sensitive);
69  StringMatcher(std::unique_ptr<RE2> regex_matcher, bool case_sensitive);
70 
71  Type type_ = Type::kExact;
72  std::string string_matcher_;
73  std::unique_ptr<RE2> regex_matcher_;
74  bool case_sensitive_ = true;
75 };
76 
78  public:
79  enum class Type {
80  kExact, // value stored in StringMatcher field
81  kPrefix, // value stored in StringMatcher field
82  kSuffix, // value stored in StringMatcher field
83  kSafeRegex, // value stored in StringMatcher field
84  kContains, // value stored in StringMatcher field
85  kRange, // uses range_start and range_end fields
86  kPresent, // uses present_match field
87  };
88 
89  // Make sure that the first five HeaderMatcher::Type enum values match up to
90  // the corresponding StringMatcher::Type enum values, so that it's safe to
91  // convert by casting when delegating to StringMatcher.
92  static_assert(static_cast<StringMatcher::Type>(Type::kExact) ==
94  "");
95  static_assert(static_cast<StringMatcher::Type>(Type::kPrefix) ==
97  "");
98  static_assert(static_cast<StringMatcher::Type>(Type::kSuffix) ==
100  "");
101  static_assert(static_cast<StringMatcher::Type>(Type::kSafeRegex) ==
103  "");
104  static_assert(static_cast<StringMatcher::Type>(Type::kContains) ==
106  "");
107 
108  // Creates HeaderMatcher instance. Returns error status on failure.
109  static absl::StatusOr<HeaderMatcher> Create(absl::string_view name, Type type,
110  absl::string_view matcher,
111  int64_t range_start = 0,
112  int64_t range_end = 0,
113  bool present_match = false,
114  bool invert_match = false);
115 
116  HeaderMatcher() = default;
117  HeaderMatcher(const HeaderMatcher& other);
118  HeaderMatcher& operator=(const HeaderMatcher& other);
119  HeaderMatcher(HeaderMatcher&& other) noexcept;
120  HeaderMatcher& operator=(HeaderMatcher&& other) noexcept;
121  bool operator==(const HeaderMatcher& other) const;
122 
123  const std::string& name() const { return name_; }
124 
125  Type type() const { return type_; }
126 
127  // Valid for kExact, kPrefix, kSuffix and kContains.
128  const std::string& string_matcher() const {
129  return matcher_.string_matcher();
130  }
131 
132  // Valid for kSafeRegex.
133  RE2* regex_matcher() const { return matcher_.regex_matcher(); }
134 
135  bool Match(const absl::optional<absl::string_view>& value) const;
136 
137  std::string ToString() const;
138 
139  private:
140  // For StringMatcher.
141  HeaderMatcher(absl::string_view name, Type type, StringMatcher matcher,
142  bool invert_match);
143  // For RangeMatcher.
144  HeaderMatcher(absl::string_view name, int64_t range_start, int64_t range_end,
145  bool invert_match);
146  // For PresentMatcher.
147  HeaderMatcher(absl::string_view name, bool present_match, bool invert_match);
148 
149  std::string name_;
150  Type type_ = Type::kExact;
151  StringMatcher matcher_;
152  int64_t range_start_;
153  int64_t range_end_;
154  bool present_match_;
155  bool invert_match_ = false;
156 };
157 
158 } // namespace grpc_core
159 
160 #endif /* GRPC_CORE_LIB_MATCHERS_MATCHERS_H */
Definition: matchers.h:77
const std::string & name() const
Definition: matchers.h:123
HeaderMatcher & operator=(const HeaderMatcher &other)
Definition: matchers.cc:231
const std::string & string_matcher() const
Definition: matchers.h:128
bool Match(const absl::optional< absl::string_view > &value) const
Definition: matchers.cc:299
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.cc:166
std::string ToString() const
Definition: matchers.cc:317
Type
Definition: matchers.h:79
Type type() const
Definition: matchers.h:125
RE2 * regex_matcher() const
Definition: matchers.h:133
bool operator==(const HeaderMatcher &other) const
Definition: matchers.cc:284
Definition: matchers.h:31
bool operator==(const StringMatcher &other) const
Definition: matchers.cc:104
bool Match(absl::string_view value) const
Definition: matchers.cc:115
RE2 * regex_matcher() const
Definition: matchers.h:63
const std::string & string_matcher() const
Definition: matchers.h:60
Type type() const
Definition: matchers.h:57
StringMatcher & operator=(const StringMatcher &other)
Definition: matchers.cc:70
Type
Definition: matchers.h:33
std::string ToString() const
Definition: matchers.cc:139
static absl::StatusOr< StringMatcher > Create(Type type, absl::string_view matcher, bool case_sensitive=true)
Definition: matchers.cc:31
bool case_sensitive() const
Definition: matchers.h:65
Round Robin Policy.
Definition: backend_metric.cc:26
uint32_t range_end
Definition: xds_resolver.cc:176