Skip to content

Commit

Permalink
fix(core): fix lua-nginx-module context was cleared when ngx.send_hea…
Browse files Browse the repository at this point in the history
…der() trigger filter_finalize case (#13316)

backport patch in openresty/lua-nginx-module#2323

Context: openresty/lua-nginx-module#2320

FTI-6005
  • Loading branch information
oowl authored Jul 15, 2024
1 parent 2da7145 commit fb63631
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
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
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

1 comment on commit fb63631

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:fb6363188246c866cecf72889053c4ba8a455fef
Artifacts available https://github.com/Kong/kong/actions/runs/9934725114

Please sign in to comment.