-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Cannot iterate on headers from kong.service.response.get_headers() #11546
Comments
We're using metatable to handle name aliases (underscore vs. hyphen, upper vs. lowercase characters) and max headers limitation, thus you find an empty table with |
Created internal ticket KAG-2602 |
This issue is marked as stale because it has been open for 14 days with no activity. |
Hello ! |
@mfeledyn Hi. Thanks for caring. We maintain an internal task track system and that system is not open. We've created the ticket but no conclusion has been made yet. I'm confirming if this can be scheduled recently. |
I did a little bit of research on this and noticed that this is not a very trivial problem. Referer: openresty/lua-nginx-module#1595 I've created a fix, but it also changes some of the behavior. |
As it's not very clear how this should be resolved, I'd like to doc the current behavior and leave it for now. |
Thx for your feed-back on this. The use case is to trace the headers sent in service's response. The work around I found is to use kong.response.get_headers() in a very high priority plugin so it gets the headers before any other plugin can modify them. For now I consider it's not only a documentation problem, because the kong.service.response.get_headers() name is simply misleading. This function should be removed and documented as missing. |
Glad to know a workaround works for you.
I agree. The current behavior is not logical (the API does not function as designed) and it should be eventually fixed. However, it is not a top priority at the moment and a resolution is not planned in the foreseeable future. We welcome community PRs. If you are interested, we could use PR #11708 as a starting point for your involvement. Also, please be assured that there is no pressure to contribute. |
Adressing KAG-2602, #11546 Co-authored-by: Datong Sun <[email protected]>
This commit attempts to fix several issues in the `kong.service.response.get_headers()` module: 1. Said method must only return response headers set by the upstream service. However, since its introcution, it is broken; in the original implementation, it added an `__index` metamethod to the full headers table (both Kong and upstream headers) to ensure a given header being indexed was found in a `ngx.var.upstream_http_<header_name> variable (ie, it was set by the upstream). However, it responded with the table itself, which contained the headers, so the `__index` metamethod would never be called. 2. Buffered proxying, later added, introduced new logic to handle buffered headers. This logic corretly implemented the metamethod by using a so-called "proxy table"; this is an empty table to which the `__index` metamethod is added. Since it's empty, `__index` is correctly invoked and ensures only upstream headers are accessible (as described by 1.). However, being empty it does not allow the headers table to be iterated on, leading to inconsistent and unexpected behavior -- a source of bug reports (e.g., #11546, KAG-4866, to name two).
This commit attempts to fix several issues in the `kong.service.response.get_headers()` module. Issues ====== 1. Said method must only return response headers set by the upstream service. However, since its introcution, it is broken; in the original implementation, it added an `__index` metamethod to the full headers table (both Kong and upstream headers) to ensure a given header being indexed was found in a `ngx.var.upstream_http_<header_name> variable (ie, it was set by the upstream). However, it responded with the table itself, which contained the headers, so the `__index` metamethod would never be called. Summarizing, since its introduction, the method has been returning both Kong and upstream headers. Tests were also broken due to a case-sensitiveness issue (implementation described above bypased the original headers table metatable, effectively breaking case-insensitiveness implemented by the `ngx.resp.get_headers` API). 2. Buffered proxying, later added, introduced new logic to handle buffered headers. This logic corretly implemented the metamethod by using a so-called "proxy table"; this is an empty table to which the `__index` metamethod is added. Since it's empty, `__index` is correctly invoked and ensures only upstream headers are accessible (as described by 1.). However, being empty it does not allow the headers table to be iterated on, leading to inconsistent and unexpected behavior -- a source of bug reports (e.g., #11546, KAG-4866, to name two). What does it change? ==================== 1. Standardize the interface: both buffered and unbuffered respond with an iterable table. 2. Add more tests, for both buffered and unbuffered modes - test that returned table is indeed iterable - test that returned table only has upstream headers Caveats ======= - Buffered proxying already reads all headers; adding additional logic to limit by `max_headers` arguably does not make sense. - Calls to this method will always respond with less than `max_headers`, as the call to `ngx.resp.get_headers` includes both Kong and upstream headers and we filter out non-upstream headers after said call. The ideal solution would use a native API that reads upstream only headers directly, and has a similar interface as `ngx.resp.get_headers`, taking a `max_headers` argument.
Is there an existing issue for this?
Kong version (
$ kong version
)3.4.0
Current Behavior
Trying to iterate over kong.service.response headers which should be a table. I cannot iterate on the object returned by
kong.service.response.get_headers()
Expected Behavior
I expected to be able to iterate on the object returned by
kong.service.response.get_headers()
to traverse all headers.Steps To Reproduce
Make a test Lua plugin for Kong.
For response() function of the test plugin, here is the code I use:
In summary I can access a service.response header if I know its name. I cannot iterate over headers, nor know headers count.
Anything else?
No response
The text was updated successfully, but these errors were encountered: