diff --git a/src/common/proxy_wasm/ngx_proxy_wasm_host.c b/src/common/proxy_wasm/ngx_proxy_wasm_host.c index 3e751241a..68700ade3 100644 --- a/src/common/proxy_wasm/ngx_proxy_wasm_host.c +++ b/src/common/proxy_wasm/ngx_proxy_wasm_host.c @@ -623,17 +623,13 @@ ngx_proxy_wasm_hfuncs_get_header_map_value(ngx_wavm_instance_t *instance, static ngx_int_t -ngx_proxy_wasm_hfuncs_set_header_map_pairs(ngx_wavm_instance_t *instance, - wasm_val_t args[], wasm_val_t rets[]) +ngx_proxy_wasm_hfuncs_set_header_check(ngx_wavm_instance_t *instance, + ngx_proxy_wasm_map_type_e map_type, wasm_val_t rets[]) { - ngx_int_t rc = NGX_ERROR; - ngx_proxy_wasm_map_type_e map_type; - ngx_array_t headers; - ngx_proxy_wasm_marshalled_map_t map; - ngx_proxy_wasm_exec_t *pwexec; - ngx_proxy_wasm_ctx_t *pwctx; + ngx_proxy_wasm_exec_t *pwexec; + ngx_proxy_wasm_ctx_t *pwctx; #ifdef NGX_WASM_HTTP - ngx_http_wasm_req_ctx_t *rctx; + ngx_http_wasm_req_ctx_t *rctx; rctx = ngx_http_proxy_wasm_get_rctx(instance); #endif @@ -641,27 +637,33 @@ ngx_proxy_wasm_hfuncs_set_header_map_pairs(ngx_wavm_instance_t *instance, pwexec = ngx_proxy_wasm_instance2pwexec(instance); pwctx = pwexec->parent; - map_type = args[0].of.i32; - map.len = args[2].of.i32; - map.data = NGX_WAVM_HOST_LIFT_SLICE(instance, args[1].of.i32, map.len); - - if (ngx_proxy_wasm_pairs_unmarshal(pwexec, &headers, &map) != NGX_OK) { - return ngx_proxy_wasm_result_err(rets); - } - switch (map_type) { #ifdef NGX_WASM_HTTP case NGX_PROXY_WASM_MAP_HTTP_REQUEST_HEADERS: - if (rctx->entered_header_filter) { - ngx_wavm_log_error(NGX_LOG_ERR, instance->log, NULL, - "cannot set request headers: response produced"); - return ngx_proxy_wasm_result_ok(rets); + /* check context */ + + switch (pwctx->step) { + case NGX_PROXY_WASM_STEP_REQ_HEADERS: + case NGX_PROXY_WASM_STEP_REQ_BODY: + case NGX_PROXY_WASM_STEP_REQ_TRAILERS: + break; + + case NGX_PROXY_WASM_STEP_DISPATCH_RESPONSE: + if (!rctx->entered_header_filter) { + return NGX_OK; + } + /* fallthrough */ + + default: + return ngx_proxy_wasm_result_trap(pwexec, + "can only set request headers " + "before response is produced", + rets, NGX_WAVM_BAD_USAGE); } - rc = ngx_http_wasm_clear_req_headers( - ngx_http_proxy_wasm_get_req(instance)); + ngx_wa_assert(!rctx->entered_header_filter); break; case NGX_PROXY_WASM_MAP_HTTP_RESPONSE_HEADERS: @@ -674,6 +676,13 @@ ngx_proxy_wasm_hfuncs_set_header_map_pairs(ngx_wavm_instance_t *instance, case NGX_PROXY_WASM_STEP_REQ_TRAILERS: case NGX_PROXY_WASM_STEP_RESP_HEADERS: break; + + case NGX_PROXY_WASM_STEP_DISPATCH_RESPONSE: + if (!rctx->r->header_sent) { + return NGX_OK; + } + /* fallthrough */ + default: return ngx_proxy_wasm_result_trap(pwexec, "can only set response headers " @@ -682,14 +691,54 @@ ngx_proxy_wasm_hfuncs_set_header_map_pairs(ngx_wavm_instance_t *instance, rets, NGX_WAVM_BAD_USAGE); } - if (rctx->r->header_sent) { - ngx_wavm_log_error(NGX_LOG_ERR, instance->log, NULL, - "cannot set response headers: " - "headers already sent"); + ngx_wa_assert(!rctx->r->header_sent); + break; +#endif - return ngx_proxy_wasm_result_ok(rets); - } + default: + ngx_wasm_log_error(NGX_LOG_WASM_NYI, instance->log, 0, + "NYI - set_map bad map_type: %d", map_type); + return ngx_proxy_wasm_result_badarg(rets); + } + + return NGX_OK; +} + + +static ngx_int_t +ngx_proxy_wasm_hfuncs_set_header_map_pairs(ngx_wavm_instance_t *instance, + wasm_val_t args[], wasm_val_t rets[]) +{ + ngx_int_t rc = NGX_ERROR; + ngx_proxy_wasm_map_type_e map_type; + ngx_array_t headers; + ngx_proxy_wasm_marshalled_map_t map; + ngx_proxy_wasm_exec_t *pwexec; + + pwexec = ngx_proxy_wasm_instance2pwexec(instance); + + map_type = args[0].of.i32; + map.len = args[2].of.i32; + map.data = NGX_WAVM_HOST_LIFT_SLICE(instance, args[1].of.i32, map.len); + + if (ngx_proxy_wasm_pairs_unmarshal(pwexec, &headers, &map) != NGX_OK) { + return ngx_proxy_wasm_result_err(rets); + } + rc = ngx_proxy_wasm_hfuncs_set_header_check(instance, map_type, rets); + if (rc != NGX_OK) { + return rc; + } + + switch (map_type) { + +#ifdef NGX_WASM_HTTP + case NGX_PROXY_WASM_MAP_HTTP_REQUEST_HEADERS: + rc = ngx_http_wasm_clear_req_headers( + ngx_http_proxy_wasm_get_req(instance)); + break; + + case NGX_PROXY_WASM_MAP_HTTP_RESPONSE_HEADERS: rc = ngx_http_wasm_clear_resp_headers( ngx_http_proxy_wasm_get_req(instance)); break; @@ -727,11 +776,6 @@ ngx_proxy_wasm_hfuncs_add_header_map_value(ngx_wavm_instance_t *instance, ngx_int_t rc; ngx_str_t key, value; ngx_proxy_wasm_map_type_e map_type; -#ifdef NGX_WASM_HTTP - ngx_http_wasm_req_ctx_t *rctx; - - rctx = ngx_http_proxy_wasm_get_rctx(instance); -#endif map_type = args[0].of.i32; key.len = args[2].of.i32; @@ -739,25 +783,10 @@ ngx_proxy_wasm_hfuncs_add_header_map_value(ngx_wavm_instance_t *instance, value.len = args[4].of.i32; value.data = NGX_WAVM_HOST_LIFT_SLICE(instance, args[3].of.i32, value.len); -#ifdef NGX_WASM_HTTP - if (map_type == NGX_PROXY_WASM_MAP_HTTP_REQUEST_HEADERS - && rctx->entered_header_filter) - { - ngx_wavm_log_error(NGX_LOG_ERR, instance->log, NULL, - "cannot add request header: response produced"); - - return ngx_proxy_wasm_result_ok(rets); - - } else if (map_type == NGX_PROXY_WASM_MAP_HTTP_RESPONSE_HEADERS - && rctx->r->header_sent) - { - ngx_wavm_log_error(NGX_LOG_ERR, instance->log, NULL, - "cannot add response header: " - "headers already sent"); - - return ngx_proxy_wasm_result_ok(rets); + rc = ngx_proxy_wasm_hfuncs_set_header_check(instance, map_type, rets); + if (rc != NGX_OK) { + return rc; } -#endif dd("adding '%.*s: %.*s' to map of type '%d'", (int) key.len, key.data, (int) value.len, value.data, map_type); @@ -781,11 +810,6 @@ ngx_proxy_wasm_hfuncs_replace_header_map_value(ngx_wavm_instance_t *instance, ngx_int_t rc; ngx_str_t key, value; ngx_proxy_wasm_map_type_e map_type; -#ifdef NGX_WASM_HTTP - ngx_http_wasm_req_ctx_t *rctx; - - rctx = ngx_http_proxy_wasm_get_rctx(instance); -#endif map_type = args[0].of.i32; key.len = args[2].of.i32; @@ -793,25 +817,10 @@ ngx_proxy_wasm_hfuncs_replace_header_map_value(ngx_wavm_instance_t *instance, value.len = args[4].of.i32; value.data = NGX_WAVM_HOST_LIFT_SLICE(instance, args[3].of.i32, value.len); -#ifdef NGX_WASM_HTTP - if (map_type == NGX_PROXY_WASM_MAP_HTTP_REQUEST_HEADERS - && rctx->entered_header_filter) - { - ngx_wavm_log_error(NGX_LOG_ERR, instance->log, NULL, - "cannot set request header: response produced"); - - return ngx_proxy_wasm_result_ok(rets); - - } else if (map_type == NGX_PROXY_WASM_MAP_HTTP_RESPONSE_HEADERS - && rctx->r->header_sent) - { - ngx_wavm_log_error(NGX_LOG_ERR, instance->log, NULL, - "cannot set response header: " - "headers already sent"); - - return ngx_proxy_wasm_result_ok(rets); + rc = ngx_proxy_wasm_hfuncs_set_header_check(instance, map_type, rets); + if (rc != NGX_OK) { + return rc; } -#endif dd("setting '%.*s: %.*s' into map of type '%d'", (int) key.len, key.data, (int) value.len, value.data, map_type); @@ -842,6 +851,11 @@ ngx_proxy_wasm_hfuncs_remove_header_map_value(ngx_wavm_instance_t *instance, klen = args[2].of.i32; key = NGX_WAVM_HOST_LIFT_SLICE(instance, args[1].of.i32, klen); + rc = ngx_proxy_wasm_hfuncs_set_header_check(instance, map_type, rets); + if (rc != NGX_OK) { + return rc; + } + dd("removing '%.*s' from map of type '%d'", (int) klen, (u_char *) key, map_type); diff --git a/t/03-proxy_wasm/hfuncs/107-proxy_add_http_request_header.t b/t/03-proxy_wasm/hfuncs/107-proxy_add_http_request_header.t index 5b7181763..c19c4befe 100644 --- a/t/03-proxy_wasm/hfuncs/107-proxy_add_http_request_header.t +++ b/t/03-proxy_wasm/hfuncs/107-proxy_add_http_request_header.t @@ -547,8 +547,7 @@ qr/.*? on_request_headers, 3 headers.* -=== TEST 27: proxy_wasm - add_http_request_header() x on_phases -should log an error (but no trap) when response is produced +=== TEST 27: proxy_wasm - add_http_request_header() x on_request_headers --- wasm_modules: hostcalls --- config location /t { @@ -556,26 +555,97 @@ should log an error (but no trap) when response is produced test=/t/add_request_header'; proxy_wasm hostcalls 'on=request_headers \ test=/t/echo/headers'; + } +--- more_headers +Hello: world +pwm-add-req-header: Hello=world +--- response_body_like +Hello: world +Hello: world +--- grep_error_log eval: qr/\[(error|hostcalls)\] [^,]*/ +--- grep_error_log_out +[hostcalls] on_request_headers +[hostcalls] testing in "RequestHeaders" +[hostcalls] on_request_headers +[hostcalls] testing in "RequestHeaders" +[hostcalls] on_response_headers +[hostcalls] on_response_headers +[hostcalls] on_response_body +[hostcalls] on_response_body +[hostcalls] on_done while logging request +[hostcalls] on_log while logging request +[hostcalls] on_done while logging request +[hostcalls] on_log while logging request +--- no_error_log +[warn] +[crit] +[alert] + + + +=== TEST 28: proxy_wasm - set_http_request_headers() x on_response_headers +--- wasm_modules: hostcalls +--- config + location /t { proxy_wasm hostcalls 'on=response_headers \ test=/t/add_request_header'; - proxy_wasm hostcalls 'on=log \ - test=/t/add_request_header'; + + return 200; } --- more_headers Hello: world pwm-add-req-header: Hello=world ---- response_body_like +--- grep_error_log eval: qr/.*?host trap.*/ +--- grep_error_log_out eval +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set request headers before response is produced.*~ +--- no_error_log +[warn] +[crit] +[alert] +[emerg] + + + +=== TEST 29: proxy_wasm - set_http_request_headers() x on_response_body +--- wasm_modules: hostcalls +--- config + location /t { + proxy_wasm hostcalls 'on=response_body \ + test=/t/add_request_header'; + + return 200; + } +--- more_headers Hello: world +pwm-add-req-header: Hello=world +--- grep_error_log eval: qr/.*?host trap.*/ +--- grep_error_log_out eval +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set request headers before response is produced.*~ +--- no_error_log +[warn] +[crit] +[alert] +[emerg] + + + +=== TEST 30: proxy_wasm - set_http_request_headers() x on_log +--- wasm_modules: hostcalls +--- config + location /t { + proxy_wasm hostcalls 'on=log \ + test=/t/add_request_header'; + + return 200; + } +--- more_headers Hello: world ---- grep_error_log eval: qr/\[(info|error)\] .*? (testing in|cannot add).*/ +pwm-add-req-header: Hello=world +--- grep_error_log eval: qr/.*?host trap.*/ --- grep_error_log_out eval -qr/.*? testing in "RequestHeaders".* -.*? testing in "RequestHeaders".* -.*? testing in "ResponseHeaders".* -.*? cannot add request header: response produced.* -.*? testing in "Log".* -.*? cannot add request header: response produced.*/ +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set request headers before response is produced.*~ --- no_error_log [warn] [crit] [alert] +[emerg] diff --git a/t/03-proxy_wasm/hfuncs/108-proxy_add_http_response_header.t b/t/03-proxy_wasm/hfuncs/108-proxy_add_http_response_header.t index 5daba5ba6..9662cb0ab 100644 --- a/t/03-proxy_wasm/hfuncs/108-proxy_add_http_response_header.t +++ b/t/03-proxy_wasm/hfuncs/108-proxy_add_http_response_header.t @@ -375,8 +375,7 @@ resp Cache-Control: no-cache.*/ -=== TEST 15: proxy_wasm - add_http_response_header() x on_phases -should log an error (but no trap) when headers are sent +=== TEST 15: proxy_wasm - add_http_response_header() x valid phases --- wasm_modules: hostcalls --- config location /t { @@ -387,27 +386,65 @@ should log an error (but no trap) when headers are sent proxy_wasm hostcalls 'on=response_headers \ test=/t/add_response_header \ value=From:response_headers'; + return 200; + } +--- response_headers +From: request_headers, response_headers +--- ignore_response_body +--- grep_error_log eval: qr/\[(error|hostcalls)\] [^,]*/ +--- grep_error_log_out +[hostcalls] on_request_headers +[hostcalls] testing in "RequestHeaders" +[hostcalls] on_request_headers +[hostcalls] on_response_headers +[hostcalls] on_response_headers +[hostcalls] testing in "ResponseHeaders" +[hostcalls] on_response_body +[hostcalls] on_response_body +[hostcalls] on_done while logging request +[hostcalls] on_log while logging request +[hostcalls] on_done while logging request +[hostcalls] on_log while logging request +--- no_error_log +[crit] +[emerg] + + +=== TEST 16: proxy_wasm - add_http_response_header() x on_response_body +--- wasm_modules: hostcalls +--- config + location /t { proxy_wasm hostcalls 'on=response_body \ test=/t/add_response_header \ - value=From:response_body'; + value=From:request_headers'; + return 200; + } +--- ignore_response_body +--- grep_error_log eval: qr/.*?host trap.*/ +--- grep_error_log_out eval +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set response headers on request path before "on_response_body".*~ +--- no_error_log +[crit] +[warn] +[alert] + + +=== TEST 17: proxy_wasm - add_http_response_header() x on_log +--- wasm_modules: hostcalls +--- config + location /t { proxy_wasm hostcalls 'on=log \ test=/t/add_response_header \ - value=From:log'; + value=From:request_headers'; return 200; } ---- response_headers -From: request_headers, response_headers --- ignore_response_body ---- grep_error_log eval: qr/\[(error|hostcalls)\] [^on_].*/ +--- grep_error_log eval: qr/.*?host trap.*/ --- grep_error_log_out eval -qr/.*? testing in "RequestHeaders".* -.*? testing in "ResponseHeaders".* -.*? testing in "ResponseBody".* -\[error\] .*? \[wasm\] cannot add response header: headers already sent.* -.*? testing in "Log".* -\[error\] .*? \[wasm\] cannot add response header: headers already sent.*/ +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set response headers on request path before "on_response_body".*~ --- no_error_log [crit] +[alert] [emerg] diff --git a/t/03-proxy_wasm/hfuncs/109-proxy_set_http_request_headers.t b/t/03-proxy_wasm/hfuncs/109-proxy_set_http_request_headers.t index 8c272386d..8c117eb15 100644 --- a/t/03-proxy_wasm/hfuncs/109-proxy_set_http_request_headers.t +++ b/t/03-proxy_wasm/hfuncs/109-proxy_set_http_request_headers.t @@ -102,8 +102,7 @@ Welcome: wasm -=== TEST 5: proxy_wasm - set_http_request_headers() x on_phases -should log an error (but no trap) when response is produced +=== TEST 5: proxy_wasm - set_http_request_headers() x request_headers phase --- wasm_modules: hostcalls --- config location /t { @@ -111,24 +110,86 @@ should log an error (but no trap) when response is produced test=/t/set_request_headers'; proxy_wasm hostcalls 'on=request_headers \ test=/t/echo/headers'; - proxy_wasm hostcalls 'on=response_headers \ - test=/t/set_request_headers'; - proxy_wasm hostcalls 'on=log \ - test=/t/set_request_headers'; } --- more_headers eval CORE::join "\n", map { "Header$_: value-$_" } 1..20 --- response_body Hello: world Welcome: wasm ---- grep_error_log eval: qr/\[(error|hostcalls)\] [^on_].*/ +--- grep_error_log eval: qr/\[(error|hostcalls)\] [^,]*/ +--- grep_error_log_out +[hostcalls] on_request_headers +[hostcalls] testing in "RequestHeaders" +[hostcalls] on_request_headers +[hostcalls] testing in "RequestHeaders" +[hostcalls] on_response_headers +[hostcalls] on_response_headers +[hostcalls] on_response_body +[hostcalls] on_response_body +[hostcalls] on_done while logging request +[hostcalls] on_log while logging request +[hostcalls] on_done while logging request +[hostcalls] on_log while logging request +--- no_error_log +[warn] +[crit] + + + +=== TEST 6: proxy_wasm - set_http_request_headers() x on_response_headers +--- wasm_modules: hostcalls +--- config + location /t { + proxy_wasm hostcalls 'on=response_headers \ + test=/t/set_request_headers \ + value=From:response_headers'; + + return 200; + } +--- grep_error_log eval: qr/.*?host trap.*/ --- grep_error_log_out eval -qr/.*? testing in "RequestHeaders".* -.*? testing in "RequestHeaders".* -.*? testing in "ResponseHeaders".* -\[error\] .*? \[wasm\] cannot set request headers: response produced.* -.*? testing in "Log".* -\[error\] .*? \[wasm\] cannot set request headers: response produced.*/ +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set request headers before response is produced.*~ --- no_error_log +[crit] [warn] +[alert] + + + +=== TEST 7: proxy_wasm - set_http_request_headers() x on_response_body +--- wasm_modules: hostcalls +--- config + location /t { + proxy_wasm hostcalls 'on=response_body \ + test=/t/set_request_headers \ + value=From:response_body'; + + return 200; + } +--- grep_error_log eval: qr/.*?host trap.*/ +--- grep_error_log_out eval +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set request headers before response is produced.*~ +--- no_error_log [crit] +[warn] +[alert] + + + +=== TEST 8: proxy_wasm - set_http_request_headers() x on_log +--- wasm_modules: hostcalls +--- config + location /t { + proxy_wasm hostcalls 'on=log \ + test=/t/set_request_headers \ + value=From:log'; + + return 200; + } +--- grep_error_log eval: qr/.*?host trap.*/ +--- grep_error_log_out eval +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set request headers before response is produced.*~ +--- no_error_log +[crit] +[warn] +[alert] diff --git a/t/03-proxy_wasm/hfuncs/110-proxy_set_http_response_headers.t b/t/03-proxy_wasm/hfuncs/110-proxy_set_http_response_headers.t index efc628017..0167eb121 100644 --- a/t/03-proxy_wasm/hfuncs/110-proxy_set_http_response_headers.t +++ b/t/03-proxy_wasm/hfuncs/110-proxy_set_http_response_headers.t @@ -104,46 +104,73 @@ qr/\[error\] .*? \[wasm\] attempt to set invalid Connection response header: "cl -=== TEST 4: proxy_wasm - set_http_response_headers() x on_phases -should log an error (but no trap) when headers are sent +=== TEST 4: proxy_wasm - set_http_response_headers() x valid phases --- wasm_modules: hostcalls --- config location /t { proxy_wasm hostcalls 'on=request_headers \ test=/t/set_response_headers \ - value=From:request_headers'; + value=From-One:request_headers'; proxy_wasm hostcalls 'on=response_headers \ test=/t/set_response_headers \ - value=From:response_headers'; + value=From-Two:response_headers'; + return 200; + } +--- response_headers +From-Two: response_headers +--- ignore_response_body +--- grep_error_log eval: qr/(\[error\].*|testing in \"[^"]*\")/ +--- grep_error_log_out +testing in "RequestHeaders" +testing in "ResponseHeaders" +--- no_error_log +[warn] +[crit] + + +=== TEST 5: proxy_wasm - set_http_response_headers() x on_response_body +--- wasm_modules: hostcalls +--- config + location /t { proxy_wasm hostcalls 'on=response_body \ test=/t/set_response_headers \ value=From:response_body'; + return 200; + } +--- grep_error_log eval: qr/.*?host trap.*/ +--- grep_error_log_out eval +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set response headers on request path before "on_response_body".*~ +--- no_error_log +[warn] +[crit] +[alert] + + + +=== TEST 6: proxy_wasm - set_http_response_headers() x on_log +--- wasm_modules: hostcalls +--- config + location /t { proxy_wasm hostcalls 'on=log \ test=/t/set_response_headers \ value=From:log'; + return 200; } ---- response_headers -From: response_headers ---- ignore_response_body ---- grep_error_log eval: qr/(\[error\]|testing in) .*/ +--- grep_error_log eval: qr/.*?host trap.*/ --- grep_error_log_out eval -qr/testing in "RequestHeaders".* -testing in "ResponseHeaders".* -testing in "ResponseBody".* -\[error\] .*? \[wasm\] cannot set response headers: headers already sent.* -testing in "Log".* -\[error\] .*? \[wasm\] cannot set response headers: headers already sent/ +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set response headers on request path before "on_response_body".*~ --- no_error_log [warn] [crit] +[alert] -=== TEST 5: proxy_wasm - set_http_response_headers() sets :status response header at request_headers +=== TEST 7: proxy_wasm - set_http_response_headers() sets :status response header at request_headers --- load_nginx_modules: ngx_http_headers_more_filter_module --- wasm_modules: ngx_rust_tests hostcalls --- config @@ -166,7 +193,7 @@ Content-Length: 0\r -=== TEST 6: proxy_wasm - set_http_response_headers() sets :status response header +=== TEST 8: proxy_wasm - set_http_response_headers() sets :status response header --- load_nginx_modules: ngx_http_headers_more_filter_module --- wasm_modules: ngx_rust_tests hostcalls --- config @@ -189,7 +216,7 @@ Connection: close\r -=== TEST 7: proxy_wasm - set_http_response_headers() ignores :status out of range +=== TEST 9: proxy_wasm - set_http_response_headers() ignores :status out of range --- load_nginx_modules: ngx_http_headers_more_filter_module --- wasm_modules: ngx_rust_tests hostcalls --- config @@ -212,7 +239,7 @@ Connection: close\r -=== TEST 8: proxy_wasm - set_http_response_headers() ignores invalid :status +=== TEST 10: proxy_wasm - set_http_response_headers() ignores invalid :status --- load_nginx_modules: ngx_http_headers_more_filter_module --- wasm_modules: ngx_rust_tests hostcalls --- config @@ -235,7 +262,7 @@ Connection: close\r -=== TEST 9: proxy_wasm - set_http_response_headers() ignores empty :status +=== TEST 11: proxy_wasm - set_http_response_headers() ignores empty :status --- load_nginx_modules: ngx_http_headers_more_filter_module --- wasm_modules: ngx_rust_tests hostcalls --- config diff --git a/t/03-proxy_wasm/hfuncs/111-proxy_set_http_request_header.t b/t/03-proxy_wasm/hfuncs/111-proxy_set_http_request_header.t index bd93456ef..c875f6c1f 100644 --- a/t/03-proxy_wasm/hfuncs/111-proxy_set_http_request_header.t +++ b/t/03-proxy_wasm/hfuncs/111-proxy_set_http_request_header.t @@ -375,8 +375,7 @@ qr/\[error\] .*? \[wasm\] cannot set read-only ":scheme" header/ -=== TEST 15: proxy_wasm - set_http_request_header() x on_phases -should log an error (but no trap) when response is produced +=== TEST 15: proxy_wasm - set_http_request_headers() x request_headers phase --- wasm_modules: hostcalls --- config location /t { @@ -385,25 +384,85 @@ should log an error (but no trap) when response is produced value=From:request_headers'; proxy_wasm hostcalls 'on=request_headers \ test=/t/echo/headers'; + } +--- more_headers +From: client +--- response_body_like +From: request_headers +--- grep_error_log eval: qr/\[(error|hostcalls)\] [^,]*/ +--- grep_error_log_out +[hostcalls] on_request_headers +[hostcalls] testing in "RequestHeaders" +[hostcalls] on_request_headers +[hostcalls] testing in "RequestHeaders" +[hostcalls] on_response_headers +[hostcalls] on_response_headers +[hostcalls] on_response_body +[hostcalls] on_response_body +[hostcalls] on_done while logging request +[hostcalls] on_log while logging request +[hostcalls] on_done while logging request +[hostcalls] on_log while logging request +--- no_error_log +[warn] +[crit] + + + +=== TEST 16: proxy_wasm - set_http_request_headers() x on_response_headers +--- wasm_modules: hostcalls +--- config + location /t { proxy_wasm hostcalls 'on=response_headers \ test=/t/set_request_header \ value=From:response_headers'; + + return 200; + } +--- grep_error_log eval: qr/.*?host trap.*/ +--- grep_error_log_out eval +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set request headers before response is produced.*~ +--- no_error_log +[warn] +[crit] +[alert] + + + +=== TEST 17: proxy_wasm - set_http_request_headers() x on_response_body +--- wasm_modules: hostcalls +--- config + location /t { + proxy_wasm hostcalls 'on=response_body \ + test=/t/set_request_header \ + value=From:response_body'; + + return 200; + } +--- grep_error_log eval: qr/.*?host trap.*/ +--- grep_error_log_out eval +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set request headers before response is produced.*~ +--- no_error_log +[warn] +[crit] +[alert] + + + +=== TEST 18: proxy_wasm - set_http_request_headers() x on_log +--- wasm_modules: hostcalls +--- config + location /t { proxy_wasm hostcalls 'on=log \ test=/t/set_request_header \ value=From:log'; + + return 200; } ---- more_headers -From: client ---- response_body_like -From: request_headers ---- grep_error_log eval: qr/\[(error|hostcalls)\] [^on_].*/ +--- grep_error_log eval: qr/.*?host trap.*/ --- grep_error_log_out eval -qr/.*? testing in "RequestHeaders".* -.*? testing in "RequestHeaders".* -.*? testing in "ResponseHeaders".* -\[error\] .*? \[wasm\] cannot set request header: response produced.* -.*? testing in "Log".* -\[error\] .*? \[wasm\] cannot set request header: response produced.*/ +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set request headers before response is produced.*~ --- no_error_log +[warn] [crit] [alert] diff --git a/t/03-proxy_wasm/hfuncs/112-proxy_set_http_response_header.t b/t/03-proxy_wasm/hfuncs/112-proxy_set_http_response_header.t index 2ed71b2da..2ac07a2c5 100644 --- a/t/03-proxy_wasm/hfuncs/112-proxy_set_http_response_header.t +++ b/t/03-proxy_wasm/hfuncs/112-proxy_set_http_response_header.t @@ -308,8 +308,7 @@ resp Cache-Control: no-cache.*/ -=== TEST 12: proxy_wasm - set_http_response_header() x on_phases -should log an error (but no trap) when headers are sent +=== TEST 12: proxy_wasm - set_http_response_header() x valid phases --- wasm_modules: hostcalls --- config location /t { @@ -320,27 +319,55 @@ should log an error (but no trap) when headers are sent proxy_wasm hostcalls 'on=response_headers \ test=/t/set_response_header \ value=From:response_headers'; + return 200; + } +--- response_headers +From: response_headers +--- ignore_response_body +--- grep_error_log eval: qr/(\[error\].*|testing in \"[^"]*\")/ +--- grep_error_log_out +testing in "RequestHeaders" +testing in "ResponseHeaders" +--- no_error_log +[warn] +[crit] + + +=== TEST 13: proxy_wasm - set_http_response_header() x on_response_body +--- wasm_modules: hostcalls +--- config + location /t { proxy_wasm hostcalls 'on=response_body \ test=/t/set_response_header \ value=From:response_body'; + return 200; + } +--- grep_error_log eval: qr/.*?host trap.*/ +--- grep_error_log_out eval +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set response headers on request path before "on_response_body".*~ +--- no_error_log +[warn] +[crit] +[alert] + + + +=== TEST 14: proxy_wasm - set_http_response_header() x on_log +--- wasm_modules: hostcalls +--- config + location /t { proxy_wasm hostcalls 'on=log \ test=/t/set_response_header \ value=From:log'; + return 200; } ---- response_headers -From: response_headers ---- ignore_response_body ---- grep_error_log eval: qr/\[(error|hostcalls)\] [^on_].*/ +--- grep_error_log eval: qr/.*?host trap.*/ --- grep_error_log_out eval -qr/.*? testing in "RequestHeaders".* -.*? testing in "ResponseHeaders".* -.*? testing in "ResponseBody".* -\[error\] .*? \[wasm\] cannot set response header: headers already sent.* -.*? testing in "Log".* -\[error\] .*? \[wasm\] cannot set response header: headers already sent.*/ +qr~(\[error\]|Uncaught RuntimeError|\s+).*?host trap \(bad usage\): can only set response headers on request path before "on_response_body".*~ --- no_error_log [warn] [crit] +[alert]