send the all the body

This commit is contained in:
wiaamm 2025-07-06 15:52:49 +03:00
parent 466abd73bc
commit a253345eb3
3 changed files with 11 additions and 36 deletions

View File

@ -40,10 +40,6 @@ function NanoHandler.access(conf)
local has_content_length = tonumber(ngx.var.http_content_length) and tonumber(ngx.var.http_content_length) > 0
local contains_body = has_content_length and 1 or 0
if has_content_length then
kong.log.debug("Request has content-length: ", ngx.var.http_content_length)
end
local verdict, response = nano.send_data(session_id, session_data, meta_data, req_headers, contains_body, nano.HttpChunkType.HTTP_REQUEST_FILTER)
if verdict == nano.AttachmentVerdict.DROP then
nano.fini_session(session_data)
@ -85,33 +81,23 @@ function NanoHandler.access(conf)
kong.log.debug("Reading request body from file: ", body_file)
local file = io.open(body_file, "rb")
if file then
local chunk_size = 8192 -- 8KB chunks
local chunks_processed = 0
local total_bytes = 0
-- Read entire body at once
local entire_body = file:read("*all")
file:close()
while true do
local chunk = file:read(chunk_size)
if not chunk then break end
chunks_processed = chunks_processed + 1
total_bytes = total_bytes + #chunk
kong.log.debug("Processing body chunk ", chunks_processed, " of size ", #chunk)
verdict, response = nano.send_body(session_id, session_data, chunk, nano.HttpChunkType.HTTP_REQUEST_BODY)
if entire_body and #entire_body > 0 then
kong.log.debug("Sending entire body of size ", #entire_body, " bytes to C module")
verdict, response = nano.send_body(session_id, session_data, entire_body, nano.HttpChunkType.HTTP_REQUEST_BODY)
if verdict == nano.AttachmentVerdict.DROP then
file:close()
nano.fini_session(session_data)
kong.ctx.plugin.blocked = true
local result = nano.handle_custom_response(session_data, response)
nano.cleanup_all()
return result
end
else
kong.log.debug("Empty body file")
end
file:close()
kong.log.debug("Processed ", chunks_processed, " chunks, total bytes: ", total_bytes)
else
kong.log.err("Failed to open request body file: ", body_file)
end
else
kong.log.warn("Request body expected but no body data or file available")

View File

@ -404,7 +404,6 @@ static int lua_send_body(lua_State *L) {
lua_pushinteger(L, res_ptr->verdict);
lua_pushlightuserdata(L, res_ptr);
// Push modifications if they exist
if (res_ptr->modifications) {
lua_pushlightuserdata(L, res_ptr->modifications);
} else {
@ -414,27 +413,22 @@ static int lua_send_body(lua_State *L) {
return 3;
}
// Calculate number of chunks (8KB each, like Envoy)
const size_t CHUNK_SIZE = 8 * 1024;
size_t num_chunks = ((body_len - 1) / CHUNK_SIZE) + 1;
// Limit number of chunks like Envoy
if (num_chunks > 10000) {
num_chunks = 10000;
}
// Create HttpBody structure
HttpBody http_chunks;
http_chunks.bodies_count = num_chunks;
// Allocate memory for chunks array
http_chunks.data = (nano_str_t*)malloc(num_chunks * sizeof(nano_str_t));
if (!http_chunks.data) {
lua_pushstring(L, "Error: Failed to allocate memory for chunks");
return lua_error(L);
}
// Prepare chunks using pointer arithmetic like Envoy
for (size_t i = 0; i < num_chunks; i++) {
nano_str_t* chunk_ptr = (nano_str_t*)((char*)http_chunks.data + (i * sizeof(nano_str_t)));
size_t chunk_start = i * CHUNK_SIZE;
@ -444,25 +438,20 @@ static int lua_send_body(lua_State *L) {
chunk_ptr->len = chunk_len;
}
// Prepare attachment data
AttachmentData attachment_data;
attachment_data.session_id = session_id;
attachment_data.session_data = session_data;
attachment_data.chunk_type = chunk_type;
attachment_data.data = &http_chunks;
// Send all chunks at once
AttachmentVerdictResponse* res_ptr = malloc(sizeof(AttachmentVerdictResponse));
*res_ptr = SendDataNanoAttachment(attachment, &attachment_data);
// Free allocated memory
free(http_chunks.data);
// Push verdict
lua_pushinteger(L, res_ptr->verdict);
lua_pushlightuserdata(L, res_ptr);
// Push modifications if they exist
if (res_ptr->modifications) {
lua_pushlightuserdata(L, res_ptr->modifications);
} else {