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_EXT_XDS_XDS_ROUTING_H
20 #define GRPC_SRC_CORE_EXT_XDS_XDS_ROUTING_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <stddef.h>
25 
26 #include <map>
27 #include <string>
28 #include <vector>
29 
30 #include "absl/status/statusor.h"
31 #include "absl/strings/string_view.h"
32 #include "absl/types/optional.h"
33 
34 #include "src/core/ext/xds/xds_http_filters.h"
35 #include "src/core/ext/xds/xds_listener.h"
36 #include "src/core/ext/xds/xds_route_config.h"
37 #include "src/core/lib/channel/channel_args.h"
38 #include "src/core/lib/transport/metadata_batch.h"
39 
40 namespace grpc_core {
41 
42 class XdsRouting {
43  public:
44   class VirtualHostListIterator {
45    public:
46     virtual ~VirtualHostListIterator() = default;
47     // Returns the number of virtual hosts in the list.
48     virtual size_t Size() const = 0;
49     // Returns the domain list for the virtual host at the specified index.
50     virtual const std::vector<std::string>& GetDomainsForVirtualHost(
51         size_t index) const = 0;
52   };
53 
54   class RouteListIterator {
55    public:
56     virtual ~RouteListIterator() = default;
57     // Number of routes.
58     virtual size_t Size() const = 0;
59     // Returns the matchers for the route at the specified index.
60     virtual const XdsRouteConfigResource::Route::Matchers& GetMatchersForRoute(
61         size_t index) const = 0;
62   };
63 
64   // Returns the index of the selected virtual host in the list.
65   static absl::optional<size_t> FindVirtualHostForDomain(
66       const VirtualHostListIterator& vhost_iterator, absl::string_view domain);
67 
68   // Returns the index in route_list_iterator to use for a request with
69   // the specified path and metadata, or nullopt if no route matches.
70   static absl::optional<size_t> GetRouteForRequest(
71       const RouteListIterator& route_list_iterator, absl::string_view path,
72       grpc_metadata_batch* initial_metadata);
73 
74   // Returns true if \a domain_pattern is a valid domain pattern, false
75   // otherwise.
76   static bool IsValidDomainPattern(absl::string_view domain_pattern);
77 
78   // Returns the metadata value(s) for the specified key.
79   // As special cases, binary headers return a value of absl::nullopt, and
80   // "content-type" header returns "application/grpc".
81   static absl::optional<absl::string_view> GetHeaderValue(
82       grpc_metadata_batch* initial_metadata, absl::string_view header_name,
83       std::string* concatenated_value);
84 
85   struct GeneratePerHttpFilterConfigsResult {
86     // Map of service config field name to list of elements for that field.
87     std::map<std::string, std::vector<std::string>> per_filter_configs;
88     ChannelArgs args;
89   };
90 
91   // Generates a map of per_filter_configs. \a args is consumed.
92   static absl::StatusOr<GeneratePerHttpFilterConfigsResult>
93   GeneratePerHTTPFilterConfigs(
94       const XdsHttpFilterRegistry& http_filter_registry,
95       const std::vector<XdsListenerResource::HttpConnectionManager::HttpFilter>&
96           http_filters,
97       const XdsRouteConfigResource::VirtualHost& vhost,
98       const XdsRouteConfigResource::Route& route,
99       const XdsRouteConfigResource::Route::RouteAction::ClusterWeight*
100           cluster_weight,
101       const ChannelArgs& args);
102 };
103 
104 }  // namespace grpc_core
105 
106 #endif  // GRPC_SRC_CORE_EXT_XDS_XDS_ROUTING_H
107