uhttpd 架构调用细节之lua
时间:2014-12-12 16:49:57
收藏:0
阅读:4953
uhttpd是openwrt系统默认集成的轻量级服务器,采用select机制对设备性能要求较低。
/usr/sbin/uhttpd -f -h /www -r wifibox -x /cgi-bin -l /slipt -L /usr/share/lua/wifibox/main.lua -t 60 -T 30 -k 20 -A 1 -n 3 -N 100 -R -p 0.0.0.0 80
这是一类智能路由器uhttpd的运行参数,其中—h指定的是网站的根目录,静态文件请求会在会以这个目录为根目录;-L指定了调用的lua主处理程序;-x指定了cgi运行程序;源文件中的运行参数描述如下,附件为utttpd完整源码
"Usage: %s -p [addr:]port [-h docroot]\n" " -f Do not fork to background\n" " -c file Configuration file, default is ‘/etc/httpd.conf‘\n" " -p [addr:]port Bind to specified address and port, multiple allowed\n" #ifdef HAVE_TLS " -s [addr:]port Like -p but provide HTTPS on this port\n" " -C file ASN.1 server certificate file\n" " -K file ASN.1 server private key file\n" #endif " -h directory Specify the document root, default is ‘.‘\n" " -E string Use given virtual URL as 404 error handler\n" " -I string Use given filename as index page for directories\n" " -S Do not follow symbolic links outside of the docroot\n" " -D Do not allow directory listings, send 403 instead\n" " -R Enable RFC1918 filter\n" #ifdef HAVE_LUA " -l string URL prefix for Lua handler, default is ‘/lua‘\n" " -L file Lua handler script, omit to disable Lua\n" #endif #ifdef HAVE_CGI " -x string URL prefix for CGI handler, default is ‘/cgi-bin‘\n" " -i .ext=path Use interpreter at path for files with the given extension\n" #endif #if defined(HAVE_CGI) || defined(HAVE_LUA) " -t seconds CGI and Lua script timeout in seconds, default is 60\n" #endif " -T seconds Network timeout in seconds, default is 30\n" " -d string URL decode given string\n" " -r string Specify basic auth realm\n" " -m string MD5 crypt given string\n"
uhttpd服务器接受的请求会根据请求头分成三类,静态文件请求,cgi请求(处理表单信息)和lua请求(功能强大实现多功能的处理和调用)
首先是lua请求,uhttpd服务器是C语言编写,通过c语言调用lua程序实现了,lua请求相关程序的处理,c语言和lua语言通过栈传送数据,下面是一个简单的c语言调用lua程序的实例
//add.c #include <stdio.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" /*the lua interpreter*/ lua_State* L; int luaadd(int x, int y) { int sum; /*the function name*/ lua_getglobal(L,"add"); /*the first argument*/ lua_pushnumber(L, x); /*the second argument*/ lua_pushnumber(L, y); /*call the function with 2 arguments, return 1 result.*/ lua_call(L, 2, 1); /*get the result.*/ sum = (int)lua_tonumber(L, -1); /*cleanup the return*/ lua_pop(L,1); return sum; } int main(int argc, char *argv[]) { int sum; /*initialize Lua*/ L = lua_open(); /*load Lua base libraries*/ luaL_openlibs(L); /*load the script*/ luaL_dofile(L, "add.lua"); /*call the add function*/ sum = luaadd(10, 15); /*print the result*/ printf("The sum is %d \n",sum); /*cleanup Lua*/ lua_close(L); return 0; }
add.lua --add two numbers function add(x,y) return x + y end
评论(0)