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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.dylibso.chicory.wasm.types.MemoryLimits;
import com.dylibso.chicory.wasm.types.ValueType;
import java.io.Closeable;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -24,8 +23,6 @@ public final class ProxyWasm implements Closeable {

private final ABI abi;
private final Handler pluginHandler;
private final byte[] pluginConfig;
private final byte[] vmConfig;
private final WasiPreview1 wasi;

private final AtomicInteger nextContextID = new AtomicInteger(1);
Expand All @@ -38,8 +35,6 @@ public final class ProxyWasm implements Closeable {
private byte[] httpCallResponseBody;

private ProxyWasm(Builder other) throws StartException {
this.vmConfig = other.vmConfig;
this.pluginConfig = other.pluginConfig;
this.pluginHandler = Objects.requireNonNullElse(other.pluginHandler, new Handler() {});
this.wasi = other.wasi;
this.abi = other.abi;
Expand All @@ -64,10 +59,14 @@ public void start() throws StartException {

this.pluginContext = new PluginContext(this, pluginHandler);
registerContext(pluginContext, 0);
if (!this.abi.proxyOnVmStart(pluginContext.id(), vmConfig.length)) {

byte[] vmConfig = this.pluginHandler.getVmConfig();
if (!this.abi.proxyOnVmStart(pluginContext.id(), len(vmConfig))) {
throw new StartException("proxy_on_vm_start failed");
}
if (!this.abi.proxyOnConfigure(pluginContext.id(), pluginConfig.length)) {

byte[] pluginConfig = this.pluginHandler.getPluginConfig();
if (!this.abi.proxyOnConfigure(pluginContext.id(), len(pluginConfig))) {
throw new StartException("proxy_on_configure failed");
}
}
Expand All @@ -87,16 +86,6 @@ protected Handler next() {
return activeContext.handler();
}

@Override
public byte[] getVmConfig() {
return vmConfig;
}

@Override
public byte[] getPluginConfig() {
return pluginConfig;
}

@Override
public WasmResult setEffectiveContextID(int contextID) {
Context context = contexts.get(contextID);
Expand Down Expand Up @@ -225,8 +214,6 @@ public static class Builder implements Cloneable {
private final ABI abi = new ABI();
private WasiPreview1 wasi;

private byte[] vmConfig = new byte[0];
private byte[] pluginConfig = new byte[0];
private Handler pluginHandler;
private ImportMemory memory;
private WasiOptions wasiOptions;
Expand All @@ -251,26 +238,6 @@ public Builder withStart(boolean start) {
return this;
}

public ProxyWasm.Builder withVmConfig(byte[] vmConfig) {
this.vmConfig = vmConfig;
return this;
}

public ProxyWasm.Builder withVmConfig(String vmConfig) {
this.vmConfig = vmConfig.getBytes(StandardCharsets.UTF_8);
return this;
}

public ProxyWasm.Builder withPluginConfig(byte[] pluginConfig) {
this.pluginConfig = pluginConfig;
return this;
}

public ProxyWasm.Builder withPluginConfig(String pluginConfig) {
this.pluginConfig = pluginConfig.getBytes(StandardCharsets.UTF_8);
return this;
}

public ProxyWasm.Builder withPluginHandler(Handler vmHandler) {
this.pluginHandler = vmHandler;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public class EchoHttpBodyTest {
@BeforeEach
void setUp() throws StartException {
this.handler = new MockHandler();
ProxyWasm.Builder builder = ProxyWasm.builder();
builder.withPluginConfig("echo");
this.proxyWasm = builder.build(module);
handler.setPluginConfig("echo");

this.proxyWasm = ProxyWasm.builder().withPluginHandler(handler).build(module);
this.httpContext = proxyWasm.createHttpContext(handler);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public void onHttpResponseHeaders() throws StartException {
var config =
String.format(
"{\"header\": \"%s\", \"value\": \"%s\"}", "x-wasm-header", "x-value");
try (var proxyWasm = ProxyWasm.builder().withPluginConfig(config).build(module)) {
handler.setPluginConfig(config);
try (var proxyWasm = ProxyWasm.builder().withPluginHandler(handler).build(module)) {
int id = 0;
try (var host = proxyWasm.createHttpContext(handler)) {
id = host.id();
Expand All @@ -82,6 +83,8 @@ public void onHttpResponseHeaders() throws StartException {
// Check logs
handler.assertSortedLogsEqual(
String.format("%d finished", id),
"loading plugin config",
"header from config: x-wasm-header = x-value",
"response header <-- key2: value2",
"response header <-- key1: value1",
"adding header: x-wasm-header=x-value",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class HttpRoutingTest {
@Test
public void canary() throws StartException {
var handler = new MockHandler();
ProxyWasm.Builder builder =
ProxyWasm.builder().withPluginHandler(handler).withPluginConfig(new byte[] {2});
handler.setPluginConfig(new byte[] {2});
ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler);
try (var host = builder.build(module)) {
try (var context = host.createHttpContext(handler)) {

Expand All @@ -47,9 +47,9 @@ public void canary() throws StartException {
@Test
public void nonCanary() throws StartException {
var handler = new MockHandler();
handler.setPluginConfig(new byte[] {1});
var module = Parser.parse(Path.of("./src/test/go-examples/http_routing/main.wasm"));
ProxyWasm.Builder builder =
ProxyWasm.builder().withPluginHandler(handler).withPluginConfig(new byte[] {1});
ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler);
try (var host = builder.build(module)) {
try (var context = host.createHttpContext(handler)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ class OnHttpRequestBody {
@BeforeEach
void setUp() throws StartException {
var config = "{\"requiredKeys\": [\"my_key\"]}";
this.host = ProxyWasm.builder().withPluginConfig(config).build(module);
handler.setPluginConfig(config);
this.host = ProxyWasm.builder().withPluginHandler(handler).build(module);
this.context = host.createHttpContext(handler);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.roastedroot.proxywasm.StreamType;
import io.roastedroot.proxywasm.WasmException;
import io.roastedroot.proxywasm.WasmResult;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -67,6 +68,8 @@ public HttpResponse(
private byte[] downStreamData = new byte[0];
private byte[] upstreamData = new byte[0];
private byte[] grpcReceiveBuffer = new byte[0];
private byte[] vmConfig;
private byte[] pluginConfig;

static final boolean DEBUG = "true".equals(System.getenv("DEBUG"));

Expand Down Expand Up @@ -119,6 +122,32 @@ public void assertLogsDoNotContain(String... message) {
}
}

@Override
public byte[] getVmConfig() {
return vmConfig;
}

public void setVmConfig(byte[] vmConfig) {
this.vmConfig = vmConfig;
}

public void setVmConfig(String vmConfig) {
this.vmConfig = vmConfig.getBytes(StandardCharsets.UTF_8);
}

@Override
public byte[] getPluginConfig() {
return pluginConfig;
}

public void setPluginConfig(byte[] pluginConfig) {
this.pluginConfig = pluginConfig;
}

public void setPluginConfig(String pluginConfig) {
this.pluginConfig = pluginConfig.getBytes(StandardCharsets.UTF_8);
}

@Override
public WasmResult setTickPeriodMilliseconds(int tickPeriodMilliseconds) {
this.tickPeriodMilliseconds = tickPeriodMilliseconds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public void testOnPluginStart() throws StartException, WasmException {

// Create and configure the http_request_headers receiver instance
var receiverHandler1 = new MockHandler(sharedData);
receiverHandler1.setPluginConfig("http_request_headers");
receiverHandler1.setProperty(List.of("vm_id"), bytes(receiverVmId));
var receiverHost1 =
deferClose(
ProxyWasm.builder()
.withPluginHandler(receiverHandler1)
.withPluginConfig("http_request_headers")
.build(receiverModule));

var requestHeadersQueueId =
Expand All @@ -76,12 +76,12 @@ public void testOnPluginStart() throws StartException, WasmException {

// Create and configure the http_response_headers receiver instance
var receiverHandler2 = new MockHandler(sharedData);
receiverHandler2.setPluginConfig("http_response_headers");
receiverHandler2.setProperty(List.of("vm_id"), bytes(receiverVmId));
var receiverHost2 =
deferClose(
ProxyWasm.builder()
.withPluginHandler(receiverHandler2)
.withPluginConfig("http_response_headers")
.build(receiverModule));

var responseHeadersQueueId =
Expand All @@ -97,14 +97,12 @@ public void testOnPluginStart() throws StartException, WasmException {

// Create and configure the sender instance
var senderHandler = new MockHandler(sharedData);
senderHandler.setPluginConfig("http");
var senderVmId = "sender";
senderHandler.setProperty(List.of("vm_id"), bytes(senderVmId));
var senderHost =
deferClose(
ProxyWasm.builder()
.withPluginHandler(senderHandler)
.withPluginConfig("http")
.build(senderModule));
ProxyWasm.builder().withPluginHandler(senderHandler).build(senderModule));
senderHandler.assertLogsContain(
String.format("contextID=%d is configured for %s", senderHost.contextId(), "http"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ public class VmPluginConfigurationTest {
@Test
public void test() throws StartException {
var handler = new MockHandler();
handler.setPluginConfig("plugin_config");
handler.setVmConfig("vm_config");

handler.setProperty(List.of("plugin_name"), bytes("vm_plugin_configuration"));
ProxyWasm.Builder builder =
ProxyWasm.builder()
.withPluginConfig("plugin_config")
.withVmConfig("vm_config")
.withPluginHandler(handler);
ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler);
try (var ignored = builder.build(module)) {

assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ public void close() {
}
}

// //////////////////////////////////////////////////////////////////////
// Plugin config
// //////////////////////////////////////////////////////////////////////

byte[] vmConfig;

@Override
public byte[] getVmConfig() {
return vmConfig;
}

byte[] pluginConfig;

@Override
public byte[] getPluginConfig() {
return pluginConfig;
}

// //////////////////////////////////////////////////////////////////////
// Properties
// //////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.roastedroot.proxywasm.jaxrs;

import static io.roastedroot.proxywasm.Helpers.bytes;

import com.dylibso.chicory.runtime.ImportMemory;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.wasm.WasmModule;
Expand Down Expand Up @@ -101,22 +103,22 @@ public WasmPlugin.Builder withShared(boolean shared) {
}

public WasmPlugin.Builder withVmConfig(byte[] vmConfig) {
proxyWasmBuilder = proxyWasmBuilder.withVmConfig(vmConfig);
this.handler.vmConfig = vmConfig;
return this;
}

public WasmPlugin.Builder withVmConfig(String vmConfig) {
proxyWasmBuilder = proxyWasmBuilder.withVmConfig(vmConfig);
this.handler.vmConfig = bytes(vmConfig);
return this;
}

public WasmPlugin.Builder withPluginConfig(byte[] pluginConfig) {
proxyWasmBuilder = proxyWasmBuilder.withPluginConfig(pluginConfig);
this.handler.pluginConfig = pluginConfig;
return this;
}

public WasmPlugin.Builder withPluginConfig(String pluginConfig) {
proxyWasmBuilder = proxyWasmBuilder.withPluginConfig(pluginConfig);
this.handler.pluginConfig = bytes(pluginConfig);
return this;
}

Expand Down