tokers commented on a change in pull request #5167:
URL: https://github.com/apache/apisix/pull/5167#discussion_r720640464



##########
File path: apisix/core/response.lua
##########
@@ -151,4 +152,52 @@ function _M.clear_header_as_body_modified()
 end
 
 
+-- Hold body chunks and return the final body once all chunks have been read.
+-- Usage:
+-- function _M.body_filter(conf, ctx)
+--  local final_body = core.response.hold_body_chunk(ctx)
+--  if not final_body then
+--      return
+--  end
+--  final_body = transform(final_body)
+--  ngx.arg[1] = final_body
+--  ...
+--
+-- Inspired by kong.response.get_raw_body()
+function _M.hold_body_chunk(ctx)
+    local body_buffer
+    local chunk, eof = arg[1], arg[2]
+    if eof then
+        body_buffer = ctx._body_buffer
+        if not body_buffer then
+            return chunk
+        end
+
+        body_buffer = concat_tab(body_buffer, "", 1, body_buffer.n)
+        ctx._body_buffer = nil
+        return body_buffer
+    end
+
+    if type(chunk) == "string" and chunk ~= "" then
+        if not eof then

Review comment:
       It seems to me that this `if` is redundant as the control flow will 
return in the last `if` block if `eof` is true. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to