• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2021 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPC_SRC_CORE_XDS_GRPC_XDS_ROUTING_H
20 #define GRPC_SRC_CORE_XDS_GRPC_XDS_ROUTING_H
21 
22 #include <grpc/support/port_platform.h>
23 #include <stddef.h>
24 
25 #include <map>
26 #include <string>
27 #include <vector>
28 
29 #include "absl/status/statusor.h"
30 #include "absl/strings/string_view.h"
31 #include "absl/types/optional.h"
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/transport/metadata_batch.h"
34 #include "src/core/xds/grpc/xds_http_filter_registry.h"
35 #include "src/core/xds/grpc/xds_listener.h"
36 #include "src/core/xds/grpc/xds_route_config.h"
37 
38 namespace grpc_core {
39 
40 class XdsRouting final {
41  public:
42   class VirtualHostListIterator {
43    public:
44     virtual ~VirtualHostListIterator() = default;
45     // Returns the number of virtual hosts in the list.
46     virtual size_t Size() const = 0;
47     // Returns the domain list for the virtual host at the specified index.
48     virtual const std::vector<std::string>& GetDomainsForVirtualHost(
49         size_t index) const = 0;
50   };
51 
52   class RouteListIterator {
53    public:
54     virtual ~RouteListIterator() = default;
55     // Number of routes.
56     virtual size_t Size() const = 0;
57     // Returns the matchers for the route at the specified index.
58     virtual const XdsRouteConfigResource::Route::Matchers& GetMatchersForRoute(
59         size_t index) const = 0;
60   };
61 
62   // Returns the index of the selected virtual host in the list.
63   static absl::optional<size_t> FindVirtualHostForDomain(
64       const VirtualHostListIterator& vhost_iterator, absl::string_view domain);
65 
66   // Returns the index in route_list_iterator to use for a request with
67   // the specified path and metadata, or nullopt if no route matches.
68   static absl::optional<size_t> GetRouteForRequest(
69       const RouteListIterator& route_list_iterator, absl::string_view path,
70       grpc_metadata_batch* initial_metadata);
71 
72   // Returns true if \a domain_pattern is a valid domain pattern, false
73   // otherwise.
74   static bool IsValidDomainPattern(absl::string_view domain_pattern);
75 
76   // Returns the metadata value(s) for the specified key.
77   // As special cases, binary headers return a value of absl::nullopt, and
78   // "content-type" header returns "application/grpc".
79   static absl::optional<absl::string_view> GetHeaderValue(
80       grpc_metadata_batch* initial_metadata, absl::string_view header_name,
81       std::string* concatenated_value);
82 
83   struct GeneratePerHttpFilterConfigsResult {
84     // Map of service config field name to list of elements for that field.
85     std::map<std::string, std::vector<std::string>> per_filter_configs;
86     ChannelArgs args;
87   };
88 
89   // Generates per-HTTP filter configs for a method config.
90   static absl::StatusOr<GeneratePerHttpFilterConfigsResult>
91   GeneratePerHTTPFilterConfigsForMethodConfig(
92       const XdsHttpFilterRegistry& http_filter_registry,
93       const std::vector<XdsListenerResource::HttpConnectionManager::HttpFilter>&
94           http_filters,
95       const XdsRouteConfigResource::VirtualHost& vhost,
96       const XdsRouteConfigResource::Route& route,
97       const XdsRouteConfigResource::Route::RouteAction::ClusterWeight*
98           cluster_weight,
99       const ChannelArgs& args);
100 
101   // Generates per-HTTP filter configs for the top-level service config.
102   static absl::StatusOr<GeneratePerHttpFilterConfigsResult>
103   GeneratePerHTTPFilterConfigsForServiceConfig(
104       const XdsHttpFilterRegistry& http_filter_registry,
105       const std::vector<XdsListenerResource::HttpConnectionManager::HttpFilter>&
106           http_filters,
107       const ChannelArgs& args);
108 };
109 
110 }  // namespace grpc_core
111 
112 #endif  // GRPC_SRC_CORE_XDS_GRPC_XDS_ROUTING_H
113