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 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) diff --git a/t/01-pdk/08-response/11-exit.t b/t/01-pdk/08-response/11-exit.t index 79b659c6f686..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 { @@ -1148,6 +1148,7 @@ GET /t --- response_body test --- response_headers +! Transfer-Encoding Content-Length: 5 X-test: test --- error_log