Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 0 additions & 68 deletions api/wasm/cpp/envoy_wasm_intrinsics.cc

This file was deleted.

110 changes: 110 additions & 0 deletions api/wasm/cpp/proxy_wasm_intrinsics.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// NOLINT(namespace-envoy)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the "NOLINT" used by build system?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

./tools/check_format.py will complain if the namespace is missing, but since this is for the API, it is essentially external code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could exclude api/wasm from that check in ./tools/check_format.py's checkNamespace.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

#include "proxy_wasm_intrinsics.h"

static std::unordered_map<int32_t, std::unique_ptr<Context>> context_map;

static Context* ensureContext(uint32_t context_id) {
auto e = context_map.insert(std::make_pair(context_id, nullptr));
if (e.second)
e.first->second = Context::New(context_id);
return e.first->second.get();
}

static Context* getContext(uint32_t context_id) {
auto it = context_map.find(context_id);
if (it == context_map.end())
return nullptr;
return it->second.get();
}

extern "C" EMSCRIPTEN_KEEPALIVE void proxy_onConfigure(uint32_t ptr, uint32_t size) {
ensureContext(0)->onConfigure(std::make_unique<WasmData>(reinterpret_cast<char*>(ptr), size));
}

extern "C" EMSCRIPTEN_KEEPALIVE void proxy_onStart() { ensureContext(0)->onStart(); }

extern "C" EMSCRIPTEN_KEEPALIVE void proxy_onCreate(uint32_t context_id) {
ensureContext(context_id)->onCreate();
}

extern "C" EMSCRIPTEN_KEEPALIVE FilterHeadersStatus proxy_onRequestHeaders(uint32_t context_id) {
auto c = getContext(context_id);
if (!c)
return FilterHeadersStatus::Continue;
return c->onRequestHeaders();
}

extern "C" EMSCRIPTEN_KEEPALIVE FilterDataStatus proxy_onRequestBody(uint32_t context_id,
uint32_t body_buffer_length,
uint32_t end_of_stream) {
auto c = getContext(context_id);
if (!c)
return FilterDataStatus::Continue;
return c->onRequestBody(static_cast<size_t>(body_buffer_length), end_of_stream != 0);
}

extern "C" EMSCRIPTEN_KEEPALIVE FilterTrailersStatus proxy_onRequestTrailers(uint32_t context_id) {
auto c = getContext(context_id);
if (!c)
return FilterTrailersStatus::Continue;
return c->onRequestTrailers();
}

extern "C" EMSCRIPTEN_KEEPALIVE FilterHeadersStatus proxy_onResponseHeaders(uint32_t context_id) {
auto c = getContext(context_id);
if (!c)
return FilterHeadersStatus::Continue;
return c->onResponseHeaders();
}

extern "C" EMSCRIPTEN_KEEPALIVE FilterDataStatus proxy_onResponseBody(uint32_t context_id,
uint32_t body_buffer_length,
uint32_t end_of_stream) {
auto c = getContext(context_id);
if (!c)
return FilterDataStatus::Continue;
return c->onResponseBody(static_cast<size_t>(body_buffer_length), end_of_stream != 0);
}

extern "C" EMSCRIPTEN_KEEPALIVE FilterTrailersStatus proxy_onResponseTrailers(uint32_t context_id) {
auto c = getContext(context_id);
if (!c)
return FilterTrailersStatus::Continue;
return c->onResponseTrailers();
}

extern "C" EMSCRIPTEN_KEEPALIVE void
proxy_onHttpCallResponse(uint32_t context_id, uint32_t token, uint32_t header_pairs_ptr,
uint32_t header_pairs_size, uint32_t body_ptr, uint32_t body_size,
uint32_t trailer_pairs_ptr, uint32_t trailer_pairs_size) {
auto c = getContext(context_id);
if (!c)
return;
c->onHttpCallResponse(
token,
std::make_unique<WasmData>(reinterpret_cast<char*>(header_pairs_ptr), header_pairs_size),
std::make_unique<WasmData>(reinterpret_cast<char*>(body_ptr), body_size),
std::make_unique<WasmData>(reinterpret_cast<char*>(trailer_pairs_ptr), trailer_pairs_size));
}

extern "C" EMSCRIPTEN_KEEPALIVE void proxy_onDone(uint32_t context_id) {
auto c = getContext(context_id);
if (!c)
return;
c->onDone();
}

extern "C" EMSCRIPTEN_KEEPALIVE void proxy_onLog(uint32_t context_id) {
auto c = getContext(context_id);
if (!c)
return;
c->onLog();
}

extern "C" EMSCRIPTEN_KEEPALIVE void proxy_onDelete(uint32_t context_id) {
auto c = getContext(context_id);
if (!c)
return;
c->onDelete();
context_map.erase(context_id);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Intrinsic functions available to WASM modules.
*/
// NOLINT(namespace-envoy)
#include <string>
#include <string_view>
#include <unordered_map>
Expand All @@ -18,6 +19,7 @@
extern "C" EMSCRIPTEN_KEEPALIVE void proxy_onStart();
extern "C" EMSCRIPTEN_KEEPALIVE int main(); // only called if proxy_onStart() is not available.
extern "C" EMSCRIPTEN_KEEPALIVE void proxy_onTick();
extern "C" ENSCRIPTEN_KEEPALIVE void proxy_onCreate(uint32_t context_id);
extern "C" ENSCRIPTEN_KEEPALIVE void proxy_onRequestHeaders(uint32_t context_id);
extern "C" ENSCRIPTEN_KEEPALIVE void proxy_onRequestBody(uint32_t context_id, uint32_t
body_buffer_length, uint32_t end_of_stream size); extern "C" ENSCRIPTEN_KEEPALIVE void
Expand All @@ -34,6 +36,8 @@
extern "C" ENSCRIPTEN_KEEPALIVE void proxy_onDone(uint32_t context_id);
// onLog occurs after onDone.
extern "C" ENSCRIPTEN_KEEPALIVE void proxy_onLog(uint32_t context_id);
// The Context in the proxy has been destroyed and no further calls will be coming.
extern "C" ENSCRIPTEN_KEEPALIVE void proxy_onDelete(uint32_t context_id);
*/

enum class LogLevel : int { trace, debug, info, warn, error, critical };
Expand Down Expand Up @@ -209,6 +213,7 @@ class Context {
virtual void onStart() {}

// Called on individual requests/response streams.
virtual void onCreate() {}
virtual FilterHeadersStatus onRequestHeaders() { return FilterHeadersStatus::Continue; }
virtual FilterDataStatus onRequestBody(size_t body_buffer_length, bool end_of_stream) {
return FilterDataStatus::Continue;
Expand All @@ -221,6 +226,7 @@ class Context {
virtual FilterTrailersStatus onResponseTrailers() { return FilterTrailersStatus::Continue; }
virtual void onDone() {}
virtual void onLog() {}
virtual void onDelete() {}

virtual void onHttpCallResponse(uint32_t token, std::unique_ptr<WasmData> header_pairs,
std::unique_ptr<WasmData> body,
Expand Down
4 changes: 2 additions & 2 deletions examples/wasm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
OPT=
API=../../api/wasm/cpp

envoy_filter_http_wasm_example.wasm: envoy_filter_http_wasm_example.cc ${API}/envoy_wasm_intrinsics.cc ${API}/envoy_wasm_intrinsics.h ${API}/envoy_wasm_intrinsics.js
em++ -s WASM=1 --std=c++14 $(OPT) -g3 -I${API} --js-library ${API}/envoy_wasm_intrinsics.js envoy_filter_http_wasm_example.cc ${API}/envoy_wasm_intrinsics.cc -o envoy_filter_http_wasm_example.js
envoy_filter_http_wasm_example.wasm: envoy_filter_http_wasm_example.cc ${API}/proxy_wasm_intrinsics.cc ${API}/proxy_wasm_intrinsics.h ${API}/proxy_wasm_intrinsics.js
em++ -s WASM=1 --std=c++14 $(OPT) -g3 -I${API} --js-library ${API}/proxy_wasm_intrinsics.js envoy_filter_http_wasm_example.cc ${API}/proxy_wasm_intrinsics.cc -o envoy_filter_http_wasm_example.js
18 changes: 13 additions & 5 deletions examples/wasm/envoy_filter_http_wasm_example.cc
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
// NOLINT(namespace-envoy)
#include <string>
#include <unordered_map>

#include "envoy_wasm_intrinsics.h"
#include "proxy_wasm_intrinsics.h"

class ExampleContext : public Context {
public:
explicit ExampleContext(uint32_t id) : Context(id) {}

void onStart() override;

void onCreate() override;
FilterHeadersStatus onRequestHeaders() override;
FilterDataStatus onRequestBody(size_t body_buffer_length, bool end_of_stream) override;
FilterHeadersStatus onResponseHeaders() override;
void onLog() override;
void onDone() override;
void onLog() override;
void onDelete() override;
};

std::unique_ptr<Context> Context::New(uint32_t id) {
Expand All @@ -21,8 +25,10 @@ std::unique_ptr<Context> Context::New(uint32_t id) {

void ExampleContext::onStart() { logTrace("onStart"); }

void ExampleContext::onCreate() { logWarn(std::string("onCreate " + std::to_string(id()))); }

FilterHeadersStatus ExampleContext::onRequestHeaders() {
logDebug(std::string("onRequestHaders ") + std::to_string(id()));
logDebug(std::string("onRequestHeaders ") + std::to_string(id()));
auto result = getRequestHeaderPairs();
auto pairs = result->pairs();
logInfo(std::string("headers: ") + std::to_string(pairs.size()));
Expand All @@ -33,7 +39,7 @@ FilterHeadersStatus ExampleContext::onRequestHeaders() {
}

FilterHeadersStatus ExampleContext::onResponseHeaders() {
logDebug(std::string("onResponseHaders ") + std::to_string(id()));
logDebug(std::string("onResponseHeaders ") + std::to_string(id()));
auto result = getResponseHeaderPairs();
auto pairs = result->pairs();
logInfo(std::string("headers: ") + std::to_string(pairs.size()));
Expand All @@ -51,6 +57,8 @@ FilterDataStatus ExampleContext::onRequestBody(size_t body_buffer_length, bool e
return FilterDataStatus::Continue;
}

void ExampleContext::onDone() { logWarn(std::string("onDone " + std::to_string(id()))); }

void ExampleContext::onLog() { logWarn(std::string("onLog " + std::to_string(id()))); }

void ExampleContext::onDone() { logWarn(std::string("onDone " + std::to_string(id()))); }
void ExampleContext::onDelete() { logWarn(std::string("onDelete " + std::to_string(id()))); }
Binary file modified examples/wasm/envoy_filter_http_wasm_example.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion examples/wasm/envoy_wasm_test.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// NOLINT(namespace-envoy)
#include <string>

#include "envoy_wasm_intrinsics.h"
#include "proxy_wasm_intrinsics.h"

extern "C" EMSCRIPTEN_KEEPALIVE void proxy_onConfigure(char* configuration, int size) {
logWarn(std::string("warn " + std::string(configuration, size)));
Expand Down
Loading