|
@@ -26,6 +26,8 @@ static void munmap(void *addr, int64_t length)
|
|
|
#endif
|
|
|
|
|
|
static const char *LUASOCKET = "luasocket";
|
|
|
+static const char lua_regkey_ctx = 1;
|
|
|
+static const char lua_regkey_connlist = 2;
|
|
|
|
|
|
/* Forward declarations */
|
|
|
static void handle_request(struct mg_connection *);
|
|
@@ -59,10 +61,10 @@ static void reg_boolean(struct lua_State *L, const char *name, int val)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-static void reg_function(struct lua_State *L, const char *name,
|
|
|
+static void reg_conn_function(struct lua_State *L, const char *name,
|
|
|
lua_CFunction func, struct mg_connection *conn)
|
|
|
{
|
|
|
- if (name!=NULL && func!=NULL) {
|
|
|
+ if (name!=NULL && func!=NULL && conn!=NULL) {
|
|
|
lua_pushstring(L, name);
|
|
|
lua_pushlightuserdata(L, conn);
|
|
|
lua_pushcclosure(L, func, 1);
|
|
@@ -70,6 +72,42 @@ static void reg_function(struct lua_State *L, const char *name,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+static void reg_function(struct lua_State *L, const char *name, lua_CFunction func)
|
|
|
+{
|
|
|
+ if (name!=NULL && func!=NULL) {
|
|
|
+ lua_pushstring(L, name);
|
|
|
+ lua_pushcclosure(L, func, 0);
|
|
|
+ lua_rawset(L, -3);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void lua_cry(struct mg_connection *conn, int err, lua_State * L, const char * lua_title, const char * lua_operation)
|
|
|
+{
|
|
|
+ switch (err) {
|
|
|
+ case LUA_OK:
|
|
|
+ case LUA_YIELD:
|
|
|
+ break;
|
|
|
+ case LUA_ERRRUN:
|
|
|
+ mg_cry(conn, "%s: %s failed: runtime error: %s", lua_title, lua_operation, lua_tostring(L, -1));
|
|
|
+ break;
|
|
|
+ case LUA_ERRSYNTAX:
|
|
|
+ mg_cry(conn, "%s: %s failed: syntax error: %s", lua_title, lua_operation, lua_tostring(L, -1));
|
|
|
+ break;
|
|
|
+ case LUA_ERRMEM:
|
|
|
+ mg_cry(conn, "%s: %s failed: out of memory", lua_title, lua_operation);
|
|
|
+ break;
|
|
|
+ case LUA_ERRGCMM:
|
|
|
+ mg_cry(conn, "%s: %s failed: error during garbage collection", lua_title, lua_operation);
|
|
|
+ break;
|
|
|
+ case LUA_ERRERR:
|
|
|
+ mg_cry(conn, "%s: %s failed: error in error handling: %s", lua_title, lua_operation, lua_tostring(L, -1));
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ mg_cry(conn, "%s: %s failed: error %i", lua_title, lua_operation, err);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
static int lsp_sock_close(lua_State *L)
|
|
|
{
|
|
|
int num_args = lua_gettop(L);
|
|
@@ -189,6 +227,7 @@ static const char * lsp_var_reader(lua_State *L, void *ud, size_t *sz)
|
|
|
{
|
|
|
struct lsp_var_reader_data * reader = (struct lsp_var_reader_data *)ud;
|
|
|
const char * ret;
|
|
|
+ (void)(L); /* unused */
|
|
|
|
|
|
switch (reader->state) {
|
|
|
case 0:
|
|
@@ -315,7 +354,7 @@ static int lsp_keep_alive(lua_State *L)
|
|
|
int num_args = lua_gettop(L);
|
|
|
|
|
|
/* This function may be called with one parameter (boolean) to set the keep_alive state.
|
|
|
- Or without a parameter to just query the current keep_alive state. */
|
|
|
+ Or without a parameter to just query the current keep_alive state. */
|
|
|
if ((num_args==1) && lua_isboolean(L, 1)) {
|
|
|
conn->must_close = !lua_toboolean(L, 1);
|
|
|
} else if (num_args != 0) {
|
|
@@ -402,7 +441,6 @@ static int lsp_send_file(lua_State *L)
|
|
|
/* mg.get_var */
|
|
|
static int lsp_get_var(lua_State *L)
|
|
|
{
|
|
|
- struct mg_connection *conn = lua_touserdata(L, lua_upvalueindex(1));
|
|
|
int num_args = lua_gettop(L);
|
|
|
const char *data, *var_name;
|
|
|
size_t data_len, occurrence;
|
|
@@ -432,18 +470,28 @@ static int lsp_get_var(lua_State *L)
|
|
|
/* mg.get_mime_type */
|
|
|
static int lsp_get_mime_type(lua_State *L)
|
|
|
{
|
|
|
- struct mg_connection *conn = lua_touserdata(L, lua_upvalueindex(1));
|
|
|
int num_args = lua_gettop(L);
|
|
|
- struct vec mime_type = {0};
|
|
|
+ struct vec mime_type = {0, 0};
|
|
|
+ struct mg_context *ctx;
|
|
|
const char *text;
|
|
|
|
|
|
+ lua_pushlightuserdata(L, (void *)&lua_regkey_ctx);
|
|
|
+ lua_gettable(L, LUA_REGISTRYINDEX);
|
|
|
+ ctx = (struct mg_context *)lua_touserdata(L, -1);
|
|
|
+
|
|
|
if (num_args==1) {
|
|
|
text = lua_tostring(L, 1);
|
|
|
if (text) {
|
|
|
- get_mime_type(conn->ctx, text, &mime_type);
|
|
|
- lua_pushlstring(L, mime_type.ptr, mime_type.len);
|
|
|
+ if (ctx) {
|
|
|
+ get_mime_type(ctx, text, &mime_type);
|
|
|
+ lua_pushlstring(L, mime_type.ptr, mime_type.len);
|
|
|
+ } else {
|
|
|
+ text = mg_get_builtin_mime_type(text);
|
|
|
+ lua_pushstring(L, text);
|
|
|
+ }
|
|
|
} else {
|
|
|
- lua_pushnil(L);
|
|
|
+ /* Syntax error */
|
|
|
+ return luaL_error(L, "invalid argument for get_mime_type() call");
|
|
|
}
|
|
|
} else {
|
|
|
/* Syntax error */
|
|
@@ -456,7 +504,6 @@ static int lsp_get_mime_type(lua_State *L)
|
|
|
static int lsp_get_cookie(lua_State *L)
|
|
|
{
|
|
|
int num_args = lua_gettop(L);
|
|
|
- struct vec mime_type = {0};
|
|
|
const char *cookie;
|
|
|
const char *var_name;
|
|
|
int ret;
|
|
@@ -572,7 +619,7 @@ static int lsp_base64_encode(lua_State *L)
|
|
|
if (text) {
|
|
|
dst = mg_malloc(text_len*8/6+4);
|
|
|
if (dst) {
|
|
|
- base64_encode(text, text_len, dst);
|
|
|
+ base64_encode((const unsigned char *)text, text_len, dst);
|
|
|
lua_pushstring(L, dst);
|
|
|
mg_free(dst);
|
|
|
} else {
|
|
@@ -602,7 +649,7 @@ static int lsp_base64_decode(lua_State *L)
|
|
|
if (text) {
|
|
|
dst = mg_malloc(text_len);
|
|
|
if (dst) {
|
|
|
- ret = base64_decode(text, text_len, dst, &dst_len);
|
|
|
+ ret = base64_decode((const unsigned char *)text, text_len, dst, &dst_len);
|
|
|
if (ret != -1) {
|
|
|
mg_free(dst);
|
|
|
return luaL_error(L, "illegal character in lsp_base64_decode() call");
|
|
@@ -623,25 +670,43 @@ static int lsp_base64_decode(lua_State *L)
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
+#ifdef USE_WEBSOCKET
|
|
|
+struct lua_websock_data {
|
|
|
+ lua_State *state;
|
|
|
+ char * script;
|
|
|
+ unsigned references;
|
|
|
+ struct mg_connection *conn[MAX_WORKER_THREADS];
|
|
|
+ pthread_mutex_t ws_mutex;
|
|
|
+};
|
|
|
+#endif
|
|
|
+
|
|
|
/* mg.write for websockets */
|
|
|
static int lwebsock_write(lua_State *L)
|
|
|
{
|
|
|
#ifdef USE_WEBSOCKET
|
|
|
int num_args = lua_gettop(L);
|
|
|
- struct mg_connection *conn = lua_touserdata(L, lua_upvalueindex(1));
|
|
|
+ struct lua_websock_data *ws;
|
|
|
const char *str;
|
|
|
size_t size;
|
|
|
int opcode = -1;
|
|
|
+ unsigned i;
|
|
|
+ struct mg_connection * client = NULL;
|
|
|
+
|
|
|
+ lua_pushlightuserdata(L, (void *)&lua_regkey_connlist);
|
|
|
+ lua_gettable(L, LUA_REGISTRYINDEX);
|
|
|
+ ws = (struct lua_websock_data *)lua_touserdata(L, -1);
|
|
|
|
|
|
if (num_args == 1) {
|
|
|
+ /* just one text: send it to all client */
|
|
|
if (lua_isstring(L, 1)) {
|
|
|
- str = lua_tolstring(L, 1, &size);
|
|
|
- mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, str, size);
|
|
|
+ opcode = WEBSOCKET_OPCODE_TEXT;
|
|
|
}
|
|
|
} else if (num_args == 2) {
|
|
|
if (lua_isnumber(L, 1)) {
|
|
|
+ /* opcode number and message text */
|
|
|
opcode = (int)lua_tointeger(L, 1);
|
|
|
} else if (lua_isstring(L,1)) {
|
|
|
+ /* opcode string and message text */
|
|
|
str = lua_tostring(L, 1);
|
|
|
if (!mg_strncasecmp(str, "text", 4)) opcode = WEBSOCKET_OPCODE_TEXT;
|
|
|
else if (!mg_strncasecmp(str, "bin", 3)) opcode = WEBSOCKET_OPCODE_BINARY;
|
|
@@ -649,40 +714,257 @@ static int lwebsock_write(lua_State *L)
|
|
|
else if (!mg_strncasecmp(str, "ping", 4)) opcode = WEBSOCKET_OPCODE_PING;
|
|
|
else if (!mg_strncasecmp(str, "pong", 4)) opcode = WEBSOCKET_OPCODE_PONG;
|
|
|
else if (!mg_strncasecmp(str, "cont", 4)) opcode = WEBSOCKET_OPCODE_CONTINUATION;
|
|
|
+ } else if (lua_isuserdata(L, 1)) {
|
|
|
+ /* client id and message text */
|
|
|
+ client = (struct mg_connection *) lua_touserdata(L, 1);
|
|
|
+ opcode = WEBSOCKET_OPCODE_TEXT;
|
|
|
}
|
|
|
- if (opcode>=0 && opcode<16 && lua_isstring(L, 2)) {
|
|
|
- str = lua_tolstring(L, 2, &size);
|
|
|
- mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, str, size);
|
|
|
+ } else if (num_args == 3) {
|
|
|
+ if (lua_isuserdata(L, 1)) {
|
|
|
+ client = (struct mg_connection *) lua_touserdata(L, 1);
|
|
|
+ if (lua_isnumber(L, 2)) {
|
|
|
+ /* client id, opcode number and message text */
|
|
|
+ opcode = (int)lua_tointeger(L, 2);
|
|
|
+ } else if (lua_isstring(L,2)) {
|
|
|
+ /* client id, opcode string and message text */
|
|
|
+ str = lua_tostring(L, 2);
|
|
|
+ if (!mg_strncasecmp(str, "text", 4)) opcode = WEBSOCKET_OPCODE_TEXT;
|
|
|
+ else if (!mg_strncasecmp(str, "bin", 3)) opcode = WEBSOCKET_OPCODE_BINARY;
|
|
|
+ else if (!mg_strncasecmp(str, "close", 5)) opcode = WEBSOCKET_OPCODE_CONNECTION_CLOSE;
|
|
|
+ else if (!mg_strncasecmp(str, "ping", 4)) opcode = WEBSOCKET_OPCODE_PING;
|
|
|
+ else if (!mg_strncasecmp(str, "pong", 4)) opcode = WEBSOCKET_OPCODE_PONG;
|
|
|
+ else if (!mg_strncasecmp(str, "cont", 4)) opcode = WEBSOCKET_OPCODE_CONTINUATION;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ if (opcode>=0 && opcode<16 && lua_isstring(L, num_args)) {
|
|
|
+ str = lua_tolstring(L, num_args, &size);
|
|
|
+ if (client) {
|
|
|
+ for (i=0; i<ws->references; i++) {
|
|
|
+ if (client == ws->conn[i]) {
|
|
|
+ mg_websocket_write(ws->conn[i], opcode, str, size);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (i=0; i<ws->references; i++) {
|
|
|
+ mg_websocket_write(ws->conn[i], opcode, str, size);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return luaL_error(L, "invalid websocket write() call");
|
|
|
+ }
|
|
|
+#else
|
|
|
+ (void)(L); /* unused */
|
|
|
#endif
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+struct laction_arg {
|
|
|
+ lua_State *state;
|
|
|
+ const char *script;
|
|
|
+ pthread_mutex_t *pmutex;
|
|
|
+ char txt[1];
|
|
|
+};
|
|
|
+
|
|
|
+static int lua_action(struct laction_arg *arg)
|
|
|
+{
|
|
|
+ int err, ok;
|
|
|
+ struct mg_context *ctx;
|
|
|
+
|
|
|
+ (void)pthread_mutex_lock(arg->pmutex);
|
|
|
+
|
|
|
+ lua_pushlightuserdata(arg->state, (void *)&lua_regkey_ctx);
|
|
|
+ lua_gettable(arg->state, LUA_REGISTRYINDEX);
|
|
|
+ ctx = (struct mg_context *)lua_touserdata(arg->state, -1);
|
|
|
+
|
|
|
+ err = luaL_loadstring(arg->state, arg->txt);
|
|
|
+ if (err != 0) {
|
|
|
+ lua_cry(fc(ctx), err, arg->state, arg->script, "timer");
|
|
|
+ (void)pthread_mutex_unlock(arg->pmutex);
|
|
|
+ mg_free(arg);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ err = lua_pcall(arg->state, 0, 1, 0);
|
|
|
+ if (err != 0) {
|
|
|
+ lua_cry(fc(ctx), err, arg->state, arg->script, "timer");
|
|
|
+ (void)pthread_mutex_unlock(arg->pmutex);
|
|
|
+ mg_free(arg);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ ok = lua_type(arg->state, -1);
|
|
|
+ if (lua_isboolean(arg->state, -1)) {
|
|
|
+ ok = lua_toboolean(arg->state, -1);
|
|
|
+ } else {
|
|
|
+ ok = 0;
|
|
|
+ }
|
|
|
+ lua_pop(arg->state, 1);
|
|
|
+
|
|
|
+ (void)pthread_mutex_unlock(arg->pmutex);
|
|
|
+
|
|
|
+ if (!ok) {
|
|
|
+ mg_free(arg);
|
|
|
+ }
|
|
|
+ return ok;
|
|
|
+}
|
|
|
+
|
|
|
+static int lua_action_free(struct laction_arg *arg)
|
|
|
+{
|
|
|
+ if (lua_action(arg)) {
|
|
|
+ mg_free(arg);
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int lwebsocket_set_timer(lua_State *L, int is_periodic)
|
|
|
+{
|
|
|
+#if defined(USE_TIMERS) && defined(USE_WEBSOCKET)
|
|
|
+ int num_args = lua_gettop(L);
|
|
|
+ struct lua_websock_data *ws;
|
|
|
+ int type1,type2, ok = 0;
|
|
|
+ double timediff;
|
|
|
+ struct mg_context *ctx;
|
|
|
+ struct laction_arg *arg;
|
|
|
+ const char *txt;
|
|
|
+ size_t txt_len;
|
|
|
+
|
|
|
+ lua_pushlightuserdata(L, (void *)&lua_regkey_ctx);
|
|
|
+ lua_gettable(L, LUA_REGISTRYINDEX);
|
|
|
+ ctx = (struct mg_context *)lua_touserdata(L, -1);
|
|
|
+
|
|
|
+ lua_pushlightuserdata(L, (void *)&lua_regkey_connlist);
|
|
|
+ lua_gettable(L, LUA_REGISTRYINDEX);
|
|
|
+ ws = (struct lua_websock_data *)lua_touserdata(L, -1);
|
|
|
+
|
|
|
+ if (num_args < 2) {
|
|
|
+ return luaL_error(L, "not enough arguments for set_timer/interval() call");
|
|
|
+ }
|
|
|
+
|
|
|
+ type1 = lua_type(L, 1);
|
|
|
+ type2 = lua_type(L, 2);
|
|
|
+
|
|
|
+ if (type1==LUA_TSTRING && type2==LUA_TNUMBER && num_args==2) {
|
|
|
+ timediff = (double)lua_tonumber(L, 2);
|
|
|
+ txt = lua_tostring(L, 1);
|
|
|
+ txt_len = strlen(txt);
|
|
|
+ arg = mg_malloc(sizeof(struct laction_arg) + txt_len + 10);
|
|
|
+ arg->state = L;
|
|
|
+ arg->script = ws->script;
|
|
|
+ arg->pmutex = &(ws->ws_mutex);
|
|
|
+ memcpy(arg->txt, "return(", 7);
|
|
|
+ memcpy(arg->txt+7, txt, txt_len);
|
|
|
+ arg->txt[txt_len+7] = ')';
|
|
|
+ arg->txt[txt_len+8] = 0;
|
|
|
+ ok = (0==timer_add(ctx, timediff, is_periodic, 1, (taction)(is_periodic ? lua_action : lua_action_free), (void*)arg));
|
|
|
+ } else if (type1==LUA_TFUNCTION && type2==LUA_TNUMBER) {
|
|
|
+ /* TODO: not implemented yet */
|
|
|
+ return luaL_error(L, "invalid arguments for set_timer/interval() call");
|
|
|
+ } else {
|
|
|
+ return luaL_error(L, "invalid arguments for set_timer/interval() call");
|
|
|
+ }
|
|
|
+
|
|
|
+ lua_pushboolean(L, ok);
|
|
|
+ return 1;
|
|
|
+
|
|
|
+#else
|
|
|
+ (void)(L); /* unused */
|
|
|
+ (void)(is_periodic); /* unused */
|
|
|
+ return 0;
|
|
|
+#endif
|
|
|
+}
|
|
|
+
|
|
|
+/* mg.set_timeout for websockets */
|
|
|
+static int lwebsocket_set_timeout(lua_State *L)
|
|
|
+{
|
|
|
+ return lwebsocket_set_timer(L, 0);
|
|
|
+}
|
|
|
+
|
|
|
+/* mg.set_interval for websockets */
|
|
|
+static int lwebsocket_set_interval(lua_State *L)
|
|
|
+{
|
|
|
+ return lwebsocket_set_timer(L, 1);
|
|
|
+}
|
|
|
+
|
|
|
enum {
|
|
|
LUA_ENV_TYPE_LUA_SERVER_PAGE = 0,
|
|
|
LUA_ENV_TYPE_PLAIN_LUA_PAGE = 1,
|
|
|
LUA_ENV_TYPE_LUA_WEBSOCKET = 2,
|
|
|
};
|
|
|
|
|
|
-static void prepare_lua_environment(struct mg_connection *conn, lua_State *L, const char *script_name, int lua_env_type)
|
|
|
+static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
|
|
|
{
|
|
|
- const struct mg_request_info *ri = mg_get_request_info(conn);
|
|
|
- char src_addr[IP_ADDR_STR_LEN];
|
|
|
- const char * preload_file = conn->ctx->config[LUA_PRELOAD_FILE];
|
|
|
+ char src_addr[IP_ADDR_STR_LEN] = "";
|
|
|
+ const char *s;
|
|
|
int i;
|
|
|
|
|
|
- extern void luaL_openlibs(lua_State *);
|
|
|
-
|
|
|
sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
|
|
|
|
|
|
+ /* Export mg.request_info */
|
|
|
+ lua_pushstring(L, "request_info");
|
|
|
+ lua_newtable(L);
|
|
|
+ reg_string(L, "request_method", conn->request_info.request_method);
|
|
|
+ reg_string(L, "uri", conn->request_info.uri);
|
|
|
+ reg_string(L, "http_version", conn->request_info.http_version);
|
|
|
+ reg_string(L, "query_string", conn->request_info.query_string);
|
|
|
+ reg_int(L, "remote_ip", conn->request_info.remote_ip); /* remote_ip is deprecated, use remote_addr instead */
|
|
|
+ reg_string(L, "remote_addr", src_addr);
|
|
|
+ /* TODO: ip version */
|
|
|
+ reg_int(L, "remote_port", conn->request_info.remote_port);
|
|
|
+ reg_int(L, "num_headers", conn->request_info.num_headers);
|
|
|
+ reg_int(L, "server_port", ntohs(conn->client.lsa.sin.sin_port));
|
|
|
+
|
|
|
+ if (conn->request_info.content_length >= 0) {
|
|
|
+ /* reg_int64: content_length */
|
|
|
+ lua_pushstring(L, "content_length");
|
|
|
+ lua_pushnumber(L, (lua_Number)conn->request_info.content_length); /* lua_Number may be used as 52 bit integer */
|
|
|
+ lua_rawset(L, -3);
|
|
|
+ }
|
|
|
+ if ((s = mg_get_header(conn, "Content-Type")) != NULL) {
|
|
|
+ reg_string(L, "content_type", s);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (conn->request_info.remote_user != NULL) {
|
|
|
+ reg_string(L, "remote_user", conn->request_info.remote_user);
|
|
|
+ reg_string(L, "auth_type", "Digest");
|
|
|
+ }
|
|
|
+
|
|
|
+ reg_boolean(L, "https", conn->ssl != NULL);
|
|
|
+
|
|
|
+ if (conn->status_code > 0) {
|
|
|
+ /* Lua error handler should show the status code */
|
|
|
+ reg_int(L, "status", conn->status_code);
|
|
|
+ }
|
|
|
+
|
|
|
+ lua_pushstring(L, "http_headers");
|
|
|
+ lua_newtable(L);
|
|
|
+ for (i = 0; i < conn->request_info.num_headers; i++) {
|
|
|
+ reg_string(L, conn->request_info.http_headers[i].name, conn->request_info.http_headers[i].value);
|
|
|
+ }
|
|
|
+ lua_rawset(L, -3);
|
|
|
+
|
|
|
+ lua_rawset(L, -3);
|
|
|
+}
|
|
|
+
|
|
|
+static void prepare_lua_environment(struct mg_context * ctx, struct mg_connection *conn, struct lua_websock_data *conn_list, lua_State *L, const char *script_name, int lua_env_type)
|
|
|
+{
|
|
|
+ const char * preload_file = ((conn != NULL) ? conn->ctx->config[LUA_PRELOAD_FILE] : NULL);
|
|
|
+
|
|
|
+ extern void luaL_openlibs(lua_State *);
|
|
|
luaL_openlibs(L);
|
|
|
+
|
|
|
#ifdef USE_LUA_SQLITE3
|
|
|
{
|
|
|
extern int luaopen_lsqlite3(lua_State *);
|
|
|
luaopen_lsqlite3(L);
|
|
|
}
|
|
|
#endif
|
|
|
+#ifdef USE_LUA_LUAXML
|
|
|
+ {
|
|
|
+ extern int luaopen_LuaXML(lua_State *);
|
|
|
+ luaopen_LuaXML(L);
|
|
|
+ }
|
|
|
+#endif
|
|
|
#ifdef USE_LUA_FILE_SYSTEM
|
|
|
{
|
|
|
extern int luaopen_lfs(lua_State *);
|
|
@@ -697,94 +979,81 @@ static void prepare_lua_environment(struct mg_connection *conn, lua_State *L, co
|
|
|
lua_pop(L, 1);
|
|
|
lua_register(L, "connect", lsp_connect);
|
|
|
|
|
|
- if (conn == NULL) {
|
|
|
- /* Do not register any connection specific functions or variables */
|
|
|
- return;
|
|
|
+ /* Store context in the registry */
|
|
|
+ if (ctx) {
|
|
|
+ lua_pushlightuserdata(L, (void *)&lua_regkey_ctx);
|
|
|
+ lua_pushlightuserdata(L, (void *)ctx);
|
|
|
+ lua_settable(L, LUA_REGISTRYINDEX);
|
|
|
+ }
|
|
|
+ if (conn_list) {
|
|
|
+ lua_pushlightuserdata(L, (void *)&lua_regkey_connlist);
|
|
|
+ lua_pushlightuserdata(L, (void *)conn_list);
|
|
|
+ lua_settable(L, LUA_REGISTRYINDEX);
|
|
|
}
|
|
|
|
|
|
/* Register mg module */
|
|
|
lua_newtable(L);
|
|
|
|
|
|
- reg_function(L, "cry", lsp_cry, conn);
|
|
|
-
|
|
|
switch (lua_env_type) {
|
|
|
- case LUA_ENV_TYPE_LUA_SERVER_PAGE:
|
|
|
- reg_string(L, "lua_type", "page");
|
|
|
- break;
|
|
|
- case LUA_ENV_TYPE_PLAIN_LUA_PAGE:
|
|
|
- reg_string(L, "lua_type", "script");
|
|
|
- break;
|
|
|
- case LUA_ENV_TYPE_LUA_WEBSOCKET:
|
|
|
- reg_string(L, "lua_type", "websocket");
|
|
|
- break;
|
|
|
+ case LUA_ENV_TYPE_LUA_SERVER_PAGE:
|
|
|
+ reg_string(L, "lua_type", "page");
|
|
|
+ break;
|
|
|
+ case LUA_ENV_TYPE_PLAIN_LUA_PAGE:
|
|
|
+ reg_string(L, "lua_type", "script");
|
|
|
+ break;
|
|
|
+ case LUA_ENV_TYPE_LUA_WEBSOCKET:
|
|
|
+ reg_string(L, "lua_type", "websocket");
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
if (lua_env_type==LUA_ENV_TYPE_LUA_SERVER_PAGE || lua_env_type==LUA_ENV_TYPE_PLAIN_LUA_PAGE) {
|
|
|
- reg_function(L, "read", lsp_read, conn);
|
|
|
- reg_function(L, "write", lsp_write, conn);
|
|
|
- reg_function(L, "keep_alive", lsp_keep_alive, conn);
|
|
|
+ reg_conn_function(L, "cry", lsp_cry, conn);
|
|
|
+ reg_conn_function(L, "read", lsp_read, conn);
|
|
|
+ reg_conn_function(L, "write", lsp_write, conn);
|
|
|
+ reg_conn_function(L, "keep_alive", lsp_keep_alive, conn);
|
|
|
+ reg_conn_function(L, "send_file", lsp_send_file, conn);
|
|
|
}
|
|
|
|
|
|
if (lua_env_type==LUA_ENV_TYPE_LUA_SERVER_PAGE) {
|
|
|
- reg_function(L, "include", lsp_include, conn);
|
|
|
- reg_function(L, "redirect", lsp_redirect, conn);
|
|
|
+ reg_conn_function(L, "include", lsp_include, conn);
|
|
|
+ reg_conn_function(L, "redirect", lsp_redirect, conn);
|
|
|
}
|
|
|
|
|
|
if (lua_env_type==LUA_ENV_TYPE_LUA_WEBSOCKET) {
|
|
|
- reg_function(L, "write", lwebsock_write, conn);
|
|
|
+ reg_function(L, "write", lwebsock_write);
|
|
|
+#ifdef USE_TIMERS
|
|
|
+ reg_function(L, "set_timeout", lwebsocket_set_timeout);
|
|
|
+ reg_function(L, "set_interval", lwebsocket_set_interval);
|
|
|
+#endif
|
|
|
+ /* reg_conn_function(L, "send_file", lsp_send_file, conn); */
|
|
|
}
|
|
|
|
|
|
- reg_function(L, "send_file", lsp_send_file, conn);
|
|
|
- reg_function(L, "get_var", lsp_get_var, conn);
|
|
|
- reg_function(L, "get_mime_type", lsp_get_mime_type, conn);
|
|
|
- reg_function(L, "get_cookie", lsp_get_cookie, conn);
|
|
|
- reg_function(L, "md5", lsp_md5, conn);
|
|
|
- reg_function(L, "url_encode", lsp_url_encode, conn);
|
|
|
- reg_function(L, "url_decode", lsp_url_decode, conn);
|
|
|
- reg_function(L, "base64_encode", lsp_base64_encode, conn);
|
|
|
- reg_function(L, "base64_decode", lsp_base64_decode, conn);
|
|
|
+ reg_function(L, "get_var", lsp_get_var);
|
|
|
+ reg_function(L, "get_mime_type", lsp_get_mime_type);
|
|
|
+ reg_function(L, "get_cookie", lsp_get_cookie);
|
|
|
+ reg_function(L, "md5", lsp_md5);
|
|
|
+ reg_function(L, "url_encode", lsp_url_encode);
|
|
|
+ reg_function(L, "url_decode", lsp_url_decode);
|
|
|
+ reg_function(L, "base64_encode", lsp_base64_encode);
|
|
|
+ reg_function(L, "base64_decode", lsp_base64_decode);
|
|
|
|
|
|
reg_string(L, "version", CIVETWEB_VERSION);
|
|
|
- reg_string(L, "document_root", conn->ctx->config[DOCUMENT_ROOT]);
|
|
|
- reg_string(L, "auth_domain", conn->ctx->config[AUTHENTICATION_DOMAIN]);
|
|
|
+ reg_string(L, "document_root", ctx->config[DOCUMENT_ROOT]);
|
|
|
+ reg_string(L, "auth_domain", ctx->config[AUTHENTICATION_DOMAIN]);
|
|
|
#if defined(USE_WEBSOCKET)
|
|
|
- reg_string(L, "websocket_root", conn->ctx->config[WEBSOCKET_ROOT]);
|
|
|
+ reg_string(L, "websocket_root", ctx->config[WEBSOCKET_ROOT]);
|
|
|
#endif
|
|
|
+ reg_string(L, "script_name", script_name);
|
|
|
|
|
|
- if (conn->ctx->systemName) {
|
|
|
- reg_string(L, "system", conn->ctx->systemName);
|
|
|
- }
|
|
|
-
|
|
|
- /* Export request_info */
|
|
|
- lua_pushstring(L, "request_info");
|
|
|
- lua_newtable(L);
|
|
|
- reg_string(L, "request_method", ri->request_method);
|
|
|
- reg_string(L, "uri", ri->uri);
|
|
|
- reg_string(L, "http_version", ri->http_version);
|
|
|
- reg_string(L, "query_string", ri->query_string);
|
|
|
- reg_int(L, "remote_ip", ri->remote_ip); /* remote_ip is deprecated, use remote_addr instead */
|
|
|
- reg_string(L, "remote_addr", src_addr);
|
|
|
- /* TODO: ip version */
|
|
|
- reg_int(L, "remote_port", ri->remote_port);
|
|
|
- reg_int(L, "num_headers", ri->num_headers);
|
|
|
- reg_int(L, "server_port", ntohs(conn->client.lsa.sin.sin_port));
|
|
|
-
|
|
|
- if (conn->request_info.remote_user != NULL) {
|
|
|
- reg_string(L, "remote_user", conn->request_info.remote_user);
|
|
|
- reg_string(L, "auth_type", "Digest");
|
|
|
+ if (ctx->systemName != NULL) {
|
|
|
+ reg_string(L, "system", ctx->systemName);
|
|
|
}
|
|
|
|
|
|
- lua_pushstring(L, "http_headers");
|
|
|
- lua_newtable(L);
|
|
|
- for (i = 0; i < ri->num_headers; i++) {
|
|
|
- reg_string(L, ri->http_headers[i].name, ri->http_headers[i].value);
|
|
|
+ /* Export connection specific info */
|
|
|
+ if (conn!=NULL) {
|
|
|
+ prepare_lua_request_info(conn, L);
|
|
|
}
|
|
|
- lua_rawset(L, -3);
|
|
|
-
|
|
|
- reg_boolean(L, "https", conn->ssl != NULL);
|
|
|
- reg_string(L, "script_name", script_name);
|
|
|
|
|
|
- lua_rawset(L, -3);
|
|
|
lua_setglobal(L, "mg");
|
|
|
|
|
|
/* Register default mg.onerror function */
|
|
@@ -795,6 +1064,10 @@ static void prepare_lua_environment(struct mg_connection *conn, lua_State *L, co
|
|
|
if ((preload_file != NULL) && (*preload_file != 0)) {
|
|
|
IGNORE_UNUSED_RESULT(luaL_dofile(L, preload_file));
|
|
|
}
|
|
|
+
|
|
|
+ if (ctx->callbacks.init_lua != NULL) {
|
|
|
+ ctx->callbacks.init_lua(conn, L);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
static int lua_error_handler(lua_State *L)
|
|
@@ -839,7 +1112,7 @@ void mg_exec_lua_script(struct mg_connection *conn, const char *path,
|
|
|
|
|
|
/* Execute a plain Lua script. */
|
|
|
if (path != NULL && (L = lua_newstate(lua_allocator, NULL)) != NULL) {
|
|
|
- prepare_lua_environment(conn, L, path, LUA_ENV_TYPE_PLAIN_LUA_PAGE);
|
|
|
+ prepare_lua_environment(conn->ctx, conn, NULL, L, path, LUA_ENV_TYPE_PLAIN_LUA_PAGE);
|
|
|
lua_pushcclosure(L, &lua_error_handler, 0);
|
|
|
|
|
|
if (exports != NULL) {
|
|
@@ -878,8 +1151,7 @@ static void lsp_send_err(struct mg_connection *conn, struct lua_State *L,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-static int handle_lsp_request(struct mg_connection *conn, const char *path,
|
|
|
-struct file *filep, struct lua_State *ls)
|
|
|
+static int handle_lsp_request(struct mg_connection *conn, const char *path, struct file *filep, struct lua_State *ls)
|
|
|
{
|
|
|
void *p = NULL;
|
|
|
lua_State *L = NULL;
|
|
@@ -896,15 +1168,12 @@ struct file *filep, struct lua_State *ls)
|
|
|
fileno(filep->fp), 0)) == MAP_FAILED) {
|
|
|
lsp_send_err(conn, ls, "mmap(%s, %zu, %d): %s", path, (size_t) filep->size,
|
|
|
fileno(filep->fp), strerror(errno));
|
|
|
- } else if ((L = ls != NULL ? ls : lua_newstate(lua_allocator, NULL)) == NULL) {
|
|
|
+ } else if ((L = (ls != NULL ? ls : lua_newstate(lua_allocator, NULL))) == NULL) {
|
|
|
send_http_error(conn, 500, http_500_error, "%s", "luaL_newstate failed");
|
|
|
} else {
|
|
|
/* We're not sending HTTP headers here, Lua page must do it. */
|
|
|
if (ls == NULL) {
|
|
|
- prepare_lua_environment(conn, L, path, LUA_ENV_TYPE_LUA_SERVER_PAGE);
|
|
|
- if (conn->ctx->callbacks.init_lua != NULL) {
|
|
|
- conn->ctx->callbacks.init_lua(conn, L);
|
|
|
- }
|
|
|
+ prepare_lua_environment(conn->ctx, conn, NULL, L, path, LUA_ENV_TYPE_LUA_SERVER_PAGE);
|
|
|
}
|
|
|
error = lsp(conn, path, filep->membuf == NULL ? p : filep->membuf,
|
|
|
filep->size, L);
|
|
@@ -917,256 +1186,188 @@ struct file *filep, struct lua_State *ls)
|
|
|
}
|
|
|
|
|
|
#ifdef USE_WEBSOCKET
|
|
|
-struct lua_websock_data {
|
|
|
- lua_State *main;
|
|
|
- lua_State *thread;
|
|
|
- char * script;
|
|
|
- unsigned shared;
|
|
|
- struct mg_connection *conn;
|
|
|
- pthread_mutex_t mutex;
|
|
|
+struct mg_shared_lua_websocket_list {
|
|
|
+ struct lua_websock_data ws;
|
|
|
+ struct mg_shared_lua_websocket_list *next;
|
|
|
};
|
|
|
|
|
|
-struct mg_shared_lua_websocket {
|
|
|
- struct lua_websock_data *sock;
|
|
|
- struct mg_shared_lua_websocket *next;
|
|
|
-};
|
|
|
-
|
|
|
-static void websock_cry(struct mg_connection *conn, int err, lua_State * L, const char * ws_operation, const char * lua_operation)
|
|
|
-{
|
|
|
- switch (err) {
|
|
|
- case LUA_OK:
|
|
|
- case LUA_YIELD:
|
|
|
- break;
|
|
|
- case LUA_ERRRUN:
|
|
|
- mg_cry(conn, "%s: %s failed: runtime error: %s", ws_operation, lua_operation, lua_tostring(L, -1));
|
|
|
- break;
|
|
|
- case LUA_ERRSYNTAX:
|
|
|
- mg_cry(conn, "%s: %s failed: syntax error: %s", ws_operation, lua_operation, lua_tostring(L, -1));
|
|
|
- break;
|
|
|
- case LUA_ERRMEM:
|
|
|
- mg_cry(conn, "%s: %s failed: out of memory", ws_operation, lua_operation);
|
|
|
- break;
|
|
|
- case LUA_ERRGCMM:
|
|
|
- mg_cry(conn, "%s: %s failed: error during garbage collection", ws_operation, lua_operation);
|
|
|
- break;
|
|
|
- case LUA_ERRERR:
|
|
|
- mg_cry(conn, "%s: %s failed: error in error handling: %s", ws_operation, lua_operation, lua_tostring(L, -1));
|
|
|
- break;
|
|
|
- default:
|
|
|
- mg_cry(conn, "%s: %s failed: error %i", ws_operation, lua_operation, err);
|
|
|
- break;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-static void * lua_websocket_new(const char * script, struct mg_connection *conn, int is_shared)
|
|
|
+static void * lua_websocket_new(const char * script, struct mg_connection *conn)
|
|
|
{
|
|
|
- struct lua_websock_data *lws_data;
|
|
|
- struct mg_shared_lua_websocket **shared_websock_list = &(conn->ctx->shared_lua_websockets);
|
|
|
- int ok = 0;
|
|
|
- int found = 0;
|
|
|
- int err, nargs;
|
|
|
+ struct mg_shared_lua_websocket_list **shared_websock_list = &(conn->ctx->shared_lua_websockets);
|
|
|
+ struct lua_websock_data *ws;
|
|
|
+ int err, ok = 0;
|
|
|
|
|
|
assert(conn->lua_websocket_state == NULL);
|
|
|
|
|
|
- /*
|
|
|
- lock list (mg_context global)
|
|
|
- check if in list
|
|
|
- yes: inc rec counter
|
|
|
- no: create state, add to list
|
|
|
- lock list element
|
|
|
- unlock list (mg_context global)
|
|
|
- call add
|
|
|
- unlock list element
|
|
|
- */
|
|
|
-
|
|
|
- if (is_shared) {
|
|
|
- (void)pthread_mutex_lock(&conn->ctx->mutex);
|
|
|
- while (*shared_websock_list) {
|
|
|
- if (!strcmp((*shared_websock_list)->sock->script, script)) {
|
|
|
- lws_data = (*shared_websock_list)->sock;
|
|
|
- lws_data->shared++;
|
|
|
- found = 1;
|
|
|
- }
|
|
|
- shared_websock_list = &((*shared_websock_list)->next);
|
|
|
+ /* lock list (mg_context global) */
|
|
|
+ mg_lock_context(conn->ctx);
|
|
|
+ while (*shared_websock_list) {
|
|
|
+ /* check if ws already in list */
|
|
|
+ if (0==strcmp(script,(*shared_websock_list)->ws.script)) {
|
|
|
+ break;
|
|
|
}
|
|
|
- (void)pthread_mutex_unlock(&conn->ctx->mutex);
|
|
|
+ shared_websock_list = &((*shared_websock_list)->next);
|
|
|
}
|
|
|
-
|
|
|
- if (!found) {
|
|
|
- lws_data = (struct lua_websock_data *) mg_malloc(sizeof(*lws_data));
|
|
|
+ if (*shared_websock_list == NULL) {
|
|
|
+ /* add ws to list */
|
|
|
+ *shared_websock_list = mg_calloc(sizeof(struct mg_shared_lua_websocket_list), 1);
|
|
|
+ if (*shared_websock_list == NULL) {
|
|
|
+ mg_unlock_context(conn->ctx);
|
|
|
+ mg_cry(conn, "Cannot create shared websocket struct, OOM");
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+ /* init ws list element */
|
|
|
+ ws = &(*shared_websock_list)->ws;
|
|
|
+ ws->script = mg_strdup(script); /* TODO: handle OOM */
|
|
|
+ pthread_mutex_init(&(ws->ws_mutex), NULL);
|
|
|
+ ws->state = lua_newstate(lua_allocator, NULL);
|
|
|
+ ws->conn[0] = conn;
|
|
|
+ ws->references = 1;
|
|
|
+ (void)pthread_mutex_lock(&(ws->ws_mutex));
|
|
|
+ prepare_lua_environment(conn->ctx, NULL, ws, ws->state, script, LUA_ENV_TYPE_LUA_WEBSOCKET);
|
|
|
+ err = luaL_loadfile(ws->state, script);
|
|
|
+ if (err != 0) {
|
|
|
+ lua_cry(conn, err, ws->state, script, "load");
|
|
|
+ }
|
|
|
+ err = lua_pcall(ws->state, 0, 0, 0);
|
|
|
+ if (err != 0) {
|
|
|
+ lua_cry(conn, err, ws->state, script, "init");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ /* inc ref count */
|
|
|
+ ws = &(*shared_websock_list)->ws;
|
|
|
+ (void)pthread_mutex_lock(&(ws->ws_mutex));
|
|
|
+ (*shared_websock_list)->ws.conn[(ws->references)++] = conn;
|
|
|
}
|
|
|
-
|
|
|
- if (lws_data) {
|
|
|
- if (!found) {
|
|
|
- lws_data->shared = is_shared;
|
|
|
- lws_data->conn = conn;
|
|
|
- lws_data->script = mg_strdup(script);
|
|
|
- lws_data->main = lua_newstate(lua_allocator, NULL);
|
|
|
- if (is_shared) {
|
|
|
- (void)pthread_mutex_lock(&conn->ctx->mutex);
|
|
|
- shared_websock_list = &(conn->ctx->shared_lua_websockets);
|
|
|
- while (*shared_websock_list) {
|
|
|
- shared_websock_list = &((*shared_websock_list)->next);
|
|
|
- }
|
|
|
- *shared_websock_list = (struct mg_shared_lua_websocket *)mg_malloc(sizeof(struct mg_shared_lua_websocket));
|
|
|
- if (*shared_websock_list) {
|
|
|
- (*shared_websock_list)->sock = lws_data;
|
|
|
- (*shared_websock_list)->next = 0;
|
|
|
- }
|
|
|
- (void)pthread_mutex_unlock(&conn->ctx->mutex);
|
|
|
- }
|
|
|
+ mg_unlock_context(conn->ctx);
|
|
|
+
|
|
|
+ /* call add */
|
|
|
+ lua_getglobal(ws->state, "open");
|
|
|
+ lua_newtable(ws->state);
|
|
|
+ prepare_lua_request_info(conn, ws->state);
|
|
|
+ lua_pushstring(ws->state, "client");
|
|
|
+ lua_pushlightuserdata(ws->state, (void *)conn);
|
|
|
+ lua_rawset(ws->state, -3);
|
|
|
+
|
|
|
+ err = lua_pcall(ws->state, 1, 1, 0);
|
|
|
+ if (err != 0) {
|
|
|
+ lua_cry(conn, err, ws->state, script, "open handler");
|
|
|
+ } else {
|
|
|
+ if (lua_isboolean(ws->state, -1)) {
|
|
|
+ ok = lua_toboolean(ws->state, -1);
|
|
|
}
|
|
|
+ lua_pop(ws->state, 1);
|
|
|
+ }
|
|
|
+ if (!ok) {
|
|
|
+ /* Remove from ws connection list. */
|
|
|
+ /* TODO: Check if list entry and Lua state needs to be deleted (see websocket_close). */
|
|
|
+ (*shared_websock_list)->ws.conn[--(ws->references)] = 0;
|
|
|
+ ws = NULL;
|
|
|
+ }
|
|
|
|
|
|
- if (lws_data->main) {
|
|
|
- prepare_lua_environment(conn, lws_data->main, script, LUA_ENV_TYPE_LUA_WEBSOCKET);
|
|
|
- if (conn->ctx->callbacks.init_lua != NULL) {
|
|
|
- conn->ctx->callbacks.init_lua(conn, lws_data->main);
|
|
|
- }
|
|
|
- lws_data->thread = lua_newthread(lws_data->main);
|
|
|
- err = luaL_loadfile(lws_data->thread, script);
|
|
|
- if (err==LUA_OK) {
|
|
|
- /* Activate the Lua script. */
|
|
|
- err = lua_resume(lws_data->thread, NULL, 0);
|
|
|
- if (err!=LUA_YIELD) {
|
|
|
- websock_cry(conn, err, lws_data->thread, __func__, "lua_resume");
|
|
|
- } else {
|
|
|
- nargs = lua_gettop(lws_data->thread);
|
|
|
- ok = (nargs==1) && lua_isboolean(lws_data->thread, 1) && lua_toboolean(lws_data->thread, 1);
|
|
|
- }
|
|
|
- } else {
|
|
|
- websock_cry(conn, err, lws_data->thread, __func__, "lua_loadfile");
|
|
|
- }
|
|
|
+ (void)pthread_mutex_unlock(&(ws->ws_mutex));
|
|
|
|
|
|
- } else {
|
|
|
- mg_cry(conn, "%s: luaL_newstate failed", __func__);
|
|
|
- }
|
|
|
+ return (void*)ws;
|
|
|
+}
|
|
|
|
|
|
- if (!ok) {
|
|
|
- if (lws_data->main) lua_close(lws_data->main);
|
|
|
- mg_free(lws_data->script);
|
|
|
- mg_free(lws_data);
|
|
|
- lws_data=0;
|
|
|
- }
|
|
|
+static int lua_websocket_data(struct mg_connection * conn, void *ws_arg, int bits, char *data, size_t data_len)
|
|
|
+{
|
|
|
+ struct lua_websock_data *ws = (struct lua_websock_data *)(ws_arg);
|
|
|
+ int err, ok = 0;
|
|
|
+
|
|
|
+ assert(ws != NULL);
|
|
|
+ assert(ws->state != NULL);
|
|
|
+
|
|
|
+ (void)pthread_mutex_lock(&ws->ws_mutex);
|
|
|
+
|
|
|
+ lua_getglobal(ws->state, "data");
|
|
|
+ lua_newtable(ws->state);
|
|
|
+ lua_pushstring(ws->state, "client");
|
|
|
+ lua_pushlightuserdata(ws->state, (void *)conn);
|
|
|
+ lua_rawset(ws->state, -3);
|
|
|
+ lua_pushstring(ws->state, "bits"); /* TODO: dont use "bits" but fields with a meaning according to http://tools.ietf.org/html/rfc6455, section 5.2 */
|
|
|
+ lua_pushnumber(ws->state, bits);
|
|
|
+ lua_rawset(ws->state, -3);
|
|
|
+ lua_pushstring(ws->state, "data");
|
|
|
+ lua_pushlstring(ws->state, data, data_len);
|
|
|
+ lua_rawset(ws->state, -3);
|
|
|
+
|
|
|
+ err = lua_pcall(ws->state, 1, 1, 0);
|
|
|
+ if (err != 0) {
|
|
|
+ lua_cry(conn, err, ws->state, ws->script, "data handler");
|
|
|
} else {
|
|
|
- mg_cry(conn, "%s: out of memory", __func__);
|
|
|
+ if (lua_isboolean(ws->state, -1)) {
|
|
|
+ ok = lua_toboolean(ws->state, -1);
|
|
|
+ }
|
|
|
+ lua_pop(ws->state, 1);
|
|
|
}
|
|
|
+ (void)pthread_mutex_unlock(&ws->ws_mutex);
|
|
|
|
|
|
- return lws_data;
|
|
|
+ return ok;
|
|
|
}
|
|
|
|
|
|
-static int lua_websocket_data(struct mg_connection *conn, int bits, char *data, size_t data_len)
|
|
|
+static int lua_websocket_ready(struct mg_connection * conn, void * ws_arg)
|
|
|
{
|
|
|
- struct lua_websock_data *lws_data = (struct lua_websock_data *)(conn->lua_websocket_state);
|
|
|
- int err, nargs, ok=0, retry;
|
|
|
- lua_Number delay;
|
|
|
-
|
|
|
- assert(lws_data != NULL);
|
|
|
- assert(lws_data->main != NULL);
|
|
|
- assert(lws_data->thread != NULL);
|
|
|
-
|
|
|
- /*
|
|
|
- lock list element
|
|
|
- call data
|
|
|
- unlock list element
|
|
|
- */
|
|
|
-
|
|
|
- do {
|
|
|
- retry=0;
|
|
|
-
|
|
|
- /* Push the data to Lua, then resume the Lua state. */
|
|
|
- /* The data will be available to Lua as the result of the coroutine.yield function. */
|
|
|
- lua_pushboolean(lws_data->thread, 1);
|
|
|
- if (bits >= 0) {
|
|
|
- lua_pushinteger(lws_data->thread, bits);
|
|
|
- if (data) {
|
|
|
- lua_pushlstring(lws_data->thread, data, data_len);
|
|
|
- err = lua_resume(lws_data->thread, NULL, 3);
|
|
|
- } else {
|
|
|
- err = lua_resume(lws_data->thread, NULL, 2);
|
|
|
- }
|
|
|
- } else {
|
|
|
- err = lua_resume(lws_data->thread, NULL, 1);
|
|
|
- }
|
|
|
+ struct lua_websock_data *ws = (struct lua_websock_data *)(ws_arg);
|
|
|
+ int err, ok = 0;
|
|
|
|
|
|
- /* Check if Lua returned by a call to the coroutine.yield function. */
|
|
|
- if (err!=LUA_YIELD) {
|
|
|
- websock_cry(conn, err, lws_data->thread, __func__, "lua_resume");
|
|
|
- } else {
|
|
|
- nargs = lua_gettop(lws_data->thread);
|
|
|
- ok = (nargs>=1) && lua_isboolean(lws_data->thread, 1) && lua_toboolean(lws_data->thread, 1);
|
|
|
- delay = (nargs>=2) && lua_isnumber(lws_data->thread, 2) ? lua_tonumber(lws_data->thread, 2) : -1.0;
|
|
|
- if (ok && delay>0) {
|
|
|
- fd_set rfds;
|
|
|
- struct timeval tv;
|
|
|
-
|
|
|
- FD_ZERO(&rfds);
|
|
|
- FD_SET(conn->client.sock, &rfds);
|
|
|
-
|
|
|
- tv.tv_sec = (unsigned long)delay;
|
|
|
- tv.tv_usec = (unsigned long)(((double)delay - (double)((unsigned long)delay))*1000000.0);
|
|
|
- retry = (0==select(conn->client.sock+1, &rfds, NULL, NULL, &tv));
|
|
|
- }
|
|
|
+ assert(ws != NULL);
|
|
|
+ assert(ws->state != NULL);
|
|
|
+
|
|
|
+ (void)pthread_mutex_lock(&ws->ws_mutex);
|
|
|
+
|
|
|
+ lua_getglobal(ws->state, "ready");
|
|
|
+ lua_newtable(ws->state);
|
|
|
+ lua_pushstring(ws->state, "client");
|
|
|
+ lua_pushlightuserdata(ws->state, (void *)conn);
|
|
|
+ lua_rawset(ws->state, -3);
|
|
|
+
|
|
|
+ err = lua_pcall(ws->state, 1, 1, 0);
|
|
|
+ if (err != 0) {
|
|
|
+ lua_cry(conn, err, ws->state, ws->script, "ready handler");
|
|
|
+ } else {
|
|
|
+ if (lua_isboolean(ws->state, -1)) {
|
|
|
+ ok = lua_toboolean(ws->state, -1);
|
|
|
}
|
|
|
- } while (retry);
|
|
|
+ lua_pop(ws->state, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ (void)pthread_mutex_unlock(&ws->ws_mutex);
|
|
|
|
|
|
return ok;
|
|
|
}
|
|
|
|
|
|
-static int lua_websocket_ready(struct mg_connection *conn)
|
|
|
+static void lua_websocket_close(struct mg_connection * conn, void * ws_arg)
|
|
|
{
|
|
|
- return lua_websocket_data(conn, -1, NULL, 0);
|
|
|
-}
|
|
|
+ struct lua_websock_data *ws = (struct lua_websock_data *)(ws_arg);
|
|
|
+ struct mg_shared_lua_websocket_list **shared_websock_list = &(conn->ctx->shared_lua_websockets);
|
|
|
+ int err = 0;
|
|
|
+ unsigned i;
|
|
|
|
|
|
-static void lua_websocket_close(struct mg_connection *conn)
|
|
|
-{
|
|
|
- struct lua_websock_data *lws_data = (struct lua_websock_data *)(conn->lua_websocket_state);
|
|
|
- struct mg_shared_lua_websocket **shared_websock_list;
|
|
|
- int err;
|
|
|
-
|
|
|
- assert(lws_data != NULL);
|
|
|
- assert(lws_data->main != NULL);
|
|
|
- assert(lws_data->thread != NULL);
|
|
|
-
|
|
|
- /*
|
|
|
- lock list element
|
|
|
- lock list (mg_context global)
|
|
|
- call remove
|
|
|
- dec ref counter
|
|
|
- if ref counter == 0 close state and remove from list
|
|
|
- unlock list element
|
|
|
- unlock list (mg_context global)
|
|
|
- */
|
|
|
-
|
|
|
-
|
|
|
- lua_pushboolean(lws_data->thread, 0);
|
|
|
- err = lua_resume(lws_data->thread, NULL, 1);
|
|
|
-
|
|
|
- if (lws_data->shared) {
|
|
|
- (void)pthread_mutex_lock(&conn->ctx->mutex);
|
|
|
- lws_data->shared--;
|
|
|
- if (lws_data->shared==0) {
|
|
|
- /*
|
|
|
- shared_websock_list = &(conn->ctx->shared_lua_websockets);
|
|
|
- while (*shared_websock_list) {
|
|
|
- if ((*shared_websock_list)->sock == lws_data) {
|
|
|
- *shared_websock_list = (*shared_websock_list)->next;
|
|
|
- } else {
|
|
|
- shared_websock_list = &((*shared_websock_list)->next);
|
|
|
- }
|
|
|
- }
|
|
|
+ assert(ws != NULL);
|
|
|
+ assert(ws->state != NULL);
|
|
|
+
|
|
|
+ (void)pthread_mutex_lock(&ws->ws_mutex);
|
|
|
|
|
|
- lua_close(lws_data->main);
|
|
|
- mg_free(lws_data->script);
|
|
|
- lws_data->script=0;
|
|
|
- mg_free(lws_data);
|
|
|
- */
|
|
|
+ lua_getglobal(ws->state, "close");
|
|
|
+ lua_newtable(ws->state);
|
|
|
+ lua_pushstring(ws->state, "client");
|
|
|
+ lua_pushlightuserdata(ws->state, (void *)conn);
|
|
|
+ lua_rawset(ws->state, -3);
|
|
|
+
|
|
|
+ err = lua_pcall(ws->state, 1, 0, 0);
|
|
|
+ if (err != 0) {
|
|
|
+ lua_cry(conn, err, ws->state, ws->script, "close handler");
|
|
|
+ }
|
|
|
+ for (i=0;i<ws->references;i++) {
|
|
|
+ if (ws->conn[i]==conn) {
|
|
|
+ ws->references--;
|
|
|
+ ws->conn[i] = ws->conn[ws->references];
|
|
|
}
|
|
|
- (void)pthread_mutex_unlock(&conn->ctx->mutex);
|
|
|
- } else {
|
|
|
- lua_close(lws_data->main);
|
|
|
- mg_free(lws_data->script);
|
|
|
- mg_free(lws_data);
|
|
|
}
|
|
|
- conn->lua_websocket_state = NULL;
|
|
|
+ /* TODO: Delete lua_websock_data and remove it from the websocket list.
|
|
|
+ This must only be done, when all connections are closed, and all
|
|
|
+ asynchronous operations and timers are completed/expired. */
|
|
|
+ (void)pthread_mutex_unlock(&ws->ws_mutex);
|
|
|
}
|
|
|
#endif
|