From 4f056abab6fb50895d26f4c3624daa988d52cdb6 Mon Sep 17 00:00:00 2001 From: windmgc Date: Tue, 7 Nov 2023 14:48:58 +0800 Subject: [PATCH 1/4] fix(pdk): response send function should ignore transfer-encoding correctly --- kong/pdk/response.lua | 1 - .../27-aws-lambda/99-access_spec.lua | 37 +++++++++++++++++++ spec/fixtures/aws-lambda.lua | 3 ++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/kong/pdk/response.lua b/kong/pdk/response.lua index b519ac12ef25..228626b62943 100644 --- a/kong/pdk/response.lua +++ b/kong/pdk/response.lua @@ -660,7 +660,6 @@ local function new(self, major_version) local has_content_length if headers ~= nil then for name, value in pairs(headers) do - ngx.header[name] = normalize_multi_header(value) local lower_name = lower(name) if lower_name == "transfer-encoding" or lower_name == "transfer_encoding" then self.log.warn("manually setting Transfer-Encoding. Ignored.") diff --git a/spec/03-plugins/27-aws-lambda/99-access_spec.lua b/spec/03-plugins/27-aws-lambda/99-access_spec.lua index dc9ec8205ebc..3ffb2d152149 100644 --- a/spec/03-plugins/27-aws-lambda/99-access_spec.lua +++ b/spec/03-plugins/27-aws-lambda/99-access_spec.lua @@ -150,6 +150,12 @@ for _, strategy in helpers.each_strategy() do service = null, } + local route24 = bp.routes:insert { + hosts = { "lambda24.com" }, + protocols = { "http", "https" }, + service = null, + } + bp.plugins:insert { name = "aws-lambda", route = { id = route1.id }, @@ -463,6 +469,19 @@ for _, strategy in helpers.each_strategy() do } } + bp.plugins:insert { + name = "aws-lambda", + route = { id = route24.id }, + config = { + port = 10001, + aws_key = "mock-key", + aws_secret = "mock-secret", + aws_region = "us-east-1", + function_name = "functionWithTransferEncodingHeader", + is_proxy_integration = true, + } + } + fixtures.dns_mock:A({ name = "custom.lambda.endpoint", address = "127.0.0.1", @@ -1148,6 +1167,24 @@ for _, strategy in helpers.each_strategy() do assert.equals("https", req.vars.scheme) end) + it("#test2 works normally by removing transfer encoding header when proxy integration mode", function () + proxy_client:set_timeout(3000) + assert.eventually(function () + local res = assert(proxy_client:send({ + method = "GET", + path = "/get", + headers = { + ["Host"] = "lambda24.com" + } + })) + + assert.res_status(200, res) + assert.is_nil(res.headers["Transfer-Encoding"]) + assert.is_nil(res.headers["transfer-encoding"]) + + return true + end).with_timeout(3).is_truthy() + end) end) describe("AWS_REGION environment is set", function() diff --git a/spec/fixtures/aws-lambda.lua b/spec/fixtures/aws-lambda.lua index 0fa0dec80964..1d99bad795c7 100644 --- a/spec/fixtures/aws-lambda.lua +++ b/spec/fixtures/aws-lambda.lua @@ -57,6 +57,9 @@ local fixtures = { elseif string.match(ngx.var.uri, "functionEcho") then require("spec.fixtures.mock_upstream").send_default_json_response() + elseif string.match(ngx.var.uri, "functionWithTransferEncodingHeader") then + ngx.say("{\"statusCode\": 200, \"headers\": { \"Transfer-Encoding\": \"chunked\", \"transfer-encoding\": \"chunked\"}}") + elseif type(res) == 'string' then ngx.header["Content-Length"] = #res + 1 ngx.say(res) From feab438802fc63a098175ece3b594119fba7ede1 Mon Sep 17 00:00:00 2001 From: windmgc Date: Tue, 7 Nov 2023 14:54:02 +0800 Subject: [PATCH 2/4] docs(changelog): add changelog --- .../kong/pdk-response-send-remove-transfer-encoding.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelog/unreleased/kong/pdk-response-send-remove-transfer-encoding.yml diff --git a/changelog/unreleased/kong/pdk-response-send-remove-transfer-encoding.yml b/changelog/unreleased/kong/pdk-response-send-remove-transfer-encoding.yml new file mode 100644 index 000000000000..f0bd4d19f65e --- /dev/null +++ b/changelog/unreleased/kong/pdk-response-send-remove-transfer-encoding.yml @@ -0,0 +1,3 @@ +message: Fix an issue that when using kong.response.exit, the Transfer-Encoding header set by user is not removed +type: bugfix +scope: PDK From ef9fd9959d9d8b952e18663218ede51f2b6ea9ca Mon Sep 17 00:00:00 2001 From: windmgc Date: Wed, 8 Nov 2023 13:49:22 +0800 Subject: [PATCH 3/4] tests(*): add pdk test --- t/01-pdk/08-response/11-exit.t | 1 + 1 file changed, 1 insertion(+) diff --git a/t/01-pdk/08-response/11-exit.t b/t/01-pdk/08-response/11-exit.t index 79b659c6f686..8c3245f31748 100644 --- a/t/01-pdk/08-response/11-exit.t +++ b/t/01-pdk/08-response/11-exit.t @@ -1148,6 +1148,7 @@ GET /t --- response_body test --- response_headers +! Transfer-Encoding Content-Length: 5 X-test: test --- error_log From 55e82105fca1f436f0950dcdb07bbc9ed232e3fe Mon Sep 17 00:00:00 2001 From: windmgc Date: Wed, 8 Nov 2023 14:13:01 +0800 Subject: [PATCH 4/4] tests(*): fix nginx test plan number and test name --- t/01-pdk/08-response/11-exit.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/01-pdk/08-response/11-exit.t b/t/01-pdk/08-response/11-exit.t index 8c3245f31748..f45564eed560 100644 --- a/t/01-pdk/08-response/11-exit.t +++ b/t/01-pdk/08-response/11-exit.t @@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua; use Test::Nginx::Socket::Lua::Stream; do "./t/Util.pm"; -plan tests => repeat_each() * (blocks() * 4) + 11; +plan tests => repeat_each() * (blocks() * 4) + 12; run_tests(); @@ -1128,7 +1128,7 @@ finalize stream session: 200 -=== TEST 18: response.exit() does not set transfer-encoding from headers +=== TEST 44: response.exit() does not set transfer-encoding from headers --- http_config eval: $t::Util::HttpConfig --- config location = /t {