2008-03-02 21:52:58 +00:00
|
|
|
--[[
|
2008-05-25 17:00:30 +00:00
|
|
|
LuCI - Dispatcher
|
2008-03-02 21:52:58 +00:00
|
|
|
|
|
|
|
Description:
|
|
|
|
The request dispatcher and module dispatcher generators
|
|
|
|
|
|
|
|
FileId:
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
License:
|
|
|
|
Copyright 2008 Steven Barth <steven@midlink.org>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
2008-05-24 22:58:45 +00:00
|
|
|
You may obtain a copy of the License at
|
2008-03-02 21:52:58 +00:00
|
|
|
|
2008-05-24 22:58:45 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2008-03-02 21:52:58 +00:00
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
]]--
|
2008-07-29 20:32:02 +00:00
|
|
|
|
|
|
|
--- LuCI web dispatcher.
|
2008-08-29 23:26:01 +00:00
|
|
|
local fs = require "luci.fs"
|
|
|
|
local sys = require "luci.sys"
|
|
|
|
local init = require "luci.init"
|
|
|
|
local util = require "luci.util"
|
|
|
|
local http = require "luci.http"
|
2008-03-02 21:52:58 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
module("luci.dispatcher", package.seeall)
|
2008-06-14 14:12:12 +00:00
|
|
|
context = luci.util.threadlocal()
|
2008-03-29 18:22:21 +00:00
|
|
|
|
2008-08-10 12:58:05 +00:00
|
|
|
authenticator = {}
|
|
|
|
|
2008-05-26 12:16:16 +00:00
|
|
|
-- Index table
|
2008-06-14 14:12:12 +00:00
|
|
|
local index = nil
|
2008-05-26 12:16:16 +00:00
|
|
|
|
2008-06-02 15:36:13 +00:00
|
|
|
-- Fastindex
|
|
|
|
local fi
|
|
|
|
|
2008-05-05 19:27:30 +00:00
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Build the URL relative to the server webroot from given virtual path.
|
|
|
|
-- @param ... Virtual path
|
|
|
|
-- @return Relative URL
|
2008-05-29 13:51:32 +00:00
|
|
|
function build_url(...)
|
2008-06-25 14:58:18 +00:00
|
|
|
return luci.http.getenv("SCRIPT_NAME") .. "/" .. table.concat(arg, "/")
|
2008-03-02 21:52:58 +00:00
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Send a 404 error code and render the "error404" template if available.
|
|
|
|
-- @param message Custom error message (optional)
|
|
|
|
-- @return false
|
2008-03-02 21:52:58 +00:00
|
|
|
function error404(message)
|
2008-05-25 17:00:30 +00:00
|
|
|
luci.http.status(404, "Not Found")
|
2008-03-02 21:52:58 +00:00
|
|
|
message = message or "Not Found"
|
2008-05-24 22:58:45 +00:00
|
|
|
|
2008-05-25 17:00:30 +00:00
|
|
|
require("luci.template")
|
2008-06-14 14:12:12 +00:00
|
|
|
if not luci.util.copcall(luci.template.render, "error404") then
|
2008-05-25 17:00:30 +00:00
|
|
|
luci.http.prepare_content("text/plain")
|
2008-06-14 14:12:12 +00:00
|
|
|
luci.http.write(message)
|
2008-03-02 21:52:58 +00:00
|
|
|
end
|
2008-05-24 22:58:45 +00:00
|
|
|
return false
|
2008-03-02 21:52:58 +00:00
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Send a 500 error code and render the "error500" template if available.
|
|
|
|
-- @param message Custom error message (optional)#
|
|
|
|
-- @return false
|
2008-03-02 21:52:58 +00:00
|
|
|
function error500(message)
|
2008-05-25 17:00:30 +00:00
|
|
|
luci.http.status(500, "Internal Server Error")
|
2008-05-24 22:58:45 +00:00
|
|
|
|
2008-05-25 17:00:30 +00:00
|
|
|
require("luci.template")
|
2008-06-14 14:12:12 +00:00
|
|
|
if not luci.util.copcall(luci.template.render, "error500", {message=message}) then
|
2008-05-25 17:00:30 +00:00
|
|
|
luci.http.prepare_content("text/plain")
|
2008-06-14 14:12:12 +00:00
|
|
|
luci.http.write(message)
|
2008-03-02 21:52:58 +00:00
|
|
|
end
|
2008-05-24 22:58:45 +00:00
|
|
|
return false
|
2008-03-02 21:52:58 +00:00
|
|
|
end
|
|
|
|
|
2008-08-22 22:13:54 +00:00
|
|
|
function authenticator.htmlauth(validator, accs, default)
|
2008-06-28 16:03:54 +00:00
|
|
|
local user = luci.http.formvalue("username")
|
|
|
|
local pass = luci.http.formvalue("password")
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-10 12:58:05 +00:00
|
|
|
if user and validator(user, pass) then
|
|
|
|
return user
|
2008-06-28 16:03:54 +00:00
|
|
|
end
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-10 12:58:05 +00:00
|
|
|
require("luci.i18n")
|
|
|
|
require("luci.template")
|
|
|
|
context.path = {}
|
|
|
|
luci.template.render("sysauth", {duser=default, fuser=user})
|
|
|
|
return false
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-06-28 16:03:54 +00:00
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Dispatch an HTTP request.
|
|
|
|
-- @param request LuCI HTTP Request object
|
2008-06-14 14:12:12 +00:00
|
|
|
function httpdispatch(request)
|
|
|
|
luci.http.context.request = request
|
|
|
|
context.request = {}
|
2008-06-15 17:45:10 +00:00
|
|
|
local pathinfo = request:getenv("PATH_INFO") or ""
|
2008-05-24 22:58:45 +00:00
|
|
|
|
2008-06-06 15:50:21 +00:00
|
|
|
for node in pathinfo:gmatch("[^/]+") do
|
2008-06-14 14:12:12 +00:00
|
|
|
table.insert(context.request, node)
|
2008-03-02 21:52:58 +00:00
|
|
|
end
|
2008-05-24 22:58:45 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
local stat, err = util.copcall(dispatch, context.request)
|
|
|
|
if not stat then
|
2008-10-31 20:57:07 +00:00
|
|
|
luci.util.perror(err)
|
2008-08-29 23:26:01 +00:00
|
|
|
error500(err)
|
|
|
|
end
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-06-14 14:12:12 +00:00
|
|
|
luci.http.close()
|
2008-09-05 20:32:20 +00:00
|
|
|
|
|
|
|
--context._disable_memtrace()
|
2008-05-04 20:53:31 +00:00
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Dispatches a LuCI virtual path.
|
|
|
|
-- @param request Virtual path
|
2008-06-14 14:12:12 +00:00
|
|
|
function dispatch(request)
|
2008-09-05 20:32:20 +00:00
|
|
|
--context._disable_memtrace = require "luci.debug".trap_memtrace()
|
2008-08-29 23:26:01 +00:00
|
|
|
local ctx = context
|
|
|
|
ctx.path = request
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
require "luci.i18n".setlanguage(require "luci.config".main.lang)
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
local c = ctx.tree
|
|
|
|
local stat
|
|
|
|
if not c then
|
|
|
|
c = createtree()
|
2008-05-26 12:16:16 +00:00
|
|
|
end
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-05-22 14:04:03 +00:00
|
|
|
local track = {}
|
2008-08-07 19:03:25 +00:00
|
|
|
local args = {}
|
2008-11-01 18:49:41 +00:00
|
|
|
ctx.args = args
|
|
|
|
ctx.requestargs = ctx.requestargs or args
|
2008-08-07 19:03:25 +00:00
|
|
|
local n
|
2008-05-24 22:58:45 +00:00
|
|
|
|
2008-05-22 14:04:03 +00:00
|
|
|
for i, s in ipairs(request) do
|
|
|
|
c = c.nodes[s]
|
2008-08-07 19:03:25 +00:00
|
|
|
n = i
|
2008-08-22 21:52:36 +00:00
|
|
|
if not c then
|
2008-05-22 14:04:03 +00:00
|
|
|
break
|
2008-05-24 22:58:45 +00:00
|
|
|
end
|
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
util.update(track, c)
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-22 21:52:36 +00:00
|
|
|
if c.leaf then
|
|
|
|
break
|
|
|
|
end
|
2008-05-04 20:53:31 +00:00
|
|
|
end
|
2008-05-24 22:58:45 +00:00
|
|
|
|
2008-08-07 19:03:25 +00:00
|
|
|
if c and c.leaf then
|
|
|
|
for j=n+1, #request do
|
|
|
|
table.insert(args, request[j])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-22 14:04:03 +00:00
|
|
|
if track.i18n then
|
2008-05-25 17:00:30 +00:00
|
|
|
require("luci.i18n").loadc(track.i18n)
|
2008-05-04 20:53:31 +00:00
|
|
|
end
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-05-26 09:45:12 +00:00
|
|
|
-- Init template engine
|
2008-10-10 14:37:53 +00:00
|
|
|
if (c and c.index) or not track.notemplate then
|
2008-08-29 23:26:01 +00:00
|
|
|
local tpl = require("luci.template")
|
2008-10-19 20:49:10 +00:00
|
|
|
local media = track.mediaurlbase or luci.config.main.mediaurlbase
|
2008-09-29 15:38:13 +00:00
|
|
|
if not pcall(tpl.Template, "themes/%s/header" % fs.basename(media)) then
|
|
|
|
media = nil
|
|
|
|
for name, theme in pairs(luci.config.themes) do
|
|
|
|
if name:sub(1,1) ~= "." and pcall(tpl.Template,
|
|
|
|
"themes/%s/header" % fs.basename(theme)) then
|
|
|
|
media = theme
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert(media, "No valid theme found")
|
|
|
|
end
|
|
|
|
|
2008-09-05 18:35:09 +00:00
|
|
|
local viewns = setmetatable({}, {__index=_G})
|
2008-08-29 23:26:01 +00:00
|
|
|
tpl.context.viewns = viewns
|
|
|
|
viewns.write = luci.http.write
|
2008-09-05 18:35:09 +00:00
|
|
|
viewns.include = function(name) tpl.Template(name):render(getfenv(2)) end
|
2008-08-29 23:26:01 +00:00
|
|
|
viewns.translate = function(...) return require("luci.i18n").translate(...) end
|
|
|
|
viewns.striptags = util.striptags
|
|
|
|
viewns.controller = luci.http.getenv("SCRIPT_NAME")
|
2008-09-29 15:38:13 +00:00
|
|
|
viewns.media = media
|
2008-10-10 14:37:53 +00:00
|
|
|
viewns.theme = fs.basename(media)
|
2008-08-29 23:26:01 +00:00
|
|
|
viewns.resource = luci.config.main.resourcebase
|
|
|
|
viewns.REQUEST_URI = (luci.http.getenv("SCRIPT_NAME") or "") .. (luci.http.getenv("PATH_INFO") or "")
|
2008-07-17 16:02:29 +00:00
|
|
|
end
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-09-05 14:34:59 +00:00
|
|
|
track.dependent = (track.dependent ~= false)
|
2008-08-29 23:26:01 +00:00
|
|
|
assert(not track.dependent or not track.auto, "Access Violation")
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-06-28 16:03:54 +00:00
|
|
|
if track.sysauth then
|
2008-08-29 23:26:01 +00:00
|
|
|
local sauth = require "luci.sauth"
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-22 20:04:04 +00:00
|
|
|
local authen = type(track.sysauth_authenticator) == "function"
|
|
|
|
and track.sysauth_authenticator
|
|
|
|
or authenticator[track.sysauth_authenticator]
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-06-28 16:03:54 +00:00
|
|
|
local def = (type(track.sysauth) == "string") and track.sysauth
|
|
|
|
local accs = def and {track.sysauth} or track.sysauth
|
2008-09-05 14:28:36 +00:00
|
|
|
local sess = ctx.authsession or luci.http.getcookie("sysauth")
|
2008-08-11 12:53:41 +00:00
|
|
|
sess = sess and sess:match("^[A-F0-9]+$")
|
2008-08-29 23:26:01 +00:00
|
|
|
local user = sauth.read(sess)
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
if not util.contains(accs, user) then
|
2008-08-10 12:58:05 +00:00
|
|
|
if authen then
|
2008-08-26 00:53:28 +00:00
|
|
|
local user, sess = authen(luci.sys.user.checkpasswd, accs, def)
|
2008-08-29 23:26:01 +00:00
|
|
|
if not user or not util.contains(accs, user) then
|
2008-08-10 12:58:05 +00:00
|
|
|
return
|
|
|
|
else
|
2008-08-26 00:53:28 +00:00
|
|
|
local sid = sess or luci.sys.uniqueid(16)
|
2008-08-10 12:58:05 +00:00
|
|
|
luci.http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
|
2008-08-26 00:53:28 +00:00
|
|
|
if not sess then
|
2008-08-29 23:26:01 +00:00
|
|
|
sauth.write(sid, user)
|
2008-08-26 00:53:28 +00:00
|
|
|
end
|
2008-09-05 14:28:36 +00:00
|
|
|
ctx.authsession = sid
|
2008-08-10 12:58:05 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
luci.http.status(403, "Forbidden")
|
2008-06-28 16:03:54 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if track.setgroup then
|
|
|
|
luci.sys.process.setgroup(track.setgroup)
|
|
|
|
end
|
|
|
|
|
|
|
|
if track.setuser then
|
|
|
|
luci.sys.process.setuser(track.setuser)
|
|
|
|
end
|
2008-05-24 22:58:45 +00:00
|
|
|
|
2008-10-11 11:30:43 +00:00
|
|
|
if c and (c.index or type(c.target) == "function") then
|
|
|
|
ctx.dispatched = c
|
|
|
|
ctx.requested = ctx.requested or ctx.dispatched
|
|
|
|
end
|
|
|
|
|
2008-10-10 14:37:53 +00:00
|
|
|
if c and c.index then
|
|
|
|
local tpl = require "luci.template"
|
2008-10-11 11:30:43 +00:00
|
|
|
|
|
|
|
if util.copcall(tpl.render, "indexer", {}) then
|
2008-10-10 14:37:53 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-22 14:04:03 +00:00
|
|
|
if c and type(c.target) == "function" then
|
2008-08-29 23:26:01 +00:00
|
|
|
util.copcall(function()
|
2008-09-05 18:35:09 +00:00
|
|
|
local oldenv = getfenv(c.target)
|
|
|
|
local module = require(c.module)
|
|
|
|
local env = setmetatable({}, {__index=
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-09-05 18:35:09 +00:00
|
|
|
function(tbl, key)
|
2008-09-23 00:10:51 +00:00
|
|
|
return rawget(tbl, key) or module[key] or oldenv[key]
|
2008-09-05 18:35:09 +00:00
|
|
|
end})
|
|
|
|
|
|
|
|
setfenv(c.target, env)
|
2008-08-29 23:26:01 +00:00
|
|
|
end)
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
c.target(unpack(args))
|
2008-03-21 19:30:53 +00:00
|
|
|
else
|
2008-05-22 14:04:03 +00:00
|
|
|
error404()
|
2008-03-02 21:52:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Generate the dispatching index using the best possible strategy.
|
2008-05-22 14:04:03 +00:00
|
|
|
function createindex()
|
2008-08-06 20:20:40 +00:00
|
|
|
local path = luci.util.libpath() .. "/controller/"
|
2008-06-02 15:36:05 +00:00
|
|
|
local suff = ".lua"
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-06-14 14:12:12 +00:00
|
|
|
if luci.util.copcall(require, "luci.fastindex") then
|
2008-06-02 15:36:05 +00:00
|
|
|
createindex_fastindex(path, suff)
|
|
|
|
else
|
|
|
|
createindex_plain(path, suff)
|
2008-06-02 15:36:13 +00:00
|
|
|
end
|
2008-06-02 15:36:05 +00:00
|
|
|
end
|
2008-05-24 22:58:45 +00:00
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Generate the dispatching index using the fastindex C-indexer.
|
|
|
|
-- @param path Controller base directory
|
|
|
|
-- @param suffix Controller file suffix
|
2008-06-14 14:12:12 +00:00
|
|
|
function createindex_fastindex(path, suffix)
|
|
|
|
index = {}
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-06-02 15:36:13 +00:00
|
|
|
if not fi then
|
|
|
|
fi = luci.fastindex.new("index")
|
|
|
|
fi.add(path .. "*" .. suffix)
|
|
|
|
fi.add(path .. "*/*" .. suffix)
|
|
|
|
end
|
2008-05-26 09:45:12 +00:00
|
|
|
fi.scan()
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-05-26 09:45:12 +00:00
|
|
|
for k, v in pairs(fi.indexes) do
|
2008-05-26 12:16:16 +00:00
|
|
|
index[v[2]] = v[1]
|
2008-05-26 09:45:12 +00:00
|
|
|
end
|
2008-06-02 15:36:05 +00:00
|
|
|
end
|
2008-05-26 09:45:12 +00:00
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Generate the dispatching index using the native file-cache based strategy.
|
|
|
|
-- @param path Controller base directory
|
|
|
|
-- @param suffix Controller file suffix
|
2008-06-02 15:36:05 +00:00
|
|
|
function createindex_plain(path, suffix)
|
2008-08-29 23:26:01 +00:00
|
|
|
if indexcache then
|
|
|
|
local cachedate = fs.mtime(indexcache)
|
|
|
|
if cachedate and cachedate > fs.mtime(path) then
|
2008-09-01 16:05:34 +00:00
|
|
|
|
|
|
|
assert(
|
|
|
|
sys.process.info("uid") == fs.stat(indexcache, "uid")
|
|
|
|
and fs.stat(indexcache, "mode") == "rw-------",
|
|
|
|
"Fatal: Indexcache is not sane!"
|
|
|
|
)
|
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
index = loadfile(indexcache)()
|
|
|
|
return index
|
2008-09-23 00:10:51 +00:00
|
|
|
end
|
2008-08-29 23:26:01 +00:00
|
|
|
end
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-06-14 14:12:12 +00:00
|
|
|
index = {}
|
2008-06-02 15:36:13 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
local controllers = util.combine(
|
2008-06-02 15:36:05 +00:00
|
|
|
luci.fs.glob(path .. "*" .. suffix) or {},
|
|
|
|
luci.fs.glob(path .. "*/*" .. suffix) or {}
|
|
|
|
)
|
|
|
|
|
|
|
|
for i,c in ipairs(controllers) do
|
|
|
|
local module = "luci.controller." .. c:sub(#path+1, #c-#suffix):gsub("/", ".")
|
2008-08-29 23:26:01 +00:00
|
|
|
local mod = require(module)
|
|
|
|
local idx = mod.index
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
if type(idx) == "function" then
|
|
|
|
index[module] = idx
|
2008-06-02 15:36:05 +00:00
|
|
|
end
|
2008-08-29 23:26:01 +00:00
|
|
|
end
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
if indexcache then
|
|
|
|
fs.writefile(indexcache, util.get_bytecode(index))
|
2008-09-01 16:05:34 +00:00
|
|
|
fs.chmod(indexcache, "a-rwx,u+rw")
|
2008-06-02 15:36:05 +00:00
|
|
|
end
|
2008-03-21 19:30:53 +00:00
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Create the dispatching tree from the index.
|
|
|
|
-- Build the index before if it does not exist yet.
|
2008-05-26 12:16:16 +00:00
|
|
|
function createtree()
|
2008-06-14 14:12:12 +00:00
|
|
|
if not index then
|
2008-05-26 12:16:16 +00:00
|
|
|
createindex()
|
|
|
|
end
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
local ctx = context
|
|
|
|
local tree = {nodes={}}
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
ctx.treecache = setmetatable({}, {__mode="v"})
|
|
|
|
ctx.tree = tree
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-05-31 08:04:49 +00:00
|
|
|
-- Load default translation
|
2008-08-29 23:26:01 +00:00
|
|
|
require "luci.i18n".loadc("default")
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-09-05 19:17:48 +00:00
|
|
|
local scope = setmetatable({}, {__index = luci.dispatcher})
|
2008-05-26 12:16:16 +00:00
|
|
|
|
|
|
|
for k, v in pairs(index) do
|
2008-05-31 08:04:49 +00:00
|
|
|
scope._NAME = k
|
|
|
|
setfenv(v, scope)
|
2008-08-29 23:26:01 +00:00
|
|
|
v()
|
2008-05-26 12:16:16 +00:00
|
|
|
end
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
return tree
|
2008-05-26 12:16:16 +00:00
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Clone a node of the dispatching tree to another position.
|
|
|
|
-- @param path Virtual path destination
|
|
|
|
-- @param clone Virtual path source
|
|
|
|
-- @param title Destination node title (optional)
|
|
|
|
-- @param order Destination node order value (optional)
|
|
|
|
-- @return Dispatching tree node
|
2008-06-02 20:16:05 +00:00
|
|
|
function assign(path, clone, title, order)
|
2008-06-29 14:43:06 +00:00
|
|
|
local obj = node(unpack(path))
|
2008-06-02 20:16:05 +00:00
|
|
|
obj.nodes = nil
|
|
|
|
obj.module = nil
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-06-02 20:16:05 +00:00
|
|
|
obj.title = title
|
|
|
|
obj.order = order
|
2008-06-06 15:50:21 +00:00
|
|
|
|
2008-09-15 16:50:55 +00:00
|
|
|
setmetatable(obj, {__index = _create_node(clone)})
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-06-02 20:16:05 +00:00
|
|
|
return obj
|
|
|
|
end
|
2008-05-22 17:21:30 +00:00
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Create a new dispatching node and define common parameters.
|
|
|
|
-- @param path Virtual path
|
2008-09-23 00:10:51 +00:00
|
|
|
-- @param target Target function to call when dispatched.
|
2008-07-29 20:32:02 +00:00
|
|
|
-- @param title Destination node title
|
|
|
|
-- @param order Destination node order value (optional)
|
|
|
|
-- @return Dispatching tree node
|
2008-06-02 20:16:05 +00:00
|
|
|
function entry(path, target, title, order)
|
2008-06-29 14:42:53 +00:00
|
|
|
local c = node(unpack(path))
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-05-22 17:21:30 +00:00
|
|
|
c.target = target
|
|
|
|
c.title = title
|
|
|
|
c.order = order
|
2008-05-27 20:39:48 +00:00
|
|
|
c.module = getfenv(2)._NAME
|
2008-05-24 22:58:45 +00:00
|
|
|
|
2008-05-22 17:21:30 +00:00
|
|
|
return c
|
|
|
|
end
|
2008-05-04 20:53:31 +00:00
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Fetch or create a new dispatching node.
|
|
|
|
-- @param ... Virtual path
|
|
|
|
-- @return Dispatching tree node
|
2008-05-22 14:04:03 +00:00
|
|
|
function node(...)
|
2008-09-15 16:50:55 +00:00
|
|
|
local c = _create_node({...})
|
2008-05-24 22:58:45 +00:00
|
|
|
|
2008-06-06 15:50:21 +00:00
|
|
|
c.module = getfenv(2)._NAME
|
2008-06-04 22:41:58 +00:00
|
|
|
c.path = arg
|
2008-07-17 16:02:29 +00:00
|
|
|
c.auto = nil
|
2008-06-04 22:41:58 +00:00
|
|
|
|
2008-05-22 14:04:03 +00:00
|
|
|
return c
|
|
|
|
end
|
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
function _create_node(path, cache)
|
|
|
|
if #path == 0 then
|
|
|
|
return context.tree
|
|
|
|
end
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
cache = cache or context.treecache
|
|
|
|
local name = table.concat(path, ".")
|
|
|
|
local c = cache[name]
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
if not c then
|
|
|
|
local last = table.remove(path)
|
|
|
|
c = _create_node(path, cache)
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
local new = {nodes={}, auto=true}
|
|
|
|
c.nodes[last] = new
|
|
|
|
cache[name] = new
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-08-29 23:26:01 +00:00
|
|
|
return new
|
|
|
|
else
|
|
|
|
return c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-22 14:04:03 +00:00
|
|
|
-- Subdispatchers --
|
2008-07-29 20:32:02 +00:00
|
|
|
|
|
|
|
--- Create a redirect to another dispatching node.
|
|
|
|
-- @param ... Virtual path destination
|
2008-05-22 14:04:03 +00:00
|
|
|
function alias(...)
|
2008-10-30 19:09:52 +00:00
|
|
|
local req = {...}
|
|
|
|
return function(...)
|
|
|
|
for _, r in ipairs({...}) do
|
|
|
|
req[#req+1] = r
|
|
|
|
end
|
|
|
|
|
2008-06-14 14:12:12 +00:00
|
|
|
dispatch(req)
|
2008-05-04 20:53:31 +00:00
|
|
|
end
|
2008-03-21 19:30:53 +00:00
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Rewrite the first x path values of the request.
|
|
|
|
-- @param n Number of path values to replace
|
|
|
|
-- @param ... Virtual path to replace removed path values with
|
2008-05-29 19:18:49 +00:00
|
|
|
function rewrite(n, ...)
|
2008-10-30 19:09:52 +00:00
|
|
|
local req = {...}
|
|
|
|
return function(...)
|
|
|
|
local dispatched = util.clone(context.dispatched)
|
|
|
|
|
2008-09-23 00:10:51 +00:00
|
|
|
for i=1,n do
|
2008-10-30 19:09:52 +00:00
|
|
|
table.remove(dispatched, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
for i, r in ipairs(req) do
|
|
|
|
table.insert(dispatched, i, r)
|
2008-05-29 19:18:49 +00:00
|
|
|
end
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-10-30 19:09:52 +00:00
|
|
|
for _, r in ipairs({...}) do
|
|
|
|
dispatched[#dispatched+1] = r
|
2008-05-29 19:18:49 +00:00
|
|
|
end
|
2008-09-23 00:10:51 +00:00
|
|
|
|
2008-10-30 19:09:52 +00:00
|
|
|
dispatch(dispatched)
|
2008-05-29 19:18:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Create a function-call dispatching target.
|
2008-09-23 00:10:51 +00:00
|
|
|
-- @param name Target function of local controller
|
2008-07-29 20:32:02 +00:00
|
|
|
-- @param ... Additional parameters passed to the function
|
2008-06-14 14:12:12 +00:00
|
|
|
function call(name, ...)
|
|
|
|
local argv = {...}
|
2008-10-26 18:55:54 +00:00
|
|
|
return function(...)
|
|
|
|
if #argv > 0 then
|
|
|
|
return getfenv()[name](unpack(argv), ...)
|
|
|
|
else
|
|
|
|
return getfenv()[name](...)
|
|
|
|
end
|
|
|
|
end
|
2008-05-27 20:39:48 +00:00
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Create a template render dispatching target.
|
2008-07-29 21:16:12 +00:00
|
|
|
-- @param name Template to be rendered
|
2008-05-22 14:04:03 +00:00
|
|
|
function template(name)
|
2008-08-29 20:36:45 +00:00
|
|
|
return function()
|
|
|
|
require("luci.template")
|
|
|
|
luci.template.render(name)
|
|
|
|
end
|
2008-05-22 14:04:03 +00:00
|
|
|
end
|
2008-05-04 20:53:31 +00:00
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Create a CBI model dispatching target.
|
2008-11-01 17:11:02 +00:00
|
|
|
-- @param model CBI model to be rendered
|
2008-10-31 09:35:11 +00:00
|
|
|
function cbi(model, config)
|
|
|
|
config = config or {}
|
2008-08-07 19:03:25 +00:00
|
|
|
return function(...)
|
2008-08-29 20:36:45 +00:00
|
|
|
require("luci.cbi")
|
|
|
|
require("luci.template")
|
2008-10-20 22:35:11 +00:00
|
|
|
local http = require "luci.http"
|
2008-08-29 20:36:45 +00:00
|
|
|
|
2008-09-05 09:37:02 +00:00
|
|
|
maps = luci.cbi.load(model, ...)
|
2008-05-24 22:58:45 +00:00
|
|
|
|
2008-10-20 22:35:11 +00:00
|
|
|
local state = nil
|
|
|
|
|
2008-07-15 13:17:28 +00:00
|
|
|
for i, res in ipairs(maps) do
|
2008-10-31 09:35:11 +00:00
|
|
|
if config.autoapply then
|
|
|
|
res.autoapply = config.autoapply
|
|
|
|
end
|
2008-10-20 22:35:11 +00:00
|
|
|
local cstate = res:parse()
|
|
|
|
if not state or cstate < state then
|
|
|
|
state = cstate
|
|
|
|
end
|
2008-05-22 14:04:03 +00:00
|
|
|
end
|
2008-05-24 22:58:45 +00:00
|
|
|
|
2008-11-02 13:26:41 +00:00
|
|
|
if config.on_success_to and state and state > 0 then
|
|
|
|
luci.http.redirect(config.on_success_to)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2008-11-01 18:32:02 +00:00
|
|
|
if config.state_handler then
|
|
|
|
if not config.state_handler(state, maps) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-01 17:11:02 +00:00
|
|
|
local pageaction = true
|
2008-10-20 22:35:11 +00:00
|
|
|
http.header("X-CBI-State", state or 0)
|
2008-10-30 19:10:08 +00:00
|
|
|
luci.template.render("cbi/header", {state = state})
|
2008-07-15 13:17:28 +00:00
|
|
|
for i, res in ipairs(maps) do
|
|
|
|
res:render()
|
2008-11-01 17:11:02 +00:00
|
|
|
if res.pageaction == false then
|
|
|
|
pageaction = false
|
|
|
|
end
|
2008-07-15 13:17:28 +00:00
|
|
|
end
|
2008-11-01 17:11:02 +00:00
|
|
|
luci.template.render("cbi/footer", {pageaction=pageaction, state = state, autoapply = config.autoapply})
|
2008-03-02 21:52:58 +00:00
|
|
|
end
|
2008-05-24 22:58:45 +00:00
|
|
|
end
|
2008-08-09 14:14:04 +00:00
|
|
|
|
|
|
|
--- Create a CBI form model dispatching target.
|
|
|
|
-- @param model CBI form model tpo be rendered
|
|
|
|
function form(model)
|
|
|
|
return function(...)
|
2008-08-29 20:36:45 +00:00
|
|
|
require("luci.cbi")
|
|
|
|
require("luci.template")
|
2008-10-20 22:35:11 +00:00
|
|
|
local http = require "luci.http"
|
2008-08-29 20:36:45 +00:00
|
|
|
|
2008-09-05 09:37:02 +00:00
|
|
|
maps = luci.cbi.load(model, ...)
|
2008-08-09 14:14:04 +00:00
|
|
|
|
2008-10-20 22:35:11 +00:00
|
|
|
local state = nil
|
|
|
|
|
2008-08-09 14:14:04 +00:00
|
|
|
for i, res in ipairs(maps) do
|
2008-10-20 22:35:11 +00:00
|
|
|
local cstate = res:parse()
|
|
|
|
if not state or cstate < state then
|
|
|
|
state = cstate
|
|
|
|
end
|
2008-08-09 14:14:04 +00:00
|
|
|
end
|
|
|
|
|
2008-10-20 22:35:11 +00:00
|
|
|
http.header("X-CBI-State", state or 0)
|
2008-08-09 14:14:04 +00:00
|
|
|
luci.template.render("header")
|
|
|
|
for i, res in ipairs(maps) do
|
|
|
|
res:render()
|
|
|
|
end
|
|
|
|
luci.template.render("footer")
|
|
|
|
end
|
|
|
|
end
|