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(PDK): fix response body repeated when kong.response.get_raw_body() called multiple times #11424

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG/unreleased/kong/11424.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
message: Fix response body gets repeated when `kong.response.get_raw_body()` is called multiple times in a request lifecycle.
type: bugfix
scope: PDK
prs:
- 11424
jiras:
- "FTI-5296"
1 change: 1 addition & 0 deletions kong/pdk/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ local function new(self, major_version)
end

arg[1] = body_buffer
ngx.ctx.KONG_BODY_BUFFER = nil
return body_buffer
end

Expand Down
34 changes: 34 additions & 0 deletions t/01-pdk/08-response/14-get_raw_body.t
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,37 @@ Enhanced by Body Filter
Called 3 times
--- no_error_log
[error]



=== TEST 3: response.get_raw_body() calls multiple times
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
content_by_lua_block {
ngx.print("hello, world!\n")
}
body_filter_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
-- call pdk.response.get_raw_body() multiple times
ngx.ctx.called = (ngx.ctx.called or 0) + 1
for i = 1, 3 do
ngx.ctx.called2 = (ngx.ctx.called2 or 0) + 1
local body = pdk.response.get_raw_body()
if body then
assert("hello, world!\n" == body)
end
end
}
log_by_lua_block {
assert(ngx.ctx.called == 2)
assert(ngx.ctx.called2 == 6)
}
}
--- request
GET /t
--- response_body
hello, world!
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The response gets repeated if the fix is not applied: hello, world!hello, world!

--- no_error_log
[error]
Loading