Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): fix lua-nginx-module context was cleared when ngx.send_header() trigger filter_finalize case #13316

Merged
merged 11 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
oowl marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions spec/02-integration/05-proxy/24-buffered_spec.lua
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions t/04-patch/03-fix-ngx-send-header-filter-finalize-ctx.t
Original file line number Diff line number Diff line change
@@ -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
Loading