1
#pragma once
2

            
3
#include "envoy/config/route/v3/route_components.pb.h"
4
#include "envoy/http/hash_policy.h"
5
#include "envoy/stream_info/filter_state.h"
6

            
7
namespace Envoy {
8

            
9
namespace Regex {
10
class Engine;
11
}
12

            
13
namespace Http {
14

            
15
/**
16
 * Implementation of HashPolicy that reads from the proto route config.
17
 */
18
class HashPolicyImpl : public HashPolicy {
19
public:
20
  static absl::StatusOr<std::unique_ptr<HashPolicyImpl>>
21
  create(absl::Span<const envoy::config::route::v3::RouteAction::HashPolicy* const> hash_policy,
22
         Regex::Engine& regex_engine);
23

            
24
  // Http::HashPolicy
25
  absl::optional<uint64_t> generateHash(OptRef<const RequestHeaderMap> headers,
26
                                        OptRef<const StreamInfo::StreamInfo> info,
27
                                        AddCookieCallback add_cookie = nullptr) const override;
28

            
29
  class HashMethod {
30
  public:
31
99
    virtual ~HashMethod() = default;
32
    virtual absl::optional<uint64_t> evaluate(OptRef<const RequestHeaderMap> headers,
33
                                              OptRef<const StreamInfo::StreamInfo> info,
34
                                              AddCookieCallback add_cookie = nullptr) const PURE;
35

            
36
    // If the method is a terminal method, ignore rest of the hash policy chain.
37
    virtual bool terminal() const PURE;
38
  };
39

            
40
  using HashMethodPtr = std::unique_ptr<HashMethod>;
41

            
42
protected:
43
  explicit HashPolicyImpl(
44
      absl::Span<const envoy::config::route::v3::RouteAction::HashPolicy* const> hash_policy,
45
      Regex::Engine& regex_engine, absl::Status& creation_status);
46

            
47
private:
48
  std::vector<HashMethodPtr> hash_impls_;
49
};
50

            
51
} // namespace Http
52
} // namespace Envoy