From de064fd65aa8cab0f9314853116b33c1162fdd44 Mon Sep 17 00:00:00 2001
From: Andy Green <andy@warmcat.com>
Date: Thu, 3 May 2018 10:49:36 +0800
Subject: [PATCH] refactor: core code in lib/core and private-libwebsockets.h
 to core/private.h

This commit is coverity-clean as tested

cmake .. -DLWS_WITH_MINIMAL_EXAMPLES=1 -DLWS_WITHOUT_EXTENSIONS=1 -DLWS_WITH_ACME=1 -DLWS_WITH_LWSWS=1 -DLWS_WITH_LIBUV=1 -DLWS_WITH_HTTP2=1 -DLWS_WITHOUT_CLIENT=0 -DLWS_WITHOUT_SERVER=0 -DLWS_UNIX_SOCK=1 -DLWS_WITH_TLS=0 -DLWS_WITH_MBEDTLS=0 -DLWS_WITH_CGI=1 -DCMAKE_BUILD_TYPE=DEBUG -DLWS_WITH_GENERIC_SESSIONS=1 -DLWS_WITH_RANGES=1 -DLWS_ROLE_WS=1 -DLWS_MAX_SMP=16 -DLWS_ROLE_H1=1 -DLWS_WITH_WOLFSSL=0 -DLWS_WITH_LIBEV=0 -DLWS_WITH_LIBEVENT=1
---
 .gitignore                                    |   1 +
 CMakeLists.txt                                |  37 +-
 lib/README.md                                 |  14 +
 lib/{ => core}/alloc.c                        |   2 +-
 lib/{ => core}/context.c                      |   2 +-
 lib/{ => core}/libwebsockets.c                |   9 +-
 lib/{ => core}/output.c                       |   2 +-
 lib/{ => core}/pollfd.c                       |   2 +-
 .../private.h}                                |  16 +-
 lib/{ => core}/service.c                      |   2 +-
 lib/event-libs/libev/libev.c                  |   2 +-
 lib/event-libs/libev/private.h                |   2 +-
 lib/event-libs/libevent/libevent.c            |   2 +-
 lib/event-libs/libevent/private.h             |   2 +-
 lib/event-libs/libuv/libuv.c                  |   9 +-
 lib/event-libs/libuv/private.h                |   2 +-
 lib/event-libs/poll/poll.c                    |   4 +-
 lib/event-libs/private.h                      |   2 +-
 lib/libwebsockets.h                           |   2 +-
 lib/misc/base64-decode.c                      |   2 +-
 lib/misc/daemonize.c                          |   2 +-
 lib/misc/getifaddrs.c                         |   2 +-
 lib/misc/jws/jwk.c                            |   2 +-
 lib/misc/jws/jws.c                            |   2 +-
 lib/misc/lws-ring.c                           |   2 +-
 lib/misc/peer-limits.c                        |   2 +-
 lib/misc/sha-1.c                              |   2 +-
 lib/misc/smtp.c                               |   2 +-
 lib/plat/lws-plat-esp32.c                     |   2 +-
 lib/plat/lws-plat-optee.c                     |   2 +-
 lib/plat/lws-plat-unix.c                      |   2 +-
 lib/plat/lws-plat-win.c                       |   2 +-
 lib/roles/cgi/cgi-server.c                    |   2 +-
 lib/roles/cgi/ops-cgi.c                       |   2 +-
 lib/roles/cgi/private.h                       |   2 +-
 lib/roles/h1/ops-h1.c                         |   2 +-
 lib/roles/h1/private.h                        |   2 +-
 lib/roles/h2/hpack.c                          |   2 +-
 lib/roles/h2/http2.c                          |   2 +-
 lib/roles/h2/ops-h2.c                         |   2 +-
 lib/roles/h2/private.h                        |   2 +-
 lib/roles/http/client/client-handshake.c      |  14 +-
 lib/roles/http/client/client.c                |   2 +-
 lib/roles/http/header.c                       |   2 +-
 lib/roles/http/private.h                      |   2 +-
 lib/roles/http/server/access-log.c            |   2 +-
 lib/roles/http/server/fops-zip.c              |   2 +-
 lib/roles/http/server/lejp-conf.c             |   2 +-
 lib/roles/http/server/lws-spa.c               |   2 +-
 lib/roles/http/server/parsers.c               |   2 +-
 lib/roles/http/server/ranges.c                |   2 +-
 lib/roles/http/server/rewrite.c               |   2 +-
 lib/roles/http/server/server.c                |   2 +-
 lib/roles/listen/ops-listen.c                 |   2 +-
 lib/roles/pipe/ops-pipe.c                     |   2 +-
 lib/roles/private.h                           |   2 +-
 lib/roles/raw/ops-raw.c                       |   2 +-
 lib/roles/ws/client-parser-ws.c               |  25 +-
 lib/roles/ws/client-ws.c                      |   2 +-
 .../ws/ext/extension-permessage-deflate.c     |   2 +-
 lib/roles/ws/ext/extension.c                  |   2 +-
 lib/roles/ws/ops-ws.c                         |  11 +-
 lib/roles/ws/private.h                        |   2 +-
 lib/roles/ws/server-ws.c                      |   2 +-
 lib/tls/mbedtls/lws-genrsa.c                  |   2 +-
 lib/tls/mbedtls/mbedtls-client.c              |   2 +-
 lib/tls/mbedtls/mbedtls-server.c              |   2 +-
 lib/tls/mbedtls/ssl.c                         |   2 +-
 lib/tls/openssl/lws-genrsa.c                  |   2 +-
 lib/tls/openssl/openssl-client.c              |   2 +-
 lib/tls/openssl/openssl-server.c              |   2 +-
 lib/tls/openssl/ssl.c                         |   2 +-
 lib/tls/private.h                             |   2 +-
 lib/tls/tls-client.c                          |   2 +-
 lib/tls/tls-server.c                          |   2 +-
 lib/tls/tls.c                                 |   2 +-
 .../minimal-http-server-sse-ring.c            |   3 +-
 test-apps/test-server-libev.c                 | 349 -----------------
 test-apps/test-server-libevent.c              | 351 ------------------
 79 files changed, 145 insertions(+), 830 deletions(-)
 create mode 100644 lib/README.md
 rename lib/{ => core}/alloc.c (98%)
 rename lib/{ => core}/context.c (99%)
 rename lib/{ => core}/libwebsockets.c (99%)
 rename lib/{ => core}/output.c (99%)
 rename lib/{ => core}/pollfd.c (99%)
 rename lib/{private-libwebsockets.h => core/private.h} (99%)
 rename lib/{ => core}/service.c (99%)
 delete mode 100644 test-apps/test-server-libev.c
 delete mode 100644 test-apps/test-server-libevent.c

diff --git a/.gitignore b/.gitignore
index 74c0e3c7..eea4395c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -49,3 +49,4 @@ build/
 doc
 /build2/
 /build3/
+/cov-int/
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 93d7210d..c59bc990 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -718,7 +718,7 @@ include_directories("${PROJECT_SOURCE_DIR}/lib")
 # Group headers and sources.
 # Some IDEs use this for nicer file structure.
 set(HDR_PRIVATE
-	lib/private-libwebsockets.h)
+	lib/core/private.h)
 
 set(HDR_PUBLIC
 	"${PROJECT_SOURCE_DIR}/lib/libwebsockets.h"
@@ -728,12 +728,12 @@ set(HDR_PUBLIC
 
 set(SOURCES
 	lib/misc/base64-decode.c
-	lib/libwebsockets.c
-	lib/service.c
-	lib/pollfd.c
-	lib/output.c
-	lib/context.c
-	lib/alloc.c
+	lib/core/libwebsockets.c
+	lib/core/service.c
+	lib/core/pollfd.c
+	lib/core/output.c
+	lib/core/context.c
+	lib/core/alloc.c
 	lib/roles/pipe/ops-pipe.c
 	lib/misc/lws-ring.c)
 
@@ -1645,29 +1645,6 @@ if ((LWS_ROLE_H1 OR LWS_ROLE_H2) AND NOT LWS_WITHOUT_TESTAPPS)
 					""
 					"")
 			endif()
-			if (NOT ((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
-				AND LWS_WITH_LIBEV)
-				create_test_app(test-server-libev
-					"test-apps/test-server-libev.c"
-					""
-					""
-					""
-					""
-					"")
-				# libev generates a big mess of warnings with gcc, maintainer claims gcc to blame
-				set_source_files_properties( test-apps/test-server-libev.c PROPERTIES COMPILE_FLAGS "-Wno-error" )
-
-			endif()
-			if (NOT ((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
-				AND LWS_WITH_LIBEVENT)
-				create_test_app(test-server-libevent
-					"test-apps/test-server-libevent.c"
-					""
-					""
-					""
-					""
-					"")
-			endif()
 		endif()
 
 		#
diff --git a/lib/README.md b/lib/README.md
new file mode 100644
index 00000000..d6475b8d
--- /dev/null
+++ b/lib/README.md
@@ -0,0 +1,14 @@
+## Library sources layout
+
+Code that goes in the libwebsockets library itself lives down ./lib
+
+Path|Sources
+---|---
+lib/core|Core lws code related to generic fd and wsi servicing and management
+lib/event-libs|Code containing optional event-lib specific adaptations
+lib/misc|Code for various mostly optional miscellaneous features
+lib/plat|Platform-specific adaptation code
+lib/roles|Code for specific optional wsi roles, eg, http/1, h2, ws, raw, etc
+lib/tls|Code supporting the various TLS libraries
+libwebsockets.h|Public API header for the whole of lws
+
diff --git a/lib/alloc.c b/lib/core/alloc.c
similarity index 98%
rename from lib/alloc.c
rename to lib/core/alloc.c
index e8a37667..f169fc37 100644
--- a/lib/alloc.c
+++ b/lib/core/alloc.c
@@ -1,4 +1,4 @@
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 #if defined(LWS_PLAT_OPTEE)
 
diff --git a/lib/context.c b/lib/core/context.c
similarity index 99%
rename from lib/context.c
rename to lib/core/context.c
index 2ccfbe96..db9151b9 100644
--- a/lib/context.c
+++ b/lib/core/context.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 #ifndef LWS_BUILD_HASH
 #define LWS_BUILD_HASH "unknown-build-hash"
diff --git a/lib/libwebsockets.c b/lib/core/libwebsockets.c
similarity index 99%
rename from lib/libwebsockets.c
rename to lib/core/libwebsockets.c
index 28bb2fe0..343b63be 100644
--- a/lib/libwebsockets.c
+++ b/lib/core/libwebsockets.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 #ifdef LWS_HAVE_SYS_TYPES_H
 #include <sys/types.h>
@@ -672,8 +672,9 @@ __lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason, const char *
 
 	if (!wsi->told_user_closed && lwsi_role_http(wsi) &&
 	    lwsi_role_server(wsi)) {
-		if (wsi->user_space && wsi->protocol_bind_balance) {
-			wsi->vhost->protocols->callback(wsi,
+		if (wsi->user_space && wsi->protocol &&
+		    wsi->protocol_bind_balance) {
+			wsi->protocol->callback(wsi,
 						LWS_CALLBACK_HTTP_DROP_PROTOCOL,
 					       wsi->user_space, NULL, 0);
 			wsi->protocol_bind_balance = 0;
@@ -2927,7 +2928,7 @@ lws_cmdline_option(int argc, const char **argv, const char *val)
 
 	while (--c > 0)
 		if (!strncmp(argv[c], val, n)) {
-			if (!*(argv[c] + n) && c != argc - 1)
+			if (!*(argv[c] + n) && c < argc - 1)
 				return argv[c + 1];
 
 			return argv[c] + n;
diff --git a/lib/output.c b/lib/core/output.c
similarity index 99%
rename from lib/output.c
rename to lib/core/output.c
index 7d1a7486..e2ff18ef 100644
--- a/lib/output.c
+++ b/lib/core/output.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 /*
  * notice this returns number of bytes consumed, or -1
diff --git a/lib/pollfd.c b/lib/core/pollfd.c
similarity index 99%
rename from lib/pollfd.c
rename to lib/core/pollfd.c
index 6a06d51f..fbc6132a 100644
--- a/lib/pollfd.c
+++ b/lib/core/pollfd.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 int
 _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa)
diff --git a/lib/private-libwebsockets.h b/lib/core/private.h
similarity index 99%
rename from lib/private-libwebsockets.h
rename to lib/core/private.h
index 9f60bd60..cb3ab4ca 100644
--- a/lib/private-libwebsockets.h
+++ b/lib/core/private.h
@@ -26,8 +26,14 @@
  #define  _GNU_SOURCE
 #endif
 
-#if defined(__COVERITY__)
- typedef struct { long double x, y; } _Float128;
+#if defined(__COVERITY__) && !defined(LWS_COVERITY_WORKAROUND)
+ #define LWS_COVERITY_WORKAROUND
+ typedef float _Float32;
+ typedef float _Float64;
+ typedef float _Float128;
+ typedef float _Float32x;
+ typedef float _Float64x;
+ typedef float _Float128x;
 #endif
 
 #ifdef LWS_HAVE_SYS_TYPES_H
@@ -1076,8 +1082,8 @@ struct lws {
 #endif
 #endif
 
-	lws_usec_t pending_timer;
-	time_t pending_timeout_set;
+	lws_usec_t pending_timer; /* hrtimer fires */
+	time_t pending_timeout_set; /* second-resolution timeout start */
 
 	/* ints */
 	int position_in_fds_table;
@@ -1443,7 +1449,7 @@ lws_pt_unlock(struct lws_context_per_thread *pt)
 		pt->lock_depth--;
 		return;
 	}
-	pt->last_lock_reason ="free";
+	pt->last_lock_reason = "free";
 	pt->lock_owner = 0;
 	//lwsl_notice("tid %d: unlock %s\n", pt->tid, pt->last_lock_reason);
 	pthread_mutex_unlock(&pt->lock);
diff --git a/lib/service.c b/lib/core/service.c
similarity index 99%
rename from lib/service.c
rename to lib/core/service.c
index a08506fb..65230588 100644
--- a/lib/service.c
+++ b/lib/core/service.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 int
 lws_callback_as_writeable(struct lws *wsi)
diff --git a/lib/event-libs/libev/libev.c b/lib/event-libs/libev/libev.c
index d60072c0..3a9853d4 100644
--- a/lib/event-libs/libev/libev.c
+++ b/lib/event-libs/libev/libev.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 static void
 lws_ev_hrtimer_cb(struct ev_loop *loop, struct ev_timer *watcher, int revents)
diff --git a/lib/event-libs/libev/private.h b/lib/event-libs/libev/private.h
index 52de727a..9359f34b 100644
--- a/lib/event-libs/libev/private.h
+++ b/lib/event-libs/libev/private.h
@@ -18,7 +18,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA  02110-1301  USA
  *
- *  This is included from private-libwebsockets.h if LWS_WITH_LIBEV
+ *  This is included from core/private.h if LWS_WITH_LIBEV
  */
 
 #include <ev.h>
diff --git a/lib/event-libs/libevent/libevent.c b/lib/event-libs/libevent/libevent.c
index 5cb47263..f4f5cfd5 100644
--- a/lib/event-libs/libevent/libevent.c
+++ b/lib/event-libs/libevent/libevent.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 static void
 lws_event_hrtimer_cb(int fd, short event, void *p)
diff --git a/lib/event-libs/libevent/private.h b/lib/event-libs/libevent/private.h
index 1c2d3607..04fbbdf6 100644
--- a/lib/event-libs/libevent/private.h
+++ b/lib/event-libs/libevent/private.h
@@ -18,7 +18,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA  02110-1301  USA
  *
- *  This is included from private-libwebsockets.h if LWS_WITH_LIBEVENT
+ *  This is included from core/private.h if LWS_WITH_LIBEVENT
  */
 
 #include <event2/event.h>
diff --git a/lib/event-libs/libuv/libuv.c b/lib/event-libs/libuv/libuv.c
index ee948b64..b5c8b85f 100644
--- a/lib/event-libs/libuv/libuv.c
+++ b/lib/event-libs/libuv/libuv.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 static void
 lws_uv_hrtimer_cb(uv_timer_t *timer
@@ -666,21 +666,18 @@ static int
 elops_init_vhost_listen_wsi_uv(struct lws *wsi)
 {
 	struct lws_context_per_thread *pt;
-	struct lws_vhost *vh = wsi->vhost;
 	int n;
 
-	if (!wsi)
-		wsi = vh->lserv_wsi;
 	if (!wsi)
 		return 0;
 	if (wsi->w_read.context)
 		return 0;
 
-	pt = &vh->context->pt[(int)wsi->tsi];
+	pt = &wsi->context->pt[(int)wsi->tsi];
 	if (!pt->uv.io_loop)
 		return 0;
 
-	wsi->w_read.context = vh->context;
+	wsi->w_read.context = wsi->context;
 	n = uv_poll_init_socket(pt->uv.io_loop,
 				&wsi->w_read.uv.watcher, wsi->desc.sockfd);
 	if (n) {
diff --git a/lib/event-libs/libuv/private.h b/lib/event-libs/libuv/private.h
index cf8f7104..58a26391 100644
--- a/lib/event-libs/libuv/private.h
+++ b/lib/event-libs/libuv/private.h
@@ -18,7 +18,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA  02110-1301  USA
  *
- *  This is included from private-libwebsockets.h if LWS_WITH_LIBUV
+ *  This is included from core/private.h if LWS_WITH_LIBUV
  */
 
 #include <uv.h>
diff --git a/lib/event-libs/poll/poll.c b/lib/event-libs/poll/poll.c
index ad6b29b0..09af5b15 100644
--- a/lib/event-libs/poll/poll.c
+++ b/lib/event-libs/poll/poll.c
@@ -18,10 +18,10 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA  02110-1301  USA
  *
- *  This is included from private-libwebsockets.h if LWS_ROLE_WS
+ *  This is included from core/private.h if LWS_ROLE_WS
  */
 
-#include <private-libwebsockets.h>
+#include <core/private.h>
 
 struct lws_event_loop_ops event_loop_ops_poll = {
 	/* name */			"poll",
diff --git a/lib/event-libs/private.h b/lib/event-libs/private.h
index 59705c9e..c36d39c8 100644
--- a/lib/event-libs/private.h
+++ b/lib/event-libs/private.h
@@ -18,7 +18,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA  02110-1301  USA
  *
- *  This is included from private-libwebsockets.h
+ *  This is included from core/private.h
  */
 
 struct lws_event_loop_ops {
diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h
index 14c65ac0..c4f8bcbe 100644
--- a/lib/libwebsockets.h
+++ b/lib/libwebsockets.h
@@ -27,7 +27,7 @@
 #ifdef __cplusplus
 #include <cstddef>
 #include <cstdarg>
-#
+
 extern "C" {
 #else
 #include <stdarg.h>
diff --git a/lib/misc/base64-decode.c b/lib/misc/base64-decode.c
index 27db51f6..64b84d78 100644
--- a/lib/misc/base64-decode.c
+++ b/lib/misc/base64-decode.c
@@ -40,7 +40,7 @@
 
 #include <stdio.h>
 #include <string.h>
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 static const char encode_orig[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 			     "abcdefghijklmnopqrstuvwxyz0123456789+/";
diff --git a/lib/misc/daemonize.c b/lib/misc/daemonize.c
index eb92821c..457f2903 100644
--- a/lib/misc/daemonize.c
+++ b/lib/misc/daemonize.c
@@ -22,7 +22,7 @@
 #include <unistd.h>
 #include <errno.h>
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 int pid_daemon;
 static char *lock_path;
diff --git a/lib/misc/getifaddrs.c b/lib/misc/getifaddrs.c
index 4f42ab45..735b899f 100644
--- a/lib/misc/getifaddrs.c
+++ b/lib/misc/getifaddrs.c
@@ -43,7 +43,7 @@
 #include <string.h>
 #include <sys/ioctl.h>
 #include <unistd.h>
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 #ifdef LWS_HAVE_SYS_SOCKIO_H
 #include <sys/sockio.h>
diff --git a/lib/misc/jws/jwk.c b/lib/misc/jws/jwk.c
index 000da84f..2337d5e8 100644
--- a/lib/misc/jws/jwk.c
+++ b/lib/misc/jws/jwk.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 #include <fcntl.h>
 #include <unistd.h>
diff --git a/lib/misc/jws/jws.c b/lib/misc/jws/jws.c
index feacfe0c..23a863ca 100644
--- a/lib/misc/jws/jws.c
+++ b/lib/misc/jws/jws.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 /*
  * JSON Web Signature is defined in RFC7515
diff --git a/lib/misc/lws-ring.c b/lib/misc/lws-ring.c
index d1079cae..bbd4df96 100644
--- a/lib/misc/lws-ring.c
+++ b/lib/misc/lws-ring.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 LWS_VISIBLE LWS_EXTERN struct lws_ring *
 lws_ring_create(size_t element_len, size_t count,
diff --git a/lib/misc/peer-limits.c b/lib/misc/peer-limits.c
index 373ec54c..66deef49 100644
--- a/lib/misc/peer-limits.c
+++ b/lib/misc/peer-limits.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 /* requires context->lock */
 static void
diff --git a/lib/misc/sha-1.c b/lib/misc/sha-1.c
index 50205a01..2e4db526 100644
--- a/lib/misc/sha-1.c
+++ b/lib/misc/sha-1.c
@@ -32,7 +32,7 @@
  * implemented by Jun-ichiro itojun Itoh <itojun@itojun.org>
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 #ifdef LWS_HAVE_SYS_TYPES_H
 #include <sys/types.h>
diff --git a/lib/misc/smtp.c b/lib/misc/smtp.c
index dc76360c..290499c1 100644
--- a/lib/misc/smtp.c
+++ b/lib/misc/smtp.c
@@ -19,7 +19,7 @@
  * MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 static unsigned int
 lwsgs_now_secs(void)
diff --git a/lib/plat/lws-plat-esp32.c b/lib/plat/lws-plat-esp32.c
index 89f1fb6b..ff53e133 100644
--- a/lib/plat/lws-plat-esp32.c
+++ b/lib/plat/lws-plat-esp32.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 #include "freertos/timers.h"
 #include <esp_attr.h>
 #include <esp_system.h>
diff --git a/lib/plat/lws-plat-optee.c b/lib/plat/lws-plat-optee.c
index ab04f842..ec649cd1 100644
--- a/lib/plat/lws-plat-optee.c
+++ b/lib/plat/lws-plat-optee.c
@@ -1,4 +1,4 @@
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 /*
  * included from libwebsockets.c for OPTEE builds
diff --git a/lib/plat/lws-plat-unix.c b/lib/plat/lws-plat-unix.c
index 2c103bb1..5b59f20a 100644
--- a/lib/plat/lws-plat-unix.c
+++ b/lib/plat/lws-plat-unix.c
@@ -20,7 +20,7 @@
  */
 
 #define _GNU_SOURCE
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 #include <pwd.h>
 #include <grp.h>
diff --git a/lib/plat/lws-plat-win.c b/lib/plat/lws-plat-win.c
index 287e144c..b7f28de6 100644
--- a/lib/plat/lws-plat-win.c
+++ b/lib/plat/lws-plat-win.c
@@ -1,7 +1,7 @@
 #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
 #define _WINSOCK_DEPRECATED_NO_WARNINGS
 #endif
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 int
 lws_plat_socket_offset(void)
diff --git a/lib/roles/cgi/cgi-server.c b/lib/roles/cgi/cgi-server.c
index 4fc8305b..e5a466a7 100644
--- a/lib/roles/cgi/cgi-server.c
+++ b/lib/roles/cgi/cgi-server.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 #if defined(WIN32) || defined(_WIN32)
 #else
diff --git a/lib/roles/cgi/ops-cgi.c b/lib/roles/cgi/ops-cgi.c
index bc42c10e..8555f7ba 100644
--- a/lib/roles/cgi/ops-cgi.c
+++ b/lib/roles/cgi/ops-cgi.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include <private-libwebsockets.h>
+#include <core/private.h>
 
 static int
 rops_handle_POLLIN_cgi(struct lws_context_per_thread *pt, struct lws *wsi,
diff --git a/lib/roles/cgi/private.h b/lib/roles/cgi/private.h
index 868dba03..b964ce03 100644
--- a/lib/roles/cgi/private.h
+++ b/lib/roles/cgi/private.h
@@ -18,7 +18,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA  02110-1301  USA
  *
- *  This is included from private-libwebsockets.h if LWS_ROLE_WS
+ *  This is included from core/private.h if LWS_ROLE_WS
  */
 
 extern struct lws_role_ops role_ops_cgi;
diff --git a/lib/roles/h1/ops-h1.c b/lib/roles/h1/ops-h1.c
index 172b8d82..8afd6268 100644
--- a/lib/roles/h1/ops-h1.c
+++ b/lib/roles/h1/ops-h1.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include <private-libwebsockets.h>
+#include <core/private.h>
 
 #ifndef min
 #define min(a, b) ((a) < (b) ? (a) : (b))
diff --git a/lib/roles/h1/private.h b/lib/roles/h1/private.h
index 17e7a90e..3f53954d 100644
--- a/lib/roles/h1/private.h
+++ b/lib/roles/h1/private.h
@@ -18,7 +18,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA  02110-1301  USA
  *
- *  This is included from private-libwebsockets.h if LWS_ROLE_H1
+ *  This is included from core/private.h if LWS_ROLE_H1
  *
  *  Most of the h1 business is defined in the h1 / h2 common roles/http dir
  */
diff --git a/lib/roles/h2/hpack.c b/lib/roles/h2/hpack.c
index 6e802764..d25849d0 100644
--- a/lib/roles/h2/hpack.c
+++ b/lib/roles/h2/hpack.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 /*
  * Official static header table for HPACK
diff --git a/lib/roles/h2/http2.c b/lib/roles/h2/http2.c
index c61eb841..d630872f 100644
--- a/lib/roles/h2/http2.c
+++ b/lib/roles/h2/http2.c
@@ -20,7 +20,7 @@
  */
 
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 /*
  * bitmap of control messages that are valid to receive for each http2 state
diff --git a/lib/roles/h2/ops-h2.c b/lib/roles/h2/ops-h2.c
index abd7e125..0e0afeea 100644
--- a/lib/roles/h2/ops-h2.c
+++ b/lib/roles/h2/ops-h2.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include <private-libwebsockets.h>
+#include <core/private.h>
 
 /*
  * These are the standardized defaults.
diff --git a/lib/roles/h2/private.h b/lib/roles/h2/private.h
index 0341b4ad..664c509d 100644
--- a/lib/roles/h2/private.h
+++ b/lib/roles/h2/private.h
@@ -18,7 +18,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA  02110-1301  USA
  *
- *  This is included from private-libwebsockets.h if LWS_ROLE_H2
+ *  This is included from core/private.h if LWS_ROLE_H2
  */
 
 extern struct lws_role_ops role_ops_h2;
diff --git a/lib/roles/http/client/client-handshake.c b/lib/roles/http/client/client-handshake.c
index a28d7fe1..337ccea9 100644
--- a/lib/roles/http/client/client-handshake.c
+++ b/lib/roles/http/client/client-handshake.c
@@ -1,4 +1,4 @@
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 static int
 lws_getaddrinfo46(struct lws *wsi, const char *ads, struct addrinfo **result)
@@ -78,7 +78,9 @@ lws_client_connect_2(struct lws *wsi)
 	 */
 
 	adsin = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
-	lws_vhost_lock(wsi->vhost);
+
+	lws_vhost_lock(wsi->vhost); /* ----------------------------------- { */
+
 	lws_start_foreach_dll_safe(struct lws_dll_lws *, d, d1,
 				   wsi->vhost->dll_active_client_conns.next) {
 		struct lws *w = lws_container_of(d, struct lws,
@@ -101,6 +103,7 @@ lws_client_connect_2(struct lws *wsi)
 			if (w->keepalive_rejected) {
 				lwsl_info("defeating pipelining due to no "
 					    "keepalive on server\n");
+				lws_vhost_unlock(wsi->vhost); /* } ---------- */
 				goto create_new_conn;
 			}
 #if defined (LWS_WITH_HTTP2)
@@ -117,7 +120,7 @@ lws_client_connect_2(struct lws *wsi)
 
 				wsi->client_h2_alpn = 1;
 				lws_wsi_h2_adopt(w, wsi);
-				lws_vhost_unlock(wsi->vhost);
+				lws_vhost_unlock(wsi->vhost); /* } ---------- */
 
 				return wsi;
 			}
@@ -140,12 +143,13 @@ lws_client_connect_2(struct lws *wsi)
 
 			wsi_piggyback = w;
 
-			lws_vhost_unlock(wsi->vhost);
+			lws_vhost_unlock(wsi->vhost); /* } ---------- */
 			goto send_hs;
 		}
 
 	} lws_end_foreach_dll_safe(d, d1);
-	lws_vhost_unlock(wsi->vhost);
+
+	lws_vhost_unlock(wsi->vhost); /* } ---------------------------------- */
 
 create_new_conn:
 #endif
diff --git a/lib/roles/http/client/client.c b/lib/roles/http/client/client.c
index e035dbac..ce42dc6c 100644
--- a/lib/roles/http/client/client.c
+++ b/lib/roles/http/client/client.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 LWS_VISIBLE LWS_EXTERN void
 lws_client_http_body_pending(struct lws *wsi, int something_left_to_send)
diff --git a/lib/roles/http/header.c b/lib/roles/http/header.c
index eef5fde0..99e56f75 100644
--- a/lib/roles/http/header.c
+++ b/lib/roles/http/header.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 #include "lextable-strings.h"
 
 
diff --git a/lib/roles/http/private.h b/lib/roles/http/private.h
index 2411ec89..2aa7a92f 100644
--- a/lib/roles/http/private.h
+++ b/lib/roles/http/private.h
@@ -18,7 +18,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA  02110-1301  USA
  *
- *  This is included from private-libwebsockets.h if either H1 or H2 roles are
+ *  This is included from core/private.h if either H1 or H2 roles are
  *  enabled
  */
 
diff --git a/lib/roles/http/server/access-log.c b/lib/roles/http/server/access-log.c
index c07e5570..0e75309d 100644
--- a/lib/roles/http/server/access-log.c
+++ b/lib/roles/http/server/access-log.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 /*
  * Produce Apache-compatible log string for wsi, like this:
diff --git a/lib/roles/http/server/fops-zip.c b/lib/roles/http/server/fops-zip.c
index f8ede1f2..4db83ce6 100644
--- a/lib/roles/http/server/fops-zip.c
+++ b/lib/roles/http/server/fops-zip.c
@@ -51,7 +51,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 #include <zlib.h>
 
diff --git a/lib/roles/http/server/lejp-conf.c b/lib/roles/http/server/lejp-conf.c
index b41779d2..e9ce854c 100644
--- a/lib/roles/http/server/lejp-conf.c
+++ b/lib/roles/http/server/lejp-conf.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 #ifndef _WIN32
 /* this is needed for Travis CI */
diff --git a/lib/roles/http/server/lws-spa.c b/lib/roles/http/server/lws-spa.c
index b3ed050c..88675ef9 100644
--- a/lib/roles/http/server/lws-spa.c
+++ b/lib/roles/http/server/lws-spa.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 #define LWS_MAX_ELEM_NAME 32
 
diff --git a/lib/roles/http/server/parsers.c b/lib/roles/http/server/parsers.c
index 21d44dba..5754f909 100644
--- a/lib/roles/http/server/parsers.c
+++ b/lib/roles/http/server/parsers.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 static const unsigned char lextable[] = {
 	#include "../lextable.h"
diff --git a/lib/roles/http/server/ranges.c b/lib/roles/http/server/ranges.c
index bc1578d7..f69c33cb 100644
--- a/lib/roles/http/server/ranges.c
+++ b/lib/roles/http/server/ranges.c
@@ -21,7 +21,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 /*
  * RFC7233 examples
diff --git a/lib/roles/http/server/rewrite.c b/lib/roles/http/server/rewrite.c
index 2f9b0c49..61bb613d 100644
--- a/lib/roles/http/server/rewrite.c
+++ b/lib/roles/http/server/rewrite.c
@@ -1,4 +1,4 @@
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 
 LWS_EXTERN struct lws_rewrite *
diff --git a/lib/roles/http/server/server.c b/lib/roles/http/server/server.c
index 83f5de8a..a308683f 100644
--- a/lib/roles/http/server/server.c
+++ b/lib/roles/http/server/server.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 const char * const method_names[] = {
 	"GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE", "CONNECT", "HEAD",
diff --git a/lib/roles/listen/ops-listen.c b/lib/roles/listen/ops-listen.c
index 02f028a8..0e463231 100644
--- a/lib/roles/listen/ops-listen.c
+++ b/lib/roles/listen/ops-listen.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include <private-libwebsockets.h>
+#include <core/private.h>
 
 static int
 rops_handle_POLLIN_listen(struct lws_context_per_thread *pt, struct lws *wsi,
diff --git a/lib/roles/pipe/ops-pipe.c b/lib/roles/pipe/ops-pipe.c
index 4fd49027..b9348d58 100644
--- a/lib/roles/pipe/ops-pipe.c
+++ b/lib/roles/pipe/ops-pipe.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include <private-libwebsockets.h>
+#include <core/private.h>
 
 static int
 rops_handle_POLLIN_pipe(struct lws_context_per_thread *pt, struct lws *wsi,
diff --git a/lib/roles/private.h b/lib/roles/private.h
index f13e14ae..4e0b3126 100644
--- a/lib/roles/private.h
+++ b/lib/roles/private.h
@@ -18,7 +18,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA  02110-1301  USA
  *
- *  This is included from private-libwebsockets.h
+ *  This is included from core/private.h
  */
 
 typedef uint32_t lws_wsi_state_t;
diff --git a/lib/roles/raw/ops-raw.c b/lib/roles/raw/ops-raw.c
index e94af046..68b52bde 100644
--- a/lib/roles/raw/ops-raw.c
+++ b/lib/roles/raw/ops-raw.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include <private-libwebsockets.h>
+#include <core/private.h>
 
 static int
 rops_handle_POLLIN_raw_skt(struct lws_context_per_thread *pt, struct lws *wsi,
diff --git a/lib/roles/ws/client-parser-ws.c b/lib/roles/ws/client-parser-ws.c
index 8e74a6dd..aa561ce0 100644
--- a/lib/roles/ws/client-parser-ws.c
+++ b/lib/roles/ws/client-parser-ws.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 /*
  * parsers.c: lws_ws_rx_sm() needs to be roughly kept in
@@ -29,10 +29,13 @@
 int lws_ws_client_rx_sm(struct lws *wsi, unsigned char c)
 {
 	int callback_action = LWS_CALLBACK_CLIENT_RECEIVE;
-	int handled, n, m, rx_draining_ext = 0;
+	int handled, m;
 	unsigned short close_code;
 	struct lws_tokens ebuf;
 	unsigned char *pp;
+#if !defined(LWS_WITHOUT_EXTENSIONS)
+	int rx_draining_ext = 0, n;
+#endif
 
 	ebuf.token = NULL;
 	ebuf.len = 0;
@@ -498,12 +501,14 @@ drain_extension:
 			wsi->socket_is_permanently_unusable = 1;
 			return -1;
 		}
-#else
-		n = 0;
 #endif
 		lwsl_debug("post inflate ebuf len %d\n", ebuf.len);
 
-		if (rx_draining_ext && !ebuf.len) {
+		if (
+#if !defined(LWS_WITHOUT_EXTENSIONS)
+		    rx_draining_ext &&
+#endif
+		    !ebuf.len) {
 			lwsl_debug("   --- ending drain on 0 read result\n");
 			goto already_done;
 		}
@@ -520,7 +525,11 @@ drain_extension:
 
 			/* we are ending partway through utf-8 character? */
 			if (!wsi->ws->rx_packet_length && wsi->ws->final &&
-			    wsi->ws->utf8 && !n) {
+			    wsi->ws->utf8
+#if !defined(LWS_WITHOUT_EXTENSIONS)
+			    && !n
+#endif
+			    ) {
 				lwsl_info("FINAL utf8 error\n");
 				lws_close_reason(wsi,
 					LWS_CLOSE_STATUS_INVALID_PAYLOAD,
@@ -550,9 +559,9 @@ utf8_fail:
 
 		if (
 				/* coverity says dead code otherwise */
-//#if !defined(LWS_WITHOUT_EXTENSIONS)
+#if !defined(LWS_WITHOUT_EXTENSIONS)
 				n &&
-//#endif
+#endif
 				ebuf.len)
 			/* extension had more... main loop will come back
 			 * we want callback to be done with this set, if so,
diff --git a/lib/roles/ws/client-ws.c b/lib/roles/ws/client-ws.c
index 10b69f01..fd6cf425 100644
--- a/lib/roles/ws/client-ws.c
+++ b/lib/roles/ws/client-ws.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include <private-libwebsockets.h>
+#include <core/private.h>
 
 /*
  * In-place str to lower case
diff --git a/lib/roles/ws/ext/extension-permessage-deflate.c b/lib/roles/ws/ext/extension-permessage-deflate.c
index e23d80d2..728aa5c7 100644
--- a/lib/roles/ws/ext/extension-permessage-deflate.c
+++ b/lib/roles/ws/ext/extension-permessage-deflate.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 #include "extension-permessage-deflate.h"
 #include <stdio.h>
 #include <string.h>
diff --git a/lib/roles/ws/ext/extension.c b/lib/roles/ws/ext/extension.c
index d20c9568..a45f3bac 100644
--- a/lib/roles/ws/ext/extension.c
+++ b/lib/roles/ws/ext/extension.c
@@ -1,4 +1,4 @@
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 #include "extension-permessage-deflate.h"
 
diff --git a/lib/roles/ws/ops-ws.c b/lib/roles/ws/ops-ws.c
index 9e2a52f0..fd036c31 100644
--- a/lib/roles/ws/ops-ws.c
+++ b/lib/roles/ws/ops-ws.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include <private-libwebsockets.h>
+#include <core/private.h>
 
 #define LWS_CPYAPP(ptr, str) { strcpy(ptr, str); ptr += strlen(str); }
 
@@ -32,12 +32,13 @@ int
 lws_ws_rx_sm(struct lws *wsi, char already_processed, unsigned char c)
 {
 	int callback_action = LWS_CALLBACK_RECEIVE;
-	int ret = 0, rx_draining_ext = 0;
+	int ret = 0;
 	unsigned short close_code;
 	struct lws_tokens ebuf;
 	unsigned char *pp;
 	int n = 0;
 #if !defined(LWS_WITHOUT_EXTENSIONS)
+	int rx_draining_ext = 0;
 	int lin;
 #endif
 
@@ -586,7 +587,11 @@ drain_extension:
 			return -1;
 		}
 #endif
-		if (rx_draining_ext && ebuf.len == 0)
+		if (
+#if !defined(LWS_WITHOUT_EXTENSIONS)
+		    rx_draining_ext &&
+#endif
+		    ebuf.len == 0)
 			goto already_done;
 
 		if (
diff --git a/lib/roles/ws/private.h b/lib/roles/ws/private.h
index 82eb84b0..71ffcaea 100644
--- a/lib/roles/ws/private.h
+++ b/lib/roles/ws/private.h
@@ -18,7 +18,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA  02110-1301  USA
  *
- *  This is included from private-libwebsockets.h if LWS_ROLE_WS
+ *  This is included from core/private.h if LWS_ROLE_WS
  */
 
 extern struct lws_role_ops role_ops_ws;
diff --git a/lib/roles/ws/server-ws.c b/lib/roles/ws/server-ws.c
index 0d8a0b98..62bcd852 100644
--- a/lib/roles/ws/server-ws.c
+++ b/lib/roles/ws/server-ws.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include <private-libwebsockets.h>
+#include <core/private.h>
 
 #define LWS_CPYAPP(ptr, str) { strcpy(ptr, str); ptr += strlen(str); }
 
diff --git a/lib/tls/mbedtls/lws-genrsa.c b/lib/tls/mbedtls/lws-genrsa.c
index ba5f6d58..70a9fcf4 100644
--- a/lib/tls/mbedtls/lws-genrsa.c
+++ b/lib/tls/mbedtls/lws-genrsa.c
@@ -21,7 +21,7 @@
  *  lws_genhash provides a hash / hmac abstraction api in lws that works the
  *  same whether you are using openssl or mbedtls hash functions underneath.
  */
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 LWS_VISIBLE void
 lws_jwk_destroy_genrsa_elements(struct lws_genrsa_elements *el)
diff --git a/lib/tls/mbedtls/mbedtls-client.c b/lib/tls/mbedtls/mbedtls-client.c
index fd9957b2..a7864ab7 100644
--- a/lib/tls/mbedtls/mbedtls-client.c
+++ b/lib/tls/mbedtls/mbedtls-client.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 static int
 OpenSSL_client_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
diff --git a/lib/tls/mbedtls/mbedtls-server.c b/lib/tls/mbedtls/mbedtls-server.c
index 2a93468d..12829a37 100644
--- a/lib/tls/mbedtls/mbedtls-server.c
+++ b/lib/tls/mbedtls/mbedtls-server.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 #include <mbedtls/x509_csr.h>
 
 int
diff --git a/lib/tls/mbedtls/ssl.c b/lib/tls/mbedtls/ssl.c
index b024dfa6..6ae9d255 100644
--- a/lib/tls/mbedtls/ssl.c
+++ b/lib/tls/mbedtls/ssl.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 #include <mbedtls/oid.h>
 
 void
diff --git a/lib/tls/openssl/lws-genrsa.c b/lib/tls/openssl/lws-genrsa.c
index 3eabe4c3..d04aea87 100644
--- a/lib/tls/openssl/lws-genrsa.c
+++ b/lib/tls/openssl/lws-genrsa.c
@@ -21,7 +21,7 @@
  *  lws_genhash provides a hash / hmac abstraction api in lws that works the
  *  same whether you are using openssl or mbedtls hash functions underneath.
  */
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 LWS_VISIBLE void
 lws_jwk_destroy_genrsa_elements(struct lws_genrsa_elements *el)
diff --git a/lib/tls/openssl/openssl-client.c b/lib/tls/openssl/openssl-client.c
index e1602eda..bff3d404 100644
--- a/lib/tls/openssl/openssl-client.c
+++ b/lib/tls/openssl/openssl-client.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 extern int openssl_websocket_private_data_index,
     openssl_SSL_CTX_private_data_index;
diff --git a/lib/tls/openssl/openssl-server.c b/lib/tls/openssl/openssl-server.c
index 8a6bf149..a87c920a 100644
--- a/lib/tls/openssl/openssl-server.c
+++ b/lib/tls/openssl/openssl-server.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 extern int openssl_websocket_private_data_index,
 	   openssl_SSL_CTX_private_data_index;
diff --git a/lib/tls/openssl/ssl.c b/lib/tls/openssl/ssl.c
index ec43770c..0ec55428 100644
--- a/lib/tls/openssl/ssl.c
+++ b/lib/tls/openssl/ssl.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 #include <errno.h>
 
 int openssl_websocket_private_data_index,
diff --git a/lib/tls/private.h b/lib/tls/private.h
index 5c6beeb9..606e2574 100644
--- a/lib/tls/private.h
+++ b/lib/tls/private.h
@@ -18,7 +18,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA  02110-1301  USA
  *
- *  This is included from private-libwebsockets.h if LWS_WITH_TLS
+ *  This is included from core/private.h if LWS_WITH_TLS
  */
 
 #if defined(LWS_WITH_TLS)
diff --git a/lib/tls/tls-client.c b/lib/tls/tls-client.c
index a122155e..70eb6f60 100644
--- a/lib/tls/tls-client.c
+++ b/lib/tls/tls-client.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 int
 lws_ssl_client_connect1(struct lws *wsi)
diff --git a/lib/tls/tls-server.c b/lib/tls/tls-server.c
index 19549a5d..440e7906 100644
--- a/lib/tls/tls-server.c
+++ b/lib/tls/tls-server.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 #if defined(LWS_WITH_MBEDTLS) || (defined(OPENSSL_VERSION_NUMBER) && \
 				  OPENSSL_VERSION_NUMBER >= 0x10002000L)
diff --git a/lib/tls/tls.c b/lib/tls/tls.c
index 916b99f2..c865c89d 100644
--- a/lib/tls/tls.c
+++ b/lib/tls/tls.c
@@ -19,7 +19,7 @@
  *  MA  02110-1301  USA
  */
 
-#include "private-libwebsockets.h"
+#include "core/private.h"
 
 /*
  * fakes POLLIN on all tls guys with buffered rx
diff --git a/minimal-examples/http-server/minimal-http-server-sse-ring/minimal-http-server-sse-ring.c b/minimal-examples/http-server/minimal-http-server-sse-ring/minimal-http-server-sse-ring.c
index 80279827..616300c6 100644
--- a/minimal-examples/http-server/minimal-http-server-sse-ring/minimal-http-server-sse-ring.c
+++ b/minimal-examples/http-server/minimal-http-server-sse-ring/minimal-http-server-sse-ring.c
@@ -120,7 +120,8 @@ wait_unlock:
 		pthread_mutex_unlock(&vhd->lock_ring); /* } ring lock ------- */
 
 wait:
-		usleep(100000 + (rand() & 0xfffff));
+		/* rand() would make more sense but coverity shrieks */
+		usleep(100000 + (time(NULL) & 0xffff));
 
 	} while (!vhd->finished);
 
diff --git a/test-apps/test-server-libev.c b/test-apps/test-server-libev.c
deleted file mode 100644
index 42eca940..00000000
--- a/test-apps/test-server-libev.c
+++ /dev/null
@@ -1,349 +0,0 @@
-/*
- * libwebsockets-test-server - libwebsockets test implementation
- *
- * Copyright (C) 2011-2018 Andy Green <andy@warmcat.com>
- *
- * This file is made available under the Creative Commons CC0 1.0
- * Universal Public Domain Dedication.
- *
- * The person who associated a work with this deed has dedicated
- * the work to the public domain by waiving all of his or her rights
- * to the work worldwide under copyright law, including all related
- * and neighboring rights, to the extent allowed by law. You can copy,
- * modify, distribute and perform the work, even for commercial purposes,
- * all without asking permission.
- *
- * The test apps are intended to be adapted for use in your code, which
- * may be proprietary.  So unlike the library itself, they are licensed
- * Public Domain.
- */
-#include <libwebsockets.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <getopt.h>
-
-int close_testing;
-int max_poll_elements;
-int debug_level = 7;
-volatile int force_exit = 0;
-struct lws_context *context;
-struct lws_plat_file_ops fops_plat;
-
-/* http server gets files from this path */
-#define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
-char *resource_path = LOCAL_RESOURCE_PATH;
-
-#if defined(LWS_WITH_TLS) && defined(LWS_HAVE_SSL_CTX_set1_param)
-char crl_path[1024] = "";
-#endif
-
-#define LWS_PLUGIN_STATIC
-#include "../plugins/protocol_dumb_increment.c"
-#include "../plugins/protocol_lws_mirror.c"
-#include "../plugins/protocol_lws_status.c"
-#include "../plugins/protocol_post_demo.c"
-
-/* list of supported protocols and callbacks */
-
-static struct lws_protocols protocols[] = {
-	/* first protocol must always be HTTP handler */
-	{ "http-only", lws_callback_http_dummy, 0, 0, },
-	LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT,
-	LWS_PLUGIN_PROTOCOL_MIRROR,
-	LWS_PLUGIN_PROTOCOL_LWS_STATUS,
-	LWS_PLUGIN_PROTOCOL_POST_DEMO,
-	{ NULL, NULL, 0, 0 } /* terminator */
-};
-
-static const struct lws_extension exts[] = {
-	{
-		"permessage-deflate",
-		lws_extension_callback_pm_deflate,
-		"permessage-deflate; client_no_context_takeover; client_max_window_bits"
-	},
-	{ NULL, NULL, NULL /* terminator */ }
-};
-
-/*
- * mount handlers for sections of the URL space
- */
-
-static const struct lws_http_mount mount_ziptest = {
-	NULL,			/* linked-list pointer to next*/
-	"/ziptest",		/* mountpoint in URL namespace on this vhost */
-	LOCAL_RESOURCE_PATH"/candide.zip",	/* handler */
-	NULL,	/* default filename if none given */
-	NULL,
-	NULL,
-	NULL,
-	NULL,
-	0,
-	0,
-	0,
-	0,
-	0,
-	0,
-	LWSMPRO_FILE,	/* origin points to a callback */
-	8,			/* strlen("/ziptest"), ie length of the mountpoint */
-	NULL,
-
-	{ NULL, NULL } // sentinel
-};
-
-static const struct lws_http_mount mount_post = {
-	(struct lws_http_mount *)&mount_ziptest, /* linked-list pointer to next*/
-	"/formtest",		/* mountpoint in URL namespace on this vhost */
-	"protocol-post-demo",	/* handler */
-	NULL,	/* default filename if none given */
-	NULL,
-	NULL,
-	NULL,
-	NULL,
-	0,
-	0,
-	0,
-	0,
-	0,
-	0,
-	LWSMPRO_CALLBACK,	/* origin points to a callback */
-	9,			/* strlen("/formtest"), ie length of the mountpoint */
-	NULL,
-
-	{ NULL, NULL } // sentinel
-};
-
-/*
- * mount a filesystem directory into the URL space at /
- * point it to our /usr/share directory with our assets in
- * stuff from here is autoserved by the library
- */
-
-static const struct lws_http_mount mount = {
-	(struct lws_http_mount *)&mount_post,	/* linked-list pointer to next*/
-	"/",		/* mountpoint in URL namespace on this vhost */
-	LOCAL_RESOURCE_PATH, /* where to go on the filesystem for that */
-	"test.html",	/* default filename if none given */
-	NULL,
-	NULL,
-	NULL,
-	NULL,
-	0,
-	0,
-	0,
-	0,
-	0,
-	0,
-	LWSMPRO_FILE,	/* mount type is a directory in a filesystem */
-	1,		/* strlen("/"), ie length of the mountpoint */
-	NULL,
-
-	{ NULL, NULL } // sentinel
-};
-
-/* this shows how to override the lws file operations.  You don't need
- * to do any of this unless you have a reason (eg, want to serve
- * compressed files without decompressing the whole archive)
- */
-static lws_fop_fd_t
-test_server_fops_open(const struct lws_plat_file_ops *fops,
-		      const char *vfs_path, const char *vpath,
-		      lws_fop_flags_t *flags)
-{
-	lws_fop_fd_t n;
-
-	/* call through to original platform implementation */
-	n = fops_plat.open(fops, vfs_path, vpath, flags);
-
-	lwsl_debug("%s: opening %s, ret %p\n", __func__, vfs_path, n);
-
-	return n;
-}
-
-void signal_cb(struct ev_loop *loop, struct ev_signal* watcher, int revents)
-{
-	lwsl_notice("Signal caught, exiting...\n");
-	force_exit = 1;
-	switch (watcher->signum) {
-	case SIGTERM:
-	case SIGINT:
-		ev_break(loop, EVBREAK_ALL);
-		break;
-	default:
-		signal(SIGABRT, SIG_DFL);
-		abort();
-		break;
-	}
-}
-
-static struct option options[] = {
-	{ "help",	no_argument,		NULL, 'h' },
-	{ "debug",	required_argument,	NULL, 'd' },
-	{ "port",	required_argument,	NULL, 'p' },
-	{ "ssl",	no_argument,		NULL, 's' },
-	{ "allow-non-ssl",	no_argument,	NULL, 'a' },
-	{ "interface",  required_argument,	NULL, 'i' },
-	{ "closetest",  no_argument,		NULL, 'c' },
-#ifndef LWS_NO_DAEMONIZE
-	{ "daemonize", 	no_argument,		NULL, 'D' },
-#endif
-	{ "resource_path", required_argument,	NULL, 'r' },
-	{ NULL, 0, 0, 0 }
-};
-
-int main(int argc, char **argv)
-{
-	int sigs[] = { SIGINT, SIGKILL, SIGTERM, SIGSEGV, SIGFPE };
-	struct ev_signal signals[ARRAY_SIZE(sigs)];
-	struct ev_loop *loop = ev_default_loop(0);
-	struct lws_context_creation_info info;
-	char interface_name[128] = "";
-	const char *iface = NULL;
-	void *foreign_loops[1];
-	char cert_path[1024];
-	char key_path[1024];
-	int use_ssl = 0;
-	int opts = 0;
-	int n = 0;
-#ifndef LWS_NO_DAEMONIZE
-	int daemonize = 0;
-#endif
-
-	/*
-	 * take care to zero down the info struct, he contains random garbaage
-	 * from the stack otherwise
-	 */
-	memset(&info, 0, sizeof info);
-	info.port = 7681;
-
-	while (n >= 0) {
-		n = getopt_long(argc, argv, "ci:hsap:d:Dr:", options, NULL);
-		if (n < 0)
-			continue;
-		switch (n) {
-#ifndef LWS_NO_DAEMONIZE
-		case 'D':
-			daemonize = 1;
-			#ifndef _WIN32
-			syslog_options &= ~LOG_PERROR;
-			#endif
-			break;
-#endif
-		case 'd':
-			debug_level = atoi(optarg);
-			break;
-		case 's':
-			use_ssl = 1;
-			break;
-		case 'a':
-			opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
-			break;
-		case 'p':
-			info.port = atoi(optarg);
-			break;
-		case 'i':
-			lws_strncpy(interface_name, optarg, sizeof interface_name);
-			iface = interface_name;
-			break;
-		case 'c':
-			close_testing = 1;
-			fprintf(stderr, " Close testing mode -- closes on "
-					   "client after 50 dumb increments"
-					   "and suppresses lws_mirror spam\n");
-			break;
-		case 'r':
-			resource_path = optarg;
-			printf("Setting resource path to \"%s\"\n", resource_path);
-			break;
-		case 'h':
-			fprintf(stderr, "Usage: test-server "
-					"[--port=<p>] [--ssl] "
-					"[-d <log bitfield>] "
-					"[--resource_path <path>]\n");
-			exit(1);
-		}
-	}
-
-#if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
-	/*
-	 * normally lock path would be /var/lock/lwsts or similar, to
-	 * simplify getting started without having to take care about
-	 * permissions or running as root, set to /tmp/.lwsts-lock
-	 */
-	if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
-		fprintf(stderr, "Failed to daemonize\n");
-		return 1;
-	}
-#endif
-
-	for (n = 0; n < (int)ARRAY_SIZE(sigs); n++) {
-		ev_init(&signals[n], signal_cb);
-		ev_signal_set(&signals[n], sigs[n]);
-		ev_signal_start(loop, &signals[n]);
-	}
-
-	/* tell the library what debug level to emit and to send it to stderr */
-	lws_set_log_level(debug_level, NULL);
-
-	lwsl_notice("libwebsockets test server libev - license LGPL2.1+SLE\n");
-	lwsl_notice("(C) Copyright 2010-2018 Andy Green <andy@warmcat.com>\n");
-
-	printf("Using resource path \"%s\"\n", resource_path);
-
-	info.iface = iface;
-	info.protocols = protocols;
-	info.extensions = exts;
-	info.mounts = &mount;
-
-	info.ssl_cert_filepath = NULL;
-	info.ssl_private_key_filepath = NULL;
-
-	if (use_ssl) {
-		if (strlen(resource_path) > sizeof(cert_path) - 32) {
-			lwsl_err("resource path too long\n");
-			return -1;
-		}
-		sprintf(cert_path, "%s/libwebsockets-test-server.pem",
-								resource_path);
-		if (strlen(resource_path) > sizeof(key_path) - 32) {
-			lwsl_err("resource path too long\n");
-			return -1;
-		}
-		sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
-								resource_path);
-
-		info.ssl_cert_filepath = cert_path;
-		info.ssl_private_key_filepath = key_path;
-
-		opts |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
-	}
-	info.gid = -1;
-	info.uid = -1;
-	info.options = opts | LWS_SERVER_OPTION_LIBEV;
-
-	foreign_loops[0] = &loop;
-	info.foreign_loops = foreign_loops;
-
-	context = lws_create_context(&info);
-	if (context == NULL) {
-		lwsl_err("libwebsocket init failed\n");
-		return -1;
-	}
-
-	/*
-	 * this shows how to override the lws file operations.  You don't need
-	 * to do any of this unless you have a reason (eg, want to serve
-	 * compressed files without decompressing the whole archive)
-	 */
-	/* stash original platform fops */
-	fops_plat = *(lws_get_fops(context));
-	/* override the active fops */
-	lws_get_fops(context)->open = test_server_fops_open;
-
-	lws_service(context, 0);
-
-	lws_context_destroy(context);
-
-	lwsl_notice("libwebsockets-test-server exited cleanly\n");
-
-	return 0;
-}
diff --git a/test-apps/test-server-libevent.c b/test-apps/test-server-libevent.c
deleted file mode 100644
index bc47f999..00000000
--- a/test-apps/test-server-libevent.c
+++ /dev/null
@@ -1,351 +0,0 @@
-/*
- * libwebsockets-test-server - libwebsockets test implementation
- *
- * Copyright (C) 2011-2017 Andy Green <andy@warmcat.com>
- *
- * This file is made available under the Creative Commons CC0 1.0
- * Universal Public Domain Dedication.
- *
- * The person who associated a work with this deed has dedicated
- * the work to the public domain by waiving all of his or her rights
- * to the work worldwide under copyright law, including all related
- * and neighboring rights, to the extent allowed by law. You can copy,
- * modify, distribute and perform the work, even for commercial purposes,
- * all without asking permission.
- *
- * The test apps are intended to be adapted for use in your code, which
- * may be proprietary.  So unlike the library itself, they are licensed
- * Public Domain.
- */
-#include <libwebsockets.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <getopt.h>
-
-int close_testing;
-int max_poll_elements;
-int debug_level = 7;
-volatile int force_exit = 0;
-struct lws_context *context;
-struct lws_plat_file_ops fops_plat;
-
-/* http server gets files from this path */
-#define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
-char *resource_path = LOCAL_RESOURCE_PATH;
-
-#if defined(LWS_WITH_TLS) && defined(LWS_HAVE_SSL_CTX_set1_param)
-char crl_path[1024] = "";
-#endif
-
-#define LWS_PLUGIN_STATIC
-#include "../plugins/protocol_dumb_increment.c"
-#include "../plugins/protocol_lws_mirror.c"
-#include "../plugins/protocol_lws_status.c"
-#include "../plugins/protocol_post_demo.c"
-
-/* list of supported protocols and callbacks */
-
-static struct lws_protocols protocols[] = {
-	/* first protocol must always be HTTP handler */
-	{ "http-only", lws_callback_http_dummy, 0, 0, },
-	LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT,
-	LWS_PLUGIN_PROTOCOL_MIRROR,
-	LWS_PLUGIN_PROTOCOL_LWS_STATUS,
-	LWS_PLUGIN_PROTOCOL_POST_DEMO,
-	{ NULL, NULL, 0, 0 } /* terminator */
-};
-
-static const struct lws_extension exts[] = {
-	{
-		"permessage-deflate",
-		lws_extension_callback_pm_deflate,
-		"permessage-deflate; client_no_context_takeover; client_max_window_bits"
-	},
-	{ NULL, NULL, NULL /* terminator */ }
-};
-
-/* this shows how to override the lws file operations.  You don't need
- * to do any of this unless you have a reason (eg, want to serve
- * compressed files without decompressing the whole archive)
- */
-static lws_fop_fd_t
-test_server_fops_open(const struct lws_plat_file_ops *fops,
-		const char *vfs_path, const char *vpath,
-		lws_fop_flags_t *flags)
-{
-	lws_fop_fd_t n;
-
-	/* call through to original platform implementation */
-	n = fops_plat.open(fops, vfs_path, vpath, flags);
-
-	lwsl_notice("%s: opening %s, ret %p\n", __func__, vfs_path, n);
-
-	return n;
-}
-
-void signal_cb(evutil_socket_t sock_fd, short events, void *ctx)
-{
-	struct event_base *event_base_loop = ctx;
-
-	lwsl_notice("Signal caught, exiting...\n");
-	force_exit = 1;
-	if (events & EV_SIGNAL)
-		event_base_loopbreak(event_base_loop);
-}
-
-/*
- * mount handlers for sections of the URL space
- */
-
-static const struct lws_http_mount mount_ziptest = {
-	NULL,			/* linked-list pointer to next*/
-	"/ziptest",		/* mountpoint in URL namespace on this vhost */
-	LOCAL_RESOURCE_PATH"/candide.zip",	/* handler */
-	NULL,	/* default filename if none given */
-	NULL,
-	NULL,
-	NULL,
-	NULL,
-	0,
-	0,
-	0,
-	0,
-	0,
-	0,
-	LWSMPRO_FILE,	/* origin points to a callback */
-	8,			/* strlen("/ziptest"), ie length of the mountpoint */
-	NULL,
-
-	{ NULL, NULL } // sentinel
-};
-
-static const struct lws_http_mount mount_post = {
-	(struct lws_http_mount *)&mount_ziptest, /* linked-list pointer to next*/
-	"/formtest",		/* mountpoint in URL namespace on this vhost */
-	"protocol-post-demo",	/* handler */
-	NULL,	/* default filename if none given */
-	NULL,
-	NULL,
-	NULL,
-	NULL,
-	0,
-	0,
-	0,
-	0,
-	0,
-	0,
-	LWSMPRO_CALLBACK,	/* origin points to a callback */
-	9,			/* strlen("/formtest"), ie length of the mountpoint */
-	NULL,
-
-	{ NULL, NULL } // sentinel
-};
-
-/*
- * mount a filesystem directory into the URL space at /
- * point it to our /usr/share directory with our assets in
- * stuff from here is autoserved by the library
- */
-
-static const struct lws_http_mount mount = {
-	(struct lws_http_mount *)&mount_post,	/* linked-list pointer to next*/
-	"/",		/* mountpoint in URL namespace on this vhost */
-	LOCAL_RESOURCE_PATH, /* where to go on the filesystem for that */
-	"test.html",	/* default filename if none given */
-	NULL,
-	NULL,
-	NULL,
-	NULL,
-	0,
-	0,
-	0,
-	0,
-	0,
-	0,
-	LWSMPRO_FILE,	/* mount type is a directory in a filesystem */
-	1,		/* strlen("/"), ie length of the mountpoint */
-	NULL,
-
-	{ NULL, NULL } // sentinel
-};
-
-static struct option options[] = {
-		{ "help",  no_argument,    NULL, 'h' },
-		{ "debug",  required_argument,  NULL, 'd' },
-		{ "port",  required_argument,  NULL, 'p' },
-		{ "ssl",  no_argument,    NULL, 's' },
-		{ "allow-non-ssl",  no_argument,  NULL, 'a' },
-		{ "interface",  required_argument,  NULL, 'i' },
-		{ "closetest",  no_argument,    NULL, 'c' },
-		{ "libevent",  no_argument,    NULL, 'e' },
-#ifndef LWS_NO_DAEMONIZE
-		{ "daemonize",   no_argument,    NULL, 'D' },
-#endif
-		{ "resource_path", required_argument,  NULL, 'r' },
-		{ NULL, 0, 0, 0 }
-};
-
-int main(int argc, char **argv)
-{
-	int sigs[] = { SIGINT, SIGKILL, SIGTERM, SIGSEGV, SIGFPE };
-	struct event *signals[ARRAY_SIZE(sigs)];
-	struct event_base *event_base_loop = event_base_new();
-	struct lws_context_creation_info info;
-	char interface_name[128] = "";
-	void *foreign_loops[1];
-	const char *iface = NULL;
-	char cert_path[1024];
-	char key_path[1024];
-	int use_ssl = 0;
-	int opts = 0;
-	int n = 0;
-#ifndef LWS_NO_DAEMONIZE
-	int daemonize = 0;
-#endif
-
-	/*
-	 * take care to zero down the info struct, he contains random garbaage
-	 * from the stack otherwise
-	 */
-	memset(&info, 0, sizeof info);
-	info.port = 7681;
-
-	while (n >= 0) {
-		n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL);
-		if (n < 0)
-			continue;
-		switch (n) {
-		case 'e':
-			opts |= LWS_SERVER_OPTION_LIBEVENT;
-			break;
-#ifndef LWS_NO_DAEMONIZE
-		case 'D':
-			daemonize = 1;
-#ifndef _WIN32
-			syslog_options &= ~LOG_PERROR;
-#endif
-			break;
-#endif
-		case 'd':
-			debug_level = atoi(optarg);
-			break;
-		case 's':
-			use_ssl = 1;
-			break;
-		case 'a':
-			opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
-			break;
-		case 'p':
-			info.port = atoi(optarg);
-			break;
-		case 'i':
-			lws_strncpy(interface_name, optarg, sizeof interface_name);
-			iface = interface_name;
-			break;
-		case 'c':
-			close_testing = 1;
-			fprintf(stderr, " Close testing mode -- closes on "
-					"client after 50 dumb increments"
-					"and suppresses lws_mirror spam\n");
-			break;
-		case 'r':
-			resource_path = optarg;
-			printf("Setting resource path to \"%s\"\n", resource_path);
-			break;
-		case 'h':
-			fprintf(stderr, "Usage: test-server "
-					"[--port=<p>] [--ssl] "
-					"[-d <log bitfield>] "
-					"[--resource_path <path>]\n");
-			exit(1);
-		}
-	}
-
-#if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
-	/*
-	 * normally lock path would be /var/lock/lwsts or similar, to
-	 * simplify getting started without having to take care about
-	 * permissions or running as root, set to /tmp/.lwsts-lock
-	 */
-	if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
-		fprintf(stderr, "Failed to daemonize\n");
-		return 1;
-	}
-#endif
-
-	for (n = 0; n < (int)ARRAY_SIZE(sigs); n++) {
-		signals[n] = evsignal_new(event_base_loop, sigs[n], signal_cb,
-					  event_base_loop);
-
-		evsignal_add(signals[n], NULL);
-	}
-
-	/* tell the library what debug level to emit and to send it to stderr */
-	lws_set_log_level(debug_level, NULL);
-
-	lwsl_notice("libwebsockets test server libevent - license LGPL2.1+SLE\n");
-	lwsl_notice("(C) Copyright 2010-2018 Andy Green <andy@warmcat.com>\n");
-
-	printf("Using resource path \"%s\"\n", resource_path);
-
-	info.iface = iface;
-	info.protocols = protocols;
-	info.extensions = exts;
-	info.mounts = &mount;
-
-	info.ssl_cert_filepath = NULL;
-	info.ssl_private_key_filepath = NULL;
-
-	if (use_ssl) {
-		if (strlen(resource_path) > sizeof(cert_path) - 32) {
-			lwsl_err("resource path too long\n");
-			return -1;
-		}
-		sprintf(cert_path, "%s/libwebsockets-test-server.pem",
-				resource_path);
-		if (strlen(resource_path) > sizeof(key_path) - 32) {
-			lwsl_err("resource path too long\n");
-			return -1;
-		}
-		sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
-				resource_path);
-
-		info.ssl_cert_filepath = cert_path;
-		info.ssl_private_key_filepath = key_path;
-
-		opts |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
-	}
-	info.gid = -1;
-	info.uid = -1;
-	info.options = opts | LWS_SERVER_OPTION_LIBEVENT;
-
-	foreign_loops[0] = event_base_loop;
-	info.foreign_loops = foreign_loops;
-
-	context = lws_create_context(&info);
-	if (context == NULL) {
-		lwsl_err("libwebsocket init failed\n");
-		return -1;
-	}
-
-	/*
-	 * this shows how to override the lws file operations.  You don't need
-	 * to do any of this unless you have a reason (eg, want to serve
-	 * compressed files without decompressing the whole archive)
-	 */
-	/* stash original platform fops */
-	fops_plat = *(lws_get_fops(context));
-	/* override the active fops */
-	lws_get_fops(context)->open = test_server_fops_open;
-
-	event_base_dispatch(event_base_loop);
-
-	for (n = 0; n < (int)ARRAY_SIZE(sigs); n++)
-		event_free(signals[n]);
-
-	lws_context_destroy(context);
-
-	lwsl_notice("libwebsockets-test-server exited cleanly\n");
-
-	return 0;
-}
-- 
GitLab