diff options
author | Steven Barth <steven@midlink.org> | 2008-08-26 17:50:32 +0000 |
---|---|---|
committer | Steven Barth <steven@midlink.org> | 2008-08-26 17:50:32 +0000 |
commit | df40e4df5e3485d1d70548b420e02b81aa61e60b (patch) | |
tree | 9cb095e2733430076ac1611787e6058c45901b29 /modules/rpc | |
parent | 05b30bba2adf9974378fc484273b709c55ebc011 (diff) |
libs/json: Completed JSON library
modules/rpc: Added experimental JSON-RPC API
Diffstat (limited to 'modules/rpc')
-rw-r--r-- | modules/rpc/luasrc/controller/rpc.lua | 35 | ||||
-rw-r--r-- | modules/rpc/luasrc/jsonrpc.lua | 18 |
2 files changed, 37 insertions, 16 deletions
diff --git a/modules/rpc/luasrc/controller/rpc.lua b/modules/rpc/luasrc/controller/rpc.lua index aa77a8f24..98fafe3f1 100644 --- a/modules/rpc/luasrc/controller/rpc.lua +++ b/modules/rpc/luasrc/controller/rpc.lua @@ -51,6 +51,7 @@ function rpc_auth() local sauth = require "luci.sauth" local http = require "luci.http" local sys = require "luci.sys" + local ltn12 = require "luci.ltn12" http.setfilehandler() @@ -70,35 +71,53 @@ function rpc_auth() end http.prepare_content("application/json") - http.write(jsonrpc.handle(server, http.content())) + ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write) end function rpc_uci() local uci = require "luci.controller.rpc.uci" local jsonrpc = require "luci.jsonrpc" local http = require "luci.http" + local ltn12 = require "luci.ltn12" - http.setfilehandler() http.prepare_content("application/json") - http.write(jsonrpc.handle(uci, http.content())) + ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write) end function rpc_fs() - local fs = require "luci.fs" + local util = require "luci.util" + local fs = util.clone(require "luci.fs") local jsonrpc = require "luci.jsonrpc" local http = require "luci.http" + local ltn12 = require "luci.ltn12" + + function fs.readfile(filename) + if not pcall(require, "mime") then + error("Base64 support not available. Please install LuaSocket.") + end + + return ltn12.source.chain(ltn12.source.file(filename), mime.encode("base64")) + end + + function fs.writefile(filename, data) + if not pcall(require, "mime") then + error("Base64 support not available. Please install LuaSocket.") + end + + local sink = ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(filename)) + return ltn12.pump.all(ltn12.source.string(data), sink) + end - http.setfilehandler() http.prepare_content("application/json") - http.write(jsonrpc.handle(fs, http.content())) + ltn12.pump.all(jsonrpc.handle(fs, http.source()), http.write) end function rpc_sys() local sys = require "luci.sys" local jsonrpc = require "luci.jsonrpc" local http = require "luci.http" + local ltn12 = require "luci.ltn12" - http.setfilehandler() http.prepare_content("application/json") - http.write(jsonrpc.handle(sys, http.content())) + ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write) end
\ No newline at end of file diff --git a/modules/rpc/luasrc/jsonrpc.lua b/modules/rpc/luasrc/jsonrpc.lua index c4fed2acc..1c0db8bce 100644 --- a/modules/rpc/luasrc/jsonrpc.lua +++ b/modules/rpc/luasrc/jsonrpc.lua @@ -34,8 +34,10 @@ function resolve(mod, method) 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 @@ -54,22 +56,22 @@ function handle(tbl, rawdata) 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 - return luci.json.Encode(response) + return luci.json.Encoder(response, ...):source() 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 - 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} @@ -83,7 +85,7 @@ function proxy(method, ...) 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 |