|
| 1 | +// Copyright 2016-2020 Envoy Project Authors |
| 2 | +// Copyright 2020 Google LLC |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +#include <string> |
| 17 | +#include <string_view> |
| 18 | +#include <unordered_map> |
| 19 | + |
| 20 | +#include "proxy_wasm_intrinsics.h" |
| 21 | + |
| 22 | +class ExampleRootContext : public RootContext { |
| 23 | +public: |
| 24 | + explicit ExampleRootContext(uint32_t id, std::string_view root_id) : RootContext(id, root_id) {} |
| 25 | + |
| 26 | + bool onStart(size_t) override; |
| 27 | + bool onConfigure(size_t) override; |
| 28 | + void onTick() override; |
| 29 | +}; |
| 30 | + |
| 31 | +class ExampleContext : public Context { |
| 32 | +public: |
| 33 | + explicit ExampleContext(uint32_t id, RootContext *root) : Context(id, root) {} |
| 34 | + |
| 35 | + void onCreate() override; |
| 36 | + FilterHeadersStatus onRequestHeaders(uint32_t headers, bool end_of_stream) override; |
| 37 | + FilterDataStatus onRequestBody(size_t body_buffer_length, bool end_of_stream) override; |
| 38 | + FilterHeadersStatus onResponseHeaders(uint32_t headers, bool end_of_stream) override; |
| 39 | + FilterDataStatus onResponseBody(size_t body_buffer_length, bool end_of_stream) override; |
| 40 | + void onDone() override; |
| 41 | + void onLog() override; |
| 42 | + void onDelete() override; |
| 43 | +}; |
| 44 | +static RegisterContextFactory register_ExampleContext(CONTEXT_FACTORY(ExampleContext), |
| 45 | + ROOT_FACTORY(ExampleRootContext), |
| 46 | + "my_root_id"); |
| 47 | + |
| 48 | +bool ExampleRootContext::onStart(size_t) { |
| 49 | + LOG_TRACE("onStart"); |
| 50 | + return true; |
| 51 | +} |
| 52 | + |
| 53 | +bool ExampleRootContext::onConfigure(size_t) { |
| 54 | + LOG_TRACE("onConfigure"); |
| 55 | + proxy_set_tick_period_milliseconds(1000); // 1 sec |
| 56 | + return true; |
| 57 | +} |
| 58 | + |
| 59 | +void ExampleRootContext::onTick() { LOG_TRACE("onTick"); } |
| 60 | + |
| 61 | +void ExampleContext::onCreate() { LOG_WARN(std::string("onCreate " + std::to_string(id()))); } |
| 62 | + |
| 63 | +FilterHeadersStatus ExampleContext::onRequestHeaders(uint32_t, bool) { |
| 64 | + LOG_DEBUG(std::string("onRequestHeaders ") + std::to_string(id())); |
| 65 | + auto result = getRequestHeaderPairs(); |
| 66 | + auto pairs = result->pairs(); |
| 67 | + LOG_INFO(std::string("headers: ") + std::to_string(pairs.size())); |
| 68 | + for (auto &p : pairs) { |
| 69 | + LOG_INFO(std::string(p.first) + std::string(" -> ") + std::string(p.second)); |
| 70 | + } |
| 71 | + return FilterHeadersStatus::Continue; |
| 72 | +} |
| 73 | + |
| 74 | +FilterHeadersStatus ExampleContext::onResponseHeaders(uint32_t, bool) { |
| 75 | + LOG_DEBUG(std::string("onResponseHeaders ") + std::to_string(id())); |
| 76 | + auto result = getResponseHeaderPairs(); |
| 77 | + auto pairs = result->pairs(); |
| 78 | + LOG_INFO(std::string("headers: ") + std::to_string(pairs.size())); |
| 79 | + for (auto &p : pairs) { |
| 80 | + LOG_INFO(std::string(p.first) + std::string(" -> ") + std::string(p.second)); |
| 81 | + } |
| 82 | + addResponseHeader("X-Wasm-custom", "FOO"); |
| 83 | + replaceResponseHeader("content-type", "text/plain; charset=utf-8"); |
| 84 | + removeResponseHeader("content-length"); |
| 85 | + return FilterHeadersStatus::Continue; |
| 86 | +} |
| 87 | + |
| 88 | +FilterDataStatus ExampleContext::onRequestBody(size_t body_buffer_length, |
| 89 | + bool /* end_of_stream */) { |
| 90 | + auto body = getBufferBytes(WasmBufferType::HttpRequestBody, 0, body_buffer_length); |
| 91 | + LOG_ERROR(std::string("onRequestBody ") + std::string(body->view())); |
| 92 | + return FilterDataStatus::Continue; |
| 93 | +} |
| 94 | + |
| 95 | +FilterDataStatus ExampleContext::onResponseBody(size_t /* body_buffer_length */, |
| 96 | + bool /* end_of_stream */) { |
| 97 | + setBuffer(WasmBufferType::HttpResponseBody, 0, 12, "Hello, world"); |
| 98 | + return FilterDataStatus::Continue; |
| 99 | +} |
| 100 | + |
| 101 | +void ExampleContext::onDone() { LOG_WARN(std::string("onDone " + std::to_string(id()))); } |
| 102 | + |
| 103 | +void ExampleContext::onLog() { LOG_WARN(std::string("onLog " + std::to_string(id()))); } |
| 104 | + |
| 105 | +void ExampleContext::onDelete() { LOG_WARN(std::string("onDelete " + std::to_string(id()))); } |
0 commit comments