Luci流程分析(openwrt下)
1. 页面请求:
1.1. 代码结构
在openwrt文件系统中,lua语言的代码不要编译,类似一种脚本语言被执行,还有一些uhttpd服务器的主目录,它们是:
/www/index.html
cgi-bin/luci
luci-static/xxx/xx.css、js、gif
/usr/lib/lua/nixio.so、uci.so
luci/http.lua、dispatcher.lua、core…
controller/xxx.lua
model/xxx.lua
view/xxx.lua
1.2. 界面显示
网页请求格式基本都如下所示:http://10.10.82.238/cgi-bin/luci,说明处理都在服务器的默认网站下的/cgi-bin/luci文件进行处理。
1.2.1. /www/cgi-bin/luci
luci.dispatcher.indexcache = "/tmp/luci-indexcache"--缓存文件位置“/tmp/luci-indexcache”
luci.sgi.cgi.run()--cgi程序接下来执行程序,Luci的默认路径是/usr/lib/lua/luci,所以luci.sgi.cgi.run()是运行/usr/lib/lua/luci/sgi/cgi.lua文件中的run函数。
1.2.2. /usr/lib/lua/luci/sgi/cgi.lua
local r = luci.http.Request(…)--把web请求放于r中(包括环境变量,web请求,出错处理接口)
local x = coroutine.create(luci.dispatcher.httpdispatch)--创建一个协同程序
local res, id, data1, data2 = coroutine.resume(x, r)--运行上面创建的协同程序,即运行httpdispatch,参数为上面local r里的变量。
if active then
if id == 1 then
io.write("Status: " .. tostring(data1) .. " " .. data2 .. "\r\n")
elseif id == 2 then
hcache = hcache .. data1 .. ": " .. data2 .. "\r\n"—准备header
elseif id == 3 then--写header、blank
io.write(hcache)—默认到stdout
io.write("\r\n")
elseif id == 4 then
io.write(tostring(data1 or ""))--写body
elseif id == 5 then
io.flush()
io.close()--EOF
active = false
elseif id == 6 then
data1:copyz(nixio.stdout, data2)
data1:close()
1.2.3. /usr/lib/lua/luci/dispatcher.lua
httpdispatch:解析请求,获得请求节点,并调用dispatch处理请求节点,如:
Request :http://10.10.82.238/cgi-bin/luci/;stok=e10fa5c70fbb55d478eb8b8a2eaabc6f/admin/network/firewall/ get: admin network firewall
dispatch:四个部分处理请求
A.节点树node-tree创立
if not c then
c = createtree()
B.需要显示的部分
if (c and c.index) or not track.notemplate then
C.认证
if track.sysauth then
D.显示/处理
ok, err = util.copcall(target, c.target, unpack(args))
1.2.4. 请求页面network
http://10.10.82.238/cgi-bin/luci/;stok=4b77c83a89c7b9cd8f4dcc0fcbc28024/admin/network/
1.2.3中D显示部分与entry()函数(形如entry(path,target,title,order))有关,其中定义的target方法或者target部分。在以上http请求中会根据请求路径去访问到/usr/lib/lua/luci/controller/admin/network.lua,调用顺序如下:
ok, err = util.copcall(target, unpack(args))-- dispatcher.luaà
page.target = firstchild() -- network.luaà
function firstchild()-- dispatcher.luaà
_firstchild()-- dispatcher.luaàdispatch(path)-- 自动链接到它的第一个子节点,
在network.lua中定义order,Interfaces是10,为第一个子节点:
page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), _("Interfaces"), 10)--通过cbi方法处理admin_network/ifaces.lua和admin_network/network.lua,生成html文件
2. 页面响应
2.1. Web请求
当点击页面“Save & Apply”按钮时,浏览器会把每一个有name的web元素的对应值下传,下传form表格如下:
-----------------------------151563007122428
Content-Disposition: form-data; name="cbi.submit" 1
-----------------------------151563007122428
Content-Disposition: form-data; name="cbi.cbe.firewall.cfg02e63d.syn_flood" 1 -----------------------------151563007122428
Content-Disposition: form-data; name="cbi.cbe.firewall.cfg02e63d.drop_invalid" 1
……
……
-----------------------------151563007122428
Content-Disposition: form-data; name="cbi.apply" Save & Apply -----------------------------151563007122428—
2.2. 处理
服务器处理过程和页面生成基本类似,也调用到/usr/lib/lua/luci/dispatcher.lua并走到显示/处理部分,后继处理如下:
ok, err = util.copcall(target, c.target, unpack(args)) à(target在luci/controller/firewall中被赋值为arcombine(cbi("firewall/zones"), cbi("firewall/zone-details")),即两个cbi函数的集合)
function cbi(model, config) à
local function _cbi(self, ...) à
local cstate = res:parse()à
function Map.parse(self, readinput, ...) à
Node.parse(self, ...)
Node.parse会调用Map中的每一个子元素自身的处理
EX:
如调用Flag的处理:function Flag.parse(self, section),他会通过遍历处理from传下来的每一个Flag,并通过本身的write/remove来启用和禁用这个选项。
当form保存下来cbid.firewall.cfg02e63d.syn_flood这个Network/Firewall/General Setting下的Flag标签的值时,处理函数就会调用Flag.parse处理:调用self:formvalue来匹配标签值,然后调用model/cbi/firewall/zones.lua的write或者remove来禁用或者启用这个选项所控制的开关。
由于Flag = class(AbstractValue),继承于AbstractValue类,所以其write/remove是调用的AbstractValue类的write/remove方法。
AbstractValue.write调用self.map:set即function Map.set(self, section, option, value),Map.set 再调用self.uci:set(self.config, section, option, value)来设置对应config文件,然后Map.parse 会调用self.uci:commit(config)对已修改的config逐一提交。
生效的两种方式
1、按照固定格式设置对应选项,系统自动调用来使各个参数生效,self.uci:apply(self.parsechain) (应用刚设置的config设置服务)àfunction Cursor.apply(self, configlist, command) àreturn { "/sbin/luci-reload", unpack(configlist) };
2、self:_run_hooks("on_apply", "on_after_apply"),自己在对应的.lua文件中写m.on_apply来启动或者处理方式。
ps:openwrt个人理解加上前辈的blog来写的,基本是一路打log来了解流程,若有文中问题还请指正
参考:http://www.verydemo.com/demo_c101_i48675.html
Luci流程分析(openwrt下)的更多相关文章
- openwrt luci web分析
openwrt luci web分析 来源 https://www.jianshu.com/p/596485f95cf2 www/cbi-bin/luci #!/usr/bin/lua --cgi的执 ...
- freeswitch呼叫流程分析
今天翻文档时发现之前整理的关于freeswitch呼叫相关的内容,写成博文分享出来也方便我以后查阅. 整体结构图 FreeswitchCore 模块加载过程 freeswitch主程序初始化时会从mo ...
- u-boot 流程分析
u-boot 介绍: 对于计算机来说 , 从一开始上机通电是无法直接启动操作系统的 , 这中间需要一个引导过程 , 嵌入式Linux系统同样离不开引导程序 , 这个启动程序就叫启动加载程序(Boot ...
- thttpd和cgilua安装与运行流程分析
安装 参考如下博文安装thttpd软件 http://blog.csdn.net/21aspnet/article/details/7045845 http://blog.csdn.net/drago ...
- u-boot中nandflash初始化流程分析(转)
u-boot中nandflash初始化流程分析(转) 原文地址http://zhuairlunjj.blog.163.com/blog/static/80050945201092011249136/ ...
- Android7.0 Phone应用源码分析(二) phone来电流程分析
接上篇博文:Android7.0 Phone应用源码分析(一) phone拨号流程分析 今天我们再来分析下Android7.0 的phone的来电流程 1.1TelephonyFramework 当有 ...
- 从注册流程 分析如何安全退出多个Activity 多种方式(附DEMO)
退出Activity注册Android遍历 目录(?)[+] 前言 知识结构 具体方案 方案1 方法采用FLAG_ACTIVITY_CLEAR_TOP退出整个程序多activity 方案2 方 ...
- ofbiz进击 。 ofbiz 退货流程(包含获取可退货项流程分析 以及 取消退货项的过程分析)
根据订单获取可退货项流程分析 退货的时候,调用 services_return.xml 中的获取可进行退货的退货项 getReturnableItems ,该服务调用了Java类 org.ofbi ...
- u-boot启动流程分析(2)_板级(board)部分
转自:http://www.wowotech.net/u-boot/boot_flow_2.html 目录: 1. 前言 2. Generic Board 3. _main 4. global dat ...
随机推荐
- 字符串转换为float<2>
Configuration OK zjtest7-frontend:/usr/local/logstash-2.3.4/config# ../bin/logstash -f g01.conf Sett ...
- 10个工具让你的 shell 脚本更强大
10个工具让你的 shell 脚本更强大 很多人误以为shell脚本只能在命令行下使用.其实shell也可以调用一些GUI组件,例如菜单,警告框,进度条等等.你可以控制最终的输出,光标位 置还有各种输 ...
- ucfirst() strtoupper() strtolower()
ucfirst 将字符串第一个字符改大写. 语法: string ucfirst(string str); 返回值: 字符串 函数种类: 资料处理 内容说明 本函数返回字符串 str 第一个字的字 ...
- tyvj1297 小气的小B
描述 其实你们都不知道,小B是很小气的.一天小B带着他的弟弟小B'一起去摘果子,走着走着,他们忽然发现了一颗长满了果子的树.由于弟弟长得太矮了,弟弟只有让哥哥小B帮他摘一些果子下来.哥哥小B说:&qu ...
- 我的Android进阶之旅------>Android拍照小例子
今天简单的学习了一下android拍照的简单实现. 当然该程序是个小例子,非常简单,没有什么复杂的操作,但是可以学习到Android 拍照API流程. 1.在布局文件中添加一个 surfaceView ...
- poj 3262 Protecting the Flowers 贪心
题意:给定n个奶牛,FJ把奶牛i从其位置送回牛棚并回到草坪要花费2*t[i]时间,同时留在草地上的奶牛j每分钟会消耗d[j]个草 求把所有奶牛送回牛棚内,所消耗草的最小值 思路:贪心,假设奶牛a和奶牛 ...
- Linux SD/MMC/SDIO驱动分析
一.SD/MMC/SDIO概念区分 SD(SecureDigital)与 MMC(MultimediaCard) SD 是一种 flash memory card 的标准,也就是一般常见的 SD 记忆 ...
- spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例
在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...
- WinForm 控件不闪烁
1: [DllImport("user32")] 2: public static extern int SendMessage(IntPtr hwnd, int wMsg, in ...
- hdu3397 Sequence operation
感觉自己好像搞定了一个不得了得题呢.. 对于这种区间性质合并的线段树,对于每个节点保存一下当前区间内1的个数,左右边界相邻的1个的个数与0的个数,还有当前区间最大连续的1和0的个数. 合并的时候的细节 ...