Skip to content

Commit

Permalink
feat: adapt proxy-wasm-rust-sdk 0.2.0 (#105)
Browse files Browse the repository at this point in the history
Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander authored May 10, 2022
1 parent 733e5d3 commit ff257a0
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 40 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ jobs:
- name: Script
run: |
sudo chown -R runner:root ~/.cache
make build.go.testdata
pushd ./t/testdata/assemblyscript && npm install && npm run asbuild && popd
pushd ./t/testdata/rust && cargo build --target=wasm32-wasi && popd
make build.all.testdata
export PATH=$OPENRESTY_PREFIX/nginx/sbin:$PATH
WASM_VM=wasmtime prove -I. -Itest-nginx/lib -r t/
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ wasmtime-c-api

t/servroot
node_modules/
t/testdata/rust/target
t/testdata/rust/*/target
utils/reindex
2 changes: 1 addition & 1 deletion .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ header:
- 't/certs/'
- 't/testdata/assemblyscript/node_modules/'
- 't/testdata/assemblyscript/build/'
- 't/testdata/rust/Cargo.lock'
- '**/Cargo.lock'
- '**/*.json'
# Exclude dev files
- '.ccls-cache/'
Expand Down
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#
OPENRESTY_PREFIX ?= /usr/local/openresty
INSTALL ?= install
RUST_DIR = $(wildcard t/testdata/rust/*)

.PHONY: install
install:
Expand All @@ -28,10 +29,20 @@ build.go.testdata:
build.testdata:
@find ./t/testdata -type f -name "main.go" | grep ${name} | xargs -Ip tinygo build -o p.wasm -scheduler=none -target=wasi p

.PHONY: build.all.testdata
build.all.testdata: build.go.testdata
.PHONY: build.assemblyscript.testdata
build.assemblyscript.testdata:
@cd ./t/testdata/assemblyscript && npm install && npm run asbuild
@cd ./t/testdata/rust && cargo build --target=wasm32-wasi

.PHONY: build.rust.testdata
build.rust.testdata:
$(foreach DIR, $(RUST_DIR), \
cd $(DIR) && \
cargo build --target=wasm32-wasi && \
cd ../../../..; \
)

.PHONY: build.all.testdata
build.all.testdata: build.go.testdata build.assemblyscript.testdata build.rust.testdata

.PHONY: utils
utils:
Expand Down
59 changes: 42 additions & 17 deletions src/http/ngx_http_wasm_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ int32_t
proxy_set_buffer_bytes(int32_t type, int32_t start, int32_t length,
int32_t addr, int32_t size_addr)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


Expand Down Expand Up @@ -789,14 +789,14 @@ proxy_send_http_response(int32_t res_code,
int32_t
proxy_get_current_time_nanoseconds(int32_t time_addr)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


int32_t
proxy_set_tick_period_milliseconds(int32_t tick)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


Expand Down Expand Up @@ -1253,7 +1253,7 @@ proxy_get_header_map_pairs(int32_t type, int32_t addr, int32_t size_addr)
int32_t
proxy_set_header_map_pairs(int32_t type, int32_t data, int32_t size)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


Expand Down Expand Up @@ -1504,7 +1504,7 @@ proxy_get_shared_data(int32_t key_data, int32_t key_size,
int32_t addr, int32_t size,
int32_t cas)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


Expand All @@ -1513,14 +1513,14 @@ proxy_set_shared_data(int32_t key_data, int32_t key_size,
int32_t data, int32_t size,
int32_t cas)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


int32_t
proxy_register_shared_queue(int32_t data, int32_t size, int32_t id)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


Expand All @@ -1529,21 +1529,21 @@ proxy_resolve_shared_queue(int32_t vm_id_data, int32_t vm_id_size,
int32_t name_data, int32_t name_size,
int32_t return_id)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


int32_t
proxy_dequeue_shared_queue(int32_t id, int32_t addr, int32_t size)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


int32_t
proxy_enqueue_shared_queue(int32_t id, int32_t addr, int32_t size)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


Expand All @@ -1564,6 +1564,22 @@ proxy_continue_response(void)
int32_t
proxy_clear_route_cache(void)
{
return PROXY_RESULT_UNIMPLEMENTED;
}


int32_t
proxy_continue_stream(int32_t stream_type)
{
/* we don't need to continue the HTTP request as it always waits the HTTP call to finish */
return PROXY_RESULT_OK;
}


int32_t
proxy_close_stream(int32_t stream_type)
{
/* do nothing */
return PROXY_RESULT_OK;
}

Expand Down Expand Up @@ -1675,7 +1691,7 @@ proxy_grpc_call(int32_t service_data, int32_t service_size,
int32_t request_data, int32_t request_size,
int32_t timeout, int32_t callout_addr)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


Expand All @@ -1686,41 +1702,50 @@ proxy_grpc_stream(int32_t service_data, int32_t service_size,
int32_t metadata_data, int32_t metadata_size,
int32_t callout_addr)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


int32_t
proxy_grpc_send(int32_t id, int32_t message_data, int32_t message_size,
int32_t end_of_stream)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


int32_t
proxy_grpc_cancel(int32_t id)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


int32_t
proxy_grpc_close(int32_t id)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


int32_t
proxy_get_status(int32_t code, int32_t addr, int32_t size)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


int32_t
proxy_done(void)
{
return PROXY_RESULT_OK;
return PROXY_RESULT_UNIMPLEMENTED;
}


int32_t
proxy_call_foreign_function(int32_t fn_data, int32_t fn_size,
int32_t param_data, int32_t param_size,
int32_t res_data, int32_t res_size)
{
return PROXY_RESULT_UNIMPLEMENTED;
}
3 changes: 3 additions & 0 deletions src/http/ngx_http_wasm_api_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ int32_t proxy_enqueue_shared_queue(DEFINE_WASM_API_ARG_I32_3);
int32_t proxy_continue_request(DEFINE_WASM_API_ARG_VOID);
int32_t proxy_continue_response(DEFINE_WASM_API_ARG_VOID);
int32_t proxy_clear_route_cache(DEFINE_WASM_API_ARG_VOID);
int32_t proxy_continue_stream(DEFINE_WASM_API_ARG_I32_1);
int32_t proxy_close_stream(DEFINE_WASM_API_ARG_I32_1);
int32_t proxy_http_call(DEFINE_WASM_API_ARG_I32_10);
int32_t proxy_grpc_call(DEFINE_WASM_API_ARG_I32_12);
int32_t proxy_grpc_stream(DEFINE_WASM_API_ARG_I32_9);
Expand All @@ -88,6 +90,7 @@ int32_t proxy_grpc_cancel(DEFINE_WASM_API_ARG_I32_1);
int32_t proxy_grpc_close(DEFINE_WASM_API_ARG_I32_1);
int32_t proxy_get_status(DEFINE_WASM_API_ARG_I32_3);
int32_t proxy_done(DEFINE_WASM_API_ARG_VOID);
int32_t proxy_call_foreign_function(DEFINE_WASM_API_ARG_I32_6);



Expand Down
9 changes: 9 additions & 0 deletions src/http/ngx_http_wasm_api_wasmtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ DEFINE_WASM_API(proxy_continue_response,
DEFINE_WASM_API_ARG_CHECK_VOID(proxy_continue_response))
DEFINE_WASM_API(proxy_clear_route_cache,
DEFINE_WASM_API_ARG_CHECK_VOID(proxy_clear_route_cache))
DEFINE_WASM_API(proxy_continue_stream,
DEFINE_WASM_API_ARG_CHECK_I32_1(proxy_continue_stream))
DEFINE_WASM_API(proxy_close_stream,
DEFINE_WASM_API_ARG_CHECK_I32_1(proxy_close_stream))
DEFINE_WASM_API(proxy_http_call,
DEFINE_WASM_API_ARG_CHECK_I32_10(proxy_http_call))
DEFINE_WASM_API(proxy_grpc_call,
Expand All @@ -290,6 +294,8 @@ DEFINE_WASM_API(proxy_get_status,
DEFINE_WASM_API_ARG_CHECK_I32_3(proxy_get_status))
DEFINE_WASM_API(proxy_done,
DEFINE_WASM_API_ARG_CHECK_VOID(proxy_done))
DEFINE_WASM_API(proxy_call_foreign_function,
DEFINE_WASM_API_ARG_CHECK_I32_6(proxy_call_foreign_function))

static ngx_wasm_wasmtime_host_api_t host_apis[] = {
DEFINE_WASM_NAME(proxy_set_effective_context, DEFINE_WASM_NAME_ARG_I32_1)
Expand Down Expand Up @@ -318,6 +324,8 @@ static ngx_wasm_wasmtime_host_api_t host_apis[] = {
DEFINE_WASM_NAME(proxy_continue_request, DEFINE_WASM_NAME_ARG_VOID)
DEFINE_WASM_NAME(proxy_continue_response, DEFINE_WASM_NAME_ARG_VOID)
DEFINE_WASM_NAME(proxy_clear_route_cache, DEFINE_WASM_NAME_ARG_VOID)
DEFINE_WASM_NAME(proxy_continue_stream, DEFINE_WASM_NAME_ARG_I32_1)
DEFINE_WASM_NAME(proxy_close_stream, DEFINE_WASM_NAME_ARG_I32_1)
DEFINE_WASM_NAME(proxy_http_call, DEFINE_WASM_NAME_ARG_I32_10)
DEFINE_WASM_NAME(proxy_grpc_call, DEFINE_WASM_NAME_ARG_I32_12)
DEFINE_WASM_NAME(proxy_grpc_stream, DEFINE_WASM_NAME_ARG_I32_9)
Expand All @@ -326,6 +334,7 @@ static ngx_wasm_wasmtime_host_api_t host_apis[] = {
DEFINE_WASM_NAME(proxy_grpc_close, DEFINE_WASM_NAME_ARG_I32_1)
DEFINE_WASM_NAME(proxy_get_status, DEFINE_WASM_NAME_ARG_I32_3)
DEFINE_WASM_NAME(proxy_done, DEFINE_WASM_NAME_ARG_VOID)
DEFINE_WASM_NAME(proxy_call_foreign_function, DEFINE_WASM_NAME_ARG_I32_6)
{ ngx_null_string, NULL, 0, {} }
};

Expand Down
11 changes: 9 additions & 2 deletions src/http/ngx_http_wasm_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ static bool ngx_http_wasm_vm_inited = false;


static ngx_str_t plugin_start = ngx_string("_start");
static ngx_str_t plugin_initialize = ngx_string("_initialize");
static ngx_str_t abi_versions[] = {
ngx_string("proxy_abi_version_0_1_0"),
ngx_string("proxy_abi_version_0_2_0"),
Expand Down Expand Up @@ -223,8 +224,14 @@ ngx_http_wasm_load_plugin(const char *name, size_t name_len,
return NULL;
}

rc = ngx_wasm_vm->call(plugin, &plugin_start, false,
NGX_WASM_PARAM_VOID);
if (ngx_wasm_vm->has(plugin, &plugin_start)) {
rc = ngx_wasm_vm->call(plugin, &plugin_start, false, NGX_WASM_PARAM_VOID);
} else if (ngx_wasm_vm->has(plugin, &plugin_initialize)) {
rc = ngx_wasm_vm->call(plugin, &plugin_initialize, false, NGX_WASM_PARAM_VOID);
} else {
rc = NGX_OK;
}

if (rc != NGX_OK) {
goto free_plugin;
}
Expand Down
6 changes: 3 additions & 3 deletions t/rust.t → t/rust/fault-injection.t
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ location /t {
content_by_lua_block {
local wasm = require("resty.proxy-wasm")
local plugin = assert(wasm.load("fault_injection",
"t/testdata/rust/target/wasm32-wasi/debug/fault_injection.wasm"))
"t/testdata/rust/fault-injection/target/wasm32-wasi/debug/fault_injection.wasm"))
local ctx = assert(wasm.on_configure(plugin, '{"http_status": 403, "body": "body"}'))
assert(wasm.on_http_request_headers(ctx))
}
Expand All @@ -41,7 +41,7 @@ location /t {
content_by_lua_block {
local wasm = require("resty.proxy-wasm")
local plugin = assert(wasm.load("fault_injection",
"t/testdata/rust/target/wasm32-wasi/debug/fault_injection.wasm"))
"t/testdata/rust/fault-injection/target/wasm32-wasi/debug/fault_injection.wasm"))
local ctx = assert(wasm.on_configure(plugin, '{"http_status": 401}'))
assert(wasm.on_http_request_headers(ctx))
}
Expand All @@ -58,7 +58,7 @@ location /t {
content_by_lua_block {
local wasm = require("resty.proxy-wasm")
local plugin = assert(wasm.load("fault_injection",
"t/testdata/rust/target/wasm32-wasi/debug/fault_injection.wasm"))
"t/testdata/rust/fault-injection/target/wasm32-wasi/debug/fault_injection.wasm"))
local ctx = assert(wasm.on_configure(plugin, '{"http_status": 401, "percentage": 0}'))
assert(wasm.on_http_request_headers(ctx))
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
[package]
name = "fault-injection"
version = "0.1.0"
edition = "2018"
edition = "2021"

[lib]
crate-type = ["cdylib"]
path = "./fault_injection.rs"

[dependencies]
log = "0.4"
proxy-wasm = "0.1"
proxy-wasm = "0.2"
rand = "0.8.4"
serde = { version = "1.0", features = ["derive"] }
serde_derive = { version = "1.0", default-features = false }
Expand Down
Loading

0 comments on commit ff257a0

Please sign in to comment.