Skip to content

Commit

Permalink
fix(proxy-wasm) always execute the whole filter chain on responses
Browse files Browse the repository at this point in the history
Co-authored-by: Caio Ramos Casimiro <[email protected]>
  • Loading branch information
thibaultcha and casimiro committed Oct 5, 2023
1 parent 89170ed commit d766a8d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/common/proxy_wasm/ngx_proxy_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,14 @@ ngx_proxy_wasm_resume(ngx_proxy_wasm_ctx_t *pwctx,
case NGX_PROXY_WASM_STEP_TICK:
case NGX_PROXY_WASM_STEP_DONE:
case NGX_PROXY_WASM_STEP_RESP_BODY:
case NGX_PROXY_WASM_STEP_RESP_HEADERS:
case NGX_PROXY_WASM_STEP_DISPATCH_RESPONSE:
break;
case NGX_PROXY_WASM_STEP_RESP_HEADERS:
if (pwctx->last_step < NGX_PROXY_WASM_STEP_RESP_HEADERS) {
/* first execution of response phases, ensure the chain is reset */
ngx_proxy_wasm_ctx_reset_chain(pwctx);
}

break;
default:
if (step <= pwctx->last_step) {
Expand Down
12 changes: 12 additions & 0 deletions src/common/proxy_wasm/ngx_proxy_wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,18 @@ ngx_proxy_wasm_ctx_set_next_action(ngx_proxy_wasm_ctx_t *pwctx,
}


static ngx_inline void
ngx_proxy_wasm_ctx_reset_chain(ngx_proxy_wasm_ctx_t *pwctx)
{
ngx_proxy_wasm_log_error(NGX_LOG_DEBUG, pwctx->log, 0,
"resetting filter chain: pwctx->exec_index "
"%ld to 0 (pwctx: %p)",
pwctx->exec_index, pwctx);

pwctx->exec_index = 0;
}


static ngx_inline ngx_proxy_wasm_exec_t *
ngx_proxy_wasm_instance2pwexec(ngx_wavm_instance_t *instance)
{
Expand Down
35 changes: 33 additions & 2 deletions t/03-proxy_wasm/hfuncs/102-proxy_send_local_response.t
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,38 @@ Hello world
=== TEST 18: proxy_wasm - send_local_response() in chained filters
=== TEST 18: proxy_wasm - send_local_response() executes all chained filters response steps
should run all response steps of all chained filters
--- wasm_modules: hostcalls
--- config
location /t {
proxy_wasm hostcalls;
proxy_wasm hostcalls 'test=/t/send_local_response/body';
proxy_wasm hostcalls;
}
--- response_body
Hello world
--- grep_error_log eval: qr/\[info\] .*? on_(request|response|log).*/
--- grep_error_log_out eval
qr/\A.*? on_request_headers, \d+ headers.*
.*? on_request_headers, \d+ headers.*
.*? on_response_headers, \d+ headers.*
.*? on_response_headers, \d+ headers.*
.*? on_response_headers, \d+ headers.*
.*? on_response_body, \d+ bytes, eof: true.*
.*? on_response_body, \d+ bytes, eof: true.*
.*? on_response_body, \d+ bytes, eof: true.*
.*? on_log.*
.*? on_log.*
.*? on_log/
--- no_error_log
[error]
[crit]
[alert]
=== TEST 19: proxy_wasm - send_local_response() invoked twice in chained filters
should interrupt the current phase, preventing "response already stashed"
should still run all response phases
--- wasm_modules: hostcalls
Expand Down Expand Up @@ -445,7 +476,7 @@ qr/.*? on_request_headers, \d+ headers.*
=== TEST 19: proxy_wasm - send_local_response() in chained filters as a subrequest
=== TEST 20: proxy_wasm - send_local_response() in chained filters as a subrequest
should interrupt the current phase, preventing "response already stashed"
should still run all response phases
should not have a log phase (subrequest)
Expand Down

0 comments on commit d766a8d

Please sign in to comment.