Skip to content

Commit 5fb8c9a

Browse files
authored
feat: add request header (#42)
1 parent 47743d8 commit 5fb8c9a

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

src/http/ngx_http_wasm_api.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -936,13 +936,20 @@ proxy_add_header_map_value(int32_t type, int32_t key_data, int32_t key_size,
936936
must_get_memory(key, log, key_data, key_size);
937937
must_get_memory(val, log, data, size);
938938

939-
if (type == PROXY_MAP_TYPE_HTTP_RESPONSE_HEADERS) {
939+
switch (type) {
940+
case PROXY_MAP_TYPE_HTTP_REQUEST_HEADERS:
941+
rc = ngx_http_wasm_set_req_header(r, key, key_size, val, size, 0);
942+
break;
943+
944+
case PROXY_MAP_TYPE_HTTP_RESPONSE_HEADERS:
940945
rc = ngx_http_wasm_set_resp_header(r, key, key_size, 0, val, size, 0);
941-
if (rc != NGX_OK) {
942-
return PROXY_RESULT_BAD_ARGUMENT;
943-
}
946+
break;
947+
948+
default:
949+
return PROXY_RESULT_BAD_ARGUMENT;
944950
}
945951

952+
946953
return PROXY_RESULT_OK;
947954
}
948955

t/http_req_header.t

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,41 @@ location /t {
160160
}
161161
--- response_body
162162
bar
163+
164+
165+
166+
=== TEST 9: add header
167+
--- config
168+
location /t {
169+
access_by_lua_block {
170+
local wasm = require("resty.proxy-wasm")
171+
local plugin = assert(wasm.load("plugin", "t/testdata/http_header/main.go.wasm"))
172+
local ctx = assert(wasm.on_configure(plugin, 'req_hdr_add'))
173+
assert(wasm.on_http_request_headers(ctx))
174+
}
175+
content_by_lua_block {
176+
ngx.say(ngx.req.get_headers()["foo"])
177+
}
178+
}
179+
--- more_headers
180+
FOO: foo
181+
--- response_body
182+
foobar
183+
184+
185+
186+
=== TEST 10: add header, missing
187+
--- config
188+
location /t {
189+
access_by_lua_block {
190+
local wasm = require("resty.proxy-wasm")
191+
local plugin = assert(wasm.load("plugin", "t/testdata/http_header/main.go.wasm"))
192+
local ctx = assert(wasm.on_configure(plugin, 'req_hdr_add'))
193+
assert(wasm.on_http_request_headers(ctx))
194+
}
195+
content_by_lua_block {
196+
ngx.say(ngx.req.get_headers()["foo"])
197+
}
198+
}
199+
--- response_body
200+
bar

t/testdata/http_header/main.go

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

6969
case "req_hdr_set":
7070
proxywasm.ReplaceHttpRequestHeader("foo", "bar")
71+
72+
case "req_hdr_add":
73+
proxywasm.AddHttpRequestHeader("foo", "bar")
7174
}
7275

7376
return types.ActionContinue

0 commit comments

Comments
 (0)