2013-06-30 19:06:55 +02:00
|
|
|
local l = require("logger")
|
2013-07-08 13:34:27 +02:00
|
|
|
local RequestClass = require("rest.request")
|
|
|
|
local ResponseClass = require("rest.response")
|
2013-04-04 10:18:08 +02:00
|
|
|
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
local DEBUG_PCALLS = false
|
2013-04-04 10:18:08 +02:00
|
|
|
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
local postData = nil
|
|
|
|
local resp = ResponseClass.new()
|
2013-04-04 10:18:08 +02:00
|
|
|
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
local function setupAutoWifiMode()
|
|
|
|
io.write("--TODO: join known network if present, fall back to access point otherwise\n")
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
local function init()
|
|
|
|
l:init(l.LEVEL.debug)
|
|
|
|
l:setStream(io.stderr)
|
|
|
|
|
|
|
|
if DEBUG_PCALLS then l:info("Wifibox CGI handler started (pcall debugging enabled)")
|
|
|
|
else l:info("Wifibox CGI handler started")
|
|
|
|
end
|
|
|
|
|
|
|
|
if (os.getenv("REQUEST_METHOD") == "POST") then
|
|
|
|
local n = tonumber(os.getenv("CONTENT_LENGTH"))
|
|
|
|
postData = io.read(n)
|
|
|
|
end
|
|
|
|
end
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
--usually returns function+nil, function+number in case of number in place of function name; or
|
|
|
|
--nil+string if given arguments could not be resolved
|
|
|
|
local function resolveApiFunction(mod, func)
|
|
|
|
if mod == nil then return nil, ("missing module name in CGI call") end
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
local ok, mObj
|
|
|
|
local reqModPath = "rest.api.api_" .. mod
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
if DEBUG_PCALLS then ok, mObj = true, require(reqModPath)
|
|
|
|
else ok, mObj = pcall(require, reqModPath)
|
|
|
|
end
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
if ok == false then return nil, ("API module '" .. mod .. "' does not exist") end
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
if mObj == nil then return nil, ("API module '" .. mod .. "' could not be found") end
|
2013-04-05 16:22:19 +02:00
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
if mObj.isApi ~= true then return nil, ("module '" .. mod .. "' is not part of the CGI API") end
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
if (func == nil or func == '') then func = "_global" end --treat empty function name as nil
|
|
|
|
local f = mObj[func]
|
|
|
|
|
|
|
|
if (type(f) ~= "function") then
|
|
|
|
if tonumber(func) ~= nil then
|
|
|
|
return mObj["_global"], tonumber(func)
|
2013-04-04 10:18:08 +02:00
|
|
|
else
|
2013-07-08 13:34:27 +02:00
|
|
|
return nil, ("function '" .. func .. "' does not exist in API module '" .. mod .. "'")
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return f
|
|
|
|
end
|
|
|
|
|
|
|
|
local function main()
|
|
|
|
local rq = RequestClass.new(postData, DEBUG_PCALLS) -- initializes itself using various environment variables and the arg array
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
l:info("received request of type " .. rq:getRequestMethod() .. " with arguments: " .. l:dump(rq:getAll()))
|
|
|
|
if rq:getRequestMethod() ~= "CMDLINE" then
|
|
|
|
l:info("remote IP/port: " .. rq:getRemoteHost() .. "/" .. rq:getRemotePort())
|
|
|
|
l:debug("user agent: " .. rq:getUserAgent())
|
|
|
|
end
|
|
|
|
|
|
|
|
if (not DEBUG_PCALLS and rq:getRequestMethod() == "CMDLINE") then
|
|
|
|
if rq:get("autowifi") ~= nil then
|
|
|
|
setupAutoWifiMode()
|
|
|
|
else
|
|
|
|
l:info("Nothing to do...bye.\n")
|
|
|
|
end
|
2013-04-04 10:18:08 +02:00
|
|
|
|
|
|
|
else
|
2013-07-08 13:34:27 +02:00
|
|
|
io.write ("Content-type: text/plain\r\n\r\n")
|
|
|
|
|
|
|
|
local mod = rq:getApiModule()
|
|
|
|
local func = rq:getApiFunction()
|
|
|
|
|
|
|
|
local sf,sr = resolveApiFunction(mod, func)
|
|
|
|
if (sf ~= nil) then
|
|
|
|
if (sr ~= nil) then
|
|
|
|
rq:setBlankArgument(sr)
|
|
|
|
end
|
|
|
|
|
|
|
|
local ok, r
|
|
|
|
if DEBUG_PCALLS then ok, r = true, sf(rq)
|
|
|
|
else ok, r = pcall(sf, rq)
|
|
|
|
end
|
|
|
|
|
|
|
|
if ok == true then
|
|
|
|
print(r:serializeAsJson())
|
|
|
|
else
|
|
|
|
resp:setError("call to function '" .. mod .. "/" .. sr .. "' failed")
|
|
|
|
print(resp:serializeAsJson())
|
|
|
|
l:error("calling function '" .. func .. "' in API module '" .. mod .. "' somehow failed ('" .. r .. "')")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
resp:setError("function unknown '" .. mod .. "/" .. func .. "'")
|
|
|
|
print(resp:serializeAsJson())
|
|
|
|
l:error("could not resolve requested API function ('" .. sr .. "')")
|
|
|
|
end
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
init()
|
2013-04-04 10:18:08 +02:00
|
|
|
main()
|