From b9a1450479353d240d63c0d2ee70338787e5a0ab Mon Sep 17 00:00:00 2001 From: Jun Ouyang Date: Mon, 1 Jul 2024 11:29:32 +0800 Subject: [PATCH 01/11] fix(core): fix lua-nginx-module context was clear when ngx.send_header() trigger filter_finalize case --- ...fix-lua-context-clean-by-send-header.patch | 45 +++++++++++++++++++ ...-fix-ngx-send-header-filter-finalize-ctx.t | 39 ++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 build/openresty/patches/fix-lua-context-clean-by-send-header.patch create mode 100644 t/04-patch/03-fix-ngx-send-header-filter-finalize-ctx.t diff --git a/build/openresty/patches/fix-lua-context-clean-by-send-header.patch b/build/openresty/patches/fix-lua-context-clean-by-send-header.patch new file mode 100644 index 00000000000..4db81ee59cb --- /dev/null +++ b/build/openresty/patches/fix-lua-context-clean-by-send-header.patch @@ -0,0 +1,45 @@ +diff --git a/bundle/ngx_lua-0.10.26/src/ngx_http_lua_util.c b/bundle/ngx_lua-0.10.26/src/ngx_http_lua_util.c +index 8fd2656..b2fdb6c 100644 +--- a/bundle/ngx_lua-0.10.26/src/ngx_http_lua_util.c ++++ b/bundle/ngx_lua-0.10.26/src/ngx_http_lua_util.c +@@ -549,6 +549,10 @@ ngx_http_lua_send_header_if_needed(ngx_http_request_t *r, + if (!ctx->buffering) { + dd("sending headers"); + rc = ngx_http_send_header(r); ++ if (r->filter_finalize) { ++ ngx_http_set_ctx(r, ctx, ngx_http_lua_module); ++ } ++ + ctx->header_sent = 1; + return rc; + } +diff --git a/bundle/ngx_lua-0.10.26/t/002-content.t b/bundle/ngx_lua-0.10.26/t/002-content.t +index 54de40e..eb9d587 100644 +--- a/bundle/ngx_lua-0.10.26/t/002-content.t ++++ b/bundle/ngx_lua-0.10.26/t/002-content.t +@@ -1098,3 +1098,25 @@ failed to load inlined Lua code: content_by_lua(...45678901234567890123456789012 + GET /lua + --- response_body_like: 503 Service Temporarily Unavailable + --- error_code: 503 ++ ++ ++ ++=== TEST 52: send_header trigger filter finalize does not clear the ctx ++--- config ++ location /lua { ++ content_by_lua_block { ++ ngx.header["Last-Modified"] = ngx.http_time(ngx.time()) ++ ngx.send_headers() ++ local phase = ngx.get_phase() ++ } ++ header_filter_by_lua_block { ++ ngx.header["X-Hello-World"] = "Hello World" ++ } ++ } ++--- request ++GET /lua ++--- more_headers ++If-Unmodified-Since: Wed, 01 Jan 2020 07:28:00 GMT ++--- error_code: 412 ++--- no_error_log ++unknown phase: 0 diff --git a/t/04-patch/03-fix-ngx-send-header-filter-finalize-ctx.t b/t/04-patch/03-fix-ngx-send-header-filter-finalize-ctx.t new file mode 100644 index 00000000000..a4cc5c1644c --- /dev/null +++ b/t/04-patch/03-fix-ngx-send-header-filter-finalize-ctx.t @@ -0,0 +1,39 @@ +# vim:set ft= ts=4 sw=4 et fdm=marker: + +use Test::Nginx::Socket::Lua; + +#worker_connections(1014); +#master_on(); +#workers(2); +#log_level('warn'); + +repeat_each(2); +#repeat_each(1); + +plan tests => repeat_each() * (blocks() * 2); + +#no_diff(); +#no_long_string(); +run_tests(); + +__DATA__ + +=== TEST 1: send_header trigger filter finalize does not clear the ctx +--- config + location /lua { + content_by_lua_block { + ngx.header["Last-Modified"] = ngx.http_time(ngx.time()) + ngx.send_headers() + local phase = ngx.get_phase() + } + header_filter_by_lua_block { + ngx.header["X-Hello-World"] = "Hello World" + } + } +--- request +GET /lua +--- more_headers +If-Unmodified-Since: Wed, 01 Jan 2020 07:28:00 GMT +--- error_code: 412 +--- no_error_log +unknown phase: 0 From 5ea11ef81ea939379b5cb554cd0cdc168fbeda6e Mon Sep 17 00:00:00 2001 From: Jun Ouyang Date: Mon, 1 Jul 2024 11:42:58 +0800 Subject: [PATCH 02/11] fix: fix code --- .../kong/fix-filter-finalize-in-send-header-clear-context.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml diff --git a/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml new file mode 100644 index 00000000000..d8f323d3433 --- /dev/null +++ b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml @@ -0,0 +1,3 @@ +message: fix lua-nginx-module context was clear when ngx.send_header() trigger filter_finalize case [openresty/lua-nginx-module#2323](https://github.com/openresty/lua-nginx-module/pull/2323) +type: bugfix +scope: Core \ No newline at end of file From e51e4ecfc7bdc795f919b6f6530c71c548ec5a61 Mon Sep 17 00:00:00 2001 From: Jun Ouyang Date: Mon, 1 Jul 2024 11:44:16 +0800 Subject: [PATCH 03/11] fix: fix code --- ...=> nginx-1.25.3_07-fix-lua-context-clean-by-send-header.patch} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename build/openresty/patches/{fix-lua-context-clean-by-send-header.patch => nginx-1.25.3_07-fix-lua-context-clean-by-send-header.patch} (100%) diff --git a/build/openresty/patches/fix-lua-context-clean-by-send-header.patch b/build/openresty/patches/nginx-1.25.3_07-fix-lua-context-clean-by-send-header.patch similarity index 100% rename from build/openresty/patches/fix-lua-context-clean-by-send-header.patch rename to build/openresty/patches/nginx-1.25.3_07-fix-lua-context-clean-by-send-header.patch From 0165bcfcaef3d224a51543e3e23c8b8745cafdec Mon Sep 17 00:00:00 2001 From: Jun Ouyang Date: Mon, 1 Jul 2024 11:48:41 +0800 Subject: [PATCH 04/11] fix: fix code --- .../kong/fix-filter-finalize-in-send-header-clear-context.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml index d8f323d3433..1f163697deb 100644 --- a/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml +++ b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml @@ -1,3 +1,3 @@ -message: fix lua-nginx-module context was clear when ngx.send_header() trigger filter_finalize case [openresty/lua-nginx-module#2323](https://github.com/openresty/lua-nginx-module/pull/2323) +message: fix an issues where lua-nginx-module context was clear when ngx.send_header() trigger filter_finalize case [openresty/lua-nginx-module#2323](https://github.com/openresty/lua-nginx-module/pull/2323) type: bugfix scope: Core \ No newline at end of file From 3e29a33b63bb66ad372be9c4751fb7bfdf2c4603 Mon Sep 17 00:00:00 2001 From: Jun Ouyang Date: Mon, 1 Jul 2024 13:39:06 +0800 Subject: [PATCH 05/11] fix: fix code --- .../kong/fix-filter-finalize-in-send-header-clear-context.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml index 1f163697deb..7ad88884925 100644 --- a/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml +++ b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml @@ -1,3 +1,3 @@ -message: fix an issues where lua-nginx-module context was clear when ngx.send_header() trigger filter_finalize case [openresty/lua-nginx-module#2323](https://github.com/openresty/lua-nginx-module/pull/2323) +message: fix an issues where lua-nginx-module context was cleared when ngx.send_header() trigger filter_finalize case [openresty/lua-nginx-module#2323](https://github.com/openresty/lua-nginx-module/pull/2323) type: bugfix scope: Core \ No newline at end of file From 74ebf6ed624ccc321221d08ce34933c1ba33d2dc Mon Sep 17 00:00:00 2001 From: Jun Ouyang Date: Mon, 1 Jul 2024 13:58:14 +0800 Subject: [PATCH 06/11] fix: fix code --- .../kong/fix-filter-finalize-in-send-header-clear-context.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml index 7ad88884925..4578ddef462 100644 --- a/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml +++ b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml @@ -1,3 +1,3 @@ -message: fix an issues where lua-nginx-module context was cleared when ngx.send_header() trigger filter_finalize case [openresty/lua-nginx-module#2323](https://github.com/openresty/lua-nginx-module/pull/2323) +message: fix an issue where lua-nginx-module context was cleared when ngx.send_header() trigger filter_finalize case [openresty/lua-nginx-module#2323](https://github.com/openresty/lua-nginx-module/pull/2323) type: bugfix scope: Core \ No newline at end of file From ebdd646f0136a03ec6089b78a12b6aea42c60d2e Mon Sep 17 00:00:00 2001 From: Jun Ouyang Date: Tue, 2 Jul 2024 17:38:11 +0800 Subject: [PATCH 07/11] Update changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml Co-authored-by: Keery Nie --- .../kong/fix-filter-finalize-in-send-header-clear-context.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml index 4578ddef462..a9dda1c5683 100644 --- a/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml +++ b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml @@ -1,3 +1,3 @@ -message: fix an issue where lua-nginx-module context was cleared when ngx.send_header() trigger filter_finalize case [openresty/lua-nginx-module#2323](https://github.com/openresty/lua-nginx-module/pull/2323) +message: Fixed an issue where lua-nginx-module context was cleared when ngx.send_header() trigger filter_finalize case [openresty/lua-nginx-module#2323](https://github.com/openresty/lua-nginx-module/pull/2323) type: bugfix scope: Core \ No newline at end of file From 3babd7106ec1753db6a5b1ff6950a940903baeda Mon Sep 17 00:00:00 2001 From: Jun Ouyang Date: Sun, 14 Jul 2024 15:53:42 +0800 Subject: [PATCH 08/11] feat: fix code --- spec/02-integration/05-proxy/24-buffered_spec.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/02-integration/05-proxy/24-buffered_spec.lua b/spec/02-integration/05-proxy/24-buffered_spec.lua index c95cd726678..e7fd0125c81 100644 --- a/spec/02-integration/05-proxy/24-buffered_spec.lua +++ b/spec/02-integration/05-proxy/24-buffered_spec.lua @@ -1,6 +1,6 @@ local helpers = require "spec.helpers" local cjson = require "cjson" - +local http_mock = require "spec.helpers.http_mock" local md5 = ngx.md5 local TCP_PORT = helpers.get_available_port() @@ -255,8 +255,8 @@ for _, strategy in helpers.each_strategy() do -- to produce an nginx output filter error and status code 412 -- the response has to go through kong_error_handler (via error_page) it("remains healthy when if-match header is used with buffering", function() - local thread = helpers.tcp_server(TCP_PORT) - + mock = http_mock.new(TCP_PORT) + mock:start() local res = assert(proxy_client:send { method = "GET", path = "/0", @@ -265,9 +265,9 @@ for _, strategy in helpers.each_strategy() do } }) - thread:join() assert.response(res).has_status(412) assert.logfile().has.no.line("exited on signal 11") + mock:stop(true) end) end) end) From c86b5d8e4e8e0a895acae67e210fe588f034341e Mon Sep 17 00:00:00 2001 From: Jun Ouyang Date: Sun, 14 Jul 2024 15:55:44 +0800 Subject: [PATCH 09/11] feat: fix code --- .../kong/fix-filter-finalize-in-send-header-clear-context.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml index a9dda1c5683..03f7170dd6f 100644 --- a/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml +++ b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml @@ -1,3 +1,3 @@ -message: Fixed an issue where lua-nginx-module context was cleared when ngx.send_header() trigger filter_finalize case [openresty/lua-nginx-module#2323](https://github.com/openresty/lua-nginx-module/pull/2323) +message: message: Fixed an issue where `lua-nginx-module` context was cleared when `ngx.send_header()` triggered `filter_finalize` [openresty/lua-nginx-module#2323](https://github.com/openresty/lua-nginx-module/pull/2323). type: bugfix scope: Core \ No newline at end of file From 97fbe6f940794e89ed2f6cd0bc1b8340eb0634a7 Mon Sep 17 00:00:00 2001 From: Jun Ouyang Date: Sun, 14 Jul 2024 15:59:02 +0800 Subject: [PATCH 10/11] feat: fix code --- .../kong/fix-filter-finalize-in-send-header-clear-context.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml index 03f7170dd6f..cac4566c7b4 100644 --- a/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml +++ b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml @@ -1,3 +1,3 @@ -message: message: Fixed an issue where `lua-nginx-module` context was cleared when `ngx.send_header()` triggered `filter_finalize` [openresty/lua-nginx-module#2323](https://github.com/openresty/lua-nginx-module/pull/2323). +message: Fixed an issue where `lua-nginx-module` context was cleared when `ngx.send_header()` triggered `filter_finalize` [openresty/lua-nginx-module#2323](https://github.com/openresty/lua-nginx-module/pull/2323). type: bugfix scope: Core \ No newline at end of file From 0c69a83acd8c501a111870bab89c8561b13e8cbe Mon Sep 17 00:00:00 2001 From: Jun Ouyang Date: Sun, 14 Jul 2024 16:09:32 +0800 Subject: [PATCH 11/11] feat: fix code --- spec/02-integration/05-proxy/24-buffered_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/02-integration/05-proxy/24-buffered_spec.lua b/spec/02-integration/05-proxy/24-buffered_spec.lua index e7fd0125c81..15e639c0bb5 100644 --- a/spec/02-integration/05-proxy/24-buffered_spec.lua +++ b/spec/02-integration/05-proxy/24-buffered_spec.lua @@ -255,7 +255,7 @@ for _, strategy in helpers.each_strategy() do -- to produce an nginx output filter error and status code 412 -- the response has to go through kong_error_handler (via error_page) it("remains healthy when if-match header is used with buffering", function() - mock = http_mock.new(TCP_PORT) + local mock = http_mock.new(TCP_PORT) mock:start() local res = assert(proxy_client:send { method = "GET",