Browse Source

Let Lua-Websockets have their own file extension

bel 11 years ago
parent
commit
2880742c2b
2 changed files with 27 additions and 5 deletions
  1. 17 4
      src/civetweb.c
  2. 10 1
      src/mod_lua.inl

+ 17 - 4
src/civetweb.c

@@ -525,6 +525,9 @@ enum {
 #if defined(USE_WEBSOCKET)
     WEBSOCKET_ROOT,
 #endif
+#if defined(USE_LUA) && defined(USE_WEBSOCKET)
+    LUA_WEBSOCKET_EXTENSIONS,
+#endif
 
     NUM_OPTIONS
 };
@@ -567,6 +570,9 @@ static const char *config_options[] = {
 #if defined(USE_WEBSOCKET)
     "websocket_root", NULL,
 #endif
+#if defined(USE_LUA) && defined(USE_WEBSOCKET)
+    "lua_websocket_pattern", "**.lua$",
+#endif
 
     NULL
 };
@@ -4777,6 +4783,10 @@ int mg_websocket_write(struct mg_connection* conn, int opcode, const char* data,
 static void handle_websocket_request(struct mg_connection *conn, const char *path, int is_script_resource)
 {
     const char *version = mg_get_header(conn, "Sec-WebSocket-Version");
+#ifdef USE_LUA
+    int lua_websock, shared_lua_websock = 0;
+#endif
+
     if (version == NULL || strcmp(version, "13") != 0) {
         send_http_error(conn, 426, "Upgrade Required", "%s", "Upgrade Required");
     } else if (conn->ctx->callbacks.websocket_connect != NULL &&
@@ -4785,10 +4795,13 @@ static void handle_websocket_request(struct mg_connection *conn, const char *pat
         /* The C callback is called before Lua and may prevent Lua from handling the websocket. */
     } else {
 #ifdef USE_LUA
-        if (match_prefix(conn->ctx->config[LUA_SCRIPT_EXTENSIONS],
-                         (int)strlen(conn->ctx->config[LUA_SCRIPT_EXTENSIONS]),
-                         path)) {
-            conn->lua_websocket_state = new_lua_websocket(path, conn);
+        lua_websock = conn->ctx->config[LUA_WEBSOCKET_EXTENSIONS] ?
+                          match_prefix(conn->ctx->config[LUA_WEBSOCKET_EXTENSIONS],
+                                       (int)strlen(conn->ctx->config[LUA_WEBSOCKET_EXTENSIONS]),
+                                       path) : 0;
+
+        if (lua_websock || shared_lua_websock) {
+            conn->lua_websocket_state = lua_websocket_new(path, conn, !!shared_lua_websock);
             if (conn->lua_websocket_state) {
                 send_websocket_handshake(conn);
                 if (lua_websocket_ready(conn)) {

+ 10 - 1
src/mod_lua.inl

@@ -807,6 +807,7 @@ struct file *filep, struct lua_State *ls)
 struct lua_websock_data {
     lua_State *main;
     lua_State *thread;
+    struct mg_connection *conn;
 };
 
 static void websock_cry(struct mg_connection *conn, int err, lua_State * L, const char * ws_operation, const char * lua_operation)
@@ -836,7 +837,7 @@ static void websock_cry(struct mg_connection *conn, int err, lua_State * L, cons
     }
 }
 
-static void * new_lua_websocket(const char * script, struct mg_connection *conn)
+static void * lua_websocket_new(const char * script, struct mg_connection *conn, int is_shared)
 {
     struct lua_websock_data *lws_data;
     int ok = 0;
@@ -846,6 +847,14 @@ static void * new_lua_websocket(const char * script, struct mg_connection *conn)
     lws_data = (struct lua_websock_data *) malloc(sizeof(*lws_data));
 
     if (lws_data) {
+        lws_data->conn = conn;
+
+        if (is_shared) {
+            (void)pthread_mutex_lock(&conn->ctx->mutex);
+            // TODO: add_to_websocket_list(lws_data);
+            (void)pthread_mutex_unlock(&conn->ctx->mutex);
+        }
+
         lws_data->main = luaL_newstate();
         if (lws_data->main) {
             prepare_lua_environment(conn, lws_data->main, script, LUA_ENV_TYPE_LUA_WEBSOCKET);