Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/extensions/filters/network/dubbo_proxy/router/route_matcher.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <memory>
4
#include <string>
5
#include <vector>
6
7
#include "envoy/config/route/v3/route_components.pb.h"
8
#include "envoy/extensions/filters/network/dubbo_proxy/v3/route.pb.h"
9
#include "envoy/server/filter_config.h"
10
#include "envoy/type/v3/range.pb.h"
11
12
#include "source/common/common/logger.h"
13
#include "source/common/common/matchers.h"
14
#include "source/common/config/utility.h"
15
#include "source/common/config/well_known_names.h"
16
#include "source/common/http/header_utility.h"
17
#include "source/common/protobuf/protobuf.h"
18
#include "source/common/router/metadatamatchcriteria_impl.h"
19
#include "source/extensions/filters/network/dubbo_proxy/message_impl.h"
20
#include "source/extensions/filters/network/dubbo_proxy/metadata.h"
21
#include "source/extensions/filters/network/dubbo_proxy/router/router.h"
22
23
#include "absl/types/optional.h"
24
25
namespace Envoy {
26
namespace Extensions {
27
namespace NetworkFilters {
28
namespace DubboProxy {
29
namespace Router {
30
31
class RouteEntryImplBase : public RouteEntry,
32
                           public Route,
33
                           public std::enable_shared_from_this<RouteEntryImplBase>,
34
                           public Logger::Loggable<Logger::Id::dubbo> {
35
public:
36
  RouteEntryImplBase(const envoy::extensions::filters::network::dubbo_proxy::v3::Route& route);
37
0
  ~RouteEntryImplBase() override = default;
38
39
  // Router::RouteEntry
40
  const std::string& clusterName() const override;
41
0
  const Envoy::Router::MetadataMatchCriteria* metadataMatchCriteria() const override {
42
0
    return metadata_match_criteria_.get();
43
0
  }
44
45
  // Router::Route
46
  const RouteEntry* routeEntry() const override;
47
48
  virtual RouteConstSharedPtr matches(const MessageMetadata& metadata,
49
                                      uint64_t random_value) const PURE;
50
51
protected:
52
  RouteConstSharedPtr clusterEntry(uint64_t random_value) const;
53
  bool headersMatch(const RpcInvocationImpl& invocation) const;
54
55
private:
56
  class WeightedClusterEntry : public RouteEntry, public Route {
57
  public:
58
    using WeightedCluster = envoy::config::route::v3::WeightedCluster::ClusterWeight;
59
    WeightedClusterEntry(const RouteEntryImplBase& parent, const WeightedCluster& cluster);
60
61
0
    uint64_t clusterWeight() const { return cluster_weight_; }
62
63
    // Router::RouteEntry
64
0
    const std::string& clusterName() const override { return cluster_name_; }
65
0
    const Envoy::Router::MetadataMatchCriteria* metadataMatchCriteria() const override {
66
0
      return metadata_match_criteria_ ? metadata_match_criteria_.get()
67
0
                                      : parent_.metadataMatchCriteria();
68
0
    }
69
70
    // Router::Route
71
0
    const RouteEntry* routeEntry() const override { return this; }
72
73
  private:
74
    const RouteEntryImplBase& parent_;
75
    const std::string cluster_name_;
76
    const uint64_t cluster_weight_;
77
    Envoy::Router::MetadataMatchCriteriaConstPtr metadata_match_criteria_;
78
  };
79
80
  using WeightedClusterEntrySharedPtr = std::shared_ptr<WeightedClusterEntry>;
81
82
  uint64_t total_cluster_weight_;
83
  const std::string cluster_name_;
84
  const std::vector<Http::HeaderUtility::HeaderDataPtr> config_headers_;
85
  std::vector<WeightedClusterEntrySharedPtr> weighted_clusters_;
86
87
  // TODO(gengleilei) Implement it.
88
  Envoy::Router::MetadataMatchCriteriaConstPtr metadata_match_criteria_;
89
};
90
91
using RouteEntryImplBaseConstSharedPtr = std::shared_ptr<const RouteEntryImplBase>;
92
93
class ParameterRouteEntryImpl : public RouteEntryImplBase {
94
public:
95
  ParameterRouteEntryImpl(const envoy::extensions::filters::network::dubbo_proxy::v3::Route& route);
96
  ~ParameterRouteEntryImpl() override;
97
98
  struct ParameterData {
99
    using ParameterMatchSpecifier =
100
        envoy::extensions::filters::network::dubbo_proxy::v3::MethodMatch::ParameterMatchSpecifier;
101
    ParameterData(uint32_t index, const ParameterMatchSpecifier& config);
102
103
    Http::HeaderUtility::HeaderMatchType match_type_;
104
    std::string value_;
105
    envoy::type::v3::Int64Range range_;
106
    uint32_t index_;
107
  };
108
109
  // RoutEntryImplBase
110
  RouteConstSharedPtr matches(const MessageMetadata& metadata,
111
                              uint64_t random_value) const override;
112
113
private:
114
  bool matchParameter(absl::string_view request_data, const ParameterData& config_data) const;
115
116
  std::vector<ParameterData> parameter_data_list_;
117
};
118
119
class MethodRouteEntryImpl : public RouteEntryImplBase {
120
public:
121
  MethodRouteEntryImpl(const envoy::extensions::filters::network::dubbo_proxy::v3::Route& route);
122
  ~MethodRouteEntryImpl() override;
123
124
  // RoutEntryImplBase
125
  RouteConstSharedPtr matches(const MessageMetadata& metadata,
126
                              uint64_t random_value) const override;
127
128
private:
129
  const Matchers::StringMatcherImpl<envoy::type::matcher::v3::StringMatcher> method_name_;
130
  std::shared_ptr<ParameterRouteEntryImpl> parameter_route_;
131
};
132
133
class SingleRouteMatcherImpl : public Logger::Loggable<Logger::Id::dubbo> {
134
public:
135
  class InterfaceMatcher {
136
  public:
137
    InterfaceMatcher(const std::string& interface_name);
138
0
    bool match(const absl::string_view interface) const { return impl_(interface); }
139
140
  private:
141
    std::function<bool(const absl::string_view)> impl_;
142
  };
143
144
  using RouteConfig = envoy::extensions::filters::network::dubbo_proxy::v3::RouteConfiguration;
145
  SingleRouteMatcherImpl(const RouteConfig& config,
146
                         Server::Configuration::ServerFactoryContext& context);
147
148
  RouteConstSharedPtr route(const MessageMetadata& metadata, uint64_t random_value) const;
149
150
private:
151
  bool matchServiceName(const RpcInvocationImpl& invocation) const;
152
  bool matchServiceVersion(const RpcInvocationImpl& invocation) const;
153
  bool matchServiceGroup(const RpcInvocationImpl& invocation) const;
154
155
  std::vector<RouteEntryImplBaseConstSharedPtr> routes_;
156
  const InterfaceMatcher interface_matcher_;
157
  const absl::optional<std::string> group_;
158
  const absl::optional<std::string> version_;
159
};
160
using SingleRouteMatcherImplPtr = std::unique_ptr<SingleRouteMatcherImpl>;
161
162
class RouteConfigImpl : public Config, public Logger::Loggable<Logger::Id::dubbo> {
163
public:
164
  using RouteConfigList = Envoy::Protobuf::RepeatedPtrField<
165
      envoy::extensions::filters::network::dubbo_proxy::v3::RouteConfiguration>;
166
  using MultipleRouteConfig =
167
      envoy::extensions::filters::network::dubbo_proxy::v3::MultipleRouteConfiguration;
168
  RouteConfigImpl(const RouteConfigList& route_config_list,
169
                  Server::Configuration::ServerFactoryContext& context,
170
                  bool validate_clusters_default = false);
171
  RouteConfigImpl(const MultipleRouteConfig& multiple_route_config,
172
                  Server::Configuration::ServerFactoryContext& context,
173
                  bool validate_clusters_default = false)
174
0
      : RouteConfigImpl(multiple_route_config.route_config(), context, validate_clusters_default) {}
175
176
  RouteConstSharedPtr route(const MessageMetadata& metadata, uint64_t random_value) const override;
177
178
private:
179
  std::vector<SingleRouteMatcherImplPtr> route_matcher_list_;
180
};
181
using RouteConfigImplPtr = std::unique_ptr<RouteConfigImpl>;
182
183
class NullRouteConfigImpl : public Config {
184
public:
185
0
  RouteConstSharedPtr route(const MessageMetadata&, uint64_t) const override { return nullptr; }
186
};
187
188
} // namespace Router
189
} // namespace DubboProxy
190
} // namespace NetworkFilters
191
} // namespace Extensions
192
} // namespace Envoy