Skip to content

Commit dea3dff

Browse files
authored
feat: remove req header (#54)
1 parent 5fb8c9a commit dea3dff

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

src/http/ngx_http_wasm_api.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -876,11 +876,21 @@ proxy_remove_header_map_value(int32_t type, int32_t key_data, int32_t key_size)
876876
must_get_req(r);
877877
must_get_memory(key, log, key_data, key_size);
878878

879-
if (type == PROXY_MAP_TYPE_HTTP_RESPONSE_HEADERS) {
879+
switch (type) {
880+
case PROXY_MAP_TYPE_HTTP_REQUEST_HEADERS:
881+
rc = ngx_http_wasm_set_req_header(r, key, key_size, NULL, 0, 1);
882+
break;
883+
884+
case PROXY_MAP_TYPE_HTTP_RESPONSE_HEADERS:
880885
rc = ngx_http_wasm_set_resp_header(r, key, key_size, 1, NULL, 0, 1);
881-
if (rc != NGX_OK) {
882-
return PROXY_RESULT_BAD_ARGUMENT;
883-
}
886+
break;
887+
888+
default:
889+
return PROXY_RESULT_BAD_ARGUMENT;
890+
}
891+
892+
if (rc != NGX_OK) {
893+
return PROXY_RESULT_BAD_ARGUMENT;
884894
}
885895

886896
return PROXY_RESULT_OK;

t/http_req_header.t

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,62 @@ location /t {
198198
}
199199
--- response_body
200200
bar
201+
202+
203+
204+
=== TEST 11: remove header
205+
--- config
206+
location /t {
207+
access_by_lua_block {
208+
local wasm = require("resty.proxy-wasm")
209+
local plugin = assert(wasm.load("plugin", "t/testdata/http_header/main.go.wasm"))
210+
local ctx = assert(wasm.on_configure(plugin, 'req_hdr_del'))
211+
assert(wasm.on_http_request_headers(ctx))
212+
}
213+
content_by_lua_block {
214+
ngx.say(ngx.req.get_headers()["foo"])
215+
}
216+
}
217+
--- more_headers
218+
FOO: foo
219+
--- response_body
220+
nil
221+
222+
223+
224+
=== TEST 12: remove header, missing
225+
--- config
226+
location /t {
227+
access_by_lua_block {
228+
local wasm = require("resty.proxy-wasm")
229+
local plugin = assert(wasm.load("plugin", "t/testdata/http_header/main.go.wasm"))
230+
local ctx = assert(wasm.on_configure(plugin, 'req_hdr_del'))
231+
assert(wasm.on_http_request_headers(ctx))
232+
}
233+
content_by_lua_block {
234+
ngx.say(ngx.req.get_headers()["foo"])
235+
}
236+
}
237+
--- response_body
238+
nil
239+
240+
241+
242+
=== TEST 13: remove header, all of it
243+
--- config
244+
location /t {
245+
access_by_lua_block {
246+
local wasm = require("resty.proxy-wasm")
247+
local plugin = assert(wasm.load("plugin", "t/testdata/http_header/main.go.wasm"))
248+
local ctx = assert(wasm.on_configure(plugin, 'req_hdr_del'))
249+
assert(wasm.on_http_request_headers(ctx))
250+
}
251+
content_by_lua_block {
252+
ngx.say(ngx.req.get_headers()["foo"])
253+
}
254+
}
255+
--- more_headers
256+
FOO: foo
257+
FOO: bar
258+
--- response_body
259+
nil

t/testdata/http_header/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) t
7171

7272
case "req_hdr_add":
7373
proxywasm.AddHttpRequestHeader("foo", "bar")
74+
75+
case "req_hdr_del":
76+
proxywasm.RemoveHttpRequestHeader("foo")
7477
}
7578

7679
return types.ActionContinue

0 commit comments

Comments
 (0)