membphis commented on code in PR #13906:
URL: https://github.com/apache/apisix/pull/13906#discussion_r3920318265


##########
apisix/plugins/batch-requests.lua:
##########
@@ -224,18 +240,80 @@ local function set_common_query(data)
 end
 
 
+local function close_http_client(httpc)
+    local ok, err = httpc:close()
+    if not ok then
+        core.log.warn("failed to close batch request connection: ", err)
+    end
+end
+
+
+local function read_response_body(httpc, resp, max_response_body_size,
+                                  response_body_size_total,
+                                  max_response_body_size_total)
+    local content_length = tonumber(resp.headers["Content-Length"])
+    if content_length then
+        if content_length > max_response_body_size then
+            close_http_client(httpc)
+            return nil, nil, "max_response_body_size"
+        end
+
+        if response_body_size_total + content_length > 
max_response_body_size_total then
+            close_http_client(httpc)
+            return nil, nil, "max_response_body_size_total"
+        end
+    end
+
+    local chunks = {}
+    local response_body_size = 0
+    while true do
+        local chunk, err = resp.body_reader(response_body_chunk_size)
+        if err then

Review Comment:
   [P2] Preserve valid EOF-delimited response bodies
   
   Please fix this before merging. For a response with neither Content-Length 
nor chunked transfer encoding, api7-lua-resty-http 0.2.3 uses connection 
closure to delimit the body. With body_reader(8192), its final return is 
partial, "closed". This branch treats that valid EOF as an error before 
consuming the partial data and discards the body accumulated so far.
   
   A focused reproduction using the pinned library's actual reader and this 
patch's read_response_body returns "hello" with the old read_body() path, but 
nil, "closed" with the new path, even though the five-byte body is below both 
limits. The aggregate still returns HTTP 200, with read_body_err: "closed" and 
no body for that item.
   
   This affects close-delimited internal responses, for example a dynamically 
generated response without Content-Length when chunked transfer encoding is 
disabled. Normal Content-Length and chunked responses do not trigger this issue.
   
   Please distinguish a valid EOF from premature truncation, account for the 
final partial data against both limits, and add a gateway regression for 
close-delimited responses. Keep truncation errors for fixed-length and chunked 
responses intact.



-- 
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