1.Luci/sgi/cgi.lua和http.lua和dispatcher.lua关系?
答:
对每一个node,最重要的属性是target,也是dispatch()流程最后要执行的方法。各个方法alise、firstchild、call、cbi、form、template。前两种主要链接其他node,后一个则是主要的操作、以及页面生成。后四种方法最终生成完整的http-response报文(包括HTML文件)。
生成的http-response报文会通过io.write()写在stdout上,然后发送给client。
http.lua
Context = util.threadlocal()
Request = util.class()
Function Request.__init__(self, env, sourcein, sinkerr)
Function Request.formvalue(self, name, noparse)
Function Request.formvaluetable(self, prefix)
Function Request.content(self)
Function Request.getcookie(self, name)
Function Request.getenv(self, name)
Function Request.setfilehandler(self, callback)
Function Request._parse_input(self)
Function close() //执行httpdispatch(request, prefix)中luci.http.close()时,
If not context.eoh then
Context.eoh = true
Coroutine.yield(3)
End
If not context.closed then
Context.closed = true
Coroutine.yield(5)
End
End
Function content()
Function formvalue()
Function formvaluetable(prefix)
Function getcookie(name)
Function getenv(name)
Function setfilehandler(callback)
Function header(key, value) //1.执行dispatch()函数luci.http.header(“SetCookie”,”sysauth=” .. Sid .. “;path=”. //.build_url())时调用此函数;
//2.调用_cbi()函数中http.header(“X-CBI-State”, state or 0)时;
If not context.headers then
Context.headers = {}
End
Context.headers[key:lower()] = value
Coroutine.yield(2, key, value)
End
Function prepare_content(mime)
Function source()
Function status(code, message) //1.dispatcher.lua中执行error404(message)函数,然后执行此函数; //2.error500(message)函数;
// 3.还有dispatch()中luci.http.status(403,”Forbidden”);
Code = code or 200
Message = message or “OK”
Context.status = code
Coroutine.yield(1, code, message)
end
Function write(content, src_err) //1.在error404()中执行luci.http.write(message)时,
//2.在error500(message)中luci.http.write(message)时,
If not content then
If src_err then
Error(src_err)
Else
Close()
End
Return true
Elseif #content == 0 then
Return true
Else
....
Context.eoh = true
Coroutine.yield(3)
End
Coroutine.yield(4, content)
Return true
End
Function splice(fd, size) //httpclient文件夹下的receive.lua包含nixio.splice()
Coroutine.yield(6, fd, size)
end
Function redirect(url)
Function build_querystring(q)
Urldecode = protocol.urldecode
Urldecode = protocol.urlencode
Function write_json(x)
Cgi.lua
Function run()
If active then
If id == 1 io.write(“status:” .. Tostring(data1).. “ “ .. Data2 .. “\r\n”)
If id == 2 hcache = hcache .. Data1 .. “:” .. “data2 .. “\r\n”
If id == 3 io.write(hcache) io.write(“\r\n”)
If id == 4 io.write(tostring(data1 or “”))
If id == 5 io.flush() io.close() active = false
If id == 6 data1:copyz(nixio.stdout, data2) data1:close()
End
End
答:当点击system下system标签,local Time选项、Hostname选项、Timezone选项,这三个开始的时候就有值,解析如下:
O = s:taboption(“general”, Value, “hostname”, translate(“Hostname”))
O.datatype = “hostname”
Function o.write(self, section, value)
Value.write(self, section, value) //调用cbi.lua中Value类,因为Value继承自AbstractValue类,然后调用AbstractValue.write(self, section, value)方法,return self.map:set(section,self.option, value),然后调用Map.set(self, section, option, value),里面语句如下:
If type(value) ~= “table” or #value > 0 then
If option then return self.uci:set(self.config, section, option,value) //然后uci.cursor:set()就把数据设置到config文件里面了。
Else return self.uci:set(self.config,section, value)
Else return Map.del(self, section, option)
End
Luci.sys.hostname(value) //调用sys.lua文件中hostname(newname)目的是得到或设置当前hostname
End
在view/header.htm中,
<%+header%>
在view/footer.htm中有apply&save、apply、reset三个按钮,其中apply&save按钮这样写:
<%if not autoapply and not flow.hideapplybtn then %>
” />
<% end %>
当点击Save&Apply按钮的时候,type=”submit”,然后执行action动作,跳转到REQUEST_URI表示的页面。执行onsubmit动作,
在cbi.lua文件中,
Template.parse(self, readinput)
Return Map.formvalue(self, “cbi.submit”) and FORM_DONE or FORM_NODATA
然后调用Map.formvalue(self, key) return self.readinput and luci.http.formvalue(key)
调用http.lua中formvalue()可以得到界面里面的数据。