Lua binding: Make the code more robust
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>main
parent
8f8a29adc5
commit
a3d1c70b8c
|
@ -28,8 +28,7 @@ static void *uh_create_userdata(lua_State *L, size_t size, const luaL_Reg *reg,
|
||||||
|
|
||||||
memset(obj, 0, size);
|
memset(obj, 0, size);
|
||||||
|
|
||||||
/* creare metatable */
|
luaL_newmetatable(L, LUA_UH_SERVER_MT);
|
||||||
lua_newtable(L);
|
|
||||||
|
|
||||||
/* metatable.__index = metatable */
|
/* metatable.__index = metatable */
|
||||||
lua_pushvalue(L, -1);
|
lua_pushvalue(L, -1);
|
||||||
|
@ -133,7 +132,7 @@ static int lua_uh_ssl_init(lua_State *L)
|
||||||
lua_pushstring(L, "SSL is not compiled in");
|
lua_pushstring(L, "SSL is not compiled in");
|
||||||
lua_error(L);
|
lua_error(L);
|
||||||
#else
|
#else
|
||||||
struct lua_uh_server *lsrv = lua_touserdata(L, 1);
|
struct lua_uh_server *lsrv = luaL_checkudata(L, 1, LUA_UH_SERVER_MT);
|
||||||
const char *cert = lua_tostring(L, 2);
|
const char *cert = lua_tostring(L, 2);
|
||||||
const char *key = lua_tostring(L, 3);
|
const char *key = lua_tostring(L, 3);
|
||||||
|
|
||||||
|
@ -148,7 +147,7 @@ static int lua_uh_ssl_init(lua_State *L)
|
||||||
|
|
||||||
static int lua_uh_add_action(lua_State *L)
|
static int lua_uh_add_action(lua_State *L)
|
||||||
{
|
{
|
||||||
struct lua_uh_server *lsrv = lua_touserdata(L, 1);
|
struct lua_uh_server *lsrv = luaL_checkudata(L, 1, LUA_UH_SERVER_MT);
|
||||||
const char *path = lua_tostring(L, -2);
|
const char *path = lua_tostring(L, -2);
|
||||||
|
|
||||||
if (!path || !path[0] || !lua_isfunction(L, -1)) {
|
if (!path || !path[0] || !lua_isfunction(L, -1)) {
|
||||||
|
@ -182,7 +181,7 @@ static void http_callback_404(struct uh_client *cl)
|
||||||
|
|
||||||
static int lua_uh_set_error404_cb(lua_State *L)
|
static int lua_uh_set_error404_cb(lua_State *L)
|
||||||
{
|
{
|
||||||
struct lua_uh_server *lsrv = lua_touserdata(L, 1);
|
struct lua_uh_server *lsrv = luaL_checkudata(L, 1, LUA_UH_SERVER_MT);
|
||||||
|
|
||||||
if (!lua_isfunction(L, 2)) {
|
if (!lua_isfunction(L, 2)) {
|
||||||
lua_pushstring(L, "invalid arg list");
|
lua_pushstring(L, "invalid arg list");
|
||||||
|
@ -201,7 +200,7 @@ static int lua_uh_set_error404_cb(lua_State *L)
|
||||||
|
|
||||||
static int lua_uh_set_options(lua_State *L)
|
static int lua_uh_set_options(lua_State *L)
|
||||||
{
|
{
|
||||||
struct lua_uh_server *lsrv = lua_touserdata(L, 1);
|
struct lua_uh_server *lsrv = luaL_checkudata(L, 1, LUA_UH_SERVER_MT);
|
||||||
struct uh_server *srv = &lsrv->srv;
|
struct uh_server *srv = &lsrv->srv;
|
||||||
|
|
||||||
luaL_checktype(L, 2, LUA_TTABLE);
|
luaL_checktype(L, 2, LUA_TTABLE);
|
||||||
|
@ -221,7 +220,7 @@ static int lua_uh_set_options(lua_State *L)
|
||||||
|
|
||||||
static int lua_uh_server_free(lua_State *L)
|
static int lua_uh_server_free(lua_State *L)
|
||||||
{
|
{
|
||||||
struct lua_uh_server *lsrv = lua_touserdata(L, 1);
|
struct lua_uh_server *lsrv = luaL_checkudata(L, 1, LUA_UH_SERVER_MT);
|
||||||
|
|
||||||
lsrv->srv.free(&lsrv->srv);
|
lsrv->srv.free(&lsrv->srv);
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,8 @@
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define LUA_UH_SERVER_MT "libuhttpd"
|
||||||
|
|
||||||
struct lua_uh_server {
|
struct lua_uh_server {
|
||||||
struct uh_server srv;
|
struct uh_server srv;
|
||||||
int error404_cb_ref;
|
int error404_cb_ref;
|
||||||
|
|
Loading…
Reference in New Issue