Browse Source

[Feature] Implement url tags concept

tags/1.5.0
Vsevolod Stakhov 7 years ago
parent
commit
d26c0a18a7
5 changed files with 86 additions and 2 deletions
  1. 1
    1
      src/libserver/html.c
  2. 15
    0
      src/libserver/url.c
  3. 10
    0
      src/libserver/url.h
  4. 59
    0
      src/lua/lua_url.c
  5. 1
    1
      src/plugins/surbl.c

+ 1
- 1
src/libserver/html.c View File

@@ -1297,7 +1297,7 @@ rspamd_html_process_url (rspamd_mempool_t *pool, const gchar *start, guint len,

*d = '\0';

url = rspamd_mempool_alloc (pool, sizeof (*url));
url = rspamd_mempool_alloc0 (pool, sizeof (*url));
rc = rspamd_url_parse (url, decoded, d - decoded, pool);

if (rc == URI_ERRNO_OK) {

+ 15
- 0
src/libserver/url.c View File

@@ -2537,3 +2537,18 @@ rspamd_url_task_callback (struct rspamd_url *url, gsize start_offset,
}
}
}

void
rspamd_url_add_tag (struct rspamd_url *url, const gchar *tag,
rspamd_mempool_t *pool)
{
g_assert (url != NULL && tag != NULL);

if (url->tags == NULL) {
url->tags = g_ptr_array_sized_new (2);
rspamd_mempool_add_destructor (pool, rspamd_ptr_array_free_hard, url->tags);
}

g_ptr_array_add (url->tags, rspamd_mempool_strdup (pool, tag));

}

+ 10
- 0
src/libserver/url.h View File

@@ -42,6 +42,7 @@ struct rspamd_url {
guint urllen;

enum rspamd_url_flags flags;
GPtrArray *tags;
};

enum uri_errno {
@@ -161,4 +162,13 @@ void rspamd_url_find_single (rspamd_mempool_t *pool, const gchar *in,
void rspamd_url_task_callback (struct rspamd_url *url, gsize start_offset,
gsize end_offset, gpointer ud);

/**
* Adds a tag for url
* @param url
* @param tag
* @param pool
*/
void rspamd_url_add_tag (struct rspamd_url *url, const gchar *tag,
rspamd_mempool_t *pool);

#endif

+ 59
- 0
src/lua/lua_url.c View File

@@ -52,6 +52,8 @@ LUA_FUNCTION_DEF (url, is_phished);
LUA_FUNCTION_DEF (url, is_redirected);
LUA_FUNCTION_DEF (url, is_obscured);
LUA_FUNCTION_DEF (url, get_phished);
LUA_FUNCTION_DEF (url, get_tags);
LUA_FUNCTION_DEF (url, add_tag);
LUA_FUNCTION_DEF (url, create);
LUA_FUNCTION_DEF (url, init);
LUA_FUNCTION_DEF (url, all);
@@ -71,6 +73,8 @@ static const struct luaL_reg urllib_m[] = {
LUA_INTERFACE_DEF (url, is_redirected),
LUA_INTERFACE_DEF (url, is_obscured),
LUA_INTERFACE_DEF (url, get_phished),
LUA_INTERFACE_DEF (url, get_tags),
LUA_INTERFACE_DEF (url, add_tag),
{"get_redirected", lua_url_get_phished},
{"__tostring", lua_url_get_text},
{NULL, NULL}
@@ -309,6 +313,61 @@ lua_url_is_obscured (lua_State *L)
return 1;
}

/***
* @method url:get_tags()
* Returns list of string tags for an url
* @return {table/strings} list of tags for an url
*/
static gint
lua_url_get_tags (lua_State *L)
{
struct rspamd_lua_url *url = lua_check_url (L, 1);
guint i;
const gchar *tag;

if (url != NULL) {
if (url->url->tags == NULL) {
lua_createtable (L, 0, 0);
}
else {
lua_createtable (L, url->url->tags->len, 0);

PTR_ARRAY_FOREACH (url->url->tags, i, tag) {
lua_pushstring (L, tag);
lua_rawseti (L, -2, i + 1);
}
}
}
else {
lua_pushnil (L);
}

return 1;
}

/***
* @method url:add_tag(tag, mempool)
* Adds a new tag for url
* @param {string} tag new tag to add
* @param {mempool} mempool memory pool (e.g. `task:get_pool()`)
*/
static gint
lua_url_add_tag (lua_State *L)
{
struct rspamd_lua_url *url = lua_check_url (L, 1);
rspamd_mempool_t *mempool = rspamd_lua_check_mempool (L, 3);
const gchar *tag = luaL_checkstring (L, 2);

if (url != NULL && mempool != NULL && tag != NULL) {
rspamd_url_add_tag (url->url, tag, mempool);
}
else {
return luaL_error (L, "invalid arguments");
}

return 0;
}

/***
* @method url:get_phished()
* Get another URL that pretends to be this URL (e.g. used in phishing)

+ 1
- 1
src/plugins/surbl.c View File

@@ -1308,7 +1308,7 @@ surbl_redirector_finish (struct rspamd_http_connection *conn,
urllen = hdr->len;
urlstr = rspamd_mempool_alloc (task->task_pool,
urllen + 1);
redirected_url = rspamd_mempool_alloc (task->task_pool,
redirected_url = rspamd_mempool_alloc0 (task->task_pool,
sizeof (*redirected_url));
rspamd_strlcpy (urlstr, hdr->begin, urllen + 1);
r = rspamd_url_parse (redirected_url, urlstr, urllen,

Loading…
Cancel
Save