diff --git a/build/openresty/patches/nginx-1.25.3_07-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 new file mode 100644 index 000000000000..4db81ee59cbf --- /dev/null +++ b/build/openresty/patches/nginx-1.25.3_07-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/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 000000000000..cac4566c7b4c --- /dev/null +++ b/changelog/unreleased/kong/fix-filter-finalize-in-send-header-clear-context.yml @@ -0,0 +1,3 @@ +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 diff --git a/spec/02-integration/05-proxy/24-buffered_spec.lua b/spec/02-integration/05-proxy/24-buffered_spec.lua index c95cd7266784..15e639c0bb53 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) - + local 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) 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 000000000000..a4cc5c1644c0 --- /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