uhttpd 架构调用细节之lua

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
时间: 2024-08-26 11:27:47

uhttpd 架构调用细节之lua的相关文章

缓存架构设计细节二三事

本文主要讨论这么几个问题: (1)"缓存与数据库"需求缘起 (2)"淘汰缓存"还是"更新缓存" (3)缓存和数据库的操作时序 (4)缓存和数据库架构简析 一.需求缘起 场景介绍 缓存是一种提高系统读性能的常见技术,对于读多写少的应用场景,我们经常使用缓存来进行优化. 例如对于用户的余额信息表account(uid, money),业务上的需求是: (1)查询用户的余额,SELECT money FROM account WHERE uid=XXX

【58沈剑架构系列】缓存架构设计细节二三事

本文主要讨论这么几个问题: (1)“缓存与数据库”需求缘起 (2)“淘汰缓存”还是“更新缓存” (3)缓存和数据库的操作时序 (4)缓存和数据库架构简析   一.需求缘起 场景介绍 缓存是一种提高系统读性能的常见技术,对于读多写少的应用场景,我们经常使用缓存来进行优化. 例如对于用户的余额信息表account(uid, money),业务上的需求是: (1)查询用户的余额,SELECT money FROM account WHERE uid=XXX,占99%的请求 (2)更改用户余额,UPDA

一个lua文件如何调用另一个lua文件的变量?

进来在学习春哥的OpenResty,个人对lua也不怎么熟练,难免会碰到很多奇奇怪怪的问题,这里就稍微记录下碰到的一些小问题,以供他人参考. 上网搜相关资料,大多数资料都只是说调用lua的require或者dofile,并且详细说命它们的区别,不是说没用,只是没有回答我的问题--如何在OpenResty服务器上,实现lua文件之间的变量互用? 首先,得要指定require的路径,需要在http段里面配置变量"lua_package_path /topath/?.lua;;",注意这里的

c#调用脚本语言Lua——简单Demo

配置: 1. 下载c#下的Lua支持类库.下载地址:http://files.luaforge.net/releases/luainterface/luainterface/2.0.3 将(lua51.dll\LuaInterface.dll)引用自己的项目中. 2. 修改App.config添加以下内容: <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup

win10系统架构调用

操作系统模型 操作系统有两种模式: 用户模式 内核模式 当用户模式调用系统服务时,CPU执行一个特殊的指令以切换到内核模式(Ring0),当系统服务调用完成时,操作系统切换回用户模式(Ring3). Windows实现了一套内核保护机制,比如PatchGuard和内核模式代码签名. 系统架构 windows简化版架构实现: 在windows下,用户程序不直接调用本地的Windows服务,而是直接通过子系统DLL来调用.子系统DLL的角色是将文档化的函数翻译成调用的非文档化的系统服务(未公开的).

7、SpringMVC源码分析(2):分析HandlerAdapter.handle方法,了解handler方法的调用细节以及@ModelAttribute注解

从上一篇 SpringMVC源码分析(1) 中我们了解到在DispatcherServlet.doDispatch方法中会通过 mv = ha.handle(processedRequest, response, mappedHandler.getHandler()) 这样的方式来执行request的handler方法. 先来分析一下ha.handle方法的调用过程:HandlerAdapter接口有一个抽象实现类AbstractHandlerMethodAdapter,在该抽象类中通过具体方法

x86架构调用栈分析

以一个简单求阶乘的代码为例: 1 #include <stdio.h> 2 3 unsigned int fact(unsigned int n) 4 { 5 if (n == 0) 6 return 1; 7 return n * fact(n - 1); 8 } 9 10 int main(void) 11 { 12 int c = 0; 13 14 c = fact(5); 15 16 return c; 17 } 1 void mbacktrace(int fps[], int n)

C++调用Lua的性能测试

游戏服务器经典的架构就是C++和Lua的结合,C++开发主体框架,Lua实现一些复杂的逻辑.我们都知道Lua是一种非常快的语言,但是到底有多块,我们测试下看看. C++调用Lua的性能测试,发现不对的地方望提出. 实验一:我们使用C++调用Lua带8个以上参数的函数,而这个函数里面什么也没有做.我们通过这个实验能够简单地测试出:使用Lua虚拟机和向调用栈当中传入8+个参数和Lua取出这些参数的时间.但是其中的参数类型比较多样,有整形,浮点型,和数组.实验的Lua函数比较简单,如下: functi

LAMP架构演进到LAMPGC,再演进到LNMLGC(linux+nginx+mysql+lua+gearman+C)

LAMP是一个大众的架构了,linux+apache+mysql+php 在我们系统的架构中,做了进一步的演进,linux+apahce+mysql+php+gearman+C php作页面的展示 核心业务逻辑由C语言实现,php通过gearman中间件调用C任务 由于apache在高并发方面不太给力,因此在需要高并发的场景中,我们进一步演进,linux+nginx+mysql+php+lua+gearman+C 页面部分由nginx+fastcgi+php-fpm来展示 高并发的业务调用由ng