luci-base: switch to lucihttp.urldecode() and lucihttp.urlencode()

Drop the Lua implementation in luci.http.protocol and use the optimized C
variants of liblucihttp instead.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2018-04-18 10:46:04 +02:00
parent 3f0abd936d
commit f52c8d0b7f
6 changed files with 237 additions and 240 deletions

View file

@ -12,7 +12,7 @@ LUCI_TYPE:=mod
LUCI_BASENAME:=base LUCI_BASENAME:=base
LUCI_TITLE:=LuCI core libraries LUCI_TITLE:=LuCI core libraries
LUCI_DEPENDS:=+lua +libuci-lua +luci-lib-nixio +luci-lib-ip +rpcd +libubus-lua +luci-lib-jsonc LUCI_DEPENDS:=+lua +libuci-lua +luci-lib-nixio +luci-lib-ip +rpcd +libubus-lua +luci-lib-jsonc +liblucihttp-lua
LUCI_EXTRA_DEPENDS:=libuci-lua (>= 2018-01-01) LUCI_EXTRA_DEPENDS:=libuci-lua (>= 2018-01-01)
PKG_SOURCE:=LuaSrcDiet-0.12.1.tar.bz2 PKG_SOURCE:=LuaSrcDiet-0.12.1.tar.bz2

View file

@ -267,9 +267,9 @@ function build_querystring(q)
return table.concat(s, "") return table.concat(s, "")
end end
urldecode = protocol.urldecode urldecode = util.urldecode
urlencode = protocol.urlencode urlencode = util.urlencode
function write_json(x) function write_json(x)
util.serialize_json(x, write) util.serialize_json(x, write)

View file

@ -6,27 +6,10 @@
module("luci.http.protocol", package.seeall) module("luci.http.protocol", package.seeall)
local ltn12 = require("luci.ltn12") local ltn12 = require("luci.ltn12")
local util = require("luci.util")
HTTP_MAX_CONTENT = 1024*8 -- 8 kB maximum content size HTTP_MAX_CONTENT = 1024*8 -- 8 kB maximum content size
-- the "+" sign to " " - and return the decoded string.
function urldecode( str, no_plus )
local function __chrdec( hex )
return string.char( tonumber( hex, 16 ) )
end
if type(str) == "string" then
if not no_plus then
str = str:gsub( "+", " " )
end
str = str:gsub( "%%([a-fA-F0-9][a-fA-F0-9])", __chrdec )
end
return str
end
-- from given url or string. Returns a table with urldecoded values. -- from given url or string. Returns a table with urldecoded values.
-- Simple parameters are stored as string values associated with the parameter -- Simple parameters are stored as string values associated with the parameter
-- name within the table. Parameters with multiple values are stored as array -- name within the table. Parameters with multiple values are stored as array
@ -42,8 +25,8 @@ function urldecode_params( url, tbl )
for pair in url:gmatch( "[^&;]+" ) do for pair in url:gmatch( "[^&;]+" ) do
-- find key and value -- find key and value
local key = urldecode( pair:match("^([^=]+)") ) local key = util.urldecode( pair:match("^([^=]+)") )
local val = urldecode( pair:match("^[^=]+=(.+)$") ) local val = util.urldecode( pair:match("^[^=]+=(.+)$") )
-- store -- store
if type(key) == "string" and key:len() > 0 then if type(key) == "string" and key:len() > 0 then
@ -62,24 +45,6 @@ function urldecode_params( url, tbl )
return params return params
end end
function urlencode( str )
local function __chrenc( chr )
return string.format(
"%%%02x", string.byte( chr )
)
end
if type(str) == "string" then
str = str:gsub(
"([^a-zA-Z0-9$_%-%.%~])",
__chrenc
)
end
return str
end
-- separated by "&". Tables are encoded as parameters with multiple values by -- separated by "&". Tables are encoded as parameters with multiple values by
-- repeating the parameter name with each value. -- repeating the parameter name with each value.
function urlencode_params( tbl ) function urlencode_params( tbl )
@ -89,11 +54,11 @@ function urlencode_params( tbl )
if type(v) == "table" then if type(v) == "table" then
for i, v2 in ipairs(v) do for i, v2 in ipairs(v) do
enc = enc .. ( #enc > 0 and "&" or "" ) .. enc = enc .. ( #enc > 0 and "&" or "" ) ..
urlencode(k) .. "=" .. urlencode(v2) util.urlencode(k) .. "=" .. util.urlencode(v2)
end end
else else
enc = enc .. ( #enc > 0 and "&" or "" ) .. enc = enc .. ( #enc > 0 and "&" or "" ) ..
urlencode(k) .. "=" .. urlencode(v) util.urlencode(k) .. "=" .. util.urlencode(v)
end end
end end

View file

@ -6,18 +6,6 @@ decoding and to retrive form data from raw http messages.
]] ]]
module "luci.http.protocol" module "luci.http.protocol"
---[[
Decode an urlencoded string - optionally without decoding
the "+" sign to " " - and return the decoded string.
@class function
@name urldecode
@param str Input string in x-www-urlencoded format
@param no_plus Don't decode "+" signs to spaces
@return The decoded string
@see urlencode
]]
---[[ ---[[
Extract and split urlencoded data pairs, separated bei either "&" or ";" Extract and split urlencoded data pairs, separated bei either "&" or ";"
@ -33,16 +21,6 @@ containing the corresponding values.
@see urlencode_params @see urlencode_params
]] ]]
---[[
Encode given string to x-www-urlencoded format.
@class function
@name urlencode
@param str String to encode
@return String containing the encoded data
@see urldecode
]]
---[[ ---[[
Encode each key-value-pair in given table to x-www-urlencoded format, Encode each key-value-pair in given table to x-www-urlencoded format,

View file

@ -10,6 +10,7 @@ local string = require "string"
local coroutine = require "coroutine" local coroutine = require "coroutine"
local tparser = require "luci.template.parser" local tparser = require "luci.template.parser"
local json = require "luci.jsonc" local json = require "luci.jsonc"
local lhttp = require "lucihttp"
local _ubus = require "ubus" local _ubus = require "ubus"
local _ubus_connection = nil local _ubus_connection = nil
@ -160,6 +161,25 @@ function pcdata(value)
return value and tparser.pcdata(tostring(value)) return value and tparser.pcdata(tostring(value))
end 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) function striptags(value)
return value and tparser.striptags(tostring(value)) return value and tparser.striptags(tostring(value))
end end

View file

@ -15,6 +15,7 @@ Class can be instantiated by calling them. All parameters will be passed
to the __init__ function of this class - if such a function exists. to the __init__ function of this class - if such a function exists.
The __init__ function must be used to set any object parameters that are not shared The __init__ function must be used to set any object parameters that are not shared
with other objects of this class. Any return values will be ignored. with other objects of this class. Any return values will be ignored.
@class function @class function
@name class @name class
@param base The base class to inherit from (optional) @param base The base class to inherit from (optional)
@ -37,9 +38,11 @@ Test whether the given object is an instance of the given class.
---[[ ---[[
Create a new or get an already existing thread local store associated with Create a new or get an already existing thread local store associated with
the current active coroutine.
the current active coroutine. A thread local store is private a table object A thread local store is private a table object
whose values can't be accessed from outside of the running coroutine. whose values can't be accessed from outside of the running coroutine.
@class function @class function
@name threadlocal @name threadlocal
@return Table value representing the corresponding thread local store @return Table value representing the corresponding thread local store
@ -73,6 +76,27 @@ Create valid XML PCDATA from given string.
@return String value containing the escaped data @return String value containing the escaped data
]] ]]
---[[
Decode an URL-encoded string - optionally decoding the "+" sign to space.
@class function
@name urldecode
@param str Input string in x-www-urlencoded format
@param decode_plus Decode "+" signs to spaces if true (optional)
@return The decoded string
@see urlencode
]]
---[[
URL-encode given string.
@class function
@name urlencode
@param str String to encode
@return String containing the encoded data
@see urldecode
]]
---[[ ---[[
Strip HTML tags from given string. Strip HTML tags from given string.
@ -93,11 +117,13 @@ Safely quote value for use in shell commands.
---[[ ---[[
Splits given string on a defined separator sequence and return a table Splits given string on a defined separator sequence and return a table
containing the resulting substrings.
The optional max parameter specifies the number of bytes to process,
regardless of the actual length of the given string. The optional last
parameter, regex, specifies whether the separator sequence is
nterpreted as regular expression.
containing the resulting substrings. The optional max parameter specifies
the number of bytes to process, regardless of the actual length of the given
string. The optional last parameter, regex, specifies whether the separator
sequence is interpreted as regular expression.
@class function @class function
@name split @name split
@param str String value containing the data to split up @param str String value containing the data to split up
@ -128,11 +154,12 @@ Count the occurrences of given substring in given string.
]] ]]
---[[ ---[[
Return a matching iterator for the given value. The iterator will return Return a matching iterator for the given value.
The iterator will return one token per invocation, the tokens are separated by
whitespace. If the input value is a table, it is transformed into a string first.
A nil value will result in a valid interator which aborts with the first invocation.
one token per invocation, the tokens are separated by whitespace. If the
input value is a table, it is transformed into a string first. A nil value
will result in a valid interator which aborts with the first invocation.
@class function @class function
@name imatch @name imatch
@param val The value to scan (table, string or nil) @param val The value to scan (table, string or nil)
@ -141,9 +168,11 @@ will result in a valid interator which aborts with the first invocation.
---[[ ---[[
Parse certain units from the given string and return the canonical integer Parse certain units from the given string and return the canonical integer
value or 0 if the unit is unknown.
value or 0 if the unit is unknown. Upper- or lower case is irrelevant. Upper- or lower case is irrelevant.
Recognized units are: Recognized units are:
-- o "y" - one year (60*60*24*366) -- o "y" - one year (60*60*24*366)
o "m" - one month (60*60*24*31) o "m" - one month (60*60*24*31)
o "w" - one week (60*60*24*7) o "w" - one week (60*60*24*7)
@ -156,6 +185,7 @@ Recognized units are:
o "kib" - one si kilobyte (1000) o "kib" - one si kilobyte (1000)
o "mib" - one si megabyte (1000*1000) o "mib" - one si megabyte (1000*1000)
o "gib" - one si gigabyte (1000*1000*1000) o "gib" - one si gigabyte (1000*1000*1000)
@class function @class function
@name parse_units @name parse_units
@param ustr String containing a numerical value with trailing unit @param ustr String containing a numerical value with trailing unit
@ -190,7 +220,7 @@ Checks whether the given table contains the given value.
@name contains @name contains
@param table Table value @param table Table value
@param value Value to search within the given table @param value Value to search within the given table
@return number indicating the first index at which the given value occurs @return Number indicating the first index at which the given value occurs
-- within table or false. -- within table or false.
]] ]]
@ -198,6 +228,7 @@ Checks whether the given table contains the given value.
Update values in given table with the values from the second given table. Update values in given table with the values from the second given table.
Both table are - in fact - merged together. Both table are - in fact - merged together.
@class function @class function
@name update @name update
@param t Table which should be updated @param t Table which should be updated
@ -234,8 +265,8 @@ Create a dynamic table which automatically creates subtables.
---[[ ---[[
Recursively serialize given data to lua code, suitable for restoring Recursively serialize given data to lua code, suitable for restoring
with loadstring(). with loadstring().
@class function @class function
@name serialize_data @name serialize_data
@param val Value containing the data to serialize @param val Value containing the data to serialize
@ -257,8 +288,8 @@ Restore data previously serialized with serialize_data().
---[[ ---[[
Return the current runtime bytecode of the given data. The byte code Return the current runtime bytecode of the given data. The byte code
will be stripped before it is returned. will be stripped before it is returned.
@class function @class function
@name get_bytecode @name get_bytecode
@param val Value to return as bytecode @param val Value to return as bytecode
@ -266,10 +297,11 @@ will be stripped before it is returned.
]] ]]
---[[ ---[[
Strips unnescessary lua bytecode from given string. Information like line Strips unnescessary lua bytecode from given string.
Information like line numbers and debugging numbers will be discarded.
Original version by Peter Cawley (http://lua-users.org/lists/lua-l/2008-02/msg01158.html)
numbers and debugging numbers will be discarded. Original version by
Peter Cawley (http://lua-users.org/lists/lua-l/2008-02/msg01158.html)
@class function @class function
@name strip_bytecode @name strip_bytecode
@param code String value containing the original lua byte code @param code String value containing the original lua byte code
@ -278,8 +310,8 @@ Peter Cawley (http://lua-users.org/lists/lua-l/2008-02/msg01158.html)
---[[ ---[[
Return a key, value iterator which returns the values sorted according to Return a key, value iterator which returns the values sorted according to
the provided callback function. the provided callback function.
@class function @class function
@name spairs @name spairs
@param t The table to iterate @param t The table to iterate
@ -291,6 +323,7 @@ the provided callback function.
Return a key, value iterator for the given table. Return a key, value iterator for the given table.
The table pairs are sorted by key. The table pairs are sorted by key.
@class function @class function
@name kspairs @name kspairs
@param t The table to iterate @param t The table to iterate
@ -301,6 +334,7 @@ The table pairs are sorted by key.
Return a key, value iterator for the given table. Return a key, value iterator for the given table.
The table pairs are sorted by value. The table pairs are sorted by value.
@class function @class function
@name vspairs @name vspairs
@param t The table to iterate @param t The table to iterate