|
| 1 | +// NOLINT(namespace-envoy) |
| 2 | +#include <string> |
| 3 | +#include <string_view> |
| 4 | +#include <unordered_map> |
| 5 | + |
| 6 | +#include "proxy_wasm_intrinsics.h" |
| 7 | + |
| 8 | +class ExampleRootContext : public RootContext { |
| 9 | +public: |
| 10 | + explicit ExampleRootContext(uint32_t id, std::string_view root_id) : RootContext(id, root_id) {} |
| 11 | + |
| 12 | + bool onStart(size_t) override; |
| 13 | + bool onConfigure(size_t) override; |
| 14 | + void onTick() override; |
| 15 | +}; |
| 16 | + |
| 17 | +class ExampleContext : public Context { |
| 18 | +public: |
| 19 | + explicit ExampleContext(uint32_t id, RootContext* root) : Context(id, root) {} |
| 20 | + |
| 21 | + void onCreate() override; |
| 22 | + FilterHeadersStatus onRequestHeaders(uint32_t headers, bool end_of_stream) override; |
| 23 | + FilterDataStatus onRequestBody(size_t body_buffer_length, bool end_of_stream) override; |
| 24 | + FilterHeadersStatus onResponseHeaders(uint32_t headers, bool end_of_stream) override; |
| 25 | + FilterDataStatus onResponseBody(size_t body_buffer_length, bool end_of_stream) override; |
| 26 | + void onDone() override; |
| 27 | + void onLog() override; |
| 28 | + void onDelete() override; |
| 29 | +}; |
| 30 | +static RegisterContextFactory register_ExampleContext(CONTEXT_FACTORY(ExampleContext), |
| 31 | + ROOT_FACTORY(ExampleRootContext), |
| 32 | + "my_root_id"); |
| 33 | + |
| 34 | +bool ExampleRootContext::onStart(size_t) { |
| 35 | + LOG_TRACE("onStart"); |
| 36 | + return true; |
| 37 | +} |
| 38 | + |
| 39 | +bool ExampleRootContext::onConfigure(size_t) { |
| 40 | + LOG_TRACE("onConfigure"); |
| 41 | + proxy_set_tick_period_milliseconds(1000); // 1 sec |
| 42 | + return true; |
| 43 | +} |
| 44 | + |
| 45 | +void ExampleRootContext::onTick() { LOG_TRACE("onTick"); } |
| 46 | + |
| 47 | +void ExampleContext::onCreate() { LOG_WARN(std::string("onCreate " + std::to_string(id()))); } |
| 48 | + |
| 49 | +FilterHeadersStatus ExampleContext::onRequestHeaders(uint32_t, bool) { |
| 50 | + LOG_DEBUG(std::string("onRequestHeaders ") + std::to_string(id())); |
| 51 | + auto result = getRequestHeaderPairs(); |
| 52 | + auto pairs = result->pairs(); |
| 53 | + LOG_INFO(std::string("headers: ") + std::to_string(pairs.size())); |
| 54 | + for (auto& p : pairs) { |
| 55 | + LOG_INFO(std::string(p.first) + std::string(" -> ") + std::string(p.second)); |
| 56 | + } |
| 57 | + return FilterHeadersStatus::Continue; |
| 58 | +} |
| 59 | + |
| 60 | +FilterHeadersStatus ExampleContext::onResponseHeaders(uint32_t, bool) { |
| 61 | + LOG_DEBUG(std::string("onResponseHeaders ") + std::to_string(id())); |
| 62 | + auto result = getResponseHeaderPairs(); |
| 63 | + auto pairs = result->pairs(); |
| 64 | + LOG_INFO(std::string("headers: ") + std::to_string(pairs.size())); |
| 65 | + for (auto& p : pairs) { |
| 66 | + LOG_INFO(std::string(p.first) + std::string(" -> ") + std::string(p.second)); |
| 67 | + } |
| 68 | + addResponseHeader("X-Wasm-custom", "FOO"); |
| 69 | + replaceResponseHeader("content-type", "text/plain; charset=utf-8"); |
| 70 | + removeResponseHeader("content-length"); |
| 71 | + return FilterHeadersStatus::Continue; |
| 72 | +} |
| 73 | + |
| 74 | +FilterDataStatus ExampleContext::onRequestBody(size_t body_buffer_length, |
| 75 | + bool /* end_of_stream */) { |
| 76 | + auto body = getBufferBytes(WasmBufferType::HttpRequestBody, 0, body_buffer_length); |
| 77 | + LOG_ERROR(std::string("onRequestBody ") + std::string(body->view())); |
| 78 | + return FilterDataStatus::Continue; |
| 79 | +} |
| 80 | + |
| 81 | +FilterDataStatus ExampleContext::onResponseBody(size_t body_buffer_length, |
| 82 | + bool /* end_of_stream */) { |
| 83 | + setBuffer(WasmBufferType::HttpResponseBody, 0, body_buffer_length, "Hello, world\n"); |
| 84 | + return FilterDataStatus::Continue; |
| 85 | +} |
| 86 | + |
| 87 | +void ExampleContext::onDone() { LOG_WARN(std::string("onDone " + std::to_string(id()))); } |
| 88 | + |
| 89 | +void ExampleContext::onLog() { LOG_WARN(std::string("onLog " + std::to_string(id()))); } |
| 90 | + |
| 91 | +void ExampleContext::onDelete() { LOG_WARN(std::string("onDelete " + std::to_string(id()))); } |
0 commit comments