mirror of
https://github.com/openappsec/attachment.git
synced 2025-11-21 03:16:41 +03:00
fix response body
This commit is contained in:
@@ -198,28 +198,54 @@ function NanoHandler.body_filter(conf)
|
|||||||
|
|
||||||
kong.log.debug("[body_filter] Session: ", session_id, " | Chunk size: ", chunk and #chunk or 0, " | EOF: ", tostring(eof))
|
kong.log.debug("[body_filter] Session: ", session_id, " | Chunk size: ", chunk and #chunk or 0, " | EOF: ", tostring(eof))
|
||||||
|
|
||||||
-- Initialize chunk counter
|
-- Initialize on first call
|
||||||
if not ctx.body_buffer_chunk then
|
if not ctx.chunk_buffer then
|
||||||
ctx.body_buffer_chunk = 0
|
ctx.body_buffer_chunk = 0
|
||||||
|
ctx.chunk_buffer = {}
|
||||||
|
ctx.chunk_buffer_size = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Process ONE chunk at a time (matching nginx module behavior for response bodies)
|
-- Batch configuration: combine small chunks to reduce nano service calls
|
||||||
if chunk and #chunk > 0 and not ctx.chunk_sent_this_call then
|
local MAX_BATCH_SIZE = 64 * 1024 -- 64KB batches
|
||||||
kong.log.debug("[body_filter] Session: ", session_id, " | Sending chunk #", ctx.body_buffer_chunk, ", size: ", #chunk, " bytes")
|
|
||||||
|
|
||||||
local verdict, response, modifications = nano.send_body(session_id, session_data, chunk, nano.HttpChunkType.HTTP_RESPONSE_BODY)
|
-- Process current chunk if present
|
||||||
|
if chunk and #chunk > 0 then
|
||||||
|
-- Add chunk to buffer
|
||||||
|
table.insert(ctx.chunk_buffer, chunk)
|
||||||
|
ctx.chunk_buffer_size = ctx.chunk_buffer_size + #chunk
|
||||||
|
|
||||||
|
local should_send = false
|
||||||
|
|
||||||
|
-- Send if: batch full or EOF coming
|
||||||
|
if ctx.chunk_buffer_size >= MAX_BATCH_SIZE or eof then
|
||||||
|
should_send = true
|
||||||
|
end
|
||||||
|
|
||||||
|
if should_send and #ctx.chunk_buffer > 0 then
|
||||||
|
-- Combine buffered chunks
|
||||||
|
local combined_chunk = table.concat(ctx.chunk_buffer)
|
||||||
|
|
||||||
|
kong.log.debug("[body_filter] Session: ", session_id, " | Sending batched chunk #", ctx.body_buffer_chunk,
|
||||||
|
", size: ", #combined_chunk, " bytes (", #ctx.chunk_buffer, " chunks combined)")
|
||||||
|
|
||||||
|
local verdict, response, modifications = nano.send_body(session_id, session_data, combined_chunk, nano.HttpChunkType.HTTP_RESPONSE_BODY)
|
||||||
|
|
||||||
kong.log.debug("[body_filter] Session: ", session_id, " | Verdict after chunk #", ctx.body_buffer_chunk, ": ", verdict)
|
kong.log.debug("[body_filter] Session: ", session_id, " | Verdict after chunk #", ctx.body_buffer_chunk, ": ", verdict)
|
||||||
|
|
||||||
if modifications then
|
if modifications then
|
||||||
kong.log.debug("[body_filter] Session: ", session_id, " | Applying body modifications to chunk")
|
kong.log.debug("[body_filter] Session: ", session_id, " | Applying body modifications to chunk")
|
||||||
chunk = nano.handle_body_modifications(chunk, modifications, ctx.body_buffer_chunk)
|
combined_chunk = nano.handle_body_modifications(combined_chunk, modifications, ctx.body_buffer_chunk)
|
||||||
ngx.arg[1] = chunk
|
ngx.arg[1] = combined_chunk
|
||||||
|
else
|
||||||
|
ngx.arg[1] = combined_chunk
|
||||||
end
|
end
|
||||||
|
|
||||||
ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1
|
ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1
|
||||||
ctx.body_seen = true
|
ctx.body_seen = true
|
||||||
ctx.chunk_sent_this_call = true
|
|
||||||
|
-- Clear buffer
|
||||||
|
ctx.chunk_buffer = {}
|
||||||
|
ctx.chunk_buffer_size = 0
|
||||||
|
|
||||||
if verdict == nano.AttachmentVerdict.DROP then
|
if verdict == nano.AttachmentVerdict.DROP then
|
||||||
kong.log.warn("[body_filter] Body chunk verdict DROP for session: ", session_id)
|
kong.log.warn("[body_filter] Body chunk verdict DROP for session: ", session_id)
|
||||||
@@ -229,10 +255,12 @@ function NanoHandler.body_filter(conf)
|
|||||||
nano.cleanup_all()
|
nano.cleanup_all()
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
-- Buffering chunk, don't send to client yet
|
||||||
|
kong.log.debug("[body_filter] Session: ", session_id, " | Buffering chunk (", #chunk, " bytes), total buffered: ", ctx.chunk_buffer_size)
|
||||||
|
ngx.arg[1] = nil -- Don't send this chunk to client yet
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Reset flag for next body_filter call
|
|
||||||
ctx.chunk_sent_this_call = false
|
|
||||||
|
|
||||||
-- End inspection at EOF
|
-- End inspection at EOF
|
||||||
if eof then
|
if eof then
|
||||||
|
|||||||
Reference in New Issue
Block a user