fix response body

This commit is contained in:
wiaamm
2025-11-17 20:38:25 +02:00
parent a83a94d031
commit 1a4765a3c6

View File

@@ -180,17 +180,22 @@ function NanoHandler.body_filter(conf)
return
end
local body = kong.response.get_raw_body()
-- Get the current chunk from ngx.arg
local chunk = ngx.arg[1]
local eof = ngx.arg[2]
if body and #body > 0 then
-- Process body chunk if present
if chunk and #chunk > 0 then
ctx.body_seen = true
local verdict, response, modifications = nano.send_body(session_id, session_data, body, nano.HttpChunkType.HTTP_RESPONSE_BODY)
kong.log.debug("Processing response body chunk, size: ", #chunk, " bytes, EOF: ", eof)
local verdict, response, modifications = nano.send_body(session_id, session_data, chunk, nano.HttpChunkType.HTTP_RESPONSE_BODY)
ctx.body_buffer_chunk = ctx.body_buffer_chunk or 0
if modifications then
body = nano.handle_body_modifications(body, modifications, ctx.body_buffer_chunk)
kong.response.set_raw_body(body)
chunk = nano.handle_body_modifications(chunk, modifications, ctx.body_buffer_chunk)
ngx.arg[1] = chunk
end
ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1
@@ -202,74 +207,11 @@ function NanoHandler.body_filter(conf)
nano.cleanup_all()
return result
end
return
elseif ctx.expect_body then
kong.log.debug("Response body not in memory, attempting to read from buffer/file")
local body_data = ngx.var.response_body
if body_data and #body_data > 0 then
kong.log.debug("Found response body in nginx var, size: ", #body_data)
ctx.body_seen = true
local verdict, response, modifications = nano.send_body(session_id, session_data, body_data, nano.HttpChunkType.HTTP_RESPONSE_BODY)
ctx.body_buffer_chunk = ctx.body_buffer_chunk or 0
if modifications then
body_data = nano.handle_body_modifications(body_data, modifications, ctx.body_buffer_chunk)
kong.response.set_raw_body(body_data)
end
ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1
if verdict == nano.AttachmentVerdict.DROP then
nano.fini_session(session_data)
ctx.session_finalized = true
local result = nano.handle_custom_response(session_data, response)
nano.cleanup_all()
return result
end
return
else
-- Try to read from response body file
local body_file = ngx.var.response_body_file
if body_file then
kong.log.debug("Reading response body from file: ", body_file)
local file = io.open(body_file, "rb")
if file then
local entire_body = file:read("*all")
file:close()
if entire_body and #entire_body > 0 then
kong.log.debug("Sending entire response body of size ", #entire_body, " bytes to C module")
ctx.body_seen = true
local verdict, response, modifications = nano.send_body(session_id, session_data, entire_body, nano.HttpChunkType.HTTP_RESPONSE_BODY)
ctx.body_buffer_chunk = ctx.body_buffer_chunk or 0
if modifications then
entire_body = nano.handle_body_modifications(entire_body, modifications, ctx.body_buffer_chunk)
kong.response.set_raw_body(entire_body)
end
ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1
if verdict == nano.AttachmentVerdict.DROP then
nano.fini_session(session_data)
ctx.session_finalized = true
local result = nano.handle_custom_response(session_data, response)
nano.cleanup_all()
return result
end
return
end
else
kong.log.warn("Failed to open response body file: ", body_file)
end
else
kong.log.debug("Response body expected but no body data or file available")
end
end
end
-- Handle end of response
if eof then
kong.log.debug("Response body end of stream reached")
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)
@@ -277,16 +219,15 @@ function NanoHandler.body_filter(conf)
nano.fini_session(session_data)
ctx.session_finalized = true
local result = nano.handle_custom_response(session_data, response)
-- Clean up allocated memory
nano.cleanup_all()
return result
end
nano.fini_session(session_data)
-- Clean up allocated memory
nano.cleanup_all()
ctx.session_finalized = true
end
end
end
return NanoHandler