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)
|
||||
|
120
lib/asl/antd.c
120
lib/asl/antd.c
@ -22,26 +22,30 @@ void lua_new_light_byte_array(lua_State*L, int n, char* ptr)
|
||||
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);
|
||||
lua_new_byte_array(L, n);
|
||||
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);
|
||||
int n = luaL_checknumber(L, 2);
|
||||
lua_new_light_byte_array(L, n, ptr);
|
||||
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);
|
||||
luaL_argcheck(L, ud != NULL, idx, "`byte array' expected");
|
||||
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);
|
||||
int index = luaL_checknumber(L, 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];
|
||||
}
|
||||
|
||||
static int l_set_barray (lua_State *L) {
|
||||
static int l_set_barray(lua_State *L)
|
||||
{
|
||||
unsigned char value = luaL_checknumber(L, 3);
|
||||
*get_bel(L) = value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_get_barray (lua_State *L) {
|
||||
static int l_get_barray(lua_State *L)
|
||||
{
|
||||
lua_pushnumber(L, *get_bel(L));
|
||||
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);
|
||||
lua_pushnumber(L, a->size);
|
||||
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);
|
||||
char *d = (char *)malloc(a->size + 1);
|
||||
memcpy(d, a->data, a->size);
|
||||
@ -105,10 +113,10 @@ static const struct luaL_Reg barraylib[] = {
|
||||
{"size", l_get_barray_size},
|
||||
{"__tostring", l_barray_to_string},
|
||||
{"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_newlib(L, barraylib);
|
||||
lua_pushstring(L, "__index");
|
||||
@ -138,19 +146,22 @@ void lua_new_array(lua_State*L, int n)
|
||||
lua_setmetatable(L, -2);
|
||||
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);
|
||||
lua_new_array(L, n);
|
||||
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);
|
||||
luaL_argcheck(L, ud != NULL, idx, "`array' expected");
|
||||
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);
|
||||
int index = luaL_checknumber(L, 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];
|
||||
}
|
||||
|
||||
static int l_set_array (lua_State *L) {
|
||||
static int l_set_array(lua_State *L)
|
||||
{
|
||||
double value = luaL_checknumber(L, 3);
|
||||
*get_el(L) = value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_get_array (lua_State *L) {
|
||||
static int l_get_array(lua_State *L)
|
||||
{
|
||||
lua_pushnumber(L, *get_el(L));
|
||||
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);
|
||||
lua_pushnumber(L, a->size);
|
||||
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);
|
||||
lua_pushfstring(L, "number array(%d)", a->size);
|
||||
return 1;
|
||||
@ -188,9 +203,9 @@ static const struct luaL_Reg arraylib [] = {
|
||||
{"get", l_get_array},
|
||||
{"size", l_get_array_size},
|
||||
{"__tostring", l_array_to_string},
|
||||
{NULL, NULL}
|
||||
};
|
||||
int luaopen_array (lua_State *L) {
|
||||
{NULL, NULL}};
|
||||
int luaopen_array(lua_State *L)
|
||||
{
|
||||
luaL_newmetatable(L, ARRAY);
|
||||
luaL_newlib(L, arraylib);
|
||||
lua_pushstring(L, "__index");
|
||||
@ -212,7 +227,6 @@ int luaopen_array (lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// int mime
|
||||
static int l_mime(lua_State *L)
|
||||
{
|
||||
@ -248,20 +262,52 @@ static int l_ti (lua_State *L) {
|
||||
*/
|
||||
|
||||
// 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);
|
||||
const char *v = luaL_checkstring(L, 2);
|
||||
lua_pushnumber(L, __t(client, v));
|
||||
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);
|
||||
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)
|
||||
{
|
||||
lua_pushnil(L);
|
||||
}
|
||||
else
|
||||
{
|
||||
trim(buff, '\n');
|
||||
lua_pushstring(L, buff);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if (len == 1)
|
||||
@ -283,7 +329,8 @@ static int l_antd_recv (lua_State *L) {
|
||||
|
||||
// TODO: add __b to LUA
|
||||
// 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);
|
||||
byte_array_t *arr = l_check_barray(L, 2);
|
||||
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*);
|
||||
static int l_f (lua_State *L) {
|
||||
static int l_f(lua_State *L)
|
||||
{
|
||||
void *client = lua_touserdata(L, 1);
|
||||
const char *v = luaL_checkstring(L, 2);
|
||||
lua_pushnumber(L, __f(client, v));
|
||||
@ -307,7 +355,8 @@ static int l_f (lua_State *L) {
|
||||
}*/
|
||||
|
||||
// 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 *d = luaL_checkstring(L, 2);
|
||||
lua_pushnumber(L, upload(s, d));
|
||||
@ -574,7 +623,8 @@ static int l_ws_read_header(lua_State *L)
|
||||
{
|
||||
lua_pushnil(L);
|
||||
free(header);
|
||||
if(data) free(data);
|
||||
if (data)
|
||||
free(data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -600,7 +650,6 @@ static int l_ws_read_header(lua_State *L)
|
||||
lua_pushnumber(L,(int)(data[i]));
|
||||
lua_settable(L,-3);
|
||||
}*/
|
||||
|
||||
}
|
||||
if (data)
|
||||
free(data);
|
||||
@ -723,6 +772,7 @@ static const struct luaL_Reg standard [] = {
|
||||
//{"_text", l_text},
|
||||
//{"_json", l_json},
|
||||
//{"_jpeg", l_jpeg},
|
||||
{"antd_set_zlevel", l_antd_set_zlevel},
|
||||
{"antd_recv", l_antd_recv},
|
||||
{"_error", l_std_error},
|
||||
{"_send_header", l_send_header},
|
||||
@ -754,10 +804,7 @@ static const struct luaL_Reg standard [] = {
|
||||
{"ws_b", l_ws_bin},
|
||||
{"ws_close", l_ws_close},
|
||||
{"is_dir", l_is_dir},
|
||||
{NULL,NULL}
|
||||
};
|
||||
|
||||
|
||||
{NULL, NULL}};
|
||||
|
||||
int luaopen_std(lua_State *L)
|
||||
{
|
||||
@ -769,10 +816,7 @@ static const struct luaL_Reg modules [] = {
|
||||
{"bytes", luaopen_bytes},
|
||||
{"array", luaopen_array},
|
||||
{"std", luaopen_std},
|
||||
{NULL,NULL}
|
||||
};
|
||||
|
||||
|
||||
{NULL, NULL}};
|
||||
|
||||
int luaopen_antd(lua_State *L)
|
||||
{
|
||||
|
@ -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);
|
||||
@ -89,7 +83,6 @@ void* lua_handle(void* ptr)
|
||||
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");
|
||||
}
|
||||
|
||||
/*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