Explorar o código

Lua websockets need to be script ressources (*.lua)

bel %!s(int64=11) %!d(string=hai) anos
pai
achega
db9c7fa303
Modificáronse 1 ficheiros con 19 adicións e 14 borrados
  1. 19 14
      src/civetweb.c

+ 19 - 14
src/civetweb.c

@@ -521,11 +521,9 @@ enum {
 
 
 #if defined(USE_LUA)
 #if defined(USE_LUA)
     LUA_SCRIPT_EXTENSIONS, LUA_SERVER_PAGE_EXTENSIONS,
     LUA_SCRIPT_EXTENSIONS, LUA_SERVER_PAGE_EXTENSIONS,
-
-#if defined(USE_WEBSOCKET)
-    LUA_WEBSOCKET_SCRIPT,
 #endif
 #endif
-
+#if defined(USE_WEBSOCKET)
+    WEBSOCKET_ROOT,
 #endif
 #endif
 
 
     NUM_OPTIONS
     NUM_OPTIONS
@@ -565,10 +563,9 @@ static const char *config_options[] = {
 #if defined(USE_LUA)
 #if defined(USE_LUA)
     "lua_script_pattern", "**.lua$",
     "lua_script_pattern", "**.lua$",
     "lua_server_page_pattern", "**.lp$|**.lsp$",
     "lua_server_page_pattern", "**.lp$|**.lsp$",
-
-#if defined(USE_WEBSOCKET)
-    "lua_websocket_script", NULL,
 #endif
 #endif
+#if defined(USE_WEBSOCKET)
+    "lua_websocket_root", NULL,
 #endif
 #endif
 
 
     NULL
     NULL
@@ -656,6 +653,10 @@ struct de {
     struct file file;
     struct file file;
 };
 };
 
 
+#if defined(USE_WEBSOCKET)
+static int is_websocket_request(const struct mg_connection *conn);
+#endif
+
 const char **mg_get_valid_option_names(void)
 const char **mg_get_valid_option_names(void)
 {
 {
     return config_options;
     return config_options;
@@ -2265,6 +2266,12 @@ static void convert_uri_to_file_name(struct mg_connection *conn, char *buf,
 
 
     *is_script_ressource = 0;
     *is_script_ressource = 0;
 
 
+#if defined(USE_WEBSOCKET)
+    if (is_websocket_request(conn) && conn->ctx->config[WEBSOCKET_ROOT]) {
+        root = conn->ctx->config[WEBSOCKET_ROOT];
+    }
+#endif
+
     /* Using buf_len - 1 because memmove() for PATH_INFO may shift part
     /* Using buf_len - 1 because memmove() for PATH_INFO may shift part
        of the path one byte on the right.
        of the path one byte on the right.
        If document_root is NULL, leave the file empty. */
        If document_root is NULL, leave the file empty. */
@@ -4761,9 +4768,8 @@ int mg_websocket_write(struct mg_connection* conn, int opcode, const char* data,
     return retval;
     return retval;
 }
 }
 
 
-static void handle_websocket_request(struct mg_connection *conn)
+static void handle_websocket_request(struct mg_connection *conn, const char *path, int is_script_resource)
 {
 {
-    const char *ws_page = NULL;
     const char *version = mg_get_header(conn, "Sec-WebSocket-Version");
     const char *version = mg_get_header(conn, "Sec-WebSocket-Version");
     if (version == NULL || strcmp(version, "13") != 0) {
     if (version == NULL || strcmp(version, "13") != 0) {
         send_http_error(conn, 426, "Upgrade Required", "%s", "Upgrade Required");
         send_http_error(conn, 426, "Upgrade Required", "%s", "Upgrade Required");
@@ -4772,10 +4778,9 @@ static void handle_websocket_request(struct mg_connection *conn)
         /* C callback has returned non-zero, do not proceed with handshake. */
         /* C callback has returned non-zero, do not proceed with handshake. */
         /* The C callback is called before Lua and may prevent Lua from handling the websocket. */
         /* The C callback is called before Lua and may prevent Lua from handling the websocket. */
     } else {
     } else {
-#ifdef USE_LUA
-        ws_page = conn->ctx->config[LUA_WEBSOCKET_SCRIPT];
-        if (ws_page) {
-            conn->lua_websocket_state = new_lua_websocket(ws_page, conn);
+#ifdef USE_LUA        
+        if (is_script_resource) {        
+            conn->lua_websocket_state = new_lua_websocket(path, conn);
             if (conn->lua_websocket_state) {
             if (conn->lua_websocket_state) {
                 send_websocket_handshake(conn);
                 send_websocket_handshake(conn);
                 lua_websocket_ready(conn);
                 lua_websocket_ready(conn);
@@ -5165,7 +5170,7 @@ static void handle_request(struct mg_connection *conn)
         /* Do nothing, callback has served the request */
         /* Do nothing, callback has served the request */
 #if defined(USE_WEBSOCKET)
 #if defined(USE_WEBSOCKET)
     } else if (is_websocket_request(conn)) {
     } else if (is_websocket_request(conn)) {
-        handle_websocket_request(conn);
+        handle_websocket_request(conn, path, is_script_resource);
 #endif
 #endif
     } else if (!is_script_resource && !strcmp(ri->request_method, "OPTIONS")) {
     } else if (!is_script_resource && !strcmp(ri->request_method, "OPTIONS")) {
         send_options(conn);
         send_options(conn);