关于luci的几个问题二
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) EndFunction 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 EndFunction 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) EndLuci.sys.hostname(value) //調用sys.lua文件中hostname(newname)目的是得到或設置當前hostname End在view/header.htm中,
<%+header%> <form mthod=”post” name=”cbi” action=”<%=REQUEST_URI%>” enctype=”multipart/form-data” οnreset=”return cbi_validate_reset(this)” οnsubmit=”return cbi_validate_form(this,’<%:Some fields are invalid, cannot save values!%>’)”>在view/footer.htm中有apply&save、apply、reset三個按鈕,其中apply&save按鈕這樣寫:
<%if not autoapply and not flow.hideapplybtn then %> <input class = “cbi-button cbi-button-apply” type = “submit” name =”cbi.apply” value = “<%:Save & Apply%>” /> <% end %> </FORM>當點擊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()可以得到界面里面的數據。
總結
以上是生活随笔為你收集整理的关于luci的几个问题二的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于luci的几个问题一
- 下一篇: 中文pppoe中文拨号的解决方案