Skip to content

Commit b891d3d

Browse files
committed
add wasm abi: injectEncodedDataToFilterChainOnHeader
1 parent b37a298 commit b891d3d

File tree

6 files changed

+89
-3
lines changed

6 files changed

+89
-3
lines changed

source/extensions/common/wasm/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ envoy_cc_extension(
9191
"//source/common/network/dns_resolver:dns_factory_util_lib",
9292
"//source/common/tracing:http_tracer_lib",
9393
"//source/extensions/common/wasm/ext:declare_property_cc_proto",
94+
"//source/extensions/common/wasm/ext:inject_encoded_data_cc_proto",
9495
"//source/extensions/common/wasm/ext:envoy_null_vm_wasm_api",
9596
"//source/extensions/filters/common/expr:context_lib",
9697
"@com_google_cel_cpp//eval/public:builtin_func_registrar",

source/extensions/common/wasm/context.cc

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,19 @@ WasmResult Context::injectEncodedDataToFilterChain(std::string_view body_text, b
19401940
}
19411941
return WasmResult::Ok;
19421942
}
1943+
1944+
WasmResult Context::injectEncodedDataToFilterChainOnHeader(std::string_view body_text,
1945+
bool end_stream) {
1946+
if (encoder_callbacks_) {
1947+
std::string body_text_copy(body_text);
1948+
encoder_callbacks_->dispatcher().post([=]() {
1949+
auto buffer = ::Envoy::Buffer::OwnedImpl(body_text_copy);
1950+
encoder_callbacks_->injectEncodedDataToFilterChain(buffer, end_stream);
1951+
});
1952+
}
1953+
return WasmResult::Ok;
1954+
}
1955+
19431956
std::string convertHealthStatusToString(Upstream::Host::Health status) {
19441957
if (status == Upstream::Host::Health::Unhealthy) {
19451958
return "Unhealthy";
@@ -1949,13 +1962,16 @@ std::string convertHealthStatusToString(Upstream::Host::Health status) {
19491962
return "Healthy";
19501963
}
19511964
}
1952-
WasmResult Context::getUpstreamHosts(StringPairs * result) {
1965+
WasmResult Context::getUpstreamHosts(StringPairs* result) {
19531966
if (decoder_callbacks_) {
19541967
auto upstream_cluster = decoder_callbacks_->clusterInfo();
19551968
if (!upstream_cluster) {
19561969
return WasmResult::Ok;
19571970
}
1958-
for (auto& p : this->clusterManager().getThreadLocalCluster(upstream_cluster->name())->prioritySet().hostSetsPerPriority()) {
1971+
for (auto& p : this->clusterManager()
1972+
.getThreadLocalCluster(upstream_cluster->name())
1973+
->prioritySet()
1974+
.hostSetsPerPriority()) {
19591975
for (auto& h : p->hosts()) {
19601976
std::map<std::string, std::string> info_map;
19611977
if (!h->getEndpointMetrics().empty()) {
@@ -1967,7 +1983,8 @@ WasmResult Context::getUpstreamHosts(StringPairs * result) {
19671983
result->push_back(std::make_pair(h->address()->asString(), j.dump(0)));
19681984
} catch (const std::exception& e) {
19691985
ENVOY_LOG(error, "getUpstreamHosts json dump failed: {}", e.what());
1970-
result->push_back(std::make_pair(h->address()->asString(), "{\"error\": \"Failed to get host info\"}"));
1986+
result->push_back(
1987+
std::make_pair(h->address()->asString(), "{\"error\": \"Failed to get host info\"}"));
19711988
}
19721989
}
19731990
}

source/extensions/common/wasm/context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ class Context : public proxy_wasm::ContextBase,
220220
std::string_view details) override;
221221
#if defined(HIGRESS)
222222
WasmResult injectEncodedDataToFilterChain(std::string_view body_text, bool end_stream) override;
223+
WasmResult injectEncodedDataToFilterChainOnHeader(std::string_view body_text, bool end_stream);
223224
WasmResult getUpstreamHosts(StringPairs * result) override;
224225
WasmResult setUpstreamOverrideHost(std::string_view address) override;
225226
#endif

source/extensions/common/wasm/ext/BUILD

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,21 @@ cc_proto_library(
8989
# "//external:protobuf_clib",
9090
],
9191
)
92+
93+
# NB: this target is compiled both to native code and to Wasm. Hence the generic rule.
94+
proto_library(
95+
name = "inject_encoded_data_proto",
96+
srcs = ["inject_encoded_data.proto"],
97+
deps = [
98+
"@com_google_protobuf//:struct_proto",
99+
],
100+
)
101+
102+
# NB: this target is compiled both to native code and to Wasm. Hence the generic rule.
103+
cc_proto_library(
104+
name = "inject_encoded_data_cc_proto",
105+
deps = [
106+
":inject_encoded_data_proto",
107+
# "//external:protobuf_clib",
108+
],
109+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
syntax = "proto3";
2+
3+
package envoy.source.extensions.common.wasm;
4+
5+
message InjectEncodedDataToFilterChainArguments {
6+
string body = 1;
7+
bool endstream = 2;
8+
};

source/extensions/common/wasm/foreign.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "source/common/common/logger.h"
22
#include "source/extensions/common/wasm/ext/declare_property.pb.h"
3+
#include "source/extensions/common/wasm/ext/inject_encoded_data.pb.h"
34
#include "source/extensions/common/wasm/wasm.h"
45

56
#if defined(WASM_USE_CEL_PARSER)
@@ -271,6 +272,46 @@ RegisterForeignFunction
271272
registerDeclarePropertyForeignFunction("declare_property",
272273
createFromClass<DeclarePropertyFactory>());
273274

275+
#if defined(HIGRESS)
276+
class InjectEncodedDataToFilterChainFactory: public Logger::Loggable<Logger::Id::wasm> {
277+
public:
278+
WasmForeignFunction create(std::shared_ptr<InjectEncodedDataToFilterChainFactory> self) const {
279+
WasmForeignFunction f = [self](WasmBase&, std::string_view arguments,
280+
const std::function<void*(size_t size)>&) -> WasmResult {
281+
envoy::source::extensions::common::wasm::InjectEncodedDataToFilterChainArguments args;
282+
if (args.ParseFromArray(arguments.data(), arguments.size())) {
283+
auto context = static_cast<Context*>(proxy_wasm::current_context_);
284+
return context->injectEncodedDataToFilterChain(args.body(), args.endstream());
285+
}
286+
return WasmResult::BadArgument;
287+
};
288+
return f;
289+
}
290+
};
291+
RegisterForeignFunction
292+
registerInjectEncodedDataToFilterChainFactory("inject_encoded_data_to_filter_chain",
293+
createFromClass<InjectEncodedDataToFilterChainFactory>());
294+
295+
class InjectEncodedDataToFilterChainOnHeaderFactory: public Logger::Loggable<Logger::Id::wasm> {
296+
public:
297+
WasmForeignFunction create(std::shared_ptr<InjectEncodedDataToFilterChainOnHeaderFactory> self) const {
298+
WasmForeignFunction f = [self](WasmBase&, std::string_view arguments,
299+
const std::function<void*(size_t size)>&) -> WasmResult {
300+
envoy::source::extensions::common::wasm::InjectEncodedDataToFilterChainArguments args;
301+
if (args.ParseFromArray(arguments.data(), arguments.size())) {
302+
auto context = static_cast<Context*>(proxy_wasm::current_context_);
303+
return context->injectEncodedDataToFilterChainOnHeader(args.body(), args.endstream());
304+
}
305+
return WasmResult::BadArgument;
306+
};
307+
return f;
308+
}
309+
};
310+
RegisterForeignFunction
311+
registerInjectEncodedDataToFilterChainOnHeaderFactory("inject_encoded_data_to_filter_chain_on_header",
312+
createFromClass<InjectEncodedDataToFilterChainOnHeaderFactory>());
313+
#endif
314+
274315
} // namespace Wasm
275316
} // namespace Common
276317
} // namespace Extensions

0 commit comments

Comments
 (0)