1
0
mirror of https://github.com/lxsang/antd-lua-plugin synced 2024-12-27 09:58:21 +01:00

lua plugin now used a separated process to handle requests

This commit is contained in:
DanyLE 2022-08-27 21:44:55 +02:00 committed by Dany LE
parent 7814611008
commit c7ea2af3f1
6 changed files with 997 additions and 1018 deletions

View File

@ -1,32 +1,23 @@
package.cpath = __api__.apiroot..'/?.so' package.cpath = __api__.apiroot..'/?.so'
require("antd") require("antd")
std = modules.std()
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
local read_header =function() local read_header =function()
local l
repeat repeat
local l = readline() l = std.antd_recv(HTTP_REQUEST.id)
if l ~= '\r' then 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 if l == "HTTP_REQUEST" or l == "request" or l == "COOKIE" or l == "REQUEST_HEADER" or l == "REQUEST_DATA" then
coroutine.yield(l, "LUA_TABLE") coroutine.yield(l, "LUA_TABLE")
else else
local l1 = readline() local l1 = std.antd_recv(HTTP_REQUEST.id)
if l1 ~= '\r' then if l1 ~= '\r' then
coroutine.yield(l, l1) coroutine.yield(l, l1)
end end
l = l1 l = l1
end end
end end
until l == '\r' until not l or l == '\r'
end end
@ -85,11 +76,11 @@ if HEADER["User-Agent"] and HEADER["User-Agent"]:match("Mobi") then
end end
function LOG_INFO(fmt,...) function LOG_INFO(fmt,...)
ulib.syslog(5,string.format(fmt, table.unpack({...}))) ulib.syslog(5,string.format(fmt or "LOG", table.unpack({...}) or ""))
end end
function LOG_ERROR(fmt,...) function LOG_ERROR(fmt,...)
ulib.syslog(3,string.format(fmt, table.unpack({...}))) ulib.syslog(3,string.format(fmt or "ERROR", table.unpack({...}) or ""))
end end
function has_module(m) function has_module(m)
@ -190,88 +181,13 @@ function loadscript(file, args)
end end
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 -- decode post data if any
local decode_request = function() local decode_request_data = function()
LOG_INFO("Request method %s", REQUEST.method)
if (not REQUEST.method) if (not REQUEST.method)
or (REQUEST.method ~= "POST" or (REQUEST.method ~= "POST"
and REQUEST.method ~= "PUT" and REQUEST.method ~= "PUT"
and REQUEST.method ~= "PATCH") then and REQUEST.method ~= "PATCH")
or (not REQUEST.HAS_RAW_BODY) then
return 0 return 0
end end
local ctype = HEADER['Content-Type'] 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) LOG_ERROR("Invalid content type %s or content length %d", ctype, clen)
return 400, "Bad Request, missing content description" return 400, "Bad Request, missing content description"
end end
if ctype == "application/x-www-form-urlencoded" then local raw_data, len = std.antd_recv(HTTP_REQUEST.id, clen)
return decode_post_data(ctype, clen, true) if len ~= clen then
elseif ctype == "multipart/form-data" then LOG_ERROR("Unable to read all data: read %d expected %d", len, clen)
return decode_multi_part(ctype, clen) return 400, "Bad Request, missing content data"
end
if ctype:find("application/json") then
REQUEST.json = bytes.__tostring(raw_data)
else 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
end end
local code, error = decode_request() local code, error = decode_request_data()
if code ~= 0 then if code ~= 0 then
LOG_ERROR(error) LOG_ERROR(error)

View File

@ -1,5 +1,5 @@
function std.extra_mime(name) function std.extra_mime(name)
local ext = std.ext(name); local ext = std.ext(name)
local mpath = __ROOT__.."/".."mimes.json" local mpath = __ROOT__.."/".."mimes.json"
local xmimes = {} local xmimes = {}
if utils.file_exists(mpath) then 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 == "cpp" or ext == "hpp" then return "text/cpp",false
elseif ext == "md" then return "text/markdown",false elseif ext == "md" then return "text/markdown",false
elseif ext == "lua" then return "text/lua",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 xmimes[ext] then return xmimes[ext].mime, xmimes[ext].binary
--elseif ext == "pgm" then return "image/x-portable-graymap", true --elseif ext == "pgm" then return "image/x-portable-graymap", true
else else

View File

@ -1,4 +1,3 @@
std = modules.std()
bytes = modules.bytes() bytes = modules.bytes()
array = modules.array() array = modules.array()
@ -61,7 +60,8 @@ function std.b(s)
std._b(HTTP_REQUEST.id,s) std._b(HTTP_REQUEST.id,s)
end end
function std.f(v) 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 end
function std.setCookie(v) function std.setCookie(v)

View File

@ -22,26 +22,30 @@ void lua_new_light_byte_array(lua_State*L, int n, char* ptr)
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
} }
static int l_new_barray (lua_State *L) { static int l_new_barray(lua_State *L)
{
int n = luaL_checknumber(L, 1); int n = luaL_checknumber(L, 1);
lua_new_byte_array(L, n); lua_new_byte_array(L, n);
return 1; /* new userdatum is already on the stack */ return 1; /* new userdatum is already on the stack */
} }
static int l_new_lightbarray (lua_State *L) { static int l_new_lightbarray(lua_State *L)
{
unsigned char *ptr = lua_touserdata(L, 1); unsigned char *ptr = lua_touserdata(L, 1);
int n = luaL_checknumber(L, 2); int n = luaL_checknumber(L, 2);
lua_new_light_byte_array(L, n, ptr); lua_new_light_byte_array(L, n, ptr);
return 1; /* new userdatum is already on the stack */ return 1; /* new userdatum is already on the stack */
} }
byte_array_t *l_check_barray (lua_State *L,int idx) { byte_array_t *l_check_barray(lua_State *L, int idx)
{
void *ud = luaL_checkudata(L, idx, BYTEARRAY); void *ud = luaL_checkudata(L, idx, BYTEARRAY);
luaL_argcheck(L, ud != NULL, idx, "`byte array' expected"); luaL_argcheck(L, ud != NULL, idx, "`byte array' expected");
return (byte_array_t *)ud; return (byte_array_t *)ud;
} }
static unsigned char *get_bel(lua_State *L) { static unsigned char *get_bel(lua_State *L)
{
byte_array_t *a = l_check_barray(L, 1); byte_array_t *a = l_check_barray(L, 1);
int index = luaL_checknumber(L, 2); int index = luaL_checknumber(L, 2);
luaL_argcheck(L, 1 <= index && index <= a->size, 2, luaL_argcheck(L, 1 <= index && index <= a->size, 2,
@ -51,24 +55,28 @@ static unsigned char *get_bel(lua_State *L) {
return &a->data[index - 1]; return &a->data[index - 1];
} }
static int l_set_barray (lua_State *L) { static int l_set_barray(lua_State *L)
{
unsigned char value = luaL_checknumber(L, 3); unsigned char value = luaL_checknumber(L, 3);
*get_bel(L) = value; *get_bel(L) = value;
return 0; return 0;
} }
static int l_get_barray (lua_State *L) { static int l_get_barray(lua_State *L)
{
lua_pushnumber(L, *get_bel(L)); lua_pushnumber(L, *get_bel(L));
return 1; return 1;
} }
static int l_get_barray_size (lua_State *L) { static int l_get_barray_size(lua_State *L)
{
byte_array_t *a = l_check_barray(L, 1); byte_array_t *a = l_check_barray(L, 1);
lua_pushnumber(L, a->size); lua_pushnumber(L, a->size);
return 1; return 1;
} }
static int l_barray_to_string (lua_State *L) { static int l_barray_to_string(lua_State *L)
{
byte_array_t *a = l_check_barray(L, 1); byte_array_t *a = l_check_barray(L, 1);
char *d = (char *)malloc(a->size + 1); char *d = (char *)malloc(a->size + 1);
memcpy(d, a->data, a->size); memcpy(d, a->data, a->size);
@ -105,10 +113,10 @@ static const struct luaL_Reg barraylib[] = {
{"size", l_get_barray_size}, {"size", l_get_barray_size},
{"__tostring", l_barray_to_string}, {"__tostring", l_barray_to_string},
{"write", l_barray_write}, {"write", l_barray_write},
{NULL, NULL} {NULL, NULL}};
};
int luaopen_bytes (lua_State *L) { int luaopen_bytes(lua_State *L)
{
luaL_newmetatable(L, BYTEARRAY); luaL_newmetatable(L, BYTEARRAY);
luaL_newlib(L, barraylib); luaL_newlib(L, barraylib);
lua_pushstring(L, "__index"); lua_pushstring(L, "__index");
@ -138,19 +146,22 @@ void lua_new_array(lua_State*L, int n)
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
a->size = n; a->size = n;
} }
static int l_new_array (lua_State *L) { static int l_new_array(lua_State *L)
{
int n = luaL_checknumber(L, 1); int n = luaL_checknumber(L, 1);
lua_new_array(L, n); lua_new_array(L, n);
return 1; /* new userdatum is already on the stack */ return 1; /* new userdatum is already on the stack */
} }
array_t *l_check_array (lua_State *L, int idx) { array_t *l_check_array(lua_State *L, int idx)
{
void *ud = luaL_checkudata(L, idx, ARRAY); void *ud = luaL_checkudata(L, idx, ARRAY);
luaL_argcheck(L, ud != NULL, idx, "`array' expected"); luaL_argcheck(L, ud != NULL, idx, "`array' expected");
return (array_t *)ud; return (array_t *)ud;
} }
static double *get_el(lua_State *L) { static double *get_el(lua_State *L)
{
array_t *a = l_check_array(L, 1); array_t *a = l_check_array(L, 1);
int index = luaL_checknumber(L, 2); int index = luaL_checknumber(L, 2);
luaL_argcheck(L, 1 <= index && index <= a->size, 2, luaL_argcheck(L, 1 <= index && index <= a->size, 2,
@ -160,23 +171,27 @@ static double *get_el(lua_State *L) {
return &a->data[index - 1]; return &a->data[index - 1];
} }
static int l_set_array (lua_State *L) { static int l_set_array(lua_State *L)
{
double value = luaL_checknumber(L, 3); double value = luaL_checknumber(L, 3);
*get_el(L) = value; *get_el(L) = value;
return 0; return 0;
} }
static int l_get_array (lua_State *L) { static int l_get_array(lua_State *L)
{
lua_pushnumber(L, *get_el(L)); lua_pushnumber(L, *get_el(L));
return 1; return 1;
} }
static int l_get_array_size (lua_State *L) { static int l_get_array_size(lua_State *L)
{
array_t *a = l_check_array(L, 1); array_t *a = l_check_array(L, 1);
lua_pushnumber(L, a->size); lua_pushnumber(L, a->size);
return 1; return 1;
} }
static int l_array_to_string (lua_State *L) { static int l_array_to_string(lua_State *L)
{
array_t *a = l_check_array(L, 1); array_t *a = l_check_array(L, 1);
lua_pushfstring(L, "number array(%d)", a->size); lua_pushfstring(L, "number array(%d)", a->size);
return 1; return 1;
@ -188,9 +203,9 @@ static const struct luaL_Reg arraylib [] = {
{"get", l_get_array}, {"get", l_get_array},
{"size", l_get_array_size}, {"size", l_get_array_size},
{"__tostring", l_array_to_string}, {"__tostring", l_array_to_string},
{NULL, NULL} {NULL, NULL}};
}; int luaopen_array(lua_State *L)
int luaopen_array (lua_State *L) { {
luaL_newmetatable(L, ARRAY); luaL_newmetatable(L, ARRAY);
luaL_newlib(L, arraylib); luaL_newlib(L, arraylib);
lua_pushstring(L, "__index"); lua_pushstring(L, "__index");
@ -212,7 +227,6 @@ int luaopen_array (lua_State *L) {
return 1; return 1;
} }
// int mime // int mime
static int l_mime(lua_State *L) static int l_mime(lua_State *L)
{ {
@ -248,20 +262,52 @@ static int l_ti (lua_State *L) {
*/ */
// int __t(int, const char*,...); // int __t(int, const char*,...);
static int l_t (lua_State *L) { static int l_t(lua_State *L)
{
void *client = lua_touserdata(L, 1); void *client = lua_touserdata(L, 1);
const char *v = luaL_checkstring(L, 2); const char *v = luaL_checkstring(L, 2);
lua_pushnumber(L, __t(client, v)); lua_pushnumber(L, __t(client, v));
return 1; /* number of results */ return 1; /* number of results */
} }
static int l_antd_set_zlevel(lua_State* L)
{
antd_client_t *client = (antd_client_t *)lua_touserdata(L, 1);
const char * lvl_str = luaL_checkstring(L, 2);
if(strncmp(lvl_str, "gzip", 4) == 0)
{
client->z_level = ANTD_CGZ;
return 0;
}
if(strncmp(lvl_str,"deflate", 7) == 0)
{
client->z_level = ANTD_CDEFL;
}
return 0;
}
static int l_antd_recv (lua_State *L) { static int l_antd_recv(lua_State *L)
{
void *client = lua_touserdata(L, 1); void *client = lua_touserdata(L, 1);
int len = luaL_checknumber(L,2); int len = 0;
if (lua_isnumber(L, 2))
{
len = (int)luaL_checknumber(L, 2);
}
if (len == 0)
{
char buff[BUFFLEN * 2] = {0};
len = read_buf(client, buff, sizeof(buff));
if (len == 0) if (len == 0)
{ {
lua_pushnil(L); lua_pushnil(L);
}
else
{
trim(buff, '\n');
lua_pushstring(L, buff);
}
return 1; return 1;
} }
if (len == 1) if (len == 1)
@ -283,7 +329,8 @@ static int l_antd_recv (lua_State *L) {
// TODO: add __b to LUA // TODO: add __b to LUA
// int __b(int, const unsigned char*, int); // int __b(int, const unsigned char*, int);
static int l_b (lua_State *L) { static int l_b(lua_State *L)
{
void *client = lua_touserdata(L, 1); void *client = lua_touserdata(L, 1);
byte_array_t *arr = l_check_barray(L, 2); byte_array_t *arr = l_check_barray(L, 2);
lua_pushnumber(L, __b(client, arr->data, arr->size)); lua_pushnumber(L, __b(client, arr->data, arr->size));
@ -291,7 +338,8 @@ static int l_b (lua_State *L) {
} }
// int __f(int, const char*); // int __f(int, const char*);
static int l_f (lua_State *L) { static int l_f(lua_State *L)
{
void *client = lua_touserdata(L, 1); void *client = lua_touserdata(L, 1);
const char *v = luaL_checkstring(L, 2); const char *v = luaL_checkstring(L, 2);
lua_pushnumber(L, __f(client, v)); lua_pushnumber(L, __f(client, v));
@ -307,7 +355,8 @@ static int l_f (lua_State *L) {
}*/ }*/
// int upload(const char*, const char*); // int upload(const char*, const char*);
static int l_upload (lua_State *L) { static int l_upload(lua_State *L)
{
const char *s = luaL_checkstring(L, 1); const char *s = luaL_checkstring(L, 1);
const char *d = luaL_checkstring(L, 2); const char *d = luaL_checkstring(L, 2);
lua_pushnumber(L, upload(s, d)); lua_pushnumber(L, upload(s, d));
@ -574,7 +623,8 @@ static int l_ws_read_header(lua_State *L)
{ {
lua_pushnil(L); lua_pushnil(L);
free(header); free(header);
if(data) free(data); if (data)
free(data);
return 1; return 1;
} }
@ -600,7 +650,6 @@ static int l_ws_read_header(lua_State *L)
lua_pushnumber(L,(int)(data[i])); lua_pushnumber(L,(int)(data[i]));
lua_settable(L,-3); lua_settable(L,-3);
}*/ }*/
} }
if (data) if (data)
free(data); free(data);
@ -723,6 +772,7 @@ static const struct luaL_Reg standard [] = {
//{"_text", l_text}, //{"_text", l_text},
//{"_json", l_json}, //{"_json", l_json},
//{"_jpeg", l_jpeg}, //{"_jpeg", l_jpeg},
{"antd_set_zlevel", l_antd_set_zlevel},
{"antd_recv", l_antd_recv}, {"antd_recv", l_antd_recv},
{"_error", l_std_error}, {"_error", l_std_error},
{"_send_header", l_send_header}, {"_send_header", l_send_header},
@ -754,10 +804,7 @@ static const struct luaL_Reg standard [] = {
{"ws_b", l_ws_bin}, {"ws_b", l_ws_bin},
{"ws_close", l_ws_close}, {"ws_close", l_ws_close},
{"is_dir", l_is_dir}, {"is_dir", l_is_dir},
{NULL,NULL} {NULL, NULL}};
};
int luaopen_std(lua_State *L) int luaopen_std(lua_State *L)
{ {
@ -769,10 +816,7 @@ static const struct luaL_Reg modules [] = {
{"bytes", luaopen_bytes}, {"bytes", luaopen_bytes},
{"array", luaopen_array}, {"array", luaopen_array},
{"std", luaopen_std}, {"std", luaopen_std},
{NULL,NULL} {NULL, NULL}};
};
int luaopen_antd(lua_State *L) int luaopen_antd(lua_State *L)
{ {

View File

@ -1,6 +1,4 @@
#include <antd/plugin.h> #include <antd/plugin.h>
#include <sys/stat.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <errno.h> #include <errno.h>
@ -8,6 +6,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "../lualib.h" #include "../lualib.h"
typedef struct { typedef struct {
@ -17,16 +17,16 @@ typedef struct {
void* lua_handle(void* ptr) 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; lua_State* L = NULL;
antd_client_t cl = {0}; antd_client_t* cl = (antd_client_t*) malloc(sizeof(antd_client_t));
cl.sock = data->fd; cl->sock = data->fd;
time(&cl.last_io); time(&cl->last_io);
cl.ssl = NULL; cl->ssl = NULL;
cl.state = ANTD_CLIENT_PLUGIN_EXEC; cl->state = ANTD_CLIENT_PLUGIN_EXEC;
cl.z_status = 0; cl->z_status = 0;
cl.z_level = ANTD_CNONE; cl->z_level = ANTD_CNONE;
cl.zstream = NULL; cl->zstream = NULL;
//char * index = __s("%s/%s",__plugin__->htdocs,"router.lua"); //char * index = __s("%s/%s",__plugin__->htdocs,"router.lua");
char* cnf = __s("%s%s%s", data->__plugin__->pdir,DIR_SEP, data->__plugin__->name); char* cnf = __s("%s%s%s", data->__plugin__->pdir,DIR_SEP, data->__plugin__->name);
char * apis = __s("%s/%s",cnf,"api.lua"); char * apis = __s("%s/%s",cnf,"api.lua");
@ -64,21 +64,15 @@ void* lua_handle(void* ptr)
// Request // Request
lua_newtable(L); lua_newtable(L);
lua_pushstring(L,"id"); lua_pushstring(L,"id");
lua_pushlightuserdata(L, &cl); lua_pushlightuserdata(L, cl);
//lua_pushnumber(L,client); //lua_pushnumber(L,client);
lua_settable(L, -3); lua_settable(L, -3);
lua_pushstring(L,"socket"); lua_pushstring(L,"socket");
lua_pushnumber(L, cl.sock); lua_pushnumber(L, cl->sock);
//lua_pushnumber(L,client); //lua_pushnumber(L,client);
lua_settable(L, -3); 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"); //lua_pushstring(L,"request");
//push_dict_to_lua(L,rq->request); //push_dict_to_lua(L,rq->request);
//lua_settable(L, -3); //lua_settable(L, -3);
@ -89,7 +83,6 @@ void* lua_handle(void* ptr)
if (luaL_loadfile(L, apis) || lua_pcall(L, 0, 0, 0)) if (luaL_loadfile(L, apis) || lua_pcall(L, 0, 0, 0))
{ {
ERROR("cannot start API file: [%s] %s\n", apis, lua_tostring(L, -1)); ERROR("cannot start API file: [%s] %s\n", apis, lua_tostring(L, -1));
antd_error(&cl, 503, "Internal server error");
} }
/*if (luaL_loadfile(L, index) || lua_pcall(L, 0, 0, 0)) /*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)); __t(client, "Cannot run router: %s", lua_tostring(L, -1));
} }
free(index);*/ free(index);*/
LOG("LUA handle exit on %d", cl.sock); LOG("LUA handle exit on %d", cl->sock);
// clear request // clear request
if(L) if(L)
lua_close(L); lua_close(L);
@ -106,7 +99,7 @@ void* lua_handle(void* ptr)
free(cnf); free(cnf);
if(apis) if(apis)
free(apis); free(apis);
(void) close(cl.sock); (void) antd_close(cl);
return 0; return 0;
//lua_close(L); //lua_close(L);
} }

View File

@ -35,14 +35,13 @@ typedef struct {
} lua_thread_data_t; } lua_thread_data_t;
static pid_t pid = 0; static pid_t pid = 0;
static char sock_path[108];
static int open_unix_socket() static int open_unix_socket()
{ {
struct sockaddr_un address; struct sockaddr_un address;
address.sun_family = AF_UNIX; address.sun_family = AF_UNIX;
char path[sizeof(address.sun_path)]; (void) strncpy(address.sun_path, sock_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));
int fd = socket(AF_UNIX, SOCK_STREAM, 0); int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if(fd == -1) 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)); ERROR( "Unable to connect to socket '%s': %s", address.sun_path, strerror(errno));
return -1; return -1;
} }
LOG( "Socket %s is created successfully", path); LOG( "Socket %s is created successfully", sock_path);
return fd; return fd;
} }
static int mk_socket() static int mk_socket()
{ {
struct sockaddr_un address; 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; address.sun_family = AF_UNIX;
// create the socket // create the socket
(void)snprintf(path, sizeof(address.sun_path), "%s/%s", __plugin__.tmpdir, SOCKET_NAME); (void)strncpy(address.sun_path, sock_path, sizeof(address.sun_path));
(void)strncpy(address.sun_path, path, sizeof(address.sun_path));
int fd = socket(AF_UNIX, SOCK_STREAM, 0); int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) if (fd == -1)
{ {
@ -82,10 +77,10 @@ static int mk_socket()
// mark the socket as passive mode // mark the socket as passive mode
if (listen(fd, 500) == -1) 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; return -1;
} }
LOG("Socket %s is created successfully: %d", path, fd); LOG("Socket %s is created successfully: %d", sock_path, fd);
return fd; return fd;
} }
@ -103,7 +98,6 @@ static void lua_serve()
ERROR("Cannot load Lua core: %s", dlerror()); ERROR("Cannot load Lua core: %s", dlerror());
return; return;
} }
LOG("Lua core loaded");
// now load the handle // now load the handle
(void)snprintf(path, BUFFLEN, "%s/lua/handle.so", __plugin__.pdir); (void)snprintf(path, BUFFLEN, "%s/lua/handle.so", __plugin__.pdir);
lua_handle = dlopen(path, RTLD_LAZY); 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)); 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) while((fd = accept(socket, NULL, NULL)) > 0)
{ {
pthread_t thread; pthread_t thread;
@ -161,14 +168,14 @@ static void lua_serve()
void init() 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(); pid = fork();
if (pid == 0) if (pid == 0)
{ {
// child // child
lua_serve(); lua_serve();
} }
// parent
LOG("Lua module initialized"); LOG("Lua module initialized");
} }
@ -214,7 +221,8 @@ static void *process(void *data)
{ {
case -1: case -1:
ERROR("Error on select(): %s", strerror(errno)); 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); return antd_create_task(NULL, data, NULL, rq->client->last_io);
case 0: case 0:
// time out // time out
@ -229,19 +237,20 @@ static void *process(void *data)
{ {
while((ret = antd_recv_upto(rq->client,buff, BUFFLEN)) > 0) while((ret = antd_recv_upto(rq->client,buff, BUFFLEN)) > 0)
{ {
LOG("Receive %d bytes from antd ", ret);
// write data to the other side // write data to the other side
if(antd_send(cl,buff, ret) != ret) if(antd_send(cl,buff, ret) != ret)
{ {
ERROR("Error atnd_send(): %s", strerror(errno)); ERROR("Error on 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); 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)); LOG("antd_recv_upto() on %d: %s",rq->client->sock, 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); 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) 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 // write data to the other side
if(antd_send(rq->client,buff, ret) != ret) if(antd_send(rq->client,buff, ret) != ret)
{ {
ERROR("Error atnd_send(): %s", strerror(errno)); 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); 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", cl->sock, strerror(errno)); LOG("antd_recv_upto() on %d: %s", cl->sock, 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); 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_status = 0;
cl->z_level = ANTD_CNONE; cl->z_level = ANTD_CNONE;
cl->zstream = NULL; cl->zstream = NULL;
rq->client->z_level = ANTD_CNONE;
push_dict_to_socket(cl, "request","HTTP_REQUEST", rq->request); push_dict_to_socket(cl, "request","HTTP_REQUEST", rq->request);
antd_send(cl,"\r\n", 2); antd_send(cl,"\r\n", 2);
dput(rq->request, "LUA_CL_DATA", cl); dput(rq->request, "LUA_CL_DATA", cl);
@ -306,7 +317,7 @@ void destroy()
{ {
if(pid > 0) if(pid > 0)
{ {
kill(pid, SIGKILL); kill(pid, SIGHUP);
} }
LOG("Exit LUA Handle"); LOG("Exit LUA Handle");
} }