標籤:uhttpd lua c語言
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;intluaadd(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;}intmain(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 numbersfunction add(x,y) return x + yend
uhttpd 架構調用細節之lua