1
#pragma once
2

            
3
#include <cinttypes>
4
#include <string>
5
#include <vector>
6

            
7
#include "envoy/config/core/v3/base.pb.h"
8
#include "envoy/config/route/v3/route_components.pb.h"
9
#include "envoy/http/codes.h"
10
#include "envoy/upstream/resource_manager.h"
11

            
12
#include "source/common/common/matchers.h"
13
#include "source/common/common/utility.h"
14
#include "source/common/http/headers.h"
15
#include "source/common/http/utility.h"
16
#include "source/common/protobuf/utility.h"
17

            
18
#include "absl/container/flat_hash_map.h"
19
#include "absl/types/optional.h"
20

            
21
namespace Envoy {
22
namespace Router {
23

            
24
/**
25
 * Utility routines for loading route configuration and matching runtime request headers.
26
 */
27
class ConfigUtility {
28
public:
29
  // A QueryParameterMatcher specifies one "name" or "name=value" element
30
  // to match in a request's query string. It is the optimized, runtime
31
  // equivalent of the QueryParameterMatcher proto in the RDS v2 API.
32
  class QueryParameterMatcher {
33
  public:
34
    QueryParameterMatcher(const envoy::config::route::v3::QueryParameterMatcher& config,
35
                          Server::Configuration::CommonFactoryContext& context);
36

            
37
    /**
38
     * Check if the query parameters for a request contain a match for this
39
     * QueryParameterMatcher.
40
     * @param request_query_params supplies the parsed query parameters from a request.
41
     * @return bool true if a match for this QueryParameterMatcher exists in request_query_params.
42
     */
43
    bool matches(const Http::Utility::QueryParamsMulti& request_query_params) const;
44

            
45
  private:
46
    const std::string name_;
47
    const absl::optional<bool> present_match_;
48
    const absl::optional<Matchers::StringMatcherImpl> matcher_;
49
  };
50

            
51
  using QueryParameterMatcherPtr = std::unique_ptr<const QueryParameterMatcher>;
52

            
53
  // A CookieMatcher specifies match criteria for a specific cookie name parsed
54
  // from the Cookie header.
55
  class CookieMatcher {
56
  public:
57
    CookieMatcher(const envoy::config::route::v3::CookieMatcher& config,
58
                  Server::Configuration::CommonFactoryContext& context);
59

            
60
13
    const std::string& name() const { return name_; }
61

            
62
    bool matches(const absl::optional<absl::string_view>& cookie_value) const;
63

            
64
  private:
65
    const std::string name_;
66
    const bool invert_match_;
67
    const Matchers::StringMatcherImpl string_match_;
68
  };
69

            
70
  using CookieMatcherPtr = std::unique_ptr<const CookieMatcher>;
71

            
72
  /**
73
   * @return the resource priority parsed from proto.
74
   */
75
  static Upstream::ResourcePriority
76
  parsePriority(const envoy::config::core::v3::RoutingPriority& priority);
77

            
78
  /**
79
   * See if the query parameters specified in the config are present in a request.
80
   * @param query_params supplies the query parameters from the request's query string.
81
   * @param config_params supplies the list of configured query param conditions on which to match.
82
   * @return bool true if all the query params (and values) in the config_params are found in the
83
   *         query_params
84
   */
85
  static bool matchQueryParams(const Http::Utility::QueryParamsMulti& query_params,
86
                               const std::vector<QueryParameterMatcherPtr>& config_query_params);
87

            
88
  /**
89
   * See if the cookies specified in the config are present/matching in a request.
90
   * @param cookies supplies the parsed cookies from the request.
91
   * @param matchers supplies the list of configured cookie matchers on which to match.
92
   * @return bool true if all cookie matchers succeed.
93
   */
94
  static bool matchCookies(const absl::flat_hash_map<std::string, std::string>& cookies,
95
                           const std::vector<CookieMatcherPtr>& matchers);
96

            
97
  /**
98
   * Returns the redirect HTTP Status Code enum parsed from proto.
99
   * @param code supplies the RedirectResponseCode enum.
100
   * @return Returns the Http::Code version of the RedirectResponseCode.
101
   */
102
  static Http::Code parseRedirectResponseCode(
103
      const envoy::config::route::v3::RedirectAction::RedirectResponseCode& code);
104

            
105
  /**
106
   * Returns the HTTP Status Code enum parsed from the route's redirect or direct_response.
107
   * @param route supplies the Route configuration.
108
   * @return absl::optional<Http::Code> the HTTP status from the route's direct_response if
109
   * specified, or the HTTP status code from the route's redirect if specified, or an empty
110
   * absl::optional otherwise.
111
   */
112
  static absl::optional<Http::Code>
113
  parseDirectResponseCode(const envoy::config::route::v3::Route& route);
114

            
115
  /**
116
   * Returns the HTTP Status Code enum parsed from proto.
117
   * @param code supplies the ClusterNotFoundResponseCode enum.
118
   * @return Returns the Http::Code version of the ClusterNotFoundResponseCode enum.
119
   */
120
  static Http::Code parseClusterNotFoundResponseCode(
121
      const envoy::config::route::v3::RouteAction::ClusterNotFoundResponseCode& code);
122
};
123

            
124
void mergeTransforms(Http::HeaderTransforms& dest, const Http::HeaderTransforms& src);
125

            
126
} // namespace Router
127
} // namespace Envoy