61524476ab73d84374769c72056679de7c3ee30c
[project/luci.git] / modules / rpc / luasrc / jsonrpc.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 module("luci.jsonrpc", package.seeall)
17
18 function resolve(mod, method)
19         local path = luci.util.split(value, ".")
20         
21         for j=1, #path-1 do
22                 if not type(mod) == "table" then
23                         break
24                 end
25                 mod = mod[path[j]]
26                 if not mod then
27                         break
28                 end
29         end
30         mod = type(mod) == "table" and mod[path[#path]] or nil
31         if type(mod) == "function" then
32                 return mod
33         end
34 end
35
36 function handle(tbl, rawdata)
37         local stat, json = luci.util.copcall(luci.json.Decode, rawdata)
38         local response
39         local success = false
40         
41         if stat then
42                 if type(json.method) == "string"
43                  and (not json.params or type(json.params) == "table") then
44                         if tbl[json.method] then
45                                 response = reply(json.jsonrpc, json.id,
46                                  proxy(resolve(tbl, json.method), unpack(json.params)))
47                         else
48                                 response = reply(json.jsonrpc, json.id,
49                                  nil, {code=-32601, message="Method not found."})
50                         end
51                 else
52                         response = reply(json.jsonrpc, json.id,
53                          nil, {code=-32600, message="Invalid request."})
54                 end
55         else
56                 response = reply(json.jsonrpc, nil,
57                  nil, {code=-32700, message="Parse error."})
58         end
59
60         return luci.json.Encode(response)
61 end
62
63 function reply(jsonrpc, id, res, err)
64         require "luci.json"
65         id = id or luci.json.Null
66         
67         -- 1.0 compatibility
68         if jsonrpc ~= "2.0" then
69                 jsonrpc = nil
70                 res = res or luci.json.Null
71                 err = err or luci.json.Null
72         end
73         
74         return {id=id, result=res, error=err, jsonrpc=jsonrpc}
75 end
76
77 function proxy(method, ...)
78         local res = {luci.util.copcall(method, unpack(params))}
79         local stat = table.remove(res, 1)
80         
81         if not stat then
82                 return nil, {code=-32602, message="Invalid params.", data=table.remove(res, 1)} 
83         else
84                 return (#res <= 1) and res[1] or res
85         end
86 end