Skip to content

Commit 82a75c3

Browse files
authored
feat: replace response header (#32)
1 parent 4bec622 commit 82a75c3

File tree

7 files changed

+103
-17
lines changed

7 files changed

+103
-17
lines changed

lib/resty/proxy-wasm.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ function _M.on_configure(plugin, conf)
6161
return nil, "bad plugin"
6262
end
6363

64-
conf = conf or ""
64+
if type(conf) ~= "string" or conf == "" then
65+
return nil, "bad conf"
66+
end
67+
6568
local plugin_ctx = C.ngx_http_wasm_on_configure(plugin, conf, #conf)
6669
if plugin_ctx == nil then
6770
return nil, "failed to run proxy_on_configure"

proxy_wasm_abi.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,22 @@ configuration separately because proxy-wasm-rust-sdk still uses it.
174174
- `i32 (proxy_result_t) call_result`
175175

176176

177-
### `proxy_add_map_value`
177+
### `proxy_set_map_value` (`proxy_replace_header_map_value`)
178+
179+
* params:
180+
- `i32 (proxy_map_type_t) map_type`
181+
- `i32 (const char*) key_data`
182+
- `i32 (size_t) key_size`
183+
- `i32 (const char*) value_data`
184+
- `i32 (size_t) value_size`
185+
* returns:
186+
- `i32 (proxy_result_t) call_result`
187+
188+
Set or replace the content of key (`key_data`, `key_size`) to the value (`value_data`, `value_size`)
189+
in a given map (`map_type`).
190+
191+
192+
### `proxy_add_map_value` (`proxy_add_header_map_value`)
178193

179194
* params:
180195
- `i32 (proxy_map_type_t) map_type`

src/http/ngx_http_wasm_api.c

+31-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
#define FFI_NO_REQ_CTX -100
1111
#define FFI_BAD_CONTEXT -101
1212

13+
#define must_get_memory(key, log, key_data, key_len) \
14+
key = (char *) ngx_wasm_vm.get_memory((log), (key_data), (key_size)); \
15+
if (key == NULL) { \
16+
return PROXY_RESULT_INVALID_MEMORY_ACCESS; \
17+
}
18+
#define must_get_req(r) \
19+
r = ngx_http_wasm_get_req(); \
20+
if (r == NULL) { \
21+
return PROXY_RESULT_BAD_ARGUMENT; \
22+
}
23+
1324

1425
static int (*set_resp_header) (ngx_http_request_t *r,
1526
const char *key_data, size_t key_len, int is_nil,
@@ -388,6 +399,23 @@ int32_t
388399
proxy_replace_header_map_value(int32_t type, int32_t key_data, int32_t key_size,
389400
int32_t data, int32_t size)
390401
{
402+
ngx_int_t rc;
403+
ngx_log_t *log;
404+
ngx_http_request_t *r;
405+
char *key, *val;
406+
407+
log = ngx_http_wasm_get_log();
408+
must_get_req(r);
409+
must_get_memory(key, log, key_data, key_len);
410+
must_get_memory(val, log, data, size);
411+
412+
if (type == PROXY_MAP_TYPE_HTTP_RESPONSE_HEADERS) {
413+
rc = ngx_http_wasm_set_resp_header(r, key, key_size, 0, val, size, 1);
414+
if (rc != NGX_OK) {
415+
return PROXY_RESULT_BAD_ARGUMENT;
416+
}
417+
}
418+
391419
return PROXY_RESULT_OK;
392420
}
393421

@@ -402,20 +430,9 @@ proxy_add_header_map_value(int32_t type, int32_t key_data, int32_t key_size,
402430
char *key, *val;
403431

404432
log = ngx_http_wasm_get_log();
405-
r = ngx_http_wasm_get_req();
406-
if (r == NULL) {
407-
return PROXY_RESULT_BAD_ARGUMENT;
408-
}
409-
410-
key = (char *) ngx_wasm_vm.get_memory(log, key_data, key_size);
411-
if (key == NULL) {
412-
return PROXY_RESULT_INVALID_MEMORY_ACCESS;
413-
}
414-
415-
val = (char *) ngx_wasm_vm.get_memory(log, data, size);
416-
if (val == NULL) {
417-
return PROXY_RESULT_INVALID_MEMORY_ACCESS;
418-
}
433+
must_get_req(r);
434+
must_get_memory(key, log, key_data, key_len);
435+
must_get_memory(val, log, data, size);
419436

420437
if (type == PROXY_MAP_TYPE_HTTP_RESPONSE_HEADERS) {
421438
rc = ngx_http_wasm_set_resp_header(r, key, key_size, 0, val, size, 0);

t/http_header.t

+32
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,35 @@ location /t {
1717
}
1818
--- response_headers
1919
add: foo, bar
20+
21+
22+
23+
=== TEST 2: response header set
24+
--- config
25+
location /t {
26+
return 200;
27+
header_filter_by_lua_block {
28+
local wasm = require("resty.proxy-wasm")
29+
local plugin = assert(wasm.load("plugin", "t/testdata/http_header/main.go.wasm"))
30+
local ctx = assert(wasm.on_configure(plugin, 'resp_hdr_set'))
31+
assert(wasm.on_http_response_headers(ctx))
32+
}
33+
}
34+
--- response_headers
35+
add: bar
36+
37+
38+
39+
=== TEST 3: response header set empty
40+
--- config
41+
location /t {
42+
return 200;
43+
header_filter_by_lua_block {
44+
local wasm = require("resty.proxy-wasm")
45+
local plugin = assert(wasm.load("plugin", "t/testdata/http_header/main.go.wasm"))
46+
local ctx = assert(wasm.on_configure(plugin, 'resp_hdr_set_empty'))
47+
assert(wasm.on_http_response_headers(ctx))
48+
}
49+
}
50+
--- response_headers
51+
add:

t/log.t

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ location /t {
1111
content_by_lua_block {
1212
local wasm = require("resty.proxy-wasm")
1313
local p = wasm.load("plugin", "t/testdata/log/main.go.wasm")
14-
wasm.on_configure(p)
14+
wasm.on_configure(p, "blah")
1515
}
1616
}
1717
--- grep_error_log eval

t/plugin_lifecycle.t

+15
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,18 @@ location /t {
191191
}
192192
--- shutdown_error_log
193193
failed to mark context 1 as done
194+
195+
196+
197+
=== TEST 9: empty configure
198+
--- config
199+
location /t {
200+
content_by_lua_block {
201+
local wasm = require("resty.proxy-wasm")
202+
local plugin = wasm.load("plugin", "t/testdata/plugin_lifecycle/main.go.wasm")
203+
local _, err = wasm.on_configure(plugin, '')
204+
ngx.say(err)
205+
}
206+
}
207+
--- response_body
208+
bad conf

t/testdata/http_header/main.go

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func (ctx *httpContext) OnHttpResponseHeaders(numHeaders int, endOfStream bool)
4242

4343
if action == "resp_hdr_add" {
4444
proxywasm.AddHttpResponseHeader("add", "bar")
45+
} else if action == "resp_hdr_set" {
46+
proxywasm.ReplaceHttpResponseHeader("add", "bar")
47+
} else if action == "resp_hdr_set_empty" {
48+
proxywasm.ReplaceHttpResponseHeader("add", "")
4549
}
4650

4751
return types.ActionContinue

0 commit comments

Comments
 (0)