luci-base: switch to lucihttp.urldecode() and lucihttp.urlencode()
[project/luci.git] / modules / luci-base / luasrc / util.lua
index 06a889c..36bbaaf 100644 (file)
@@ -10,6 +10,7 @@ local string = require "string"
 local coroutine = require "coroutine"
 local tparser = require "luci.template.parser"
 local json = require "luci.jsonc"
 local coroutine = require "coroutine"
 local tparser = require "luci.template.parser"
 local json = require "luci.jsonc"
+local lhttp = require "lucihttp"
 
 local _ubus = require "ubus"
 local _ubus_connection = nil
 
 local _ubus = require "ubus"
 local _ubus_connection = nil
@@ -160,6 +161,25 @@ function pcdata(value)
        return value and tparser.pcdata(tostring(value))
 end
 
        return value and tparser.pcdata(tostring(value))
 end
 
+function urlencode(value)
+       if value ~= nil then
+               local str = tostring(value)
+               return lhttp.urlencode(str, lhttp.ENCODE_IF_NEEDED + lhttp.ENCODE_FULL)
+                       or str
+       end
+       return nil
+end
+
+function urldecode(value, decode_plus)
+       if value ~= nil then
+               local flag = decode_plus and lhttp.DECODE_PLUS or 0
+               local str = tostring(value)
+               return lhttp.urldecode(str, lhttp.DECODE_IF_NEEDED + flag)
+                       or str
+       end
+       return nil
+end
+
 function striptags(value)
        return value and tparser.striptags(tostring(value))
 end
 function striptags(value)
        return value and tparser.striptags(tostring(value))
 end