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): X-Kong-Upstream-Status header is not set #12744

Merged
merged 1 commit into from
Mar 22, 2024

Conversation

liverpool8056
Copy link
Contributor

Summary

Fix a bug that X-Kong-Upstream-Status header is not set when the response is provided by the proxy-cache plugin.

Checklist

  • The Pull Request has tests
  • A changelog file has been created under changelog/unreleased/kong or skip-changelog label added on PR if changelog is unnecessary. README.md
  • There is a user-facing docs PR against https://github.com/Kong/docs.konghq.com - PUT DOCS PR HERE

Issue reference

FTI-5827

@liverpool8056 liverpool8056 requested a review from ADD-SP March 18, 2024 06:41
@github-actions github-actions bot added core/proxy cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee labels Mar 18, 2024
@liverpool8056 liverpool8056 requested a review from bungle March 18, 2024 06:41
@liverpool8056 liverpool8056 force-pushed the fix-upstream-status-header-unset branch from 7b9ec99 to d7df62d Compare March 18, 2024 06:42
…when

the response is returned from the cache of `proxy-cache` plugin.

FTI-5827
@liverpool8056 liverpool8056 force-pushed the fix-upstream-status-header-unset branch from d7df62d to aba3f35 Compare March 18, 2024 06:42
kong/runloop/handler.lua Show resolved Hide resolved
@@ -1438,7 +1438,7 @@ return {

local upstream_status_header = constants.HEADERS.UPSTREAM_STATUS
if kong.configuration.enabled_headers[upstream_status_header] then
local upstream_status = ctx.buffered_status or tonumber(sub(var.upstream_status or "", -3))
local upstream_status = ctx.buffered_status or tonumber(sub(var.upstream_status or "", -3)) or ngx.status
Copy link
Contributor

@ms2008 ms2008 Mar 20, 2024

Choose a reason for hiding this comment

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

I'm wondering if it would be more appropriate to set a special literal like nil or 0 rather than setting it to ngx.status. After all, in this case, the request doesn't actually reach the upstream.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ms2008 If the response is returned from upstream, we set the value of this header with either ctx.buffered_status (in case it is a buffered response) or var.upstream_status (respect the status from upstream). In the case that the response is a cached one, we have to respect the status that's already cached. In the logic of proxy-cache plugin, the ngx.status is set with last cached status. Thus, I suppose here it should consider ngx.status as the last alternative.

nil is not a suitable one, as it is not a valid value for a header. And 0 is not suitable as well, as it doesn't coincide with the cached response.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, that sounds reasonable too. But I think in this scenario, we should clearly define the return value of these X-Kong-Upstream-* response headers, otherwise it will be confusing for the user.

kong/kong.conf.default

Lines 921 to 925 in ed0b96d

# - `X-Kong-Upstream-Status`: The HTTP status
# code returned by the upstream service.
# This is particularly useful for clients to
# distinguish upstream statuses if the
# response is rewritten by a plugin.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me revise our doc and describe this more clearly: This header always keep same with the status cached by proxy-cache plugin when requests hit the cache.

@windmgc windmgc merged commit 5bba619 into master Mar 22, 2024
26 checks passed
@windmgc windmgc deleted the fix-upstream-status-header-unset branch March 22, 2024 02:47
@team-gateway-bot
Copy link
Collaborator

Successfully created cherry-pick PR for master:

liverpool8056 added a commit to Kong/docs.konghq.com that referenced this pull request Mar 25, 2024
locao pushed a commit that referenced this pull request Apr 24, 2024
…when (#12744)

Fix a bug that X-Kong-Upstream-Status header is not set when the response is provided by the proxy-cache plugin.

FTI-5827

(cherry picked from commit 5bba619)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee core/proxy size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants