Lua binding: Optimize code

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
main
Jianhui Zhao 2018-07-23 23:29:12 +08:00
parent 6f756b6d99
commit f21e2001e1
2 changed files with 54 additions and 61 deletions

View File

@ -25,31 +25,6 @@ local uh = require "uhttpd"
local verbose = true
local port = 8914
-- LOG_DEBUG LOG_INFO LOG_ERR
if not verbose then
uh.set_log_threshold(uh.LOG_ERR)
end
uloop.init()
uh.log(uh.LOG_INFO, "uhttpd version:" .. uh.VERSION)
local srv = uh.new(port)
-- srv:set_options({docroot = "/home/zjh/www", index = "lua.html"})
-- srv:ssl_init("uhttpd.crt", "uhttpd.key")
uh.log(uh.LOG_INFO, "Listen on:" .. port)
srv:set_on_error404(function(cl, path)
uh.send_header(cl, 200, "OK", -1)
uh.header_end(cl)
uh.chunk_send(cl, string.format("<h1>Libuhttpd-Lua: '%s' Not found</h1>", path))
uh.request_done(cl)
end)
local http_methods = {
[uh.HTTP_METHOD_GET] = "GET",
[uh.HTTP_METHOD_POST] = "POST",
@ -61,7 +36,14 @@ local http_version = {
[uh.HTTP_VER_11] = "HTTP/1.1"
}
srv:set_on_request(function(cl, path)
local function on_error404(cl, path)
uh.send_header(cl, 200, "OK", -1)
uh.header_end(cl)
uh.chunk_send(cl, string.format("<h1>Libuhttpd-Lua: '%s' Not found</h1>", path))
uh.request_done(cl)
end
local function on_request(cl, path)
if path ~= "/hello" then
return uh.REQUEST_CONTINUE
end
@ -103,6 +85,29 @@ srv:set_on_request(function(cl, path)
uh.request_done(cl)
return uh.REQUEST_DONE
end)
end
-- LOG_DEBUG LOG_INFO LOG_ERR
if not verbose then
uh.set_log_threshold(uh.LOG_ERR)
end
uloop.init()
uh.log(uh.LOG_INFO, "uhttpd version:" .. uh.VERSION)
local srv = uh.new(port)
-- srv:ssl_init("uhttpd.crt", "uhttpd.key")
uh.log(uh.LOG_INFO, "Listen on:" .. port)
srv:set_options({
docroot = "/home/zjh/www", -- Default is .
index = "lua.html", -- Default is index.html
on_error404 = on_error404,
on_request = on_request
})
uloop.run()

View File

@ -45,7 +45,7 @@ static void *uh_create_userdata(lua_State *L, size_t size, const luaL_Reg *reg,
return obj;
}
static int lua_uh_on_request(struct uh_client *cl)
static int lua_on_request(struct uh_client *cl)
{
struct lua_uh_server *lsrv = container_of(cl->srv, struct lua_uh_server, srv);
const char *path = cl->get_path(cl);
@ -80,22 +80,7 @@ static int lua_uh_ssl_init(lua_State *L)
return 0;
}
static int lua_uh_set_on_request(lua_State *L)
{
struct lua_uh_server *lsrv = luaL_checkudata(L, 1, LUA_UH_SERVER_MT);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_getglobal(L, "__uh_on_request");
lua_pushvalue(L, 2);
lsrv->request_ref = luaL_ref(L, -2);
lsrv->srv.on_request = lua_uh_on_request;
return 0;
}
static void http_callback_404(struct uh_client *cl)
static void lua_on_error404(struct uh_client *cl)
{
struct lua_uh_server *lsrv = container_of(cl->srv, struct lua_uh_server, srv);
const char *path = cl->get_path(cl);
@ -111,21 +96,6 @@ static void http_callback_404(struct uh_client *cl)
lua_call(L, 2, 0);
}
static int lua_uh_set_on_error404(lua_State *L)
{
struct lua_uh_server *lsrv = luaL_checkudata(L, 1, LUA_UH_SERVER_MT);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_getglobal(L, "__uh_on_error404");
lua_pushvalue(L, 2);
lsrv->error404_ref = luaL_ref(L, -2);
lsrv->srv.on_error404 = http_callback_404;
return 0;
}
static int lua_uh_set_options(lua_State *L)
{
struct lua_uh_server *lsrv = luaL_checkudata(L, 1, LUA_UH_SERVER_MT);
@ -143,6 +113,26 @@ static int lua_uh_set_options(lua_State *L)
srv->set_index_file(srv, lua_tostring(L, -1));
lua_pop(L, 1);
lua_getfield(L, 2, "on_error404");
if (!lua_isnil(L, -1)) {
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_getglobal(L, "__uh_on_error404");
lua_pushvalue(L, -2);
lsrv->error404_ref = luaL_ref(L, -2);
lsrv->srv.on_error404 = lua_on_error404;
lua_pop(L, 1);
}
lua_getfield(L, 2, "on_request");
if (!lua_isnil(L, -1)) {
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_getglobal(L, "__uh_on_request");
lua_pushvalue(L, -2);
lsrv->request_ref = luaL_ref(L, -2);
lsrv->srv.on_request = lua_on_request;
lua_pop(L, 1);
}
return 0;
}
@ -157,8 +147,6 @@ static int lua_uh_server_free(lua_State *L)
static const luaL_Reg server_mt[] = {
{ "ssl_init", lua_uh_ssl_init },
{ "set_on_request", lua_uh_set_on_request },
{ "set_on_error404", lua_uh_set_on_error404 },
{ "set_options", lua_uh_set_options },
{ "free", lua_uh_server_free },
{ NULL, NULL }