Completed first version of JSON-RPC API
[project/luci.git] / modules / rpc / luasrc / jsonrpc.lua
index 84a7f70..71c95d5 100644 (file)
@@ -23,28 +23,31 @@ function resolve(mod, method)
                if not type(mod) == "table" then
                        break
                end
                if not type(mod) == "table" then
                        break
                end
-               mod = mod[path[j]]
+               mod = rawget(mod, path[j])
                if not mod then
                        break
                end
        end
                if not mod then
                        break
                end
        end
-       mod = type(mod) == "table" and mod[path[#path]] or nil
+       mod = type(mod) == "table" and rawget(mod, path[#path]) or nil
        if type(mod) == "function" then
                return mod
        end
 end
 
        if type(mod) == "function" then
                return mod
        end
 end
 
-function handle(tbl, rawdata)
-       local stat, json = luci.util.copcall(luci.json.Decode, rawdata)
+function handle(tbl, rawsource, ...)
+       local decoder = luci.json.Decoder()
+       local stat = luci.ltn12.pump.all(rawsource, decoder:sink())
+       local json = decoder:get()
        local response
        local success = false
        
        if stat then
                if type(json.method) == "string"
                 and (not json.params or type(json.params) == "table") then
        local response
        local success = false
        
        if stat then
                if type(json.method) == "string"
                 and (not json.params or type(json.params) == "table") then
-                       if tbl[json.method] then
+                       local method = resolve(tbl, json.method)
+                       if method then
                                response = reply(json.jsonrpc, json.id,
                                response = reply(json.jsonrpc, json.id,
-                                proxy(resolve(tbl, json.method), unpack(json.params or {})))
+                                proxy(method, unpack(json.params or {})))
                        else
                                response = reply(json.jsonrpc, json.id,
                                 nil, {code=-32601, message="Method not found."})
                        else
                                response = reply(json.jsonrpc, json.id,
                                 nil, {code=-32601, message="Method not found."})
@@ -54,22 +57,22 @@ function handle(tbl, rawdata)
                         nil, {code=-32600, message="Invalid request."})
                end
        else
                         nil, {code=-32600, message="Invalid request."})
                end
        else
-               response = reply(json.jsonrpc, nil,
+               response = reply("2.0", nil,
                 nil, {code=-32700, message="Parse error."})
        end
 
                 nil, {code=-32700, message="Parse error."})
        end
 
-       return luci.json.Encode(response)
+       return luci.json.Encoder(response, ...):source()
 end
 
 function reply(jsonrpc, id, res, err)
        require "luci.json"
 end
 
 function reply(jsonrpc, id, res, err)
        require "luci.json"
-       id = id or luci.json.Null
+       id = id or luci.json.null
        
        -- 1.0 compatibility
        if jsonrpc ~= "2.0" then
                jsonrpc = nil
        
        -- 1.0 compatibility
        if jsonrpc ~= "2.0" then
                jsonrpc = nil
-               res = res or luci.json.Null
-               err = err or luci.json.Null
+               res = res or luci.json.null
+               err = err or luci.json.null
        end
        
        return {id=id, result=res, error=err, jsonrpc=jsonrpc}
        end
        
        return {id=id, result=res, error=err, jsonrpc=jsonrpc}
@@ -83,9 +86,9 @@ function proxy(method, ...)
                return nil, {code=-32602, message="Invalid params.", data=table.remove(res, 1)} 
        else
                if #res <= 1 then
                return nil, {code=-32602, message="Invalid params.", data=table.remove(res, 1)} 
        else
                if #res <= 1 then
-                       return res[1] or luci.json.Null
+                       return res[1] or luci.json.null
                else
                        return res
                end
        end
                else
                        return res
                end
        end
-end
\ No newline at end of file
+end