luci-base: fix list method handling in ubus-rpc protocol proxy

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2019-09-12 14:00:47 +02:00
parent e712a8a4ac
commit cc81d5a0d4

View file

@ -147,6 +147,8 @@ local function ubus_reply(id, data, code, errmsg)
code = code, code = code,
message = errmsg message = errmsg
} }
elseif type(code) == "table" then
reply.result = code
else else
reply.result = { code, data } reply.result = { code, data }
end end
@ -178,11 +180,14 @@ local function ubus_access(sid, obj, fun)
end end
local function ubus_request(req) local function ubus_request(req)
if type(req) ~= "table" or type(req.method) ~= "string" or type(req.params) ~= "table" or if type(req) ~= "table" or type(req.method) ~= "string" or req.jsonrpc ~= "2.0" or req.id == nil then
#req.params < 2 or req.jsonrpc ~= "2.0" or req.id == nil then
return ubus_reply(nil, nil, -32600, "Invalid request") return ubus_reply(nil, nil, -32600, "Invalid request")
elseif req.method == "call" then elseif req.method == "call" then
if type(req.params) ~= "table" or #req.params < 3 then
return ubus_reply(nil, nil, -32600, "Invalid parameters")
end
local sid, obj, fun, arg = local sid, obj, fun, arg =
req.params[1], req.params[2], req.params[3], req.params[4] or {} req.params[1], req.params[2], req.params[3], req.params[4] or {}
if type(arg) ~= "table" or arg.ubus_rpc_session ~= nil then if type(arg) ~= "table" or arg.ubus_rpc_session ~= nil then
@ -203,34 +208,38 @@ local function ubus_request(req)
return ubus_reply(req.id, res, code or 0) return ubus_reply(req.id, res, code or 0)
elseif req.method == "list" then elseif req.method == "list" then
if type(params) ~= "table" or #params == 0 then if req.params == nil or (type(req.params) == "table" and #req.params == 0) then
local objs = { luci.util.ubus() } local objs = luci.util.ubus()
return ubus_reply(req.id, objs, 0) return ubus_reply(req.id, nil, objs)
else
elseif type(req.params) == "table" then
local n, rv = nil, {} local n, rv = nil, {}
for n = 1, #params do for n = 1, #req.params do
if type(params[n]) ~= "string" then if type(req.params[n]) ~= "string" then
return ubus_reply(req.id, nil, -32602, "Invalid parameters") return ubus_reply(req.id, nil, -32602, "Invalid parameters")
end end
local sig = luci.util.ubus(params[n]) local sig = luci.util.ubus(req.params[n])
if sig and type(sig) == "table" then if sig and type(sig) == "table" then
rv[params[n]] = {} rv[req.params[n]] = {}
local m, p local m, p
for m, p in pairs(sig) do for m, p in pairs(sig) do
if type(p) == "table" then if type(p) == "table" then
rv[params[n]][m] = {} rv[req.params[n]][m] = {}
local pn, pt local pn, pt
for pn, pt in pairs(p) do for pn, pt in pairs(p) do
rv[params[n]][m][pn] = ubus_types[pt] or "unknown" rv[req.params[n]][m][pn] = ubus_types[pt] or "unknown"
end end
end end
end end
end end
end end
return ubus_reply(req.id, rv, 0) return ubus_reply(req.id, nil, rv)
else
return ubus_reply(req.id, nil, -32602, "Invalid parameters")
end end
end end