From 06b2ec0e8f5487d23809be749d3780f0fcfe80e1 Mon Sep 17 00:00:00 2001 From: lxsang Date: Fri, 5 Oct 2018 19:02:35 +0200 Subject: [PATCH] using new plugin interface --- APIs/api.lua | 20 +++++++++++--------- APIs/std.lua | 44 ++++++++++++++++++++++---------------------- lua-api.c | 35 +++++++++++++---------------------- 3 files changed, 46 insertions(+), 53 deletions(-) diff --git a/APIs/api.lua b/APIs/api.lua index 9d46f87..3db39b8 100644 --- a/APIs/api.lua +++ b/APIs/api.lua @@ -3,22 +3,24 @@ __ROOT__ = __api__.root -- set require path package.path = __ROOT__ .. '/?.lua;'..__api__.apiroot..'/?.lua' package.cpath = __api__.apiroot..'/?.llib' +require("std") +require("utils") +require("extra_mime") -- set session SESSION = {} -if REQUEST.query ~= nil and REQUEST.query.cookie ~= nil then - SESSION = REQUEST.query.cookie + +REQUEST = HTTP_REQUEST.request.REQUEST_DATA +REQUEST.method = HTTP_REQUEST.request.METHOD +if HTTP_REQUEST.request.COOKIE then + SESSION = HTTP_REQUEST.request.COOKIE end -HEADER = REQUEST.query.__xheader__ +HEADER = HTTP_REQUEST.request.REQUEST_HEADER HEADER.mobile = false if HEADER["User-Agent"] and HEADER["User-Agent"]:match("Mobi") then HEADER.mobile = true end -require("std") -require("utils") -require("extra_mime") - function has_module(m) if utils.file_exists(__ROOT__..'/'..m) then if m:find("%.ls$") then @@ -128,7 +130,7 @@ end -- run the file -local m, s, p = has_module(REQUEST.path) +local m, s, p = has_module(HTTP_REQUEST.request.RESOURCE_PATH) if m then -- run the correct module if s then @@ -138,7 +140,7 @@ if m then require(p) end else - unknow("Resource not found for request "..REQUEST.path) + unknow("Resource not found for request "..HTTP_REQUEST.request.RESOURCE_PATH) end diff --git a/APIs/std.lua b/APIs/std.lua index 5deaf5d..4c8aaad 100644 --- a/APIs/std.lua +++ b/APIs/std.lua @@ -2,13 +2,13 @@ std = modules.std() bytes = modules.bytes() array = modules.array() function std.html() - std._html(REQUEST.id) + std._html(HTTP_REQUEST.id) end function std.text() - std._text(REQUEST.id) + std._text(HTTP_REQUEST.id) end function std.status(code, msg) - std._status(REQUEST.id, code, msg) + std._status(HTTP_REQUEST.id, code, msg) end function std.custom_header(k,v) --print(k..":"..v) @@ -19,38 +19,38 @@ function std.header_flush() end --_redirect function std.redirect(s) - std._redirect(REQUEST.id,s) + std._redirect(HTTP_REQUEST.id,s) end function std.json() - std._json(REQUEST.id) + std._json(HTTP_REQUEST.id) end function std.jpeg() - std._jpeg(REQUEST.id) + std._jpeg(HTTP_REQUEST.id) end function std.header(s) - std._header(REQUEST.id,s) + std._header(HTTP_REQUEST.id,s) end function std.octstream(s) - std._octstream(REQUEST.id,s) + std._octstream(HTTP_REQUEST.id,s) end function std.textstream() - std._textstream(REQUEST.id) + std._textstream(HTTP_REQUEST.id) end function std.ti(v) - std._ti(REQUEST.id,v) + std._ti(HTTP_REQUEST.id,v) end function std.t(s) - std._t(REQUEST.id,s) + std._t(HTTP_REQUEST.id,s) end function std.f(v) - std._f(REQUEST.id,v) + std._f(HTTP_REQUEST.id,v) end function std.fb(v) - std._fb(REQUEST.id,v) + std._fb(HTTP_REQUEST.id,v) end function std.setCookie(t,v,p) p = p or "" - std._setCookie(REQUEST.id,t,v,p) + std._setCookie(HTTP_REQUEST.id,t,v,p) end function std.cjson(v, p) @@ -65,7 +65,7 @@ end --_upload --_route function std.unknow(s) - std._unknow(REQUEST.id,s) + std._unknow(HTTP_REQUEST.id,s) end function std.readOnly(t) -- bugging @@ -84,7 +84,7 @@ function std.readOnly(t) -- bugging -- web socket std.ws = {} function std.ws.header() - local h = std.ws_header(REQUEST.id) + local h = std.ws_header(HTTP_REQUEST.id) if(h) then return h --std.readOnly(h) else @@ -93,22 +93,22 @@ function std.ws.header() end function std.ws.read(h) - return std.ws_read(REQUEST.id,h) + return std.ws_read(HTTP_REQUEST.id,h) end function std.ws.swrite(s) - std.ws_t(REQUEST.id,s) + std.ws_t(HTTP_REQUEST.id,s) end function std.ws.fwrite(s) - std.ws_f(REQUEST.id,s) + std.ws_f(HTTP_REQUEST.id,s) end function std.ws.write_bytes(arr) - std.ws_b(REQUEST.id,arr) + std.ws_b(HTTP_REQUEST.id,arr) end function std.ws.enable() - return REQUEST.query ~= nil and REQUEST.query["__web_socket__"] == "1" + return HTTP_REQUEST.query ~= nil and HTTP_REQUEST.query["__web_socket__"] == "1" end function std.ws.close(code) - std.ws_close(REQUEST.id,code) + std.ws_close(HTTP_REQUEST.id,code) end function std.basename(str) local name = string.gsub(std.trim(str,"/"), "(.*/)(.*)", "%2") diff --git a/lua-api.c b/lua-api.c index a47e184..fe29ad0 100644 --- a/lua-api.c +++ b/lua-api.c @@ -13,9 +13,6 @@ static const struct luaL_Reg modules [] = { void init() { - //signal(SIGPIPE, SIG_IGN); - //signal(SIGABRT, SIG_IGN); - // init the plugin here LOG("%s \n","INIT LUA HANDLER"); } /** @@ -33,7 +30,7 @@ static void push_dict_to_lua(lua_State* L, dictionary d) { lua_pushstring(L,as->key); //printf("KEY %s\n", as->key); - if(EQU(as->key,"cookie") || EQU(as->key,"__xheader__")) + if(EQU(as->key,"COOKIE") || EQU(as->key,"REQUEST_HEADER") || EQU(as->key,"REQUEST_DATA") ) push_dict_to_lua(L, (dictionary)as->value); else { @@ -43,8 +40,10 @@ static void push_dict_to_lua(lua_State* L, dictionary d) lua_settable(L, -3); } } -void handle(void* client, const char* method, const char* path, dictionary rq) +void* handle(void* data) { + antd_request_t* rq = (antd_request_t*) data; + plugin_header_t* __plugin__ = meta(); lua_State* L = NULL; //char * index = __s("%s/%s",__plugin__.htdocs,"router.lua"); char* cnf = config_dir(); @@ -58,11 +57,11 @@ void handle(void* client, const char* method, const char* path, dictionary rq) // API header lua_newtable(L); lua_pushstring(L,"name"); - lua_pushstring(L, __plugin__.name); + lua_pushstring(L, __plugin__->name); lua_settable(L,-3); lua_pushstring(L,"root"); - lua_pushstring(L, __plugin__.htdocs); + lua_pushstring(L, __plugin__->htdocs); lua_settable(L,-3); lua_pushstring(L,"apiroot"); @@ -74,22 +73,13 @@ void handle(void* client, const char* method, const char* path, dictionary rq) // Request lua_newtable(L); lua_pushstring(L,"id"); - lua_pushlightuserdata(L, client); + lua_pushlightuserdata(L, rq->client); //lua_pushnumber(L,client); lua_settable(L, -3); - - lua_pushstring(L,"method"); - lua_pushstring(L,method); + lua_pushstring(L,"request"); + push_dict_to_lua(L,rq->request); lua_settable(L, -3); - - lua_pushstring(L,"path"); - lua_pushstring(L,path); - lua_settable(L, -3); - - lua_pushstring(L,"query"); - push_dict_to_lua(L,rq); - lua_settable(L, -3); - lua_setglobal(L, "REQUEST"); + lua_setglobal(L, "HTTP_REQUEST"); // load major apis if(is_file(apis)) @@ -111,9 +101,10 @@ void handle(void* client, const char* method, const char* path, dictionary rq) free(cnf); if(apis) free(apis); + return antd_create_task(NULL, (void*)rq, NULL); //lua_close(L); } -void pexit() +void destroy() { - LOG("%s \n","Exit LUA Handler"); + LOG("%s \n","Exit LUA Handle"); }