You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/10/02 07:11:01 UTC

[GitHub] [apisix] tokers commented on a change in pull request #5167: refactor: introduce hold_body_chunk to dismiss repeated code

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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org