最近,由于项目的原因,现在总结几点:
1.luci运行的流程?
答:
首先,我们从/www/cgi-bin/文件开始,运行luci文件中代码:
#!/usr/bin/lua
luci.dispatcher.indexcache = “tmp.luci-indexcache”
luci.sgi.cgi.run()
接着进入到sgi/的cgi.lua文件中,一个函数是limitsource(handle, limit),主要是将limit个数的字符从stdin里面。
另一个函数是run()后面的页面生成将一直在这个函数进行。
run()
Local x = coroutine.create(luci.dispatcher.httpdispatcher)
While coroutine.status(x) ~= “dead” do
//运行上面创建的协同程序,即运行httpdispatcher,参数为上面local r
Local res. Id, data1, data2 = coroutine.resume(x, r)
if active then
If id == 1 then -- http.response-line
If id == 2 then --准备header
If id == 3 then --写header、blank,默认到stdout
If id == 4 then --写body
If id == 5 then --EOF
If id == 6 then
End
End
End
然后,进入httpdispatcher(request, prefix) //参数都放在context里
<pre name="code" class="html">
<pre name="code" class="html">Local pathinfo = http.urldecode(request:getenv(“PATH_INFO”) or “”, true)
For _,node in ipairs(prefix) do
R[#r+1] = node --将node赋给r{}
End
Local stat, err = util.coxpcall(function()
Dispatch(context.request)
End, error500)
接着,进入
dispatch(request)
函数中,
A.设置语言
B.创建node-tree节点树结构
Local c = ctx.tree
Local stat
If not c then
C = createtree() //此函数从controller下的index函数来创建node-tree结构 文件
End
B. createtree()函数
If not index then
Createindex() //此函数定义了path、suff,判断条件然后进入不同分支,
//Createindex_fastindex(path, suff)、createindex_plain(path, suff)
End
Local track = {} -- 每一层把找到的node信息放在这个track()中
For i, s in ipairs(request) do
Util.update(track, c) -- update(t, updates) t要更新的表,updates包含需要更新的值得表。
C.需要显示的部分
If (c and c.index) or not track.template then
初始化模板
定义了tpl.context.viewns = setmetatable()
D.认证
If track.sysauth then
Local sauth = require “luci.sauth”
//将getcookie和sysauth属性赋给sess
If not sess then
Sess = luci.http.getcookie(“sysauth”)
Sess = sess and sess:match(“^[a-f0-9]*$”)
Verifytoken = true
End
//读取session值返回它的content
Local sdat = sauth.read(sess)
If sdat then
Else
Local eu = http.getenv(“HTTP_AUTH_USER”)
Local ep = http.getenv(“HTTP_AUTH_PASS”)
End
F.显示/处理 c是createtree()的返回值tree,根据不同类型不同处理。
If c then
If type(c.target) ==”function” then target = c.target
Elseif type(c.target) == “table” then target = c.target.target
End
End
2.关于root和mini登录的问题?
答:
Root=node()调用dispatcher.lua中node(...)函数,node(...)调用_create_node(path)函数,定义了最外面的节点,也就是最上层的菜单显示。
If not root.target then
Root.target = alias(“admin”) //调用dispatcher.lua中alias(...)函数,重定向到另外一个节点,在函数将admin节点继续给了req,然后dispatch(req)
Root.index = true
End
Local page = node(“admin”) //将admin.写到context.treecache[name]中
Page.target = firstchild() //调用dispatcher.lua中firstchild()函数,return {type = “firstchild”, target = _firstchild},然后调用_firstchild(),将最低顺序的node给dispatch()函数执行。
Page.title = _(“Administration”) //标题
Page.order = 10 //顺序
Page.sysauth = “root” //认证用户的登录
Page.sysauth_authenticator = “htmlauth” //调用dispatcher.lua中htmlauth()函数,检测登录的合法性。
Page.ucidata = true
Page.index = true
方法:将admin文件夹拷贝一份给mini,将mini中index.lua中将page.sysauth = “root”和page.sysauth_authenticator = “htmlauth”注释掉,那么在弹出的页面上点击User按钮,会直接进入到系统中。
3.对于entry()函数的分析?答:
3.1 entry(path, target, title, order) 例如:
Entry({“admin”, “system”}, alias(“admin”, “system”, “system”), _(“System”), 30).index = true
Path:虚拟路径
Target:目标函数调用
Title:显示在页面上的标题
Order:在同一级别下,node的顺序。(可选)
Local c = node(unpack(path)) //unpack返回path中的所有值,并传给node做参数,然后调用node两次,将node节点创建出来
//将参数分别传进去
C.target = target //将值传进去后,刚开始构造node-tree的时候,alias(..)函数会在entry(...)函数之前调用,然后alias()函数中调用dispatch(req)。
C.title = title
C.order = order
C.module = getfenv(2).__NAME
3.2. Entry({“admin”, “system”, “system”}, cbi(“admin_system/system”), _(“System”), 1)
首先,cbi()函数先执行,return {type = “cbi”, config = config, model = model, target = _cbi}这句话,开始的时候,_cbi函数不会被执行,只有到了dispatch()函数里面才可以执行。
Cbi函数返回四个值,type,config,model,target
当点击与cbi有关的request的时候,在dispatch()函数的显示/处理部分,有个if判断,
if c then
If type(c.target) == “function” then //当c.target类型为函数时候
Target = c.target
Elseif type(c.target) == “table” then //当c.target类型为table时候
Target = c.target.target //也就是把表中属性target = “_cbi”给了target,进行后续处理。
End
End
在_cbi(self, ...)函数中,
Local cbi = require “luci.cbi”
Local tpl = require “luci.template”
Local http = require “luci.http”
Local config = self.config or {} //将cbi()返回的第二个参数给config
Local maps = cbi.load(self.model, ...) //将第三个参数给load(),然后给maps
在cbi.lua文件中,model其实就是路径,load路径
接下来是一个for循环,每一个node首先需要map画出框架,然后一层一层的画控件。
For i, res in ipairs(maps) do
Res.flow = config //map.flow
Local cstate = res:parse() //调用Map.parse(self, readinput,...)
调用Node.parse(self, ...),(uci主要是与luci进行数据交互的平台。)
Function Map.parse(self, readinput, ...)
Formvalue(“cbi.skip”)
Node.parse(self, ...)
If self.save then // 如果map的保存按钮被点击,或者其他按钮被点击,都会触发uci里面的函数,来处理相关操作。
//比如:on_save, on_before_save,on_after_save, on_before_commit, on_after_commit,on_before_apply之类
If self:submitstate() then //如果map的提交按钮被点击
End
Map = class(Node) //Map是Node的子类,那么map.parse会执行Node.parse()方法,function Node.parse(self, ...)
For k, child in ipairs(self.children) do
Child:parse()
End
End在此函数执行之前,调用Node.__init__(self, title, description),self.children = {}
Local class= util.class class()函数return setmetatable({}, {__call = _instantiate,__index = base)} 当调用的时候,调用_instantiate(class, ...),函数中调用inst:__init__(...)函数初始化具体的类,返回类。
Map = class(Node)
Function Map.__init__()
Function Map.fromvalue(self, key)
Function Map.formvaluetable(self, key)
Function Map.get_scheme()
Function Map.submitstate(self)
Functoin Map.chain()
Function Map.state_handler()
Function Map.parse()
Function Map.render()
Function Map.section(self, class,...)
Function Map.add(self, sectiontype)
Function Map.set(self, section,...)
Function Map.del()
Function Map.get()
<pre name="code" class="html">
3.3 m:chain(“luci”) //向map中插入外部的config信息
S = m:section(TypedSection,””, “”) //Map:section创建了一个子section,其中如果是abstraction的实例,那么调用Node:append()中table.insert(self.children, obj)语句。S是类TypedSection产生的实例。
S.addremove = false //当执行TypedSection()函数的时候就会判断这个和下面的选项
S.anonymous = true
S.tab = s:tab("general", translate("General Settings")) //定义一个tab给s,调用abstractSection : tab(tab,title,desc)函数,其中
self.tab_names[#self.tab_names+1] = tab
self.tabs[tab] = {
title = title,
description = desc,
childs = { }
}
将将tab给了tab_names,tab的各个参数给了tabs数组。
o = s:taboption("general", DummyValue, "_systime", translate("Local Time"))
//调用AbstractSection:taboptoin(),然后调用AbstractSection.option(self, ...),
If instanceof(class, AbstractValue) then //如果DummyValue是AbstractSection的实例
local obj = class(self.map, self, option, ...) //实例化DummyValue类
self:append(obj) //返回的obj追加给AbstractSection
self.fields[option] = obj //将对象赋给fields[option]
return obj //返回obj
End
o.template = "admin_system/clock_status" //DummyValue实例后的对象o调用template
function DummyValue.__init__(self, ...)
AbstractValue.__init__(self, ...)
self.template = "cbi/dvalue" //这句话o.template调用template/cbi/dvalue.htm文件
self.value = nil
end
o = s:taboption("general", Value, "hostname", translate("Hostname")) //Value类实例化的实例给了o
o.datatype = "hostname"
//o连接到cbi文件夹下,datatype.lua文件
function o.write(self, section, value)
Value.write(self, section, value) //调用AbstractValue.write()方法,调用uci:set()写到config文件中
luci.sys.hostname(value) //获得或者更改当前的hostname
End
3.4 entry({"admin", "services"}, firstchild(), _("Services"), 40).index = true
调用dispatcher.lua 中的Firstchild(),
function firstchild()
return { type = "firstchild", target = _firstchild } //当后期显示部分执行dispatch()时候,调用_firstchild()函数,
if node and node.nodes and next(node.nodes) then
local k, v
for k, v in pairs(node.nodes) do
if not lowest or
(v.order or 100) < (node.nodes[lowest].order or 100)
then
lowest = k
end
end
End
path[#path+1] = lowest --将多出来的节点追加给path
dispatch(path) -- 最关键的一句代码,调用dispatch(path),path已经改变
3.5 entry({"admin", "logout"}, call("action_logout"), _("Logout"), 90)
调用dispatcher.lua中的call(),
function call(name, ...)
return {type = "call", argv = {...}, name = name, target = _call} //当后期显示部分执行dispatch()时候,调用_call()函数,
local function _call(self, ...)
local func = getfenv()[self.name] //获取当前函数所在文件夹路径
if #self.argv > 0 then
return func(unpack(self.argv), ...)
else
return func(...) //调用当前函数所在路径下的对应函数
end
End
3.6 entry({“admin”, “system”, “startup”},form(“admin_system/startup”),_(“Startup”), 45)
Function form(model)
Return {type = “cbi”, model = model, target = _form}
End
当执行dispatch()函数时,执行_form()函数,
Local maps = luci.cbi.load(self.model, ...)
For i, res in ipairs(maps) do
Local cstate = res:parse()
If cstate and (not state or cstate < state) then
State = cstate
End
End
Http.header(“X-CBI-State”, state or 0) //context.headers[key:lower()] = value
//Coroutine.yield(2, key, value)
Tpl.render(“header”) //render(name, scope) return template(name):render(scope or //getenv(2))然后调用Template(name):reader()画出header.htm
For i, res in ipairs(maps) do
Res:render()
End
Tpl.render(“footer”) //画出footer.htm
3<span style="font-family: 宋体;">.7 entry({“admin”,“network”,“wireless”}, arcombine(template(“admin_network/wifi_overview”), cbi(“admin_network/wifi”)), _(“Wifi”), 15)</span>
Arcombine(template, cbi)调用dispatcher.lua文件中function arcombine(trg1, trg2)
Return {type = “arcombine”, env = getfenv(), target = _arcombine, targets = {trg1, trg2}}
Function _arcombine(self, ...)
Local argv = {...}
Local target = #argv > 0 and self.targets[2] or self.targets[1]
Setfenv(target.target,self.env)
Target:target(unpack(argv)) //一个接着一个执行相应的函数,
End
3.8 entry({“admin”, “status”, “overview”}, template(“admin_status/index”), _(“Overview”), 1)
Template()调用dispatcher.lua文件中template(name)函数,
Return {type = “template”, view = name, target = _template}
当执行dispatch()函数的时候,则执行_template = function (self, ...) require “luci.template”.render(self.view) ,画出admin_status/index.htm
4.formvalue怎么处理值?
答:dispatcher.lua中authenticator.htmlauth()函数中,
Local user = luci.http.formvalue(“username”)
Local pass = luci.http.formvalue(“password”)
这句话是从http.lua文件中调用formvalue()函数获取值
调用function formvalue(name, noparse)
Return context.request:formvalue(name, noparse)
End
然后调用Request.formvalue(self, name, noparse)
If name then return self.message.params[name]
然后返回Request中的message.params[name]
- HTTP-Message table
Self.message = {
Env = env,
Headers = {},
Params = protocol.urldecode_params(env.QUERY_STRING or “”),
}
将最初的“Username”传到了params这里,然后调用http/protocol.lua文件中的
Function Urldecode_params(url, tbl)
Local params = tbl or {}
If url.find(“?”) then
Url = url:gsub(“^.+%?([^?]+)”, “%1”) //^开头表示匹配开始部分,+匹配1次或者多次,%?转义问号,第三个参数表示捕获第一个匹配字符串。^?表示非问号的部分,.+进行的是最长匹配。
End
<span style="color:#3366ff;">//由wireshark分析,Post /cgi-bin/luci HTTP/1.1(application/x-www-form-urlencoded)
Url:http://192.168.1.1/cgi-bin/luci?username=root&password=admin
Content-length:28
form iten :”username” = “root”
Key:username
Value:root
Form item:”password” = “admin”
Key: password
Value:admin
可见:还是处理成key-value对。</span>
For pair in url:gmatch(“[^&;]+”) do
-- 查找key、value
Local key = urldecode(pair:match(“^([^=]+)”) )
Local val = urldecode(pair:match(“^[^=]+=(.+)$”))
//调用urldecode中pair.match()查找key、val
-- 存储值
If type(key) == “string” and key:len() > 0 then //key就是第一句话传进去的id(username),然后val赋给params[name]
If type(val) ~= “string” then val = “” end
If not params[key] then //登录页面传进来的值进入这里面
Params[key] = val
Elseif type(params[key] ~= “table” then
Params[key] = {params[key], val}
Else
Table.insert(params[key],val)
End
End
End
Return params
End
至此,将url中的所需的值就获得了。然后再进行后续处理。
5.点击login按钮后发生了什么?
答:在sysauth.htm中,
<formmthod=”post” action=”<%=pcdata(luci.http.getenv(“REQUEST_URI”))%>”>
当点击按钮时候,就会跳转到action指定的url.在dispatcher.lua文件dispatch()函数中,
tpl.context.views setmetable({
.......},{__index=function(table,key)
If key == “controller” then return build_url()
Elseif key == “REQUEST_URI” then return build_url(unpack(ctx.requestpath))
})
当点击登录后,页面会跳转到/,接着/admin,如果需要认证,那么接下来会弹出htmlauth.htm页面,然后如果没有验证成功,则继续本页面,如果成功了,那么继续跳转/,然后admin/,此时post来了信息,然后alias到entry.order最小的那个node,很显然是/admin/status.lua,然后status这个node会alias到overview这个node。最主要的还是在diapatcher.lua文件中的dispatch()的认证部分。
Apply,apply&save,reset的逻辑跟这个也是一样的。
当点击登录后,页面会跳转到/,接着/admin,如果需要认证,那么接下来会弹出htmlauth.htm页面,然后如果没有验证成功,则继续本页面,如果成功了,那么继续跳转/,然后admin/,此时post来了信息,然后alias到entry.order最小的那个node,很显然是/admin/status.lua,然后status这个node会alias到overview这个node。最主要的还是在diapatcher.lua文件中的dispatch()的认证部分。
Apply,apply&save,reset的逻辑跟这个也是一样的。