瀏覽代碼

Replace signal by sigaction and fix websocket support

bel2125 7 年之前
父節點
當前提交
b7678afb55
共有 3 個文件被更改,包括 48 次插入13 次删除
  1. 28 6
      src/civetweb.c
  2. 13 7
      src/mod_lua.inl
  3. 7 0
      src/timer.inl

+ 28 - 6
src/civetweb.c

@@ -11300,6 +11300,8 @@ send_websocket_handshake(struct mg_connection *conn, const char *websock_key)
 		return 0;
 	}
 
+	DEBUG_TRACE("%s", "Send websocket handshake");
+
 	SHA1_Init(&sha_ctx);
 	SHA1_Update(&sha_ctx, (unsigned char *)buf, (uint32_t)strlen(buf));
 	SHA1_Final((unsigned char *)sha, &sha_ctx);
@@ -11779,6 +11781,7 @@ handle_websocket_request(struct mg_connection *conn,
 
 			conn->request_info.acceptedWebSocketSubprotocol =
 			    acceptedWebSocketSubprotocol;
+
 		} else if (nbSubprotocolHeader > 0) {
 			/* keep legacy behavior */
 			const char *protocol = protocols[0];
@@ -11816,6 +11819,7 @@ handle_websocket_request(struct mg_connection *conn,
 			return;
 		}
 	}
+
 #if defined(USE_LUA)
 	/* Step 3: No callback. Check if Lua is responsible. */
 	else {
@@ -15883,6 +15887,15 @@ websocket_client_thread(void *data)
 	struct websocket_client_thread_data *cdata =
 	    (struct websocket_client_thread_data *)data;
 
+#if !defined(_WIN32)
+	struct sigaction sa;
+
+	/* Ignore SIGPIPE */
+	memset(&sa, 0, sizeof(sa));
+	sa.sa_handler = SIG_IGN;
+	sigaction(SIGPIPE, &sa, NULL);
+#endif
+
 	mg_set_thread_name("ws-clnt");
 
 	if (cdata->conn->phys_ctx) {
@@ -16573,6 +16586,13 @@ worker_thread(void *thread_func_param)
 {
 	struct worker_thread_args *pwta =
 	    (struct worker_thread_args *)thread_func_param;
+	struct sigaction sa;
+
+	/* Ignore SIGPIPE */
+	memset(&sa, 0, sizeof(sa));
+	sa.sa_handler = SIG_IGN;
+	sigaction(SIGPIPE, &sa, NULL);
+
 	worker_thread_run(pwta);
 	mg_free(thread_func_param);
 	return NULL;
@@ -16798,6 +16818,13 @@ static unsigned __stdcall master_thread(void *thread_func_param)
 static void *
 master_thread(void *thread_func_param)
 {
+	struct sigaction sa;
+
+	/* Ignore SIGPIPE */
+	memset(&sa, 0, sizeof(sa));
+	sa.sa_handler = SIG_IGN;
+	sigaction(SIGPIPE, &sa, NULL);
+
 	master_thread_run(thread_func_param);
 	return NULL;
 }
@@ -17184,16 +17211,11 @@ mg_start(const struct mg_callbacks *callbacks,
 		return NULL;
 	}
 
-#if !defined(_WIN32) && !defined(__SYMBIAN32__)
-	/* Ignore SIGPIPE signal, so if browser cancels the request, it
-	 * won't kill the whole process. */
-	(void)signal(SIGPIPE, SIG_IGN);
-#endif /* !_WIN32 && !__SYMBIAN32__ */
-
 	ctx->cfg_worker_threads = ((unsigned int)(workerthreadcount));
 	ctx->worker_threadids = (pthread_t *)mg_calloc_ctx(ctx->cfg_worker_threads,
 	                                                   sizeof(pthread_t),
 	                                                   ctx);
+
 	if (ctx->worker_threadids == NULL) {
 		mg_cry(fc(ctx), "Not enough memory for worker thread ID array");
 		free_context(ctx);

+ 13 - 7
src/mod_lua.inl

@@ -1716,6 +1716,8 @@ prepare_lua_environment(struct mg_context *ctx,
                         const char *script_name,
                         int lua_env_type)
 {
+	const char *preload_file_name = NULL;
+
 	civetweb_open_lua_libs(L);
 
 #if LUA_VERSION_NUM == 502
@@ -1850,13 +1852,17 @@ prepare_lua_environment(struct mg_context *ctx,
 	                  "mg.onerror = function(e) mg.write('\\nLua error:\\n', "
 	                  "debug.traceback(e, 1)) end"));
 
-	if (ctx != NULL) {
-		/* Preload */
-		if (conn->dom_ctx->config[LUA_PRELOAD_FILE] != NULL) {
-			IGNORE_UNUSED_RESULT(
-			    luaL_dofile(L, conn->dom_ctx->config[LUA_PRELOAD_FILE]));
-		}
+	/* Check if a preload file is available */
+	if ((conn != NULL) && (conn->dom_ctx != NULL)) {
+		preload_file_name = conn->dom_ctx->config[LUA_PRELOAD_FILE];
+	}
+
+	/* Preload file into new Lua environment */
+	if (preload_file_name) {
+		IGNORE_UNUSED_RESULT(luaL_dofile(L, preload_file_name));
+	}
 
+	if (ctx != NULL) {
 		if (ctx->callbacks.init_lua != NULL) {
 			ctx->callbacks.init_lua(conn, L);
 		}
@@ -2103,7 +2109,7 @@ lua_websocket_new(const char *script, struct mg_connection *conn)
 		ws->conn[0] = conn;
 		ws->references = 1;
 		prepare_lua_environment(conn->phys_ctx,
-		                        NULL,
+		                        conn,
 		                        ws,
 		                        ws->state,
 		                        script,

+ 7 - 0
src/timer.inl

@@ -183,6 +183,13 @@ static unsigned __stdcall timer_thread(void *thread_func_param)
 static void *
 timer_thread(void *thread_func_param)
 {
+	struct sigaction sa;
+
+	/* Ignore SIGPIPE */
+	memset(&sa, 0, sizeof(sa));
+	sa.sa_handler = SIG_IGN;
+	sigaction(SIGPIPE, &sa, NULL);
+
 	timer_thread_run(thread_func_param);
 	return NULL;
 }