Skip to content

Commit

Permalink
refactor(pdk): get_raw_body with string.buffer (#11771)
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw authored Oct 23, 2023
1 parent 6ce55c4 commit ee9103c
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions kong/pdk/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
-- @module kong.response


local buffer = require "string.buffer"
local cjson = require "cjson.safe"
local checks = require "kong.pdk.private.checks"
local phase_checker = require "kong.pdk.private.phases"
Expand All @@ -27,7 +28,6 @@ local find = string.find
local lower = string.lower
local error = error
local pairs = pairs
local concat = table.concat
local coroutine = coroutine
local cjson_encode = cjson.encode
local normalize_header = checks.normalize_header
Expand Down Expand Up @@ -568,39 +568,27 @@ local function new(self, major_version)
function _RESPONSE.get_raw_body()
check_phase(PHASES.body_filter)

local body_buffer
local body_buffer = ngx.ctx.KONG_BODY_BUFFER
local chunk = arg[1]
local eof = arg[2]
if eof then
body_buffer = ngx.ctx.KONG_BODY_BUFFER
if not body_buffer then
return chunk
end

if eof and not body_buffer then
return chunk
end

if type(chunk) == "string" and chunk ~= "" then
if not eof then
body_buffer = ngx.ctx.KONG_BODY_BUFFER
end

if body_buffer then
local n = body_buffer.n + 1
body_buffer.n = n
body_buffer[n] = chunk

else
body_buffer = {
chunk,
n = 1,
}
if not body_buffer then
body_buffer = buffer.new()

ngx.ctx.KONG_BODY_BUFFER = body_buffer
end

body_buffer:put(chunk)
end

if eof then
if body_buffer then
body_buffer = concat(body_buffer, "", 1, body_buffer.n)
body_buffer = body_buffer:get()
else
body_buffer = ""
end
Expand Down

1 comment on commit ee9103c

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

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

Bazel Build

Docker image available kong/kong:ee9103cee3ec6b5163806340541cf082d1279a7a
Artifacts available https://github.com/Kong/kong/actions/runs/6608206843

Please sign in to comment.