From 1e3cbc5e00d9ba23ac1f18e3e51659535d61833b Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 1 Jul 2015 15:59:52 +0100 Subject: [PATCH] Add missing url fields. --- src/lua/lua_url.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/lua/lua_url.c b/src/lua/lua_url.c index 14a4df9a9..41bcf37bc 100644 --- a/src/lua/lua_url.c +++ b/src/lua/lua_url.c @@ -48,8 +48,11 @@ local mistake = res:to_table() -- INVALID! as pool is destroyed /* URL methods */ LUA_FUNCTION_DEF (url, get_length); LUA_FUNCTION_DEF (url, get_host); +LUA_FUNCTION_DEF (url, get_port); LUA_FUNCTION_DEF (url, get_user); LUA_FUNCTION_DEF (url, get_path); +LUA_FUNCTION_DEF (url, get_query); +LUA_FUNCTION_DEF (url, get_fragment); LUA_FUNCTION_DEF (url, get_text); LUA_FUNCTION_DEF (url, get_tld); LUA_FUNCTION_DEF (url, to_table); @@ -61,8 +64,11 @@ LUA_FUNCTION_DEF (url, all); static const struct luaL_reg urllib_m[] = { LUA_INTERFACE_DEF (url, get_length), LUA_INTERFACE_DEF (url, get_host), + LUA_INTERFACE_DEF (url, get_port), LUA_INTERFACE_DEF (url, get_user), LUA_INTERFACE_DEF (url, get_path), + LUA_INTERFACE_DEF (url, get_query), + LUA_INTERFACE_DEF (url, get_fragment), LUA_INTERFACE_DEF (url, get_text), LUA_INTERFACE_DEF (url, get_tld), LUA_INTERFACE_DEF (url, to_table), @@ -125,6 +131,25 @@ lua_url_get_host (lua_State *L) return 1; } +/*** + * @method url:get_port() + * Get port of the url + * @return {number} url port + */ +static gint +lua_url_get_port (lua_State *L) +{ + struct rspamd_lua_url *url = lua_check_url (L, 1); + + if (url != NULL) { + lua_pushnumber (L, url->url->port); + } + else { + lua_pushnil (L); + } + return 1; +} + /*** * @method url:get_user() * Get user part of the url (e.g. username in email) @@ -165,6 +190,46 @@ lua_url_get_path (lua_State *L) return 1; } +/*** + * @method url:get_query() + * Get query of the url + * @return {string} query part of URL + */ +static gint +lua_url_get_query (lua_State *L) +{ + struct rspamd_lua_url *url = lua_check_url (L, 1); + + if (url != NULL && url->url->querylen > 0) { + lua_pushlstring (L, url->url->query, url->url->querylen); + } + else { + lua_pushnil (L); + } + + return 1; +} + +/*** + * @method url:get_fragment() + * Get fragment of the url + * @return {string} fragment part of URL + */ +static gint +lua_url_get_fragment (lua_State *L) +{ + struct rspamd_lua_url *url = lua_check_url (L, 1); + + if (url != NULL && url->url->fragmentlen > 0) { + lua_pushlstring (L, url->url->fragment, url->url->fragmentlen); + } + else { + lua_pushnil (L); + } + + return 1; +} + /*** * @method url:get_text() * Get full content of the url @@ -280,6 +345,12 @@ lua_url_to_table (lua_State *L) lua_settable (L, -3); } + if (u->port != 0) { + lua_pushstring (L, "port"); + lua_pushnumber (L, u->port); + lua_settable (L, -3); + } + if (u->tldlen > 0) { lua_pushstring (L, "tld"); lua_pushlstring (L, u->tld, u->tldlen); @@ -298,6 +369,19 @@ lua_url_to_table (lua_State *L) lua_settable (L, -3); } + if (u->querylen > 0) { + lua_pushstring (L, "query"); + lua_pushlstring (L, u->query, u->querylen); + lua_settable (L, -3); + } + + if (u->fragmentlen > 0) { + lua_pushstring (L, "fragment"); + lua_pushlstring (L, u->fragment, u->fragmentlen); + lua_settable (L, -3); + } + + lua_pushstring (L, "protocol"); switch (u->protocol) { -- 2.39.5