summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-base/src/template_lualib.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2018-04-09 09:47:40 +0200
committerJo-Philipp Wich <jo@mein.io>2018-04-10 11:41:32 +0200
commitad7dc4a4928e77ae142d0fe040f9e9e64b530e82 (patch)
tree231c94708ba29b19386c608d6b9f34f3938fbbde /modules/luci-base/src/template_lualib.c
parentedd1fab34eaa98be624bbba17f60a8ae63744a98 (diff)
luci-base: add urldecode() and urlencode() C implementations
The C implementations of urlencode and urldecode are considerably faster than their current Lua counterparts. On an AMD Geode system, the C variant is up to ten times faster when decoding strings and up to four times faster when encoding them. The functions are also designed to only allocate new strings when any actual changes are required, otherwise they reuse the existing input strings, reducing the overal memory usage somewhat. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-base/src/template_lualib.c')
-rw-r--r--modules/luci-base/src/template_lualib.c49
1 files changed, 48 insertions, 1 deletions
diff --git a/modules/luci-base/src/template_lualib.c b/modules/luci-base/src/template_lualib.c
index d5c8dd6b4c..45e23966e9 100644
--- a/modules/luci-base/src/template_lualib.c
+++ b/modules/luci-base/src/template_lualib.c
@@ -1,7 +1,7 @@
/*
* LuCI Template - Lua binding
*
- * Copyright (C) 2009 Jo-Philipp Wich <jow@openwrt.org>
+ * Copyright (C) 2009-2018 Jo-Philipp Wich <jo@mein.io>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -110,6 +110,51 @@ int template_L_striptags(lua_State *L)
return 0;
}
+int template_L_urlencode(lua_State *L)
+{
+ size_t len = 0;
+ const char *str = luaL_checkstring(L, 1);
+ char *res = urlencode(str, &len);
+
+ if (res != NULL)
+ {
+ lua_pushlstring(L, res, len);
+ free(res);
+
+ return 1;
+ }
+ else if (len == 0)
+ {
+ lua_pushvalue(L, 1);
+ return 1;
+ }
+
+ return 0;
+}
+
+int template_L_urldecode(lua_State *L)
+{
+ size_t len = 0;
+ const char *str = luaL_checkstring(L, 1);
+ int keep_plus = lua_toboolean(L, 2);
+ char *res = urldecode(str, &len, keep_plus == 1);
+
+ if (res != NULL)
+ {
+ lua_pushlstring(L, res, len);
+ free(res);
+
+ return 1;
+ }
+ else if (len == 0)
+ {
+ lua_pushvalue(L, 1);
+ return 1;
+ }
+
+ return 0;
+}
+
static int template_L_load_catalog(lua_State *L) {
const char *lang = luaL_optstring(L, 1, "en");
const char *dir = luaL_optstring(L, 2, NULL);
@@ -165,6 +210,8 @@ static const luaL_reg R[] = {
{ "utf8", template_L_utf8 },
{ "pcdata", template_L_pcdata },
{ "striptags", template_L_striptags },
+ { "urlencode", template_L_urlencode },
+ { "urldecode", template_L_urldecode },
{ "load_catalog", template_L_load_catalog },
{ "close_catalog", template_L_close_catalog },
{ "change_catalog", template_L_change_catalog },