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,135 +15,164 @@ 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
@name class @class function
@param base The base class to inherit from (optional) @name class
@return A class object @param base The base class to inherit from (optional)
@see instanceof @return A class object
@see clone @see instanceof
@see clone
]] ]]
---[[ ---[[
Test whether the given object is an instance of the given class. Test whether the given object is an instance of the given class.
@class function @class function
@name instanceof @name instanceof
@param object Object instance @param object Object instance
@param class Class object to test against @param class Class object to test against
@return Boolean indicating whether the object is an instance @return Boolean indicating whether the object is an instance
@see class @see class
@see clone @see clone
]] ]]
---[[ ---[[
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
@name threadlocal @class function
@return Table value representing the corresponding thread local store @name threadlocal
@return Table value representing the corresponding thread local store
]] ]]
---[[ ---[[
Write given object to stderr. Write given object to stderr.
@class function @class function
@name perror @name perror
@param obj Value to write to stderr @param obj Value to write to stderr
@return Boolean indicating whether the write operation was successful @return Boolean indicating whether the write operation was successful
]] ]]
---[[ ---[[
Recursively dumps a table to stdout, useful for testing and debugging. Recursively dumps a table to stdout, useful for testing and debugging.
@class function @class function
@name dumptable @name dumptable
@param t Table value to dump @param t Table value to dump
@param maxdepth Maximum depth @param maxdepth Maximum depth
@return Always nil @return Always nil
]] ]]
---[[ ---[[
Create valid XML PCDATA from given string. Create valid XML PCDATA from given string.
@class function @class function
@name pcdata @name pcdata
@param value String value containing the data to escape @param value String value containing the data to escape
@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.
@class function @class function
@name striptags @name striptags
@param value String containing the HTML text @param value String containing the HTML text
@return String with HTML tags stripped of @return String with HTML tags stripped of
]] ]]
---[[ ---[[
Safely quote value for use in shell commands. Safely quote value for use in shell commands.
@class function @class function
@name shellquote @name shellquote
@param value String containing the value to quote @param value String containing the value to quote
@return Single-quote enclosed string with embedded quotes escaped @return Single-quote enclosed string with embedded quotes escaped
]] ]]
---[[ ---[[
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.
containing the resulting substrings. The optional max parameter specifies The optional max parameter specifies the number of bytes to process,
the number of bytes to process, regardless of the actual length of the given regardless of the actual length of the given string. The optional last
string. The optional last parameter, regex, specifies whether the separator parameter, regex, specifies whether the separator sequence is
sequence is interpreted as regular expression. nterpreted as regular expression.
@class function
@name split @class function
@param str String value containing the data to split up @name split
@param pat String with separator pattern (optional, defaults to "\n") @param str String value containing the data to split up
@param max Maximum times to split (optional) @param pat String with separator pattern (optional, defaults to "\n")
@param regex Boolean indicating whether to interpret the separator @param max Maximum times to split (optional)
@param regex Boolean indicating whether to interpret the separator
-- pattern as regular expression (optional, default is false) -- pattern as regular expression (optional, default is false)
@return Table containing the resulting substrings @return Table containing the resulting substrings
]] ]]
---[[ ---[[
Remove leading and trailing whitespace from given string value. Remove leading and trailing whitespace from given string value.
@class function @class function
@name trim @name trim
@param str String value containing whitespace padded data @param str String value containing whitespace padded data
@return String value with leading and trailing space removed @return String value with leading and trailing space removed
]] ]]
---[[ ---[[
Count the occurrences of given substring in given string. Count the occurrences of given substring in given string.
@class function @class function
@name cmatch @name cmatch
@param str String to search in @param str String to search in
@param pattern String containing pattern to find @param pattern String containing pattern to find
@return Number of found occurrences @return Number of found occurrences
]] ]]
---[[ ---[[
Return a matching iterator for the given value. The iterator will return Return a matching iterator for the given value.
one token per invocation, the tokens are separated by whitespace. If the The iterator will return one token per invocation, the tokens are separated by
input value is a table, it is transformed into a string first. A nil value whitespace. If the input value is a table, it is transformed into a string first.
will result in a valid interator which aborts with the first invocation. A nil value will result in a valid interator which aborts with the first invocation.
@class function
@name imatch @class function
@param val The value to scan (table, string or nil) @name imatch
@return Iterator which returns one token per call @param val The value to scan (table, string or nil)
@return Iterator which returns one token per call
]] ]]
---[[ ---[[
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,232 +185,237 @@ 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
@name parse_units @class function
@param ustr String containing a numerical value with trailing unit @name parse_units
@return Number containing the canonical value @param ustr String containing a numerical value with trailing unit
@return Number containing the canonical value
]] ]]
---[[ ---[[
Appends numerically indexed tables or single objects to a given table. Appends numerically indexed tables or single objects to a given table.
@class function @class function
@name append @name append
@param src Target table @param src Target table
@param ... Objects to insert @param ... Objects to insert
@return Target table @return Target table
]] ]]
---[[ ---[[
Combines two or more numerically indexed tables and single objects into one table. Combines two or more numerically indexed tables and single objects into one table.
@class function @class function
@name combine @name combine
@param tbl1 Table value to combine @param tbl1 Table value to combine
@param tbl2 Table value to combine @param tbl2 Table value to combine
@param ... More tables to combine @param ... More tables to combine
@return Table value containing all values of given tables @return Table value containing all values of given tables
]] ]]
---[[ ---[[
Checks whether the given table contains the given value. Checks whether the given table contains the given value.
@class function @class function
@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.
]] ]]
---[[ ---[[
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
@name update @class function
@name update
@param t Table which should be updated @param t Table which should be updated
@param updates Table containing the values to update @param updates Table containing the values to update
@return Always nil @return Always nil
]] ]]
---[[ ---[[
Retrieve all keys of given associative table. Retrieve all keys of given associative table.
@class function @class function
@name keys @name keys
@param t Table to extract keys from @param t Table to extract keys from
@return Sorted table containing the keys @return Sorted table containing the keys
]] ]]
---[[ ---[[
Clones the given object and return it's copy. Clones the given object and return it's copy.
@class function @class function
@name clone @name clone
@param object Table value to clone @param object Table value to clone
@param deep Boolean indicating whether to do recursive cloning @param deep Boolean indicating whether to do recursive cloning
@return Cloned table value @return Cloned table value
]] ]]
---[[ ---[[
Create a dynamic table which automatically creates subtables. Create a dynamic table which automatically creates subtables.
@class function @class function
@name dtable @name dtable
@return Dynamic Table @return Dynamic Table
]] ]]
---[[ ---[[
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
@name serialize_data @class function
@param val Value containing the data to serialize @name serialize_data
@return String value containing the serialized code @param val Value containing the data to serialize
@see restore_data @return String value containing the serialized code
@see get_bytecode @see restore_data
@see get_bytecode
]] ]]
---[[ ---[[
Restore data previously serialized with serialize_data(). Restore data previously serialized with serialize_data().
@class function @class function
@name restore_data @name restore_data
@param str String containing the data to restore @param str String containing the data to restore
@return Value containing the restored data structure @return Value containing the restored data structure
@see serialize_data @see serialize_data
@see get_bytecode @see get_bytecode
]] ]]
---[[ ---[[
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
@name get_bytecode @class function
@param val Value to return as bytecode @name get_bytecode
@return String value containing the bytecode of the given data @param val Value to return as bytecode
@return String value containing the bytecode of the given data
]] ]]
---[[ ---[[
Strips unnescessary lua bytecode from given string. Information like line Strips unnescessary lua bytecode from given string.
numbers and debugging numbers will be discarded. Original version by Information like line numbers and debugging numbers will be discarded.
Peter Cawley (http://lua-users.org/lists/lua-l/2008-02/msg01158.html) Original version by Peter Cawley (http://lua-users.org/lists/lua-l/2008-02/msg01158.html)
@class function
@name strip_bytecode @class function
@param code String value containing the original lua byte code @name strip_bytecode
@return String value containing the stripped lua byte code @param code String value containing the original lua byte code
@return String value containing the stripped lua byte code
]] ]]
---[[ ---[[
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
@name spairs @class function
@param t The table to iterate @name spairs
@param f A callback function to decide the order of elements @param t The table to iterate
@return Function value containing the corresponding iterator @param f A callback function to decide the order of elements
@return Function value containing the corresponding iterator
]] ]]
---[[ ---[[
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
@name kspairs @class function
@param t The table to iterate @name kspairs
@return Function value containing the corresponding iterator @param t The table to iterate
@return Function value containing the corresponding iterator
]] ]]
---[[ ---[[
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
@name vspairs @class function
@param t The table to iterate @name vspairs
@return Function value containing the corresponding iterator @param t The table to iterate
@return Function value containing the corresponding iterator
]] ]]
---[[ ---[[
Test whether the current system is operating in big endian mode. Test whether the current system is operating in big endian mode.
@class function @class function
@name bigendian @name bigendian
@return Boolean value indicating whether system is big endian @return Boolean value indicating whether system is big endian
]] ]]
---[[ ---[[
Execute given commandline and gather stdout. Execute given commandline and gather stdout.
@class function @class function
@name exec @name exec
@param command String containing command to execute @param command String containing command to execute
@return String containing the command's stdout @return String containing the command's stdout
]] ]]
---[[ ---[[
Return a line-buffered iterator over the output of given command. Return a line-buffered iterator over the output of given command.
@class function @class function
@name execi @name execi
@param command String containing the command to execute @param command String containing the command to execute
@return Iterator @return Iterator
]] ]]
---[[ ---[[
Issue an ubus call. Issue an ubus call.
@class function @class function
@name ubus @name ubus
@param object String containing the ubus object to call @param object String containing the ubus object to call
@param method String containing the ubus method to call @param method String containing the ubus method to call
@param values Table containing the values to pass @param values Table containing the values to pass
@return Table containin the ubus result @return Table containin the ubus result
]] ]]
---[[ ---[[
Convert data structure to JSON Convert data structure to JSON
@class function @class function
@name serialize_json @name serialize_json
@param data The data to serialize @param data The data to serialize
@param writer A function to write a chunk of JSON data (optional) @param writer A function to write a chunk of JSON data (optional)
@return String containing the JSON if called without write callback @return String containing the JSON if called without write callback
]] ]]
---[[ ---[[
Returns the absolute path to LuCI base directory. Returns the absolute path to LuCI base directory.
@class function @class function
@name libpath @name libpath
@return String containing the directory path @return String containing the directory path
]] ]]
---[[ ---[[
This is a coroutine-safe drop-in replacement for Lua's "xpcall"-function This is a coroutine-safe drop-in replacement for Lua's "xpcall"-function
@class function @class function
@name coxpcall @name coxpcall
@param f Lua function to be called protected @param f Lua function to be called protected
@param err Custom error handler @param err Custom error handler
@param ... Parameters passed to the function @param ... Parameters passed to the function
@return A boolean whether the function call succeeded and the return @return A boolean whether the function call succeeded and the return
-- values of either the function or the error handler -- values of either the function or the error handler
]] ]]
---[[ ---[[
This is a coroutine-safe drop-in replacement for Lua's "pcall"-function This is a coroutine-safe drop-in replacement for Lua's "pcall"-function
@class function @class function
@name copcall @name copcall
@param f Lua function to be called protected @param f Lua function to be called protected
@param ... Parameters passed to the function @param ... Parameters passed to the function
@return A boolean whether the function call succeeded and the returns @return A boolean whether the function call succeeded and the returns
-- values of the function or the error object -- values of the function or the error object
]] ]]