luci-base: remove old fastindex support code, use cached module tables

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
This commit is contained in:
Jo-Philipp Wich 2015-01-15 16:32:03 +01:00
parent 575e1093ab
commit 7aa838988f

View file

@ -114,13 +114,13 @@ end
-- @param message Custom error message (optional) -- @param message Custom error message (optional)
-- @return false -- @return false
function error404(message) function error404(message)
luci.http.status(404, "Not Found") http.status(404, "Not Found")
message = message or "Not Found" message = message or "Not Found"
require("luci.template") require("luci.template")
if not luci.util.copcall(luci.template.render, "error404") then if not util.copcall(luci.template.render, "error404") then
luci.http.prepare_content("text/plain") http.prepare_content("text/plain")
luci.http.write(message) http.write(message)
end end
return false return false
end end
@ -129,24 +129,24 @@ end
-- @param message Custom error message (optional)# -- @param message Custom error message (optional)#
-- @return false -- @return false
function error500(message) function error500(message)
luci.util.perror(message) util.perror(message)
if not context.template_header_sent then if not context.template_header_sent then
luci.http.status(500, "Internal Server Error") http.status(500, "Internal Server Error")
luci.http.prepare_content("text/plain") http.prepare_content("text/plain")
luci.http.write(message) http.write(message)
else else
require("luci.template") require("luci.template")
if not luci.util.copcall(luci.template.render, "error500", {message=message}) then if not util.copcall(luci.template.render, "error500", {message=message}) then
luci.http.prepare_content("text/plain") http.prepare_content("text/plain")
luci.http.write(message) http.write(message)
end end
end end
return false return false
end end
function authenticator.htmlauth(validator, accs, default) function authenticator.htmlauth(validator, accs, default)
local user = luci.http.formvalue("luci_username") local user = http.formvalue("luci_username")
local pass = luci.http.formvalue("luci_password") local pass = http.formvalue("luci_password")
if user and validator(user, pass) then if user and validator(user, pass) then
return user return user
@ -163,7 +163,7 @@ end
--- Dispatch an HTTP request. --- Dispatch an HTTP request.
-- @param request LuCI HTTP Request object -- @param request LuCI HTTP Request object
function httpdispatch(request, prefix) function httpdispatch(request, prefix)
luci.http.context.request = request http.context.request = request
local r = {} local r = {}
context.request = r context.request = r
@ -195,7 +195,7 @@ function httpdispatch(request, prefix)
dispatch(context.request) dispatch(context.request)
end, error500) end, error500)
luci.http.close() http.close()
--context._disable_memtrace() --context._disable_memtrace()
end end
@ -290,7 +290,7 @@ function dispatch(request)
local scope = (type(env.self) == "table") and env.self local scope = (type(env.self) == "table") and env.self
return string.format( return string.format(
' %s="%s"', tostring(key), ' %s="%s"', tostring(key),
luci.util.pcdata(tostring( val util.pcdata(tostring( val
or (type(env[key]) ~= "function" and env[key]) or (type(env[key]) ~= "function" and env[key])
or (scope and type(scope[key]) ~= "function" and scope[key]) or (scope and type(scope[key]) ~= "function" and scope[key])
or "" )) or "" ))
@ -301,7 +301,7 @@ function dispatch(request)
end end
tpl.context.viewns = setmetatable({ tpl.context.viewns = setmetatable({
write = luci.http.write; write = http.write;
include = function(name) tpl.Template(name):render(getfenv(2)) end; include = function(name) tpl.Template(name):render(getfenv(2)) end;
translate = i18n.translate; translate = i18n.translate;
translatef = i18n.translatef; translatef = i18n.translatef;
@ -342,7 +342,7 @@ function dispatch(request)
local sess = ctx.authsession local sess = ctx.authsession
local verifytoken = false local verifytoken = false
if not sess then if not sess then
sess = luci.http.getcookie("sysauth") sess = http.getcookie("sysauth")
sess = sess and sess:match("^[a-f0-9]*$") sess = sess and sess:match("^[a-f0-9]*$")
verifytoken = true verifytoken = true
end end
@ -387,13 +387,13 @@ function dispatch(request)
end end
if sess then if sess then
luci.http.header("Set-Cookie", "sysauth=" .. sess.."; path="..build_url()) http.header("Set-Cookie", "sysauth=" .. sess.."; path="..build_url())
ctx.authsession = sess ctx.authsession = sess
ctx.authuser = user ctx.authuser = user
end end
end end
else else
luci.http.status(403, "Forbidden") http.status(403, "Forbidden")
return return
end end
else else
@ -469,46 +469,18 @@ function dispatch(request)
end end
end end
--- Generate the dispatching index using the best possible strategy.
function createindex()
local path = luci.util.libpath() .. "/controller/"
local suff = { ".lua", ".lua.gz" }
if luci.util.copcall(require, "luci.fastindex") then
createindex_fastindex(path, suff)
else
createindex_plain(path, suff)
end
end
--- Generate the dispatching index using the fastindex C-indexer.
-- @param path Controller base directory
-- @param suffixes Controller file suffixes
function createindex_fastindex(path, suffixes)
index = {}
if not fi then
fi = luci.fastindex.new("index")
for _, suffix in ipairs(suffixes) do
fi.add(path .. "*" .. suffix)
fi.add(path .. "*/*" .. suffix)
end
end
fi.scan()
for k, v in pairs(fi.indexes) do
index[v[2]] = v[1]
end
end
--- Generate the dispatching index using the native file-cache based strategy. --- Generate the dispatching index using the native file-cache based strategy.
-- @param path Controller base directory function createindex()
-- @param suffixes Controller file suffixes
function createindex_plain(path, suffixes)
local controllers = { } local controllers = { }
for _, suffix in ipairs(suffixes) do local base = "%s/controller/" % util.libpath()
nixio.util.consume((fs.glob(path .. "*" .. suffix)), controllers) local _, path
nixio.util.consume((fs.glob(path .. "*/*" .. suffix)), controllers)
for path in (fs.glob("%s*.lua" % base) or function() end) do
controllers[#controllers+1] = path
end
for path in (fs.glob("%s*/*.lua" % base) or function() end) do
controllers[#controllers+1] = path
end end
if indexcache then if indexcache then
@ -520,7 +492,7 @@ function createindex_plain(path, suffixes)
realdate = (omtime and omtime > realdate) and omtime or realdate realdate = (omtime and omtime > realdate) and omtime or realdate
end end
if cachedate > realdate then if cachedate > realdate and sys.process.info("uid") == 0 then
assert( assert(
sys.process.info("uid") == fs.stat(indexcache, "uid") sys.process.info("uid") == fs.stat(indexcache, "uid")
and fs.stat(indexcache, "modestr") == "rw-------", and fs.stat(indexcache, "modestr") == "rw-------",
@ -535,23 +507,19 @@ function createindex_plain(path, suffixes)
index = {} index = {}
for i,c in ipairs(controllers) do for _, path in ipairs(controllers) do
local modname = "luci.controller." .. c:sub(#path+1, #c):gsub("/", ".") local modname = "luci.controller." .. path:sub(#base+1, #path-4):gsub("/", ".")
for _, suffix in ipairs(suffixes) do
modname = modname:gsub(suffix.."$", "")
end
local mod = require(modname) local mod = require(modname)
assert(mod ~= true, assert(mod ~= true,
"Invalid controller file found\n" .. "Invalid controller file found\n" ..
"The file '" .. c .. "' contains an invalid module line.\n" .. "The file '" .. path .. "' contains an invalid module line.\n" ..
"Please verify whether the module name is set to '" .. modname .. "Please verify whether the module name is set to '" .. modname ..
"' - It must correspond to the file path!") "' - It must correspond to the file path!")
local idx = mod.index local idx = mod.index
assert(type(idx) == "function", assert(type(idx) == "function",
"Invalid controller file found\n" .. "Invalid controller file found\n" ..
"The file '" .. c .. "' contains no index() function.\n" .. "The file '" .. path .. "' contains no index() function.\n" ..
"Please make sure that the controller contains a valid " .. "Please make sure that the controller contains a valid " ..
"index function and verify the spelling!") "index function and verify the spelling!")