关于luci的几个问题<二>

来源:互联网 发布:nginx命令启动服务 编辑:程序博客网 时间:2024/05/22 08:10

1.Luci/sgi/cgi.luahttp.luadispatcher.lua关系?

答:


对每一个node,最重要的属性是target,也是dispatch()流程最后要执行的方法。各个方法alisefirstchildcallcbiformtemplate。前两种主要链接其他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 thenContext.eoh = trueCoroutine.yield(3)EndIf not context.closed thenContext.closed = trueCoroutine.yield(5)EndEndFunction 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  thenContext.headers = {}EndContext.headers[key:lower()] = valueCoroutine.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 200Message = message or “OK”Context.status = codeCoroutine.yield(1, code, message)endFunction write(content, src_err)  //1.在error404()中执行luci.http.write(message)时,
                                  //2.在error500(message)中luci.http.write(message)时,If not content thenIf src_err thenError(src_err)Else Close()EndReturn trueElseif #content == 0 thenReturn trueElse....Context.eoh = trueCoroutine.yield(3)EndCoroutine.yield(4, content)Return trueEnd Function splice(fd, size)       //httpclient文件夹下的receive.lua包含nixio.splice()Coroutine.yield(6, fd, size)endFunction redirect(url)Function build_querystring(q)Urldecode = protocol.urldecodeUrldecode = protocol.urlencodeFunction write_json(x) Cgi.luaFunction run()If active thenIf 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 = falseIf id == 6   data1:copyz(nixio.stdout, data2)  data1:close()EndEnd


2.Web页面和config文件中的数据如何交互?

答:当点击systemsystem标签,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 thenIf option thenreturn 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%><form mthod=”post” name=”cbi” action=”<%=REQUEST_URI%>” enctype=”multipart/form-data” onreset=”return cbi_validate_reset(this)” onsubmit=”return cbi_validate_form(this,’<%:Some fields are invalid, cannot save values!%>’)”>

view/footer.htm中有apply&saveapplyreset三个按钮,其中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.luaformvalue()可以得到界面里面的数据。



0 0
原创粉丝点击