fix send_bodu in lua_attachment_wrapper.c

This commit is contained in:
wiaamm
2025-11-26 17:33:51 +02:00
parent 9b2054f4d5
commit 3cb6bccf90

View File

@@ -363,60 +363,16 @@ static int lua_send_body(lua_State *L) {
return lua_error(L);
}
if (body_len <= 8 * 1024) {
HttpBody http_chunks;
http_chunks.bodies_count = 1;
nano_str_t chunk;
chunk.data = (unsigned char*)body_chunk;
chunk.len = body_len;
http_chunks.data = &chunk;
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;
AttachmentVerdictResponse* res_ptr = malloc(sizeof(AttachmentVerdictResponse));
*res_ptr = SendDataNanoAttachment(attachment, &attachment_data);
lua_pushinteger(L, res_ptr->verdict);
lua_pushlightuserdata(L, res_ptr);
if (res_ptr->modifications) {
lua_pushlightuserdata(L, res_ptr->modifications);
} else {
lua_pushnil(L);
}
return 3;
}
const size_t CHUNK_SIZE = 8 * 1024;
size_t num_chunks = ((body_len - 1) / CHUNK_SIZE) + 1;
if (num_chunks > 10000) {
num_chunks = 10000;
}
// Send the chunk as-is without re-splitting
// Kong/Nginx already provides properly sized chunks from ngx.arg[1]
// Re-splitting causes memory issues and unnecessary overhead
HttpBody http_chunks;
http_chunks.bodies_count = num_chunks;
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);
}
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;
size_t chunk_len = (i == num_chunks - 1) ? (body_len - chunk_start) : CHUNK_SIZE;
chunk_ptr->data = (unsigned char*)(body_chunk + chunk_start);
chunk_ptr->len = chunk_len;
}
http_chunks.bodies_count = 1;
nano_str_t chunk;
chunk.data = (unsigned char*)body_chunk;
chunk.len = body_len;
http_chunks.data = &chunk;
AttachmentData attachment_data;
attachment_data.session_id = session_id;
@@ -427,8 +383,6 @@ static int lua_send_body(lua_State *L) {
AttachmentVerdictResponse* res_ptr = malloc(sizeof(AttachmentVerdictResponse));
*res_ptr = SendDataNanoAttachment(attachment, &attachment_data);
free(http_chunks.data);
lua_pushinteger(L, res_ptr->verdict);
lua_pushlightuserdata(L, res_ptr);