mirror of
https://github.com/openappsec/attachment.git
synced 2026-01-02 22:54:50 +03:00
add large response body handling
This commit is contained in:
@@ -224,19 +224,23 @@ function NanoHandler.body_filter(conf)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local body = kong.response.get_raw_body()
|
-- Get current chunk only, not entire body
|
||||||
|
local chunk = ngx.arg[1]
|
||||||
|
local eof = ngx.arg[2]
|
||||||
|
|
||||||
if body and #body > 0 then
|
if chunk and #chunk > 0 then
|
||||||
ctx.body_seen = true
|
ctx.body_seen = true
|
||||||
local verdict, response, modifications = nano.send_body(session_id, session_data, body, nano.HttpChunkType.HTTP_RESPONSE_BODY)
|
|
||||||
|
-- Process chunk by chunk to avoid loading entire large body into memory
|
||||||
|
local verdict, response, modifications = nano.send_body(session_id, session_data, chunk, nano.HttpChunkType.HTTP_RESPONSE_BODY)
|
||||||
|
|
||||||
-- Initialize if not exists
|
-- Initialize if not exists
|
||||||
ctx.body_buffer_chunk = ctx.body_buffer_chunk or 0
|
ctx.body_buffer_chunk = ctx.body_buffer_chunk or 0
|
||||||
|
|
||||||
-- Handle body modifications if any
|
-- Handle body modifications if any
|
||||||
if modifications then
|
if modifications then
|
||||||
body = nano.handle_body_modifications(body, modifications, ctx.body_buffer_chunk)
|
chunk = nano.handle_body_modifications(chunk, modifications, ctx.body_buffer_chunk)
|
||||||
kong.response.set_raw_body(body)
|
ngx.arg[1] = chunk
|
||||||
end
|
end
|
||||||
|
|
||||||
ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1
|
ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1
|
||||||
@@ -250,11 +254,18 @@ function NanoHandler.body_filter(conf)
|
|||||||
nano.cleanup_all()
|
nano.cleanup_all()
|
||||||
ctx.session_data = nil
|
ctx.session_data = nil
|
||||||
ctx.session_id = nil
|
ctx.session_id = nil
|
||||||
|
ngx.arg[1] = ""
|
||||||
|
ngx.arg[2] = true
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
return
|
|
||||||
|
-- Cleanup allocated memory after processing each chunk to prevent accumulation
|
||||||
|
nano.free_all_responses()
|
||||||
|
nano.free_all_nano_str()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Handle end of stream
|
||||||
|
if eof then
|
||||||
if ctx.body_seen or ctx.expect_body == false then
|
if ctx.body_seen or ctx.expect_body == false then
|
||||||
local verdict, response = nano.end_inspection(session_id, session_data, nano.HttpChunkType.HTTP_RESPONSE_END)
|
local verdict, response = nano.end_inspection(session_id, session_data, nano.HttpChunkType.HTTP_RESPONSE_END)
|
||||||
if verdict == nano.AttachmentVerdict.DROP then
|
if verdict == nano.AttachmentVerdict.DROP then
|
||||||
@@ -266,6 +277,8 @@ function NanoHandler.body_filter(conf)
|
|||||||
nano.cleanup_all()
|
nano.cleanup_all()
|
||||||
ctx.session_data = nil
|
ctx.session_data = nil
|
||||||
ctx.session_id = nil
|
ctx.session_id = nil
|
||||||
|
ngx.arg[1] = ""
|
||||||
|
ngx.arg[2] = true
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -276,6 +289,7 @@ function NanoHandler.body_filter(conf)
|
|||||||
ctx.session_data = nil
|
ctx.session_data = nil
|
||||||
ctx.session_id = nil
|
ctx.session_id = nil
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return NanoHandler
|
return NanoHandler
|
||||||
Reference in New Issue
Block a user