From 7e785d608880eae1ed9698ded30b83de944c1095 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 20 May 2015 17:28:36 +0100 Subject: [PATCH] Add API to get/set request headers from lua. --- src/lua/lua_task.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index d23f9143b..fccbaca7b 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -180,6 +180,21 @@ LUA_FUNCTION_DEF (task, get_text_parts); * @return {table rspamd_mime_part} list of mime parts */ LUA_FUNCTION_DEF (task, get_parts); + +/*** + * @method task:get_request_header(name) + * Get value of a HTTP request header. + * @param {string} name name of header to get + * @return {rspamd_text} value of an HTTP header + */ +LUA_FUNCTION_DEF (task, get_request_header); +/*** + * @method task:set_request_header(name, value) + * Set value of a HTTP request header. If value is omitted, then a header is removed + * @param {string} name name of header to get + * @param {rspamd_text/string} value new header's value + */ +LUA_FUNCTION_DEF (task, set_request_header); /*** * @method task:get_header(name[, case_sensitive]) * Get decoded value of a header specified with optional case_sensitive flag. @@ -472,6 +487,8 @@ static const struct luaL_reg tasklib_m[] = { LUA_INTERFACE_DEF (task, get_emails), LUA_INTERFACE_DEF (task, get_text_parts), LUA_INTERFACE_DEF (task, get_parts), + LUA_INTERFACE_DEF (task, get_request_header), + LUA_INTERFACE_DEF (task, set_request_header), LUA_INTERFACE_DEF (task, get_header), LUA_INTERFACE_DEF (task, get_header_raw), LUA_INTERFACE_DEF (task, get_header_full), @@ -917,6 +934,85 @@ lua_task_get_parts (lua_State * L) return 1; } +static gint +lua_task_get_request_header (lua_State *L) +{ + GString *hdr, srch; + struct rspamd_task *task = lua_check_task (L, 1); + const gchar *s; + struct rspamd_lua_text *t; + gsize len; + + s = luaL_checklstring (L, 2, &len); + + if (s) { + srch.str = (gchar *)s; + srch.len = len; + + hdr = g_hash_table_lookup (task->request_headers, &srch); + + if (hdr) { + t = lua_newuserdata (L, sizeof (*t)); + rspamd_lua_setclass (L, "rspamd{text}", -1); + t->start = hdr->str; + t->len = hdr->len; + t->own = FALSE; + + return 1; + } + } + + lua_pushnil (L); + return 1; +} + +static gint +lua_task_set_request_header (lua_State *L) +{ + struct rspamd_task *task = lua_check_task (L, 1); + const gchar *s, *v = NULL; + struct rspamd_lua_text *t; + GString *hdr, srch, *new_name; + gsize len, vlen; + + s = luaL_checklstring (L, 2, &len); + + if (s) { + if (lua_type (L, 3) == LUA_TSTRING) { + v = luaL_checklstring (L, 2, &vlen); + } + else if (lua_type (L, 3) == LUA_TUSERDATA) { + t = lua_check_text (L, 3); + + if (t != NULL) { + v = t->start; + vlen = t->len; + } + } + + if (v != NULL) { + srch.str = (gchar *)s; + srch.len = len; + + hdr = g_hash_table_lookup (task->request_headers, &srch); + + if (hdr) { + new_name = &srch; + } + else { + /* Not found, need to allocate */ + new_name = g_string_new_len (srch.str, srch.len); + } + hdr = g_string_new_len (v, vlen); + + /* This does not destroy key if it exists */ + g_hash_table_insert (task->request_headers, new_name, hdr); + } + + } + + return 0; +} gint rspamd_lua_push_header (lua_State * L, -- 2.39.5