1
#pragma once
2

            
3
#include "source/common/protobuf/utility.h"
4
#include "source/extensions/filters/http/cache/http_cache.h"
5

            
6
#include "absl/base/thread_annotations.h"
7
#include "absl/container/flat_hash_map.h"
8
#include "absl/synchronization/mutex.h"
9

            
10
namespace Envoy {
11
namespace Extensions {
12
namespace HttpFilters {
13
namespace Cache {
14

            
15
// Example cache backend that never evicts. Not suitable for production use.
16
class SimpleHttpCache : public HttpCache, public Singleton::Instance {
17
private:
18
  struct Entry {
19
    Http::ResponseHeaderMapPtr response_headers_;
20
    ResponseMetadata metadata_;
21
    std::string body_;
22
    Http::ResponseTrailerMapPtr trailers_;
23
  };
24

            
25
  // Looks for a response that has been varied. Only called from lookup.
26
  Entry varyLookup(const LookupRequest& request,
27
                   const Http::ResponseHeaderMapPtr& response_headers);
28

            
29
  // A list of headers that we do not want to update upon validation
30
  // We skip these headers because either it's updated by other application logic
31
  // or they are fall into categories defined in the IETF doc below
32
  // https://www.ietf.org/archive/id/draft-ietf-httpbis-cache-18.html s3.2
33
  static const absl::flat_hash_set<Http::LowerCaseString> headersNotToUpdate();
34

            
35
public:
36
  // HttpCache
37
  LookupContextPtr makeLookupContext(LookupRequest&& request,
38
                                     Http::StreamFilterCallbacks& callbacks) override;
39
  InsertContextPtr makeInsertContext(LookupContextPtr&& lookup_context,
40
                                     Http::StreamFilterCallbacks& callbacks) override;
41
  void updateHeaders(const LookupContext& lookup_context,
42
                     const Http::ResponseHeaderMap& response_headers,
43
                     const ResponseMetadata& metadata, UpdateHeadersCallback on_complete) override;
44
  CacheInfo cacheInfo() const override;
45

            
46
  Entry lookup(const LookupRequest& request);
47
  bool insert(const Key& key, Http::ResponseHeaderMapPtr&& response_headers,
48
              ResponseMetadata&& metadata, std::string&& body,
49
              Http::ResponseTrailerMapPtr&& trailers);
50

            
51
  // Inserts a response that has been varied on certain headers.
52
  bool varyInsert(const Key& request_key, Http::ResponseHeaderMapPtr&& response_headers,
53
                  ResponseMetadata&& metadata, std::string&& body,
54
                  const Http::RequestHeaderMap& request_headers,
55
                  const VaryAllowList& vary_allow_list, Http::ResponseTrailerMapPtr&& trailers);
56

            
57
  absl::Mutex mutex_;
58
  absl::flat_hash_map<Key, Entry, MessageUtil, MessageUtil> map_ ABSL_GUARDED_BY(mutex_);
59
};
60

            
61
} // namespace Cache
62
} // namespace HttpFilters
63
} // namespace Extensions
64
} // namespace Envoy