Browse Source

Move url functions to a separate module.

tags/0.9.0
Vsevolod Stakhov 9 years ago
parent
commit
89876fcf9b
4 changed files with 229 additions and 148 deletions
  1. 2
    1
      src/lua/CMakeLists.txt
  2. 4
    0
      src/lua/lua_common.h
  3. 3
    147
      src/lua/lua_task.c
  4. 220
    0
      src/lua/lua_url.c

+ 2
- 1
src/lua/CMakeLists.txt View File

@@ -19,6 +19,7 @@ SET(LUASRC ${CMAKE_CURRENT_SOURCE_DIR}/lua_common.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_ip.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_expression.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_trie.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_mimepart.c)
${CMAKE_CURRENT_SOURCE_DIR}/lua_mimepart.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_url.c)

SET(RSPAMD_LUA ${LUASRC} PARENT_SCOPE)

+ 4
- 0
src/lua/lua_common.h View File

@@ -67,6 +67,10 @@ struct rspamd_lua_text {
gsize len;
};

struct rspamd_lua_url {
struct rspamd_url *url;
};


/* Common utility functions */


+ 3
- 147
src/lua/lua_task.c View File

@@ -517,27 +517,6 @@ static const struct luaL_reg imagelib_m[] = {
{NULL, NULL}
};

/* URL methods */
LUA_FUNCTION_DEF (url, get_length);
LUA_FUNCTION_DEF (url, get_host);
LUA_FUNCTION_DEF (url, get_user);
LUA_FUNCTION_DEF (url, get_path);
LUA_FUNCTION_DEF (url, get_text);
LUA_FUNCTION_DEF (url, is_phished);
LUA_FUNCTION_DEF (url, get_phished);

static const struct luaL_reg urllib_m[] = {
LUA_INTERFACE_DEF (url, get_length),
LUA_INTERFACE_DEF (url, get_host),
LUA_INTERFACE_DEF (url, get_user),
LUA_INTERFACE_DEF (url, get_path),
LUA_INTERFACE_DEF (url, get_text),
LUA_INTERFACE_DEF (url, is_phished),
LUA_INTERFACE_DEF (url, get_phished),
{"__tostring", rspamd_lua_class_tostring},
{NULL, NULL}
};

/* Blob methods */
LUA_FUNCTION_DEF (text, len);
LUA_FUNCTION_DEF (text, str);
@@ -566,14 +545,6 @@ lua_check_image (lua_State * L)
return ud ? *((struct rspamd_image **)ud) : NULL;
}

static struct rspamd_url *
lua_check_url (lua_State * L)
{
void *ud = luaL_checkudata (L, 1, "rspamd{url}");
luaL_argcheck (L, ud != NULL, 1, "'url' expected");
return ud ? *((struct rspamd_url **)ud) : NULL;
}

struct rspamd_lua_text *
lua_check_text (lua_State * L, gint pos)
{
@@ -817,12 +788,12 @@ struct lua_tree_cb_data {
static void
lua_tree_url_callback (gpointer key, gpointer value, gpointer ud)
{
struct rspamd_url **purl;
struct rspamd_lua_url *url;
struct lua_tree_cb_data *cb = ud;

purl = lua_newuserdata (cb->L, sizeof (struct rspamd_url *));
url = lua_newuserdata (cb->L, sizeof (struct rspamd_lua_url));
rspamd_lua_setclass (cb->L, "rspamd{url}", -1);
*purl = value;
url->url = value;
lua_rawseti (cb->L, -2, cb->i++);
}

@@ -1980,114 +1951,6 @@ lua_image_get_filename (lua_State *L)
return 1;
}

/* URL part */
static gint
lua_url_get_length (lua_State *L)
{
struct rspamd_url *url = lua_check_url (L);

if (url != NULL) {
lua_pushinteger (L, strlen (struri (url)));
}
else {
lua_pushnil (L);
}
return 1;
}

static gint
lua_url_get_host (lua_State *L)
{
struct rspamd_url *url = lua_check_url (L);

if (url != NULL) {
lua_pushlstring (L, url->host, url->hostlen);
}
else {
lua_pushnil (L);
}
return 1;
}

static gint
lua_url_get_user (lua_State *L)
{
struct rspamd_url *url = lua_check_url (L);

if (url != NULL && url->user != NULL) {
lua_pushlstring (L, url->user, url->userlen);
}
else {
lua_pushnil (L);
}

return 1;
}

static gint
lua_url_get_path (lua_State *L)
{
struct rspamd_url *url = lua_check_url (L);

if (url != NULL) {
lua_pushlstring (L, url->data, url->datalen);
}
else {
lua_pushnil (L);
}

return 1;
}

static gint
lua_url_get_text (lua_State *L)
{
struct rspamd_url *url = lua_check_url (L);

if (url != NULL) {
lua_pushstring (L, struri (url));
}
else {
lua_pushnil (L);
}

return 1;
}

static gint
lua_url_is_phished (lua_State *L)
{
struct rspamd_url *url = lua_check_url (L);

if (url != NULL) {
lua_pushboolean (L, url->is_phished);
}
else {
lua_pushnil (L);
}

return 1;
}

static gint
lua_url_get_phished (lua_State *L)
{
struct rspamd_url **purl, *url = lua_check_url (L);

if (url) {
if (url->is_phished && url->phished_url != NULL) {
purl = lua_newuserdata (L, sizeof (struct rspamd_url *));
rspamd_lua_setclass (L, "rspamd{url}", -1);
*purl = url->phished_url;

return 1;
}
}

lua_pushnil (L);
return 1;
}

/* Text methods */
static gint
lua_text_len (lua_State *L)
@@ -2146,13 +2009,6 @@ luaopen_image (lua_State * L)
lua_pop (L, 1); /* remove metatable from stack */
}

void
luaopen_url (lua_State * L)
{
rspamd_lua_new_class (L, "rspamd{url}", urllib_m);
lua_pop (L, 1); /* remove metatable from stack */
}

void
luaopen_text (lua_State *L)
{

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

@@ -0,0 +1,220 @@
/* Copyright (c) 2015, Vsevolod Stakhov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "lua_common.h"

/* URL methods */
LUA_FUNCTION_DEF (url, get_length);
LUA_FUNCTION_DEF (url, get_host);
LUA_FUNCTION_DEF (url, get_user);
LUA_FUNCTION_DEF (url, get_path);
LUA_FUNCTION_DEF (url, get_text);
LUA_FUNCTION_DEF (url, is_phished);
LUA_FUNCTION_DEF (url, get_phished);
LUA_FUNCTION_DEF (url, create);

static const struct luaL_reg urllib_m[] = {
LUA_INTERFACE_DEF (url, get_length),
LUA_INTERFACE_DEF (url, get_host),
LUA_INTERFACE_DEF (url, get_user),
LUA_INTERFACE_DEF (url, get_path),
LUA_INTERFACE_DEF (url, get_text),
LUA_INTERFACE_DEF (url, is_phished),
LUA_INTERFACE_DEF (url, get_phished),
{"__tostring", lua_url_get_text},
{NULL, NULL}
};

static const struct luaL_reg urllib_f[] = {
LUA_INTERFACE_DEF (url, create),
{NULL, NULL}
};

static struct rspamd_lua_url *
lua_check_url (lua_State * L, gint pos)
{
void *ud = luaL_checkudata (L, pos, "rspamd{url}");
luaL_argcheck (L, ud != NULL, pos, "'url' expected");
return ud ? ((struct rspamd_lua_url *)ud) : NULL;
}


static gint
lua_url_get_length (lua_State *L)
{
struct rspamd_lua_url *url = lua_check_url (L, 1);

if (url != NULL) {
lua_pushinteger (L, url->url->urllen);
}
else {
lua_pushnil (L);
}
return 1;
}

static gint
lua_url_get_host (lua_State *L)
{
struct rspamd_lua_url *url = lua_check_url (L, 1);

if (url != NULL) {
lua_pushlstring (L, url->url->host, url->url->hostlen);
}
else {
lua_pushnil (L);
}
return 1;
}

static gint
lua_url_get_user (lua_State *L)
{
struct rspamd_lua_url *url = lua_check_url (L, 1);

if (url != NULL && url->url->user != NULL) {
lua_pushlstring (L, url->url->user, url->url->userlen);
}
else {
lua_pushnil (L);
}

return 1;
}

static gint
lua_url_get_path (lua_State *L)
{
struct rspamd_lua_url *url = lua_check_url (L, 1);

if (url != NULL) {
lua_pushlstring (L, url->url->data, url->url->datalen);
}
else {
lua_pushnil (L);
}

return 1;
}

static gint
lua_url_get_text (lua_State *L)
{
struct rspamd_lua_url *url = lua_check_url (L, 1);

if (url != NULL) {
lua_pushlstring (L, url->url->string, url->url->urllen);
}
else {
lua_pushnil (L);
}

return 1;
}

static gint
lua_url_is_phished (lua_State *L)
{
struct rspamd_lua_url *url = lua_check_url (L, 1);

if (url != NULL) {
lua_pushboolean (L, url->url->is_phished);
}
else {
lua_pushnil (L);
}

return 1;
}

static gint
lua_url_get_phished (lua_State *L)
{
struct rspamd_lua_url *purl, *url = lua_check_url (L, 1);

if (url) {
if (url->url->is_phished && url->url->phished_url != NULL) {
purl = lua_newuserdata (L, sizeof (struct rspamd_lua_url));
rspamd_lua_setclass (L, "rspamd{url}", -1);
purl->url = url->url->phished_url;

return 1;
}
}

lua_pushnil (L);
return 1;
}

static gint
lua_url_create (lua_State *L)
{
struct rspamd_url *url;
struct rspamd_lua_url *lua_url;
rspamd_mempool_t *pool = rspamd_lua_check_mempool (L, 1);
const gchar *text;

if (pool == NULL) {
lua_pushnil (L);
}
else {
text = luaL_checkstring (L, 2);

if (text != NULL) {
url = rspamd_url_get_next (pool, text, NULL, NULL);

if (url == NULL) {
lua_pushnil (L);
}
else {
lua_url = lua_newuserdata (L, sizeof (struct rspamd_lua_url));
rspamd_lua_setclass (L, "rspamd{url}", -1);
lua_url->url = url;
}
}
else {
lua_pushnil (L);
}
}


return 1;
}

static gint
lua_load_url (lua_State * L)
{
lua_newtable (L);
luaL_register (L, NULL, urllib_f);

return 1;
}

void
luaopen_url (lua_State * L)
{
rspamd_lua_new_class (L, "rspamd{url}", urllib_m);
lua_pop (L, 1);

rspamd_lua_add_preload (L, "rspamd_url", lua_load_url);
}

Loading…
Cancel
Save