Skip to content

Commit

Permalink
fix(PDK): fix response body repeated when kong.response.get_raw_body(…
Browse files Browse the repository at this point in the history
…) called multiple times (#11424)

(cherry picked from commit 48a2de2)
  • Loading branch information
vm-001 authored and github-actions[bot] committed Aug 21, 2023
1 parent 1cf0551 commit 6635c33
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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!
--- no_error_log
[error]

0 comments on commit 6635c33

Please sign in to comment.