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-07-08 16:53:45 +02:00
|
|
|
local wifi = require("network.wlanconfig")
|
2013-07-08 19:02:20 +02:00
|
|
|
local netconf = require("network.netconfig")
|
2013-07-10 00:32:43 +02:00
|
|
|
local config = require("config")
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
local postData = nil
|
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)
|
|
|
|
|
2013-07-10 00:32:43 +02:00
|
|
|
if config.DEBUG_PCALLS then l:info("Wifibox CGI handler started (pcall debugging enabled)")
|
2013-07-08 13:34:27 +02:00
|
|
|
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
|
2013-07-08 16:53:45 +02:00
|
|
|
|
2013-07-08 19:02:20 +02:00
|
|
|
local s, msg
|
|
|
|
s, msg = wifi.init()
|
|
|
|
if not s then return s, msg end
|
|
|
|
|
|
|
|
s, msg = netconf.init(wifi, true)
|
|
|
|
if not s then return s, msg end
|
|
|
|
|
|
|
|
return true
|
2013-07-08 13:34:27 +02:00
|
|
|
end
|
2013-04-04 10:18:08 +02:00
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
local function main()
|
2013-07-10 00:32:43 +02:00
|
|
|
local rq = RequestClass.new(postData, config.DEBUG_PCALLS)
|
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
|
|
|
|
|
2013-07-10 00:32:43 +02:00
|
|
|
if (not config.DEBUG_PCALLS and rq:getRequestMethod() == "CMDLINE") then
|
2013-07-08 13:34:27 +02:00
|
|
|
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")
|
2013-07-09 01:49:56 +02:00
|
|
|
local response, err = rq:handle()
|
2013-07-08 13:34:27 +02:00
|
|
|
|
2013-07-09 01:49:56 +02:00
|
|
|
if err ~= nil then l:error(err) end
|
|
|
|
response:send()
|
2013-04-04 10:18:08 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-10 00:32:43 +02:00
|
|
|
---'entry point'---
|
2013-07-08 19:02:20 +02:00
|
|
|
local s, msg = init()
|
|
|
|
if s == false then
|
2013-07-08 16:53:45 +02:00
|
|
|
local resp = ResponseClass.new()
|
2013-07-10 00:32:43 +02:00
|
|
|
local errSuffix = msg and " (" .. msg .. ")" or ""
|
|
|
|
|
|
|
|
resp:setError("initialization failed" .. errSuffix)
|
|
|
|
io.write ("Content-type: text/plain\r\n\r\n")
|
|
|
|
resp:send()
|
|
|
|
l:error("initialization failed" .. errSuffix) --NOTE: this assumes the logger has been inited properly, despite init() having failed
|
|
|
|
|
2013-07-08 16:53:45 +02:00
|
|
|
os.exit(1)
|
|
|
|
else
|
|
|
|
main()
|
|
|
|
os.exit(0)
|
|
|
|
end
|