mirror of
https://github.com/lxsang/antd-lua-plugin
synced 2024-12-26 17:38:21 +01:00
lua plugin now used a separated process to handle requests
This commit is contained in:
parent
7814611008
commit
c7ea2af3f1
133
APIs/api.lua
133
APIs/api.lua
@ -1,32 +1,23 @@
|
||||
|
||||
package.cpath = __api__.apiroot..'/?.so'
|
||||
require("antd")
|
||||
|
||||
local readline = function()
|
||||
local s = ""
|
||||
repeat
|
||||
local c = modules.std().antd_recv(HTTP_REQUEST.id,1)
|
||||
if c ~= 0 and c ~= 10 then
|
||||
s = s..utf8.char(c)
|
||||
end
|
||||
until(c == 0 or c == 10)
|
||||
return s
|
||||
end
|
||||
std = modules.std()
|
||||
local read_header =function()
|
||||
local l
|
||||
repeat
|
||||
local l = readline()
|
||||
if l ~= '\r' then
|
||||
l = std.antd_recv(HTTP_REQUEST.id)
|
||||
if l and l ~= '\r' then
|
||||
if l == "HTTP_REQUEST" or l == "request" or l == "COOKIE" or l == "REQUEST_HEADER" or l == "REQUEST_DATA" then
|
||||
coroutine.yield(l, "LUA_TABLE")
|
||||
else
|
||||
local l1 = readline()
|
||||
local l1 = std.antd_recv(HTTP_REQUEST.id)
|
||||
if l1 ~= '\r' then
|
||||
coroutine.yield(l, l1)
|
||||
end
|
||||
l = l1
|
||||
end
|
||||
end
|
||||
until l == '\r'
|
||||
until not l or l == '\r'
|
||||
end
|
||||
|
||||
|
||||
@ -85,11 +76,11 @@ if HEADER["User-Agent"] and HEADER["User-Agent"]:match("Mobi") then
|
||||
end
|
||||
|
||||
function LOG_INFO(fmt,...)
|
||||
ulib.syslog(5,string.format(fmt, table.unpack({...})))
|
||||
ulib.syslog(5,string.format(fmt or "LOG", table.unpack({...}) or ""))
|
||||
end
|
||||
|
||||
function LOG_ERROR(fmt,...)
|
||||
ulib.syslog(3,string.format(fmt, table.unpack({...})))
|
||||
ulib.syslog(3,string.format(fmt or "ERROR", table.unpack({...}) or ""))
|
||||
end
|
||||
|
||||
function has_module(m)
|
||||
@ -190,88 +181,13 @@ function loadscript(file, args)
|
||||
end
|
||||
end
|
||||
|
||||
local decode_post_data = function(ctype, clen, is_url)
|
||||
local raw_data,size = std.antd_recv(HTTP_REQUEST.id, clen)
|
||||
if not raw_data or size ~= clen then
|
||||
LOG_ERROR("Unable to read request data: received %d bytes expected %d bytes", size, clen)
|
||||
return 400, "Unable to read request data"
|
||||
end
|
||||
if is_url then
|
||||
local str = tostring(raw)
|
||||
local arr = explode(str, "&")
|
||||
LOG_INFO("encoded POST URI: %s", str)
|
||||
for i,v in ipairs(arr) do
|
||||
local assoc = explode(v,"=")
|
||||
if #assoc == 2 then
|
||||
REQUEST[assoc[1]] = untils.decodeURI(assoc[2])
|
||||
else
|
||||
REQUEST[assoc[1]] = ""
|
||||
end
|
||||
end
|
||||
else
|
||||
local key = ctype:gsub("^[^/]*", "")
|
||||
REQUEST[key] = raw_data
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
local decode_multi_part = function(ctype, clen)
|
||||
--[[
|
||||
local arr = explode(ctype, "=")
|
||||
if #arr ~= 2 then
|
||||
LOG_ERROR("Unable to parsed boundary for: %s", ctype)
|
||||
return 400, "Multipart Boundary not found"
|
||||
end
|
||||
local boundary = std.trim(arr[2]," ")
|
||||
local boundary_end = boundary.."--"
|
||||
LOG_INFO("Boundary found: %s", boundary)
|
||||
local line = nil
|
||||
repeat
|
||||
line = readline()
|
||||
until not line or line:find(boundary) or line == ""
|
||||
if not line or line == "" then
|
||||
LOG_ERROR("Cannot find first match for boundary %s", boundary)
|
||||
return 400, "Unable to decode data based on content boundary"
|
||||
end
|
||||
repeat
|
||||
line = readline()
|
||||
until not line or line:find("Content-Disposition:") or line == ""
|
||||
if not line or line == "" then
|
||||
LOG_ERROR("Content-Disposition meta data not fond")
|
||||
return 400, "Unable to query Content-Disposition from request"
|
||||
end
|
||||
line = line:gsub("Content-Disposition:",""):gsub("\r\n","")
|
||||
-- extract parameters from header
|
||||
arr = explode(line,";")
|
||||
local part_name, part_file = nil, nil
|
||||
for i,v in ipairs(arr) do
|
||||
LOG_INFO('Decoding: %s', v)
|
||||
local assoc = explode(v, "=")
|
||||
local key = std.trim(assoc[1])
|
||||
local val = assoc[1]
|
||||
if val then
|
||||
val = std.trim(val, " ")
|
||||
if key == "name" then
|
||||
LOG_INFO("Part name: %s", val)
|
||||
part_name = val
|
||||
end
|
||||
if key == "filename" then
|
||||
LOG_INFO("Part file: %s", val)
|
||||
part_file = val
|
||||
end
|
||||
end
|
||||
end
|
||||
-- TODO: to be continue
|
||||
]]
|
||||
return 0
|
||||
end
|
||||
-- decode post data if any
|
||||
local decode_request = function()
|
||||
LOG_INFO("Request method %s", REQUEST.method)
|
||||
local decode_request_data = function()
|
||||
if (not REQUEST.method)
|
||||
or (REQUEST.method ~= "POST"
|
||||
and REQUEST.method ~= "PUT"
|
||||
and REQUEST.method ~= "PATCH") then
|
||||
and REQUEST.method ~= "PATCH")
|
||||
or (not REQUEST.HAS_RAW_BODY) then
|
||||
return 0
|
||||
end
|
||||
local ctype = HEADER['Content-Type']
|
||||
@ -283,16 +199,31 @@ local decode_request = function()
|
||||
LOG_ERROR("Invalid content type %s or content length %d", ctype, clen)
|
||||
return 400, "Bad Request, missing content description"
|
||||
end
|
||||
if ctype == "application/x-www-form-urlencoded" then
|
||||
return decode_post_data(ctype, clen, true)
|
||||
elseif ctype == "multipart/form-data" then
|
||||
return decode_multi_part(ctype, clen)
|
||||
local raw_data, len = std.antd_recv(HTTP_REQUEST.id, clen)
|
||||
if len ~= clen then
|
||||
LOG_ERROR("Unable to read all data: read %d expected %d", len, clen)
|
||||
return 400, "Bad Request, missing content data"
|
||||
end
|
||||
if ctype:find("application/json") then
|
||||
REQUEST.json = bytes.__tostring(raw_data)
|
||||
else
|
||||
return decode_post_data(ctype, clen, false)
|
||||
REQUEST[ctype] = raw_data
|
||||
end
|
||||
REQUEST.HAS_RAW_BODY = nil
|
||||
return 0
|
||||
end
|
||||
|
||||
-- set compression level
|
||||
local accept_encoding = HEADER["Accept-Encoding"]
|
||||
if accept_encoding then
|
||||
if accept_encoding:find("gzip") then
|
||||
std.antd_set_zlevel(HTTP_REQUEST.id, "gzip")
|
||||
elseif accept_encoding:find("deflate") then
|
||||
std.antd_set_zlevel(HTTP_REQUEST.id, "deflate")
|
||||
end
|
||||
end
|
||||
|
||||
local code, error = decode_request()
|
||||
local code, error = decode_request_data()
|
||||
|
||||
if code ~= 0 then
|
||||
LOG_ERROR(error)
|
||||
|
@ -1,5 +1,5 @@
|
||||
function std.extra_mime(name)
|
||||
local ext = std.ext(name);
|
||||
local ext = std.ext(name)
|
||||
local mpath = __ROOT__.."/".."mimes.json"
|
||||
local xmimes = {}
|
||||
if utils.file_exists(mpath) then
|
||||
@ -15,7 +15,7 @@ function std.extra_mime(name)
|
||||
elseif ext == "cpp" or ext == "hpp" then return "text/cpp",false
|
||||
elseif ext == "md" then return "text/markdown",false
|
||||
elseif ext == "lua" then return "text/lua",false
|
||||
elseif ext == "yaml" then return "application/x-yaml", false
|
||||
elseif ext == "yml" then return "application/x-yaml", false
|
||||
elseif xmimes[ext] then return xmimes[ext].mime, xmimes[ext].binary
|
||||
--elseif ext == "pgm" then return "image/x-portable-graymap", true
|
||||
else
|
||||
|
@ -1,4 +1,3 @@
|
||||
std = modules.std()
|
||||
bytes = modules.bytes()
|
||||
array = modules.array()
|
||||
|
||||
@ -61,7 +60,8 @@ function std.b(s)
|
||||
std._b(HTTP_REQUEST.id,s)
|
||||
end
|
||||
function std.f(v)
|
||||
ulib.send_file(v, HTTP_REQUEST.socket)
|
||||
std._f(HTTP_REQUEST.id,v)
|
||||
--ulib.send_file(v, HTTP_REQUEST.socket)
|
||||
end
|
||||
|
||||
function std.setCookie(v)
|
||||
|
602
lib/asl/antd.c
602
lib/asl/antd.c
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,4 @@
|
||||
#include <antd/plugin.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h> /* See NOTES */
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <errno.h>
|
||||
@ -8,6 +6,8 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include "../lualib.h"
|
||||
|
||||
typedef struct {
|
||||
@ -17,16 +17,16 @@ typedef struct {
|
||||
|
||||
void* lua_handle(void* ptr)
|
||||
{
|
||||
lua_thread_data_t* data = (lua_thread_data_t**)ptr;
|
||||
lua_thread_data_t* data = (lua_thread_data_t*)ptr;
|
||||
lua_State* L = NULL;
|
||||
antd_client_t cl = {0};
|
||||
cl.sock = data->fd;
|
||||
time(&cl.last_io);
|
||||
cl.ssl = NULL;
|
||||
cl.state = ANTD_CLIENT_PLUGIN_EXEC;
|
||||
cl.z_status = 0;
|
||||
cl.z_level = ANTD_CNONE;
|
||||
cl.zstream = NULL;
|
||||
antd_client_t* cl = (antd_client_t*) malloc(sizeof(antd_client_t));
|
||||
cl->sock = data->fd;
|
||||
time(&cl->last_io);
|
||||
cl->ssl = NULL;
|
||||
cl->state = ANTD_CLIENT_PLUGIN_EXEC;
|
||||
cl->z_status = 0;
|
||||
cl->z_level = ANTD_CNONE;
|
||||
cl->zstream = NULL;
|
||||
//char * index = __s("%s/%s",__plugin__->htdocs,"router.lua");
|
||||
char* cnf = __s("%s%s%s", data->__plugin__->pdir,DIR_SEP, data->__plugin__->name);
|
||||
char * apis = __s("%s/%s",cnf,"api.lua");
|
||||
@ -64,21 +64,15 @@ void* lua_handle(void* ptr)
|
||||
// Request
|
||||
lua_newtable(L);
|
||||
lua_pushstring(L,"id");
|
||||
lua_pushlightuserdata(L, &cl);
|
||||
lua_pushlightuserdata(L, cl);
|
||||
//lua_pushnumber(L,client);
|
||||
lua_settable(L, -3);
|
||||
|
||||
lua_pushstring(L,"socket");
|
||||
lua_pushnumber(L, cl.sock);
|
||||
lua_pushnumber(L, cl->sock);
|
||||
//lua_pushnumber(L,client);
|
||||
lua_settable(L, -3);
|
||||
|
||||
int flag = 1;
|
||||
|
||||
if (setsockopt(cl.sock, IPPROTO_TCP, TCP_NODELAY, &(int){1}, sizeof(int)) == -1)
|
||||
{
|
||||
ERROR("Unable to set TCP_NODELAY on %d - setsockopt: %s", cl.sock, strerror(errno));
|
||||
}
|
||||
//lua_pushstring(L,"request");
|
||||
//push_dict_to_lua(L,rq->request);
|
||||
//lua_settable(L, -3);
|
||||
@ -88,8 +82,7 @@ void* lua_handle(void* ptr)
|
||||
if(is_file(apis))
|
||||
if (luaL_loadfile(L, apis) || lua_pcall(L, 0, 0, 0))
|
||||
{
|
||||
ERROR( "cannot start API file: [%s] %s\n", apis, lua_tostring(L, -1));
|
||||
antd_error(&cl, 503, "Internal server error");
|
||||
ERROR("cannot start API file: [%s] %s\n", apis, lua_tostring(L, -1));
|
||||
}
|
||||
|
||||
/*if (luaL_loadfile(L, index) || lua_pcall(L, 0, 0, 0))
|
||||
@ -98,7 +91,7 @@ void* lua_handle(void* ptr)
|
||||
__t(client, "Cannot run router: %s", lua_tostring(L, -1));
|
||||
}
|
||||
free(index);*/
|
||||
LOG("LUA handle exit on %d", cl.sock);
|
||||
LOG("LUA handle exit on %d", cl->sock);
|
||||
// clear request
|
||||
if(L)
|
||||
lua_close(L);
|
||||
@ -106,7 +99,7 @@ void* lua_handle(void* ptr)
|
||||
free(cnf);
|
||||
if(apis)
|
||||
free(apis);
|
||||
(void) close(cl.sock);
|
||||
(void) antd_close(cl);
|
||||
return 0;
|
||||
//lua_close(L);
|
||||
}
|
||||
|
63
lua-api.c
63
lua-api.c
@ -35,14 +35,13 @@ typedef struct {
|
||||
} lua_thread_data_t;
|
||||
|
||||
static pid_t pid = 0;
|
||||
static char sock_path[108];
|
||||
|
||||
static int open_unix_socket()
|
||||
{
|
||||
struct sockaddr_un address;
|
||||
address.sun_family = AF_UNIX;
|
||||
char path[sizeof(address.sun_path)];
|
||||
(void)snprintf(path, sizeof(address.sun_path), "%s/%s", __plugin__.tmpdir, SOCKET_NAME);
|
||||
(void) strncpy(address.sun_path, path, sizeof(address.sun_path));
|
||||
(void) strncpy(address.sun_path, sock_path, sizeof(address.sun_path));
|
||||
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if(fd == -1)
|
||||
{
|
||||
@ -54,20 +53,16 @@ static int open_unix_socket()
|
||||
ERROR( "Unable to connect to socket '%s': %s", address.sun_path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
LOG( "Socket %s is created successfully", path);
|
||||
LOG( "Socket %s is created successfully", sock_path);
|
||||
return fd;
|
||||
}
|
||||
|
||||
static int mk_socket()
|
||||
{
|
||||
struct sockaddr_un address;
|
||||
char path[sizeof(address.sun_path)];
|
||||
(void)snprintf(path, sizeof(address.sun_path), "%s/%s", __plugin__.tmpdir, SOCKET_NAME);
|
||||
address.sun_family = AF_UNIX;
|
||||
// create the socket
|
||||
(void)snprintf(path, sizeof(address.sun_path), "%s/%s", __plugin__.tmpdir, SOCKET_NAME);
|
||||
|
||||
(void)strncpy(address.sun_path, path, sizeof(address.sun_path));
|
||||
(void)strncpy(address.sun_path, sock_path, sizeof(address.sun_path));
|
||||
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (fd == -1)
|
||||
{
|
||||
@ -82,10 +77,10 @@ static int mk_socket()
|
||||
// mark the socket as passive mode
|
||||
if (listen(fd, 500) == -1)
|
||||
{
|
||||
ERROR("Unable to listen to socket: %d (%s): %s", fd, path, strerror(errno));
|
||||
ERROR("Unable to listen to socket: %d (%s): %s", fd, sock_path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
LOG("Socket %s is created successfully: %d", path, fd);
|
||||
LOG("Socket %s is created successfully: %d", sock_path, fd);
|
||||
return fd;
|
||||
}
|
||||
|
||||
@ -103,7 +98,6 @@ static void lua_serve()
|
||||
ERROR("Cannot load Lua core: %s", dlerror());
|
||||
return;
|
||||
}
|
||||
LOG("Lua core loaded");
|
||||
// now load the handle
|
||||
(void)snprintf(path, BUFFLEN, "%s/lua/handle.so", __plugin__.pdir);
|
||||
lua_handle = dlopen(path, RTLD_LAZY);
|
||||
@ -128,6 +122,19 @@ static void lua_serve()
|
||||
{
|
||||
ERROR("Unable to set reuse address on %d - setsockopt: %s", socket, strerror(errno));
|
||||
}
|
||||
LOG("LUA server online");
|
||||
/*set log level*/
|
||||
const char * enable_debug = getenv("ANTD_DEBUG");
|
||||
int log_level = LOG_ERR;
|
||||
if(enable_debug)
|
||||
{
|
||||
if(atoi(enable_debug))
|
||||
{
|
||||
LOG("LUA Debug is enabled");
|
||||
log_level = LOG_NOTICE;
|
||||
}
|
||||
}
|
||||
setlogmask(LOG_UPTO(log_level));
|
||||
while((fd = accept(socket, NULL, NULL)) > 0)
|
||||
{
|
||||
pthread_t thread;
|
||||
@ -161,14 +168,14 @@ static void lua_serve()
|
||||
|
||||
void init()
|
||||
{
|
||||
use_raw_body();
|
||||
(void)snprintf(sock_path, sizeof(sock_path), "%s/%s", __plugin__.tmpdir, SOCKET_NAME);
|
||||
LOG("Lua socket will be stored in %s", sock_path);
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
// child
|
||||
lua_serve();
|
||||
}
|
||||
// parent
|
||||
LOG("Lua module initialized");
|
||||
}
|
||||
|
||||
@ -214,7 +221,8 @@ static void *process(void *data)
|
||||
{
|
||||
case -1:
|
||||
ERROR("Error on select(): %s", strerror(errno));
|
||||
close(cl->sock);
|
||||
antd_close(cl);
|
||||
dput(rq->request, "LUA_CL_DATA", NULL);
|
||||
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
||||
case 0:
|
||||
// time out
|
||||
@ -229,19 +237,20 @@ static void *process(void *data)
|
||||
{
|
||||
while((ret = antd_recv_upto(rq->client,buff, BUFFLEN)) > 0)
|
||||
{
|
||||
LOG("Receive %d bytes from antd ", ret);
|
||||
// write data to the other side
|
||||
if(antd_send(cl,buff, ret) != ret)
|
||||
{
|
||||
ERROR("Error atnd_send(): %s", strerror(errno));
|
||||
close(cl->sock);
|
||||
ERROR("Error on atnd_send(): %s", strerror(errno));
|
||||
antd_close(cl);
|
||||
dput(rq->request, "LUA_CL_DATA", NULL);
|
||||
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
||||
}
|
||||
}
|
||||
if(ret <= 0)
|
||||
if(ret < 0)
|
||||
{
|
||||
ERROR("Error antd_recv_upto() on %d: %s",rq->client->sock, strerror(errno));
|
||||
close(cl->sock);
|
||||
LOG("antd_recv_upto() on %d: %s",rq->client->sock, strerror(errno));
|
||||
antd_close(cl);
|
||||
dput(rq->request, "LUA_CL_DATA", NULL);
|
||||
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
||||
}
|
||||
}
|
||||
@ -249,19 +258,20 @@ static void *process(void *data)
|
||||
{
|
||||
while((ret = antd_recv_upto(cl,buff, BUFFLEN)) > 0)
|
||||
{
|
||||
LOG("Receive %d bytes from LUA %d", ret, cl->sock);
|
||||
// write data to the other side
|
||||
if(antd_send(rq->client,buff, ret) != ret)
|
||||
{
|
||||
ERROR("Error atnd_send(): %s", strerror(errno));
|
||||
close(cl->sock);
|
||||
antd_close(cl);
|
||||
dput(rq->request, "LUA_CL_DATA", NULL);
|
||||
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
||||
}
|
||||
}
|
||||
if(ret < 0)
|
||||
{
|
||||
ERROR("Error antd_recv_upto() on %d: %s", cl->sock, strerror(errno));
|
||||
close(cl->sock);
|
||||
LOG("antd_recv_upto() on %d: %s", cl->sock, strerror(errno));
|
||||
antd_close(cl);
|
||||
dput(rq->request, "LUA_CL_DATA", NULL);
|
||||
return antd_create_task(NULL, data, NULL, rq->client->last_io);
|
||||
}
|
||||
}
|
||||
@ -294,6 +304,7 @@ void* handle(void* data)
|
||||
cl->z_status = 0;
|
||||
cl->z_level = ANTD_CNONE;
|
||||
cl->zstream = NULL;
|
||||
rq->client->z_level = ANTD_CNONE;
|
||||
push_dict_to_socket(cl, "request","HTTP_REQUEST", rq->request);
|
||||
antd_send(cl,"\r\n", 2);
|
||||
dput(rq->request, "LUA_CL_DATA", cl);
|
||||
@ -306,7 +317,7 @@ void destroy()
|
||||
{
|
||||
if(pid > 0)
|
||||
{
|
||||
kill(pid, SIGKILL);
|
||||
kill(pid, SIGHUP);
|
||||
}
|
||||
LOG("Exit LUA Handle");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user