Skip to content

Commit ff257a0

Browse files
authored
feat: adapt proxy-wasm-rust-sdk 0.2.0 (#105)
Signed-off-by: spacewander <[email protected]>
1 parent 733e5d3 commit ff257a0

File tree

12 files changed

+91
-40
lines changed

12 files changed

+91
-40
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ jobs:
6262
- name: Script
6363
run: |
6464
sudo chown -R runner:root ~/.cache
65-
make build.go.testdata
66-
67-
pushd ./t/testdata/assemblyscript && npm install && npm run asbuild && popd
68-
pushd ./t/testdata/rust && cargo build --target=wasm32-wasi && popd
69-
65+
make build.all.testdata
7066
export PATH=$OPENRESTY_PREFIX/nginx/sbin:$PATH
7167
WASM_VM=wasmtime prove -I. -Itest-nginx/lib -r t/

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ wasmtime-c-api
99

1010
t/servroot
1111
node_modules/
12-
t/testdata/rust/target
12+
t/testdata/rust/*/target
1313
utils/reindex

.licenserc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ header:
2727
- 't/certs/'
2828
- 't/testdata/assemblyscript/node_modules/'
2929
- 't/testdata/assemblyscript/build/'
30-
- 't/testdata/rust/Cargo.lock'
30+
- '**/Cargo.lock'
3131
- '**/*.json'
3232
# Exclude dev files
3333
- '.ccls-cache/'

Makefile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#
1515
OPENRESTY_PREFIX ?= /usr/local/openresty
1616
INSTALL ?= install
17+
RUST_DIR = $(wildcard t/testdata/rust/*)
1718

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

31-
.PHONY: build.all.testdata
32-
build.all.testdata: build.go.testdata
32+
.PHONY: build.assemblyscript.testdata
33+
build.assemblyscript.testdata:
3334
@cd ./t/testdata/assemblyscript && npm install && npm run asbuild
34-
@cd ./t/testdata/rust && cargo build --target=wasm32-wasi
35+
36+
.PHONY: build.rust.testdata
37+
build.rust.testdata:
38+
$(foreach DIR, $(RUST_DIR), \
39+
cd $(DIR) && \
40+
cargo build --target=wasm32-wasi && \
41+
cd ../../../..; \
42+
)
43+
44+
.PHONY: build.all.testdata
45+
build.all.testdata: build.go.testdata build.assemblyscript.testdata build.rust.testdata
3546

3647
.PHONY: utils
3748
utils:

src/http/ngx_http_wasm_api.c

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ int32_t
710710
proxy_set_buffer_bytes(int32_t type, int32_t start, int32_t length,
711711
int32_t addr, int32_t size_addr)
712712
{
713-
return PROXY_RESULT_OK;
713+
return PROXY_RESULT_UNIMPLEMENTED;
714714
}
715715

716716

@@ -789,14 +789,14 @@ proxy_send_http_response(int32_t res_code,
789789
int32_t
790790
proxy_get_current_time_nanoseconds(int32_t time_addr)
791791
{
792-
return PROXY_RESULT_OK;
792+
return PROXY_RESULT_UNIMPLEMENTED;
793793
}
794794

795795

796796
int32_t
797797
proxy_set_tick_period_milliseconds(int32_t tick)
798798
{
799-
return PROXY_RESULT_OK;
799+
return PROXY_RESULT_UNIMPLEMENTED;
800800
}
801801

802802

@@ -1253,7 +1253,7 @@ proxy_get_header_map_pairs(int32_t type, int32_t addr, int32_t size_addr)
12531253
int32_t
12541254
proxy_set_header_map_pairs(int32_t type, int32_t data, int32_t size)
12551255
{
1256-
return PROXY_RESULT_OK;
1256+
return PROXY_RESULT_UNIMPLEMENTED;
12571257
}
12581258

12591259

@@ -1504,7 +1504,7 @@ proxy_get_shared_data(int32_t key_data, int32_t key_size,
15041504
int32_t addr, int32_t size,
15051505
int32_t cas)
15061506
{
1507-
return PROXY_RESULT_OK;
1507+
return PROXY_RESULT_UNIMPLEMENTED;
15081508
}
15091509

15101510

@@ -1513,14 +1513,14 @@ proxy_set_shared_data(int32_t key_data, int32_t key_size,
15131513
int32_t data, int32_t size,
15141514
int32_t cas)
15151515
{
1516-
return PROXY_RESULT_OK;
1516+
return PROXY_RESULT_UNIMPLEMENTED;
15171517
}
15181518

15191519

15201520
int32_t
15211521
proxy_register_shared_queue(int32_t data, int32_t size, int32_t id)
15221522
{
1523-
return PROXY_RESULT_OK;
1523+
return PROXY_RESULT_UNIMPLEMENTED;
15241524
}
15251525

15261526

@@ -1529,21 +1529,21 @@ proxy_resolve_shared_queue(int32_t vm_id_data, int32_t vm_id_size,
15291529
int32_t name_data, int32_t name_size,
15301530
int32_t return_id)
15311531
{
1532-
return PROXY_RESULT_OK;
1532+
return PROXY_RESULT_UNIMPLEMENTED;
15331533
}
15341534

15351535

15361536
int32_t
15371537
proxy_dequeue_shared_queue(int32_t id, int32_t addr, int32_t size)
15381538
{
1539-
return PROXY_RESULT_OK;
1539+
return PROXY_RESULT_UNIMPLEMENTED;
15401540
}
15411541

15421542

15431543
int32_t
15441544
proxy_enqueue_shared_queue(int32_t id, int32_t addr, int32_t size)
15451545
{
1546-
return PROXY_RESULT_OK;
1546+
return PROXY_RESULT_UNIMPLEMENTED;
15471547
}
15481548

15491549

@@ -1564,6 +1564,22 @@ proxy_continue_response(void)
15641564
int32_t
15651565
proxy_clear_route_cache(void)
15661566
{
1567+
return PROXY_RESULT_UNIMPLEMENTED;
1568+
}
1569+
1570+
1571+
int32_t
1572+
proxy_continue_stream(int32_t stream_type)
1573+
{
1574+
/* we don't need to continue the HTTP request as it always waits the HTTP call to finish */
1575+
return PROXY_RESULT_OK;
1576+
}
1577+
1578+
1579+
int32_t
1580+
proxy_close_stream(int32_t stream_type)
1581+
{
1582+
/* do nothing */
15671583
return PROXY_RESULT_OK;
15681584
}
15691585

@@ -1675,7 +1691,7 @@ proxy_grpc_call(int32_t service_data, int32_t service_size,
16751691
int32_t request_data, int32_t request_size,
16761692
int32_t timeout, int32_t callout_addr)
16771693
{
1678-
return PROXY_RESULT_OK;
1694+
return PROXY_RESULT_UNIMPLEMENTED;
16791695
}
16801696

16811697

@@ -1686,41 +1702,50 @@ proxy_grpc_stream(int32_t service_data, int32_t service_size,
16861702
int32_t metadata_data, int32_t metadata_size,
16871703
int32_t callout_addr)
16881704
{
1689-
return PROXY_RESULT_OK;
1705+
return PROXY_RESULT_UNIMPLEMENTED;
16901706
}
16911707

16921708

16931709
int32_t
16941710
proxy_grpc_send(int32_t id, int32_t message_data, int32_t message_size,
16951711
int32_t end_of_stream)
16961712
{
1697-
return PROXY_RESULT_OK;
1713+
return PROXY_RESULT_UNIMPLEMENTED;
16981714
}
16991715

17001716

17011717
int32_t
17021718
proxy_grpc_cancel(int32_t id)
17031719
{
1704-
return PROXY_RESULT_OK;
1720+
return PROXY_RESULT_UNIMPLEMENTED;
17051721
}
17061722

17071723

17081724
int32_t
17091725
proxy_grpc_close(int32_t id)
17101726
{
1711-
return PROXY_RESULT_OK;
1727+
return PROXY_RESULT_UNIMPLEMENTED;
17121728
}
17131729

17141730

17151731
int32_t
17161732
proxy_get_status(int32_t code, int32_t addr, int32_t size)
17171733
{
1718-
return PROXY_RESULT_OK;
1734+
return PROXY_RESULT_UNIMPLEMENTED;
17191735
}
17201736

17211737

17221738
int32_t
17231739
proxy_done(void)
17241740
{
1725-
return PROXY_RESULT_OK;
1741+
return PROXY_RESULT_UNIMPLEMENTED;
1742+
}
1743+
1744+
1745+
int32_t
1746+
proxy_call_foreign_function(int32_t fn_data, int32_t fn_size,
1747+
int32_t param_data, int32_t param_size,
1748+
int32_t res_data, int32_t res_size)
1749+
{
1750+
return PROXY_RESULT_UNIMPLEMENTED;
17261751
}

src/http/ngx_http_wasm_api_def.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ int32_t proxy_enqueue_shared_queue(DEFINE_WASM_API_ARG_I32_3);
8080
int32_t proxy_continue_request(DEFINE_WASM_API_ARG_VOID);
8181
int32_t proxy_continue_response(DEFINE_WASM_API_ARG_VOID);
8282
int32_t proxy_clear_route_cache(DEFINE_WASM_API_ARG_VOID);
83+
int32_t proxy_continue_stream(DEFINE_WASM_API_ARG_I32_1);
84+
int32_t proxy_close_stream(DEFINE_WASM_API_ARG_I32_1);
8385
int32_t proxy_http_call(DEFINE_WASM_API_ARG_I32_10);
8486
int32_t proxy_grpc_call(DEFINE_WASM_API_ARG_I32_12);
8587
int32_t proxy_grpc_stream(DEFINE_WASM_API_ARG_I32_9);
@@ -88,6 +90,7 @@ int32_t proxy_grpc_cancel(DEFINE_WASM_API_ARG_I32_1);
8890
int32_t proxy_grpc_close(DEFINE_WASM_API_ARG_I32_1);
8991
int32_t proxy_get_status(DEFINE_WASM_API_ARG_I32_3);
9092
int32_t proxy_done(DEFINE_WASM_API_ARG_VOID);
93+
int32_t proxy_call_foreign_function(DEFINE_WASM_API_ARG_I32_6);
9194

9295

9396

src/http/ngx_http_wasm_api_wasmtime.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ DEFINE_WASM_API(proxy_continue_response,
274274
DEFINE_WASM_API_ARG_CHECK_VOID(proxy_continue_response))
275275
DEFINE_WASM_API(proxy_clear_route_cache,
276276
DEFINE_WASM_API_ARG_CHECK_VOID(proxy_clear_route_cache))
277+
DEFINE_WASM_API(proxy_continue_stream,
278+
DEFINE_WASM_API_ARG_CHECK_I32_1(proxy_continue_stream))
279+
DEFINE_WASM_API(proxy_close_stream,
280+
DEFINE_WASM_API_ARG_CHECK_I32_1(proxy_close_stream))
277281
DEFINE_WASM_API(proxy_http_call,
278282
DEFINE_WASM_API_ARG_CHECK_I32_10(proxy_http_call))
279283
DEFINE_WASM_API(proxy_grpc_call,
@@ -290,6 +294,8 @@ DEFINE_WASM_API(proxy_get_status,
290294
DEFINE_WASM_API_ARG_CHECK_I32_3(proxy_get_status))
291295
DEFINE_WASM_API(proxy_done,
292296
DEFINE_WASM_API_ARG_CHECK_VOID(proxy_done))
297+
DEFINE_WASM_API(proxy_call_foreign_function,
298+
DEFINE_WASM_API_ARG_CHECK_I32_6(proxy_call_foreign_function))
293299

294300
static ngx_wasm_wasmtime_host_api_t host_apis[] = {
295301
DEFINE_WASM_NAME(proxy_set_effective_context, DEFINE_WASM_NAME_ARG_I32_1)
@@ -318,6 +324,8 @@ static ngx_wasm_wasmtime_host_api_t host_apis[] = {
318324
DEFINE_WASM_NAME(proxy_continue_request, DEFINE_WASM_NAME_ARG_VOID)
319325
DEFINE_WASM_NAME(proxy_continue_response, DEFINE_WASM_NAME_ARG_VOID)
320326
DEFINE_WASM_NAME(proxy_clear_route_cache, DEFINE_WASM_NAME_ARG_VOID)
327+
DEFINE_WASM_NAME(proxy_continue_stream, DEFINE_WASM_NAME_ARG_I32_1)
328+
DEFINE_WASM_NAME(proxy_close_stream, DEFINE_WASM_NAME_ARG_I32_1)
321329
DEFINE_WASM_NAME(proxy_http_call, DEFINE_WASM_NAME_ARG_I32_10)
322330
DEFINE_WASM_NAME(proxy_grpc_call, DEFINE_WASM_NAME_ARG_I32_12)
323331
DEFINE_WASM_NAME(proxy_grpc_stream, DEFINE_WASM_NAME_ARG_I32_9)
@@ -326,6 +334,7 @@ static ngx_wasm_wasmtime_host_api_t host_apis[] = {
326334
DEFINE_WASM_NAME(proxy_grpc_close, DEFINE_WASM_NAME_ARG_I32_1)
327335
DEFINE_WASM_NAME(proxy_get_status, DEFINE_WASM_NAME_ARG_I32_3)
328336
DEFINE_WASM_NAME(proxy_done, DEFINE_WASM_NAME_ARG_VOID)
337+
DEFINE_WASM_NAME(proxy_call_foreign_function, DEFINE_WASM_NAME_ARG_I32_6)
329338
{ ngx_null_string, NULL, 0, {} }
330339
};
331340

src/http/ngx_http_wasm_module.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static bool ngx_http_wasm_vm_inited = false;
3434

3535

3636
static ngx_str_t plugin_start = ngx_string("_start");
37+
static ngx_str_t plugin_initialize = ngx_string("_initialize");
3738
static ngx_str_t abi_versions[] = {
3839
ngx_string("proxy_abi_version_0_1_0"),
3940
ngx_string("proxy_abi_version_0_2_0"),
@@ -223,8 +224,14 @@ ngx_http_wasm_load_plugin(const char *name, size_t name_len,
223224
return NULL;
224225
}
225226

226-
rc = ngx_wasm_vm->call(plugin, &plugin_start, false,
227-
NGX_WASM_PARAM_VOID);
227+
if (ngx_wasm_vm->has(plugin, &plugin_start)) {
228+
rc = ngx_wasm_vm->call(plugin, &plugin_start, false, NGX_WASM_PARAM_VOID);
229+
} else if (ngx_wasm_vm->has(plugin, &plugin_initialize)) {
230+
rc = ngx_wasm_vm->call(plugin, &plugin_initialize, false, NGX_WASM_PARAM_VOID);
231+
} else {
232+
rc = NGX_OK;
233+
}
234+
228235
if (rc != NGX_OK) {
229236
goto free_plugin;
230237
}

t/rust.t renamed to t/rust/fault-injection.t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ___location /t {
2424
content_by_lua_block {
2525
local wasm = require("resty.proxy-wasm")
2626
local plugin = assert(wasm.load("fault_injection",
27-
"t/testdata/rust/target/wasm32-wasi/debug/fault_injection.wasm"))
27+
"t/testdata/rust/fault-injection/target/wasm32-wasi/debug/fault_injection.wasm"))
2828
local ctx = assert(wasm.on_configure(plugin, '{"http_status": 403, "body": "body"}'))
2929
assert(wasm.on_http_request_headers(ctx))
3030
}
@@ -41,7 +41,7 @@ ___location /t {
4141
content_by_lua_block {
4242
local wasm = require("resty.proxy-wasm")
4343
local plugin = assert(wasm.load("fault_injection",
44-
"t/testdata/rust/target/wasm32-wasi/debug/fault_injection.wasm"))
44+
"t/testdata/rust/fault-injection/target/wasm32-wasi/debug/fault_injection.wasm"))
4545
local ctx = assert(wasm.on_configure(plugin, '{"http_status": 401}'))
4646
assert(wasm.on_http_request_headers(ctx))
4747
}
@@ -58,7 +58,7 @@ ___location /t {
5858
content_by_lua_block {
5959
local wasm = require("resty.proxy-wasm")
6060
local plugin = assert(wasm.load("fault_injection",
61-
"t/testdata/rust/target/wasm32-wasi/debug/fault_injection.wasm"))
61+
"t/testdata/rust/fault-injection/target/wasm32-wasi/debug/fault_injection.wasm"))
6262
local ctx = assert(wasm.on_configure(plugin, '{"http_status": 401, "percentage": 0}'))
6363
assert(wasm.on_http_request_headers(ctx))
6464
}

t/testdata/rust/Cargo.lock renamed to t/testdata/rust/fault-injection/Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)