RPC part #2
authorSteven Barth <steven@midlink.org>
Fri, 22 Aug 2008 20:04:04 +0000 (20:04 +0000)
committerSteven Barth <steven@midlink.org>
Fri, 22 Aug 2008 20:04:04 +0000 (20:04 +0000)
libs/web/luasrc/dispatcher.lua
modules/rpc/luasrc/controller/rpc.lua
modules/rpc/luasrc/jsonrpc.lua [new file with mode: 0644]

index dc551916a734de18373004ec59eaa53c1cefe9db..2de5ea4b14eb3af528d99f11a473baf1253bc1cc 100644 (file)
@@ -171,7 +171,9 @@ function dispatch(request)
        
        if track.sysauth then
                require("luci.sauth")
-               local authen = authenticator[track.sysauth_authenticator]
+               local authen = type(track.sysauth_authenticator) == "function"
+                and track.sysauth_authenticator
+                or authenticator[track.sysauth_authenticator]
                local def  = (type(track.sysauth) == "string") and track.sysauth
                local accs = def and {track.sysauth} or track.sysauth
                local sess = luci.http.getcookie("sysauth")
index 41d4c59f0dec64da679517f2a04d9e4304ea7dd5..dd00f63c370de26ac6112b614ee50a8504fd7eb9 100644 (file)
@@ -14,18 +14,31 @@ $Id$
 ]]--
 module("luci.controller.rpc", package.seeall)
 
-function index()       
-       entry({"rpc", "uci"}, call("rpc_uci")).sysauth = "root"
-       entry({"rpc", "auth"}, call("rpc_auth"))
+function index()
+       local authenticator = function(validator)
+               require "luci.jsonrpc"
+               require "luci.http"
+               luci.http.setfilehandler()
+               
+               local loginstat
+               
+               local server = {}
+               server.login = function(...)
+                       loginstat = validator(...)
+                       return loginstat
+               end
+               
+               luci.http.prepare_content("application/json")
+               luci.http.write(luci.jsonrpc.handle(server, luci.http.content()))
+               
+               return loginstat
+       end
+       
+       uci = entry({"rpc", "uci"}, call("rpc_uci"))
+       uci.sysauth = "root"
+       uci.sysauth_authenticator = authenticator
 end
 
-function rpc_proxy(tbl, jsonrpc, method, params, id)
-       local res = {luci.util.copcall(tbl[function], ...)}
-       local stat = table.remove(res, 1)
-       
-       if not stat then
-               return nil, {code=-32602, message="Invalid params.", data=table.remove(res, 1)} 
-       else
-               return res, nil
-       end
+function rpc_uci()
+       luci.http.write("HELLO THAR!")
 end
\ No newline at end of file
diff --git a/modules/rpc/luasrc/jsonrpc.lua b/modules/rpc/luasrc/jsonrpc.lua
new file mode 100644 (file)
index 0000000..6152447
--- /dev/null
@@ -0,0 +1,86 @@
+--[[
+LuCI - Lua Configuration Interface
+
+Copyright 2008 Steven Barth <steven@midlink.org>
+Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+$Id$
+]]--
+
+module("luci.jsonrpc", package.seeall)
+
+function resolve(mod, method)
+       local path = luci.util.split(value, ".")
+       
+       for j=1, #path-1 do
+               if not type(mod) == "table" then
+                       break
+               end
+               mod = mod[path[j]]
+               if not mod then
+                       break
+               end
+       end
+       mod = type(mod) == "table" and mod[path[#path]] or nil
+       if type(mod) == "function" then
+               return mod
+       end
+end
+
+function handle(tbl, rawdata)
+       local stat, json = luci.util.copcall(luci.json.Decode, rawdata)
+       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
+                               response = reply(json.jsonrpc, json.id,
+                                proxy(resolve(tbl, json.method), unpack(json.params)))
+                       else
+                               response = reply(json.jsonrpc, json.id,
+                                nil, {code=-32601, message="Method not found."})
+                       end
+               else
+                       response = reply(json.jsonrpc, json.id,
+                        nil, {code=-32600, message="Invalid request."})
+               end
+       else
+               response = reply(json.jsonrpc, nil,
+                nil, {code=-32700, message="Parse error."})
+       end
+
+       return luci.json.Encode(response)
+end
+
+function reply(jsonrpc, id, res, err)
+       require "luci.json"
+       id = id or luci.json.Null
+       
+       -- 1.0 compatibility
+       if jsonrpc ~= "2.0" then
+               jsonrpc = nil
+               res = res or luci.json.Null
+               err = err or luci.json.Null
+       end
+       
+       return {id=id, result=res, error=err, jsonrpc=jsonrpc}
+end
+
+function proxy(method, ...)
+       local res = {luci.util.copcall(method, unpack(params))}
+       local stat = table.remove(res, 1)
+       
+       if not stat then
+               return nil, {code=-32602, message="Invalid params.", data=table.remove(res, 1)} 
+       else
+               return (#res <= 1) and res[1] or res
+       end
+end
\ No newline at end of file