Skip to content

Commit 010f202

Browse files
committed
wasm: Add hello world
Signed-off-by: Ryan Northey <ryan@synca.io>
1 parent 9528fa8 commit 010f202

5 files changed

Lines changed: 141 additions & 0 deletions

File tree

WORKSPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
workspace(name = "envoy-modules")

wasm/http-hello-world/BUILD

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load("@bazel_skylib//lib:selects.bzl", "selects")
2+
load("@envoy//bazel/wasm:wasm.bzl", "envoy_wasm_cc_binary")
3+
4+
licenses(["notice"]) # Apache 2
5+
6+
exports_files(["module.rst", "module.yaml"])
7+
8+
envoy_wasm_cc_binary(
9+
name = "hello.wasm",
10+
srcs = ["hello.cc"],
11+
)
12+
13+
filegroup(
14+
name = "files",
15+
srcs = glob(["**/*"], exclude = ["module.rst", "module.yaml", "BUILD"]),
16+
visibility = ["//visibility:public"],
17+
)
18+
19+
alias(
20+
name = "module",
21+
actual = ":hello.wasm",
22+
visibility = ["//visibility:public"],
23+
)

wasm/http-hello-world/hello.cc

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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()))); }

wasm/http-hello-world/module.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Download
2+
--------
3+
4+
5+
Running
6+
-------
7+
8+
9+
Configuration
10+
-------------
11+
12+
13+
Building
14+
--------

wasm/http-hello-world/module.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: wasm-hello-world
2+
title: Hello world (Wasm)
3+
description: |
4+
Wasm Hello world HTTP filter
5+
type: wasm
6+
language: c++
7+
labels:
8+
- wasm
9+
- http
10+
- hello
11+
- world
12+
- c++

0 commit comments

Comments
 (0)