fix response body

This commit is contained in:
wiaamm
2025-11-18 00:16:43 +02:00
parent bb0834aefd
commit 0d5f8a70c1

View File

@@ -169,6 +169,14 @@ function NanoHandler.header_filter(conf)
end end
ctx.expect_body = not (status_code == 204 or status_code == 304 or (100 <= status_code and status_code < 200) or content_length == 0) ctx.expect_body = not (status_code == 204 or status_code == 304 or (100 <= status_code and status_code < 200) or content_length == 0)
-- Enable response body buffering for inspection
if ctx.expect_body then
kong.log.debug("[header_filter] Session: ", session_id, " | Enabling response body buffering")
kong.service.request.enable_buffering()
ngx.ctx.buffer_body = true
end
kong.log.debug("[header_filter] Session: ", session_id, " | Expect body: ", ctx.expect_body) kong.log.debug("[header_filter] Session: ", session_id, " | Expect body: ", ctx.expect_body)
end end
@@ -192,37 +200,54 @@ function NanoHandler.body_filter(conf)
return return
end end
-- Buffer chunks until we have the complete body -- On first call, try to get the complete buffered body
local chunk = ngx.arg[1] if not ctx.body_processed and not ctx.response_body_buffer then
local eof = ngx.arg[2] local body = kong.response.get_raw_body()
kong.log.debug("[body_filter] Session: ", session_id, " | Chunk size: ", chunk and #chunk or 0, " | EOF: ", tostring(eof)) if body and #body > 0 then
kong.log.debug("[body_filter] Session: ", session_id, " | Got complete buffered body, size: ", #body, " bytes")
-- Accumulate body chunks ctx.complete_body = body
if chunk and #chunk > 0 then ctx.body_processed = true
if not ctx.response_body_buffer then else
kong.log.debug("[body_filter] Session: ", session_id, " | No buffered body available, will collect chunks")
ctx.response_body_buffer = {} ctx.response_body_buffer = {}
end end
table.insert(ctx.response_body_buffer, chunk)
kong.log.debug("[body_filter] Session: ", session_id, " | Buffered chunk, total chunks: ", #ctx.response_body_buffer)
end end
-- Process the complete body when we reach EOF -- If we're collecting chunks, buffer them
if eof then if ctx.response_body_buffer then
local chunk = ngx.arg[1]
local eof = ngx.arg[2]
kong.log.debug("[body_filter] Session: ", session_id, " | Chunk size: ", chunk and #chunk or 0, " | EOF: ", tostring(eof))
if chunk and #chunk > 0 then
table.insert(ctx.response_body_buffer, chunk)
kong.log.debug("[body_filter] Session: ", session_id, " | Buffered chunk, total chunks: ", #ctx.response_body_buffer)
end
-- When we reach EOF, concatenate chunks
if eof and #ctx.response_body_buffer > 0 then
ctx.complete_body = table.concat(ctx.response_body_buffer)
kong.log.debug("[body_filter] Session: ", session_id, " | Concatenated chunks, total size: ", #ctx.complete_body, " bytes")
end
end
-- Process the complete body at EOF
local eof = ngx.arg[2]
if eof and not ctx.session_finalized then
kong.log.debug("[body_filter] Session: ", session_id, " | EOF reached") kong.log.debug("[body_filter] Session: ", session_id, " | EOF reached")
-- Process buffered body if we have it -- Send body to nano service if we have it
if ctx.response_body_buffer and #ctx.response_body_buffer > 0 then if ctx.complete_body and #ctx.complete_body > 0 then
local complete_body = table.concat(ctx.response_body_buffer) kong.log.debug("[body_filter] Session: ", session_id, " | Processing complete body, size: ", #ctx.complete_body, " bytes")
kong.log.debug("[body_filter] Session: ", session_id, " | Processing complete body, size: ", #complete_body, " bytes")
local verdict, response, modifications = nano.send_body(session_id, session_data, complete_body, nano.HttpChunkType.HTTP_RESPONSE_BODY) local verdict, response, modifications = nano.send_body(session_id, session_data, ctx.complete_body, nano.HttpChunkType.HTTP_RESPONSE_BODY)
if modifications then if modifications then
kong.log.debug("[body_filter] Session: ", session_id, " | Applying body modifications") kong.log.debug("[body_filter] Session: ", session_id, " | Applying body modifications")
complete_body = nano.handle_body_modifications(complete_body, modifications, 0) ctx.complete_body = nano.handle_body_modifications(ctx.complete_body, modifications, 0)
-- Update the buffered body with modifications ngx.arg[1] = ctx.complete_body
ngx.arg[1] = complete_body
end end
if verdict == nano.AttachmentVerdict.DROP then if verdict == nano.AttachmentVerdict.DROP then
@@ -233,29 +258,29 @@ function NanoHandler.body_filter(conf)
nano.cleanup_all() nano.cleanup_all()
return result return result
end end
else
kong.log.debug("[body_filter] Session: ", session_id, " | No body to process (empty response or no body expected)")
end end
-- End inspection -- End inspection
if ctx.response_body_buffer or ctx.expect_body == false then kong.log.debug("[body_filter] Session: ", session_id, " | Ending inspection")
kong.log.debug("[body_filter] Session: ", session_id, " | Ending inspection") 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)
kong.log.debug("[body_filter] Session: ", session_id, " | End inspection verdict: ", verdict)
kong.log.debug("[body_filter] Session: ", session_id, " | End inspection verdict: ", verdict)
if verdict == nano.AttachmentVerdict.DROP then
if verdict == nano.AttachmentVerdict.DROP then kong.log.warn("[body_filter] End inspection verdict DROP for session: ", session_id)
kong.log.warn("[body_filter] End inspection verdict DROP for session: ", session_id)
nano.fini_session(session_data)
ctx.session_finalized = true
local result = nano.handle_custom_response(session_data, response)
nano.cleanup_all()
return result
end
kong.log.debug("[body_filter] Session: ", session_id, " | Finalizing session normally")
nano.fini_session(session_data) nano.fini_session(session_data)
nano.cleanup_all()
ctx.session_finalized = true ctx.session_finalized = true
local result = nano.handle_custom_response(session_data, response)
nano.cleanup_all()
return result
end end
kong.log.debug("[body_filter] Session: ", session_id, " | Finalizing session normally")
nano.fini_session(session_data)
nano.cleanup_all()
ctx.session_finalized = true
end end
end end