From: Vsevolod Stakhov Date: Mon, 6 Oct 2025 09:50:04 +0000 (+0100) Subject: [Rework] Remove Lua-level HTTP header parsing in ESMTP args getters; rely solely... X-Git-Tag: 3.14.0~89^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d5ff0fb99bfd7789665ecf5fb486b194c249bd1b;p=thirdparty%2Frspamd.git [Rework] Remove Lua-level HTTP header parsing in ESMTP args getters; rely solely on protocol layer to populate task fields --- diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index a42b1aba00..1f9d1bf3b1 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -4363,53 +4363,7 @@ lua_task_get_mail_esmtp_args(lua_State *L) } } else { - /* Fallback to HTTP headers for backward compatibility */ - /* Check if this is a milter task */ - if (!(task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_MILTER)) { - lua_pushnil(L); - return 1; - } - - /* Get ESMTP args from HTTP headers */ - hdr = rspamd_message_get_header_array(task, "X-Rspamd-Mail-Esmtp-Args", FALSE); - - if (hdr) { - lua_createtable(L, 0, 0); - - while (hdr) { - const char *p, *eq; - gsize len; - - if (hdr->decoded) { - p = hdr->decoded; - len = strlen(p); - } - else { - p = hdr->value; - len = strlen(p); - } - - /* Parse KEY=VALUE format */ - eq = memchr(p, '=', len); - - if (eq) { - /* KEY=VALUE */ - lua_pushlstring(L, p, eq - p); - lua_pushlstring(L, eq + 1, len - (eq - p) - 1); - } - else { - /* KEY only */ - lua_pushlstring(L, p, len); - lua_pushstring(L, ""); - } - - lua_settable(L, -3); - hdr = hdr->next; - } - } - else { - lua_pushnil(L); - } + lua_pushnil(L); } } else { @@ -4424,7 +4378,6 @@ lua_task_get_rcpt_esmtp_args(lua_State *L) { LUA_TRACE_POINT; struct rspamd_task *task = lua_check_task(L, 1); - struct rspamd_mime_header *hdr; int idx = -1; gboolean all_rcpts = TRUE; GHashTable *rcpt_args_by_idx = NULL; @@ -4496,208 +4449,7 @@ lua_task_get_rcpt_esmtp_args(lua_State *L) } } else { - /* Fallback to HTTP headers for backward compatibility */ - - /* Get ESMTP args from HTTP headers */ - hdr = rspamd_message_get_header_array(task, "X-Rspamd-Rcpt-Esmtp-Args", FALSE); - if (hdr) { - if (all_rcpts) { - /* Build hash table mapping recipient index to args table */ - rcpt_args_by_idx = g_hash_table_new_full(g_direct_hash, g_direct_equal, - NULL, NULL); - - /* First pass: collect all args by recipient index */ - while (hdr) { - const char *p, *colon, *eq; - gsize len; - int rcpt_idx; - - if (hdr->decoded) { - p = hdr->decoded; - len = strlen(p); - } - else { - p = hdr->value; - len = strlen(p); - } - - /* Parse IDX:KEY=VALUE format */ - colon = memchr(p, ':', len); - - if (colon) { - char *endptr; - rcpt_idx = strtol(p, &endptr, 10); - - if (endptr == colon) { - /* Valid index found */ - const char *old_p = p; - p = colon + 1; - len -= (p - old_p); - - /* Store this arg for this recipient */ - if (!g_hash_table_contains(rcpt_args_by_idx, GINT_TO_POINTER(rcpt_idx))) { - /* Create new table for this recipient */ - lua_newtable(L); - g_hash_table_insert(rcpt_args_by_idx, - GINT_TO_POINTER(rcpt_idx), - GINT_TO_POINTER(lua_gettop(L))); - } - - /* Get the table for this recipient */ - int table_idx = GPOINTER_TO_INT(g_hash_table_lookup(rcpt_args_by_idx, - GINT_TO_POINTER(rcpt_idx))); - lua_pushvalue(L, table_idx); - - /* Parse KEY=VALUE */ - eq = memchr(p, '=', len); - - if (eq) { - lua_pushlstring(L, p, eq - p); - lua_pushlstring(L, eq + 1, len - (eq - p) - 1); - } - else { - lua_pushlstring(L, p, len); - lua_pushstring(L, ""); - } - - lua_settable(L, -3); - lua_pop(L, 1); /* Pop the table */ - } - } - - hdr = hdr->next; - } - - /* Now create the result array */ - if (g_hash_table_size(rcpt_args_by_idx) > 0) { - GHashTableIter iter; - gpointer key, value; - int max_idx = 0; - - /* Find max index */ - g_hash_table_iter_init(&iter, rcpt_args_by_idx); - while (g_hash_table_iter_next(&iter, &key, &value)) { - int i = GPOINTER_TO_INT(key); - if (i > max_idx) { - max_idx = i; - } - } - - /* Create result array */ - lua_createtable(L, max_idx + 1, 0); - - /* Fill array with tables or nils */ - for (int i = 0; i <= max_idx; i++) { - if (g_hash_table_contains(rcpt_args_by_idx, GINT_TO_POINTER(i))) { - int table_idx = GPOINTER_TO_INT(g_hash_table_lookup(rcpt_args_by_idx, - GINT_TO_POINTER(i))); - lua_pushvalue(L, table_idx); - } - else { - lua_pushnil(L); - } - lua_rawseti(L, -2, i + 1); /* Lua arrays are 1-based */ - } - - /* Clean up temporary tables */ - { - /* Collect indices first */ - GHashTableIter iter2; - gpointer key2, value2; - GArray *to_remove = g_array_new(FALSE, FALSE, sizeof(int)); - - g_hash_table_iter_init(&iter2, rcpt_args_by_idx); - while (g_hash_table_iter_next(&iter2, &key2, &value2)) { - int table_idx = GPOINTER_TO_INT(value2); - g_array_append_val(to_remove, table_idx); - } - - /* Remove in descending order to avoid index shifts */ - while (to_remove->len > 0) { - guint max_pos = 0; - int max_idx = g_array_index(to_remove, int, 0); - - for (guint j = 1; j < to_remove->len; j++) { - int v = g_array_index(to_remove, int, j); - if (v > max_idx) { - max_idx = v; - max_pos = j; - } - } - - lua_remove(L, max_idx); - g_array_remove_index_fast(to_remove, max_pos); - } - - g_array_free(to_remove, TRUE); - } - } - else { - lua_pushnil(L); - } - - g_hash_table_destroy(rcpt_args_by_idx); - } - else { - /* Return args for specific recipient */ - lua_newtable(L); - gboolean found = FALSE; - - while (hdr) { - const char *p, *colon, *eq; - gsize len; - int rcpt_idx; - - if (hdr->decoded) { - p = hdr->decoded; - len = strlen(p); - } - else { - p = hdr->value; - len = strlen(p); - } - - /* Parse IDX:KEY=VALUE format */ - colon = memchr(p, ':', len); - - if (colon) { - char *endptr; - rcpt_idx = strtol(p, &endptr, 10); - - if (endptr == colon && rcpt_idx == idx) { - found = TRUE; - const char *old_p = p; - p = colon + 1; - len -= (p - old_p); - - /* Parse KEY=VALUE */ - eq = memchr(p, '=', len); - - if (eq) { - lua_pushlstring(L, p, eq - p); - lua_pushlstring(L, eq + 1, len - (eq - p) - 1); - } - else { - lua_pushlstring(L, p, len); - lua_pushstring(L, ""); - } - - lua_settable(L, -3); - } - } - - hdr = hdr->next; - } - - if (!found) { - lua_pop(L, 1); - lua_pushnil(L); - } - } - } - else { - lua_pushnil(L); - } + lua_pushnil(L); } } else {