lws-vhost-destroy
[platform/upstream/libwebsockets.git] / test-server / test-server.c
index f4c4bc6..0765cfb 100644 (file)
@@ -29,7 +29,8 @@ struct lws_pollfd *pollfds;
 int *fd_lookup;
 int count_pollfds;
 #endif
-volatile int force_exit = 0;
+volatile int force_exit = 0, dynamic_vhost_enable = 0;
+struct lws_vhost *dynamic_vhost;
 struct lws_context *context;
 struct lws_plat_file_ops fops_plat;
 
@@ -159,6 +160,20 @@ test_server_fops_open(const struct lws_plat_file_ops *fops,
 
 void sighandler(int sig)
 {
+#if !defined(WIN32) && !defined(_WIN32)
+       /* because windows is too dumb to have SIGUSR1... */
+       if (sig == SIGUSR1) {
+               /*
+                * For testing, you can fire a SIGUSR1 at the test server
+                * to toggle the existence of an identical server on
+                * port + 1
+                */
+               dynamic_vhost_enable ^= 1;
+               lwsl_notice("SIGUSR1: dynamic_vhost_enable: %d\n",
+                               dynamic_vhost_enable);
+               return;
+       }
+#endif
        force_exit = 1;
        lws_cancel_service(context);
 }
@@ -266,6 +281,7 @@ int main(int argc, char **argv)
                        break;
                case 's':
                        use_ssl = 1;
+                       opts |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
                        break;
                case 'a':
                        opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
@@ -346,6 +362,11 @@ int main(int argc, char **argv)
 #endif
 
        signal(SIGINT, sighandler);
+#if !defined(WIN32) && !defined(_WIN32)
+       /* because windows is too dumb to have SIGUSR1... */
+       /* dynamic vhost create / destroy toggle (on port + 1) */
+       signal(SIGUSR1, sighandler);
+#endif
 
 #ifndef _WIN32
        /* we will only try to log things according to our debug_level */
@@ -400,7 +421,7 @@ int main(int argc, char **argv)
        info.gid = gid;
        info.uid = uid;
        info.max_http_header_pool = 16;
-       info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8 | LWS_SERVER_OPTION_EXPLICIT_VHOSTS | LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
+       info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8 | LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
        info.extensions = exts;
        info.timeout_secs = 5;
        info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
@@ -433,6 +454,14 @@ int main(int argc, char **argv)
                return -1;
        }
 
+       /*
+        * For testing dynamic vhost create / destroy later, we use port + 1
+        * Normally if you were creating more vhosts, you would set info.name
+        * for each to be the hostname external clients use to reach it
+        */
+
+       info.port++;
+
 #if !defined(LWS_NO_CLIENT) && defined(LWS_OPENSSL_SUPPORT)
        lws_init_vhost_client_ssl(&info, vhost);
 #endif
@@ -515,6 +544,17 @@ int main(int argc, char **argv)
 
                n = lws_service(context, 50);
 #endif
+
+               if (dynamic_vhost_enable && !dynamic_vhost) {
+                       lwsl_notice("creating dynamic vhost...\n");
+                       dynamic_vhost = lws_create_vhost(context, &info);
+               } else
+                       if (!dynamic_vhost_enable && dynamic_vhost) {
+                               lwsl_notice("destroying dynamic vhost...\n");
+                               lws_vhost_destroy(dynamic_vhost);
+                               dynamic_vhost = NULL;
+                       }
+
        }
 
 #ifdef EXTERNAL_POLL