-
Notifications
You must be signed in to change notification settings - Fork 68
/
http_wasm_example.cc
103 lines (86 loc) · 3.96 KB
/
http_wasm_example.cc
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2016-2020 Envoy Project Authors
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string>
#include <string_view>
#include "proxy_wasm_intrinsics.h"
class ExampleRootContext : public RootContext {
public:
explicit ExampleRootContext(uint32_t id, std::string_view root_id) : RootContext(id, root_id) {}
bool onStart(size_t) override;
bool onConfigure(size_t) override;
void onTick() override;
};
class ExampleContext : public Context {
public:
explicit ExampleContext(uint32_t id, RootContext *root) : Context(id, root) {}
void onCreate() override;
FilterHeadersStatus onRequestHeaders(uint32_t headers, bool end_of_stream) override;
FilterDataStatus onRequestBody(size_t body_buffer_length, bool end_of_stream) override;
FilterHeadersStatus onResponseHeaders(uint32_t headers, bool end_of_stream) override;
FilterDataStatus onResponseBody(size_t body_buffer_length, bool end_of_stream) override;
void onDone() override;
void onLog() override;
void onDelete() override;
};
static RegisterContextFactory register_ExampleContext(CONTEXT_FACTORY(ExampleContext),
ROOT_FACTORY(ExampleRootContext));
bool ExampleRootContext::onStart(size_t) {
LOG_TRACE("onStart");
return true;
}
bool ExampleRootContext::onConfigure(size_t) {
LOG_TRACE("onConfigure");
proxy_set_tick_period_milliseconds(1000); // 1 sec
return true;
}
void ExampleRootContext::onTick() { LOG_TRACE("onTick"); }
void ExampleContext::onCreate() { LOG_WARN(std::string("onCreate " + std::to_string(id()))); }
FilterHeadersStatus ExampleContext::onRequestHeaders(uint32_t, bool) {
LOG_DEBUG(std::string("onRequestHeaders ") + std::to_string(id()));
auto result = getRequestHeaderPairs();
auto pairs = result->pairs();
LOG_INFO(std::string("headers: ") + std::to_string(pairs.size()));
for (auto &p : pairs) {
LOG_INFO(std::string(p.first) + std::string(" -> ") + std::string(p.second));
}
return FilterHeadersStatus::Continue;
}
FilterHeadersStatus ExampleContext::onResponseHeaders(uint32_t, bool) {
LOG_DEBUG(std::string("onResponseHeaders ") + std::to_string(id()));
auto result = getResponseHeaderPairs();
auto pairs = result->pairs();
LOG_INFO(std::string("headers: ") + std::to_string(pairs.size()));
for (auto &p : pairs) {
LOG_INFO(std::string(p.first) + std::string(" -> ") + std::string(p.second));
}
addResponseHeader("X-Wasm-custom", "FOO");
replaceResponseHeader("content-type", "text/plain; charset=utf-8");
removeResponseHeader("content-length");
return FilterHeadersStatus::Continue;
}
FilterDataStatus ExampleContext::onRequestBody(size_t body_buffer_length,
bool /* end_of_stream */) {
auto body = getBufferBytes(WasmBufferType::HttpRequestBody, 0, body_buffer_length);
LOG_ERROR(std::string("onRequestBody ") + std::string(body->view()));
return FilterDataStatus::Continue;
}
FilterDataStatus ExampleContext::onResponseBody(size_t /* body_buffer_length */,
bool /* end_of_stream */) {
setBuffer(WasmBufferType::HttpResponseBody, 0, 12, "Hello, world");
return FilterDataStatus::Continue;
}
void ExampleContext::onDone() { LOG_WARN(std::string("onDone " + std::to_string(id()))); }
void ExampleContext::onLog() { LOG_WARN(std::string("onLog " + std::to_string(id()))); }
void ExampleContext::onDelete() { LOG_WARN(std::string("onDelete " + std::to_string(id()))); }