1
#pragma once
2

            
3
#include <memory>
4
#include <string>
5

            
6
#include "envoy/common/pure.h"
7
#include "envoy/config/typed_config.h"
8
#include "envoy/http/header_map.h"
9
#include "envoy/server/factory_context.h"
10
#include "envoy/upstream/upstream.h"
11

            
12
namespace Envoy {
13
namespace Http {
14

            
15
/**
16
 * Interface class for session state. Session state is used to get address of upstream host
17
 * assigned to the session.
18
 */
19
class SessionState {
20
public:
21
72
  virtual ~SessionState() = default;
22

            
23
  /**
24
   * Get address of upstream host that the current session stuck on.
25
   *
26
   * @return absl::optional<absl::string_view> optional upstream address. If there is no available
27
   * session or no available address, absl::nullopt will be returned.
28
   */
29
  virtual absl::optional<absl::string_view> upstreamAddress() const PURE;
30

            
31
  /**
32
   * Called when a request is completed to update the session state.
33
   *
34
   * @param host_address the upstream host that was finally selected.
35
   * @param headers the response headers.
36
   * @return bool true if the selected host differs from the previously stored session host.
37
   */
38
  virtual bool onUpdate(absl::string_view host_address, ResponseHeaderMap& headers) PURE;
39
};
40

            
41
using SessionStatePtr = std::unique_ptr<SessionState>;
42

            
43
/**
44
 * Interface class for creating session state from request headers.
45
 */
46
class SessionStateFactory {
47
public:
48
65
  virtual ~SessionStateFactory() = default;
49

            
50
  /**
51
   * Create session state from request headers.
52
   *
53
   * @param headers request headers.
54
   */
55
  virtual SessionStatePtr create(RequestHeaderMap& headers) const PURE;
56
};
57

            
58
using SessionStateFactorySharedPtr = std::shared_ptr<SessionStateFactory>;
59

            
60
/*
61
 * Extension configuration for session state factory.
62
 */
63
class SessionStateFactoryConfig : public Envoy::Config::TypedFactory {
64
public:
65
13
  ~SessionStateFactoryConfig() override = default;
66

            
67
  /**
68
   * Creates a particular session state factory implementation.
69
   *
70
   * @param config supplies the configuration for the session state factory extension.
71
   * @param context supplies the factory context. Please don't store the reference to
72
   * the context as it is only valid during the call.
73
   * @return SessionStateFactorySharedPtr the session state factory.
74
   */
75
  virtual SessionStateFactorySharedPtr
76
  createSessionStateFactory(const Protobuf::Message& config,
77
                            Server::Configuration::GenericFactoryContext& context) PURE;
78

            
79
35
  std::string category() const override { return "envoy.http.stateful_session"; }
80
};
81

            
82
using SessionStateFactoryConfigPtr = std::unique_ptr<SessionStateFactoryConfig>;
83

            
84
} // namespace Http
85
} // namespace Envoy