parent
f03acc8b5c
commit
69d7e91fb6
|
@ -24,21 +24,25 @@
|
||||||
|
|
||||||
static void *uh_create_userdata(lua_State *L, size_t size, const luaL_Reg *reg, lua_CFunction gc)
|
static void *uh_create_userdata(lua_State *L, size_t size, const luaL_Reg *reg, lua_CFunction gc)
|
||||||
{
|
{
|
||||||
void *ret = lua_newuserdata(L, size);
|
void *obj = lua_newuserdata(L, size);
|
||||||
|
|
||||||
memset(ret, 0, size);
|
memset(obj, 0, size);
|
||||||
lua_createtable(L, 0, 2);
|
|
||||||
lua_pushvalue(L, -1);
|
|
||||||
lua_setfield(L, -2, "__index");
|
|
||||||
lua_pushcfunction(L, gc);
|
|
||||||
lua_setfield(L, -2, "__gc");
|
|
||||||
lua_pushvalue(L, -1);
|
|
||||||
lua_setmetatable(L, -3);
|
|
||||||
lua_pushvalue(L, -2);
|
|
||||||
luaI_openlib(L, NULL, reg, 1);
|
|
||||||
lua_pushvalue(L, -2);
|
|
||||||
|
|
||||||
return ret;
|
/* creare metatable */
|
||||||
|
lua_newtable(L);
|
||||||
|
|
||||||
|
/* metatable.__index = metatable */
|
||||||
|
lua_pushvalue(L, -1);
|
||||||
|
lua_setfield(L, -2, "__index");
|
||||||
|
|
||||||
|
lua_pushcfunction(L, gc);
|
||||||
|
lua_setfield(L, -2, "__gc");
|
||||||
|
|
||||||
|
luaL_setfuncs(L, reg, 0);
|
||||||
|
|
||||||
|
lua_setmetatable(L, -2);
|
||||||
|
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void add_all_var(struct uh_client *cl, lua_State *L)
|
static inline void add_all_var(struct uh_client *cl, lua_State *L)
|
||||||
|
@ -82,8 +86,8 @@ static inline void add_body(struct uh_client *cl, lua_State *L)
|
||||||
|
|
||||||
static void lua_uh_action(struct uh_client *cl)
|
static void lua_uh_action(struct uh_client *cl)
|
||||||
{
|
{
|
||||||
struct uh_server *srv = cl->srv;
|
struct uh_server *srv = cl->srv;
|
||||||
lua_State *L = srv->L;
|
lua_State *L = srv->L;
|
||||||
|
|
||||||
lua_getglobal(L, "__uh_action_cb");
|
lua_getglobal(L, "__uh_action_cb");
|
||||||
lua_getfield(L, -1, cl->get_path(cl));
|
lua_getfield(L, -1, cl->get_path(cl));
|
||||||
|
@ -120,7 +124,7 @@ static void lua_uh_action(struct uh_client *cl)
|
||||||
|
|
||||||
static int lua_uh_ssl_init(lua_State *L)
|
static int lua_uh_ssl_init(lua_State *L)
|
||||||
{
|
{
|
||||||
struct lua_uh_server *lsrv = lua_touserdata(L, 1);
|
struct lua_uh_server *lsrv = lua_touserdata(L, 1);
|
||||||
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);
|
||||||
|
|
||||||
|
@ -131,9 +135,9 @@ 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 = lua_touserdata(L, 1);
|
||||||
struct uh_server *srv = lsrv->srv;
|
struct uh_server *srv = lsrv->srv;
|
||||||
const char *path = lua_tostring(L, -2);
|
const char *path = lua_tostring(L, -2);
|
||||||
|
|
||||||
if (!srv) {
|
if (!srv) {
|
||||||
lua_pushstring(L, "Not initialized");
|
lua_pushstring(L, "Not initialized");
|
||||||
|
@ -141,48 +145,48 @@ static int lua_uh_add_action(lua_State *L)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!path || !path[0] || !lua_isfunction(L, -1)) {
|
if (!path || !path[0] || !lua_isfunction(L, -1)) {
|
||||||
lua_pushstring(L, "invalid arg list");
|
lua_pushstring(L, "invalid arg list");
|
||||||
lua_error(L);
|
lua_error(L);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
srv->add_action(srv, path, lua_uh_action);
|
srv->add_action(srv, path, lua_uh_action);
|
||||||
|
|
||||||
lua_getglobal(L, "__uh_action_cb");
|
lua_getglobal(L, "__uh_action_cb");
|
||||||
lua_pushvalue(L, -2);
|
lua_pushvalue(L, -2);
|
||||||
lua_setfield(L, -2, path);
|
lua_setfield(L, -2, path);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = lua_touserdata(L, 1);
|
||||||
|
|
||||||
if (lsrv->srv) {
|
if (lsrv->srv) {
|
||||||
lsrv->srv->free(lsrv->srv);
|
lsrv->srv->free(lsrv->srv);
|
||||||
lsrv->srv = NULL;
|
lsrv->srv = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const luaL_Reg server_mt[] = {
|
static const luaL_Reg server_mt[] = {
|
||||||
{ "ssl_init", lua_uh_ssl_init },
|
{ "ssl_init", lua_uh_ssl_init },
|
||||||
{ "add_action", lua_uh_add_action },
|
{ "add_action", lua_uh_add_action },
|
||||||
{ "free", lua_uh_server_free },
|
{ "free", lua_uh_server_free },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
static int lua_uh_new(lua_State *L)
|
static int lua_uh_new(lua_State *L)
|
||||||
{
|
{
|
||||||
int port = lua_tointeger(L, -1);
|
int port = lua_tointeger(L, -1);
|
||||||
const char *address = lua_tostring(L, -2);
|
const char *address = lua_tostring(L, -2);
|
||||||
struct lua_uh_server *lsrv;
|
struct lua_uh_server *lsrv;
|
||||||
struct uh_server *srv;
|
struct uh_server *srv;
|
||||||
|
|
||||||
srv = uh_server_new(address, port);
|
srv = uh_server_new(address, port);
|
||||||
if (!srv) {
|
if (!srv) {
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
lua_pushstring(L, "Bind failed");
|
lua_pushstring(L, "Bind failed");
|
||||||
|
@ -193,7 +197,7 @@ static int lua_uh_new(lua_State *L)
|
||||||
lsrv->srv = srv;
|
lsrv->srv = srv;
|
||||||
lsrv->srv->L = L;
|
lsrv->srv->L = L;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lua_uh_send_header(lua_State *L)
|
static int lua_uh_send_header(lua_State *L)
|
||||||
|
@ -264,23 +268,23 @@ static int lua_uh_request_done(lua_State *L)
|
||||||
}
|
}
|
||||||
|
|
||||||
static const luaL_Reg uhttpd_fun[] = {
|
static const luaL_Reg uhttpd_fun[] = {
|
||||||
{"new", lua_uh_new},
|
{"new", lua_uh_new},
|
||||||
{"send_header", lua_uh_send_header},
|
{"send_header", lua_uh_send_header},
|
||||||
{"append_header", lua_uh_append_header},
|
{"append_header", lua_uh_append_header},
|
||||||
{"header_end", lua_uh_header_end},
|
{"header_end", lua_uh_header_end},
|
||||||
{"send", lua_uh_send},
|
{"send", lua_uh_send},
|
||||||
{"chunk_send", lua_uh_chunk_send},
|
{"chunk_send", lua_uh_chunk_send},
|
||||||
{"request_done", lua_uh_request_done},
|
{"request_done", lua_uh_request_done},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
int luaopen_uhttpd(lua_State *L)
|
int luaopen_uhttpd(lua_State *L)
|
||||||
{
|
{
|
||||||
lua_createtable(L, 1, 0);
|
lua_newtable(L);
|
||||||
lua_setglobal(L, "__uh_action_cb");
|
lua_setglobal(L, "__uh_action_cb");
|
||||||
|
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
luaL_setfuncs(L, uhttpd_fun, 0);
|
luaL_setfuncs(L, uhttpd_fun, 0);
|
||||||
|
|
||||||
lua_pushstring(L, UHTTPD_VERSION_STRING);
|
lua_pushstring(L, UHTTPD_VERSION_STRING);
|
||||||
lua_setfield(L, -2, "VERSION");
|
lua_setfield(L, -2, "VERSION");
|
||||||
|
|
|
@ -26,18 +26,13 @@
|
||||||
/* Compatibility defines */
|
/* Compatibility defines */
|
||||||
#if LUA_VERSION_NUM <= 501
|
#if LUA_VERSION_NUM <= 501
|
||||||
|
|
||||||
#define lua_setuservalue(L, i) lua_setfenv((L), (i))
|
|
||||||
#define lua_getuservalue(L, i) lua_getfenv((L), (i))
|
|
||||||
|
|
||||||
/* NOTE: this only works if nups == 0! */
|
/* NOTE: this only works if nups == 0! */
|
||||||
#define luaL_setfuncs(L, fns, nups) luaL_register((L), NULL, (fns))
|
#define luaL_setfuncs(L, fns, nups) luaL_register((L), NULL, (fns))
|
||||||
|
|
||||||
#define lua_rawlen(L, i) lua_objlen((L), (i))
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct lua_uh_server {
|
struct lua_uh_server {
|
||||||
struct uh_server *srv;
|
struct uh_server *srv;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue