From df40e4df5e3485d1d70548b420e02b81aa61e60b Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Tue, 26 Aug 2008 17:50:32 +0000 Subject: libs/json: Completed JSON library modules/rpc: Added experimental JSON-RPC API --- modules/rpc/luasrc/controller/rpc.lua | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'modules/rpc/luasrc/controller/rpc.lua') diff --git a/modules/rpc/luasrc/controller/rpc.lua b/modules/rpc/luasrc/controller/rpc.lua index aa77a8f248..98fafe3f1b 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 -- cgit v1.2.3