commit f5e540e: [Minor] Lua_task: Simplify has_header method

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Dec 22 15:56:09 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-12-22 15:50:47 +0000
URL: https://github.com/rspamd/rspamd/commit/f5e540efcbb10cc3e260997ef3460093e8bd5152

[Minor] Lua_task: Simplify has_header method

---
 src/lua/lua_task.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)

diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index c0d5c85e0..9cc037796 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -353,10 +353,10 @@ LUA_FUNCTION_DEF (task, get_header);
 /***
  * @method task:has_header(name[, case_sensitive])
  * Get decoded value of a header specified with optional case_sensitive flag.
- * By default headers are searched in caseless matter.
+ * By default headers are searched in the case insensitive matter.
  * @param {string} name name of header to get
  * @param {boolean} case_sensitive case sensitiveness flag to search for a header
- * @return {boolean},{number} true if header exists, the second value is number of headers with this name
+ * @return {boolean} true if header exists
  */
 LUA_FUNCTION_DEF (task, has_header);
 /***
@@ -2828,8 +2828,7 @@ rspamd_lua_push_header_array (lua_State *L,
 	if (rh == NULL) {
 		if (how == RSPAMD_TASK_HEADER_PUSH_HAS) {
 			lua_pushboolean (L, false);
-			lua_pushnumber (L, 0);
-			nret = 2;
+			nret = 1;
 		}
 		else if (how == RSPAMD_TASK_HEADER_PUSH_COUNT) {
 			lua_pushnumber (L, 0);
@@ -2864,17 +2863,23 @@ rspamd_lua_push_header_array (lua_State *L,
 		lua_pushinteger (L, i);
 	}
 	else if (how == RSPAMD_TASK_HEADER_PUSH_HAS) {
-		i = 0;
-		nret = 2;
-
-		DL_FOREACH (rh, cur) {
-			if (!strong || strcmp (name, cur->name) == 0) {
-				i++;
+		nret = 1;
+		bool found = false;
+
+		if (strong) {
+			/* We still have to check all headers in the chain */
+			DL_FOREACH (rh, cur) {
+				if (strcmp (name, cur->name) == 0) {
+					found = true;
+					break;
+				}
 			}
 		}
+		else {
+			found = true;
+		}
 
-		lua_pushboolean (L, true);
-		lua_pushinteger (L, i);
+		lua_pushboolean (L, found);
 	}
 	else {
 		DL_FOREACH (rh, cur) {


More information about the Commits mailing list