forked from solo-io/wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.cc
More file actions
76 lines (58 loc) · 2.73 KB
/
filter.cc
File metadata and controls
76 lines (58 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// NOLINT(namespace-envoy)
#include <string>
#include <unordered_map>
#include "google/protobuf/util/json_util.h"
#include "proxy_wasm_intrinsics.h"
#include "filter.pb.h"
class AddHeaderRootContext : public RootContext {
public:
explicit AddHeaderRootContext(uint32_t id, StringView root_id) : RootContext(id, root_id) {}
bool onConfigure(size_t /* configuration_size */) override;
bool onStart(size_t) override;
std::string header_value_;
};
class AddHeaderContext : public Context {
public:
explicit AddHeaderContext(uint32_t id, RootContext* root) : Context(id, root), root_(static_cast<AddHeaderRootContext*>(static_cast<void*>(root))) {}
void onCreate() override;
FilterHeadersStatus onRequestHeaders(uint32_t headers) override;
FilterDataStatus onRequestBody(size_t body_buffer_length, bool end_of_stream) override;
FilterHeadersStatus onResponseHeaders(uint32_t headers) override;
void onDone() override;
void onLog() override;
void onDelete() override;
private:
AddHeaderRootContext* root_;
};
static RegisterContextFactory register_AddHeaderContext(CONTEXT_FACTORY(AddHeaderContext),
ROOT_FACTORY(AddHeaderRootContext),
"add_header_root_id");
bool AddHeaderRootContext::onConfigure(size_t) {
auto conf = getConfiguration();
Config config;
google::protobuf::util::JsonParseOptions options;
options.case_insensitive_enum_parsing = true;
options.ignore_unknown_fields = false;
google::protobuf::util::JsonStringToMessage(conf->toString(), &config, options);
LOG_DEBUG("onConfigure " + config.value());
header_value_ = config.value();
return true;
}
bool AddHeaderRootContext::onStart(size_t) { LOG_DEBUG("onStart"); return true;}
void AddHeaderContext::onCreate() { LOG_DEBUG(std::string("onCreate " + std::to_string(id()))); }
FilterHeadersStatus AddHeaderContext::onRequestHeaders(uint32_t) {
LOG_DEBUG(std::string("onRequestHeaders ") + std::to_string(id()));
return FilterHeadersStatus::Continue;
}
FilterHeadersStatus AddHeaderContext::onResponseHeaders(uint32_t) {
LOG_DEBUG(std::string("onResponseHeaders ") + std::to_string(id()));
addResponseHeader("newheader", root_->header_value_);
replaceResponseHeader("location", "envoy-wasm");
return FilterHeadersStatus::Continue;
}
FilterDataStatus AddHeaderContext::onRequestBody(size_t body_buffer_length, bool end_of_stream) {
return FilterDataStatus::Continue;
}
void AddHeaderContext::onDone() { LOG_DEBUG(std::string("onDone " + std::to_string(id()))); }
void AddHeaderContext::onLog() { LOG_DEBUG(std::string("onLog " + std::to_string(id()))); }
void AddHeaderContext::onDelete() { LOG_DEBUG(std::string("onDelete " + std::to_string(id()))); }