X-Git-Url: https://git.archive.openwrt.org/?a=blobdiff_plain;ds=sidebyside;f=modules%2Frpc%2Fluasrc%2Fcontroller%2Frpc.lua;h=2a99075d2bd57dd2b89d2a11e06b068795e8a609;hb=13579a4e211de5220c653fb97c211d6ef43dfa27;hp=98fafe3f1bb6c6eda2f9b9799e39281c3601ea8d;hpb=df40e4df5e3485d1d70548b420e02b81aa61e60b;p=project%2Fluci.git diff --git a/modules/rpc/luasrc/controller/rpc.lua b/modules/rpc/luasrc/controller/rpc.lua index 98fafe3f1..2a99075d2 100644 --- a/modules/rpc/luasrc/controller/rpc.lua +++ b/modules/rpc/luasrc/controller/rpc.lua @@ -16,6 +16,8 @@ $Id$ local require = require local pairs = pairs local print = print +local pcall = pcall +local table = table module "luci.controller.rpc" @@ -31,19 +33,17 @@ function index() luci.http.status(403, "Forbidden") end - uci = entry({"rpc", "uci"}, call("rpc_uci")) - uci.sysauth = "root" - uci.sysauth_authenticator = authenticator + local rpc = node("rpc") + rpc.sysauth = "root" + rpc.sysauth_authenticator = authenticator + rpc.notemplate = true - fs = entry({"rpc", "fs"}, call("rpc_fs")) - fs.sysauth = "root" - fs.sysauth_authenticator = authenticator - - fs = entry({"rpc", "sys"}, call("rpc_sys")) - fs.sysauth = "root" - fs.sysauth_authenticator = authenticator - - uci = entry({"rpc", "auth"}, call("rpc_auth")) + entry({"rpc", "uci"}, call("rpc_uci")) + entry({"rpc", "uvl"}, call("rpc_uvl")) + entry({"rpc", "fs"}, call("rpc_fs")) + entry({"rpc", "sys"}, call("rpc_sys")) + entry({"rpc", "ipkg"}, call("rpc_ipkg")) + entry({"rpc", "auth"}, call("rpc_auth")).sysauth = false end function rpc_auth() @@ -53,8 +53,6 @@ function rpc_auth() local sys = require "luci.sys" local ltn12 = require "luci.ltn12" - http.setfilehandler() - local loginstat local server = {} @@ -75,6 +73,10 @@ function rpc_auth() end function rpc_uci() + if not pcall(require, "luci.model.uci") then + luci.http.status(404, "Not Found") + return nil + end local uci = require "luci.controller.rpc.uci" local jsonrpc = require "luci.jsonrpc" local http = require "luci.http" @@ -84,32 +86,58 @@ function rpc_uci() ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write) end +function rpc_uvl() + if not pcall(require, "luci.uvl") then + luci.http.status(404, "Not Found") + return nil + end + local uvl = require "luci.controller.rpc.uvl" + local jsonrpc = require "luci.jsonrpc" + local http = require "luci.http" + local ltn12 = require "luci.ltn12" + + http.prepare_content("application/json") + ltn12.pump.all(jsonrpc.handle(uvl, http.source()), http.write) +end + function rpc_fs() local util = require "luci.util" - local fs = util.clone(require "luci.fs") + local io = require "io" + local fs2 = 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 + + function fs2.readfile(filename) + local stat, mime = pcall(require, "mime") + if not stat then error("Base64 support not available. Please install LuaSocket.") end - - return ltn12.source.chain(ltn12.source.file(filename), mime.encode("base64")) + + local fp = io.open(filename) + if not fp then + return nil + end + + local output = {} + local sink = ltn12.sink.table(output) + local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64")) + return ltn12.pump.all(source, sink) and table.concat(output) end - function fs.writefile(filename, data) - if not pcall(require, "mime") then + function fs2.writefile(filename, data) + local stat, mime = pcall(require, "mime") + if not stat 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) + + local file = io.open(filename, "w") + local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file)) + return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false end http.prepare_content("application/json") - ltn12.pump.all(jsonrpc.handle(fs, http.source()), http.write) + ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write) end function rpc_sys() @@ -120,4 +148,18 @@ function rpc_sys() http.prepare_content("application/json") ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write) -end \ No newline at end of file +end + +function rpc_ipkg() + if not pcall(require, "luci.model.ipkg") then + luci.http.status(404, "Not Found") + return nil + end + local ipkg = require "luci.model.ipkg" + local jsonrpc = require "luci.jsonrpc" + local http = require "luci.http" + local ltn12 = require "luci.ltn12" + + http.prepare_content("application/json") + ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write) +end