From 70952106bc2367545896fe39d8d811b5272c1d79 Mon Sep 17 00:00:00 2001 From: oowl Date: Thu, 24 Aug 2023 16:50:43 +0800 Subject: [PATCH] fix(core): fix response status code is not real upstream status when using kong.response function (#11437) When kong uses kong_buffered_http and upstream timeout, the return status code needs to return 504 (which return by ngx.location.capture), not 502. FTI-5320 --- .../unreleased/kong/response_status_code.yml | 3 ++ kong/init.lua | 2 +- .../05-proxy/07-upstream_timeouts_spec.lua | 45 ++++++++++++++++++- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 changelog/unreleased/kong/response_status_code.yml diff --git a/changelog/unreleased/kong/response_status_code.yml b/changelog/unreleased/kong/response_status_code.yml new file mode 100644 index 000000000000..fcfb94f9e264 --- /dev/null +++ b/changelog/unreleased/kong/response_status_code.yml @@ -0,0 +1,3 @@ +message: Fix an issue that response status code is not real upstream status when using kong.response function. +type: bugfix +scope: Core diff --git a/kong/init.lua b/kong/init.lua index e6b95036766a..0e59302473c6 100644 --- a/kong/init.lua +++ b/kong/init.lua @@ -1455,7 +1455,7 @@ do local res = ngx.location.capture("/kong_buffered_http", options) if res.truncated and options.method ~= ngx.HTTP_HEAD then ctx.KONG_PHASE = PHASES.error - ngx.status = 502 + ngx.status = res.status or 502 if has_timing then req_dyn_hook_run_hooks(ctx, "timing", "after:response") diff --git a/spec/02-integration/05-proxy/07-upstream_timeouts_spec.lua b/spec/02-integration/05-proxy/07-upstream_timeouts_spec.lua index d6d2121aa4af..0a1596f2a2d0 100644 --- a/spec/02-integration/05-proxy/07-upstream_timeouts_spec.lua +++ b/spec/02-integration/05-proxy/07-upstream_timeouts_spec.lua @@ -32,6 +32,21 @@ for _, strategy in helpers.each_strategy() do route.service = bp.services:insert(service) + if route.enable_buffering then + route.enable_buffering = nil + bp.plugins:insert({ + name = "pre-function", + service = { id = route.service.id }, + config = { + access = { + [[ + kong.service.request.enable_buffering() + ]], + }, + } + }) + end + if not route.protocols then route.protocols = { "http" } end @@ -73,6 +88,17 @@ for _, strategy in helpers.each_strategy() do read_timeout = 1, -- ms }, }, + { + methods = { "PUT" }, + service = { + name = "api-4", + protocol = "http", + host = "konghq.com", + port = 81, + connect_timeout = 1, -- ms + }, + enable_buffering = true, + }, } bp.plugins:insert { @@ -83,7 +109,7 @@ for _, strategy in helpers.each_strategy() do } assert(helpers.start_kong({ - plugins = "ctx-checker-last", + plugins = "bundled, ctx-checker-last", database = strategy, nginx_conf = "spec/fixtures/custom_nginx.template", })) @@ -161,5 +187,22 @@ for _, strategy in helpers.each_strategy() do assert.equal(504, res.status) end) end) + + describe("upstream_connect_timeout with enable_buffering", function() + it("sets upstream send timeout value", function() + local res = assert(proxy_client:send { + method = "PUT", + path = "/put", + body = { + huge = string.rep("a", 2^25) + }, + headers = { ["Content-Type"] = "application/json" }, + }) + + assert.equal(504, res.status) + end) + end) + + end) end