Merge pull request #1818 from dibdot/lxc_fix
[project/luci.git] / modules / luci-mod-rpc / luasrc / jsonrpc.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 module("luci.jsonrpc", package.seeall)
6 require "luci.json"
7
8 function resolve(mod, method)
9         local path = luci.util.split(method, ".")
10         
11         for j=1, #path-1 do
12                 if not type(mod) == "table" then
13                         break
14                 end
15                 mod = rawget(mod, path[j])
16                 if not mod then
17                         break
18                 end
19         end
20         mod = type(mod) == "table" and rawget(mod, path[#path]) or nil
21         if type(mod) == "function" then
22                 return mod
23         end
24 end
25
26 function handle(tbl, rawsource, ...)
27         local decoder = luci.json.Decoder()
28         local stat = luci.ltn12.pump.all(rawsource, decoder:sink())
29         local json = decoder:get()
30         local response
31         local success = false
32         
33         if stat then
34                 if type(json.method) == "string"
35                  and (not json.params or type(json.params) == "table") then
36                         local method = resolve(tbl, json.method)
37                         if method then
38                                 response = reply(json.jsonrpc, json.id,
39                                  proxy(method, unpack(json.params or {})))
40                         else
41                                 response = reply(json.jsonrpc, json.id,
42                                  nil, {code=-32601, message="Method not found."})
43                         end
44                 else
45                         response = reply(json.jsonrpc, json.id,
46                          nil, {code=-32600, message="Invalid request."})
47                 end
48         else
49                 response = reply("2.0", nil,
50                  nil, {code=-32700, message="Parse error."})
51         end
52
53         return luci.json.Encoder(response, ...):source()
54 end
55
56 function reply(jsonrpc, id, res, err)
57         require "luci.json"
58         id = id or luci.json.null
59         
60         -- 1.0 compatibility
61         if jsonrpc ~= "2.0" then
62                 jsonrpc = nil
63                 res = res or luci.json.null
64                 err = err or luci.json.null
65         end
66         
67         return {id=id, result=res, error=err, jsonrpc=jsonrpc}
68 end
69
70 function proxy(method, ...)
71         local res = {luci.util.copcall(method, ...)}
72         local stat = table.remove(res, 1)
73         
74         if not stat then
75                 return nil, {code=-32602, message="Invalid params.", data=table.remove(res, 1)} 
76         else
77                 if #res <= 1 then
78                         return res[1] or luci.json.null
79                 else
80                         return res
81                 end
82         end
83 end