Lua binding: Optimize code
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>main
parent
ddf6e8dd86
commit
deaec21076
|
@ -25,6 +25,24 @@ local uh = require "uhttpd"
|
||||||
local verbose = true
|
local verbose = true
|
||||||
local port = 8914
|
local port = 8914
|
||||||
|
|
||||||
|
uloop.init()
|
||||||
|
|
||||||
|
-- LOG_DEBUG LOG_INFO LOG_ERR
|
||||||
|
if not verbose then
|
||||||
|
uh.set_log_threshold(uh.LOG_ERR)
|
||||||
|
end
|
||||||
|
|
||||||
|
uh.log(uh.LOG_INFO, "uhttpd version:" .. uh.VERSION)
|
||||||
|
|
||||||
|
local srv = uh.new(port)
|
||||||
|
|
||||||
|
-- srv:ssl_init("uhttpd.crt", "uhttpd.key")
|
||||||
|
-- srv:set_options({docroot = "/home/zjh/www", index = "lua.html"})
|
||||||
|
|
||||||
|
uh.log(uh.LOG_INFO, "Listen on:" .. port)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local http_methods = {
|
local http_methods = {
|
||||||
[uh.HTTP_METHOD_GET] = "GET",
|
[uh.HTTP_METHOD_GET] = "GET",
|
||||||
[uh.HTTP_METHOD_POST] = "POST",
|
[uh.HTTP_METHOD_POST] = "POST",
|
||||||
|
@ -36,14 +54,15 @@ local http_version = {
|
||||||
[uh.HTTP_VER_11] = "HTTP/1.1"
|
[uh.HTTP_VER_11] = "HTTP/1.1"
|
||||||
}
|
}
|
||||||
|
|
||||||
local function on_error404(cl, path)
|
srv:on_error404(function(cl, path)
|
||||||
uh.send_header(cl, 200, "OK", -1)
|
uh.send_header(cl, 200, "OK", -1)
|
||||||
uh.header_end(cl)
|
uh.header_end(cl)
|
||||||
uh.chunk_send(cl, string.format("<h1>Libuhttpd-Lua: '%s' Not found</h1>", path))
|
uh.chunk_send(cl, string.format("<h1>Libuhttpd-Lua: '%s' Not found</h1>", path))
|
||||||
uh.request_done(cl)
|
uh.request_done(cl)
|
||||||
end
|
end)
|
||||||
|
|
||||||
local function on_request(cl, path)
|
|
||||||
|
srv:on_request(function(cl, path)
|
||||||
if path ~= "/hello" then
|
if path ~= "/hello" then
|
||||||
return uh.REQUEST_CONTINUE
|
return uh.REQUEST_CONTINUE
|
||||||
end
|
end
|
||||||
|
@ -85,29 +104,7 @@ local function on_request(cl, path)
|
||||||
uh.request_done(cl)
|
uh.request_done(cl)
|
||||||
|
|
||||||
return uh.REQUEST_DONE
|
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()
|
uloop.run()
|
||||||
|
|
|
@ -113,25 +113,35 @@ static int lua_uh_set_options(lua_State *L)
|
||||||
srv->set_index_file(srv, lua_tostring(L, -1));
|
srv->set_index_file(srv, lua_tostring(L, -1));
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
|
|
||||||
lua_getfield(L, 2, "on_error404");
|
return 0;
|
||||||
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");
|
static int lua_uh_set_error404_cb(lua_State *L)
|
||||||
if (!lua_isnil(L, -1)) {
|
{
|
||||||
luaL_checktype(L, -1, LUA_TFUNCTION);
|
struct lua_uh_server *lsrv = luaL_checkudata(L, 1, LUA_UH_SERVER_MT);
|
||||||
lua_getglobal(L, "__uh_on_request");
|
|
||||||
lua_pushvalue(L, -2);
|
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||||
lsrv->request_ref = luaL_ref(L, -2);
|
lua_getglobal(L, "__uh_on_error404");
|
||||||
lsrv->srv.on_request = lua_on_request;
|
lua_pushvalue(L, -2);
|
||||||
lua_pop(L, 1);
|
lsrv->error404_ref = luaL_ref(L, -2);
|
||||||
}
|
lua_pop(L, 1);
|
||||||
|
|
||||||
|
lsrv->srv.on_error404 = lua_on_error404;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int lua_uh_set_request_cb(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);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
|
||||||
|
lsrv->srv.on_request = lua_on_request;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -148,6 +158,8 @@ static int lua_uh_server_free(lua_State *L)
|
||||||
static const luaL_Reg server_mt[] = {
|
static const luaL_Reg server_mt[] = {
|
||||||
{ "ssl_init", lua_uh_ssl_init },
|
{ "ssl_init", lua_uh_ssl_init },
|
||||||
{ "set_options", lua_uh_set_options },
|
{ "set_options", lua_uh_set_options },
|
||||||
|
{ "on_error404", lua_uh_set_error404_cb },
|
||||||
|
{ "on_request", lua_uh_set_request_cb },
|
||||||
{ "free", lua_uh_server_free },
|
{ "free", lua_uh_server_free },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue