int main(int argc, const char **argv) { struct lws_context_creation_info info; const char *p, *plugin_dirs[] = { "/usr/local/share/libwebsockets-test-server/plugins", NULL }; int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE /* for LLL_ verbosity above NOTICE to be built into lws, * lws must have been configured and built with * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ /* | LLL_DEBUG */; if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS minimal http server TLS | visit https://localhost:7681\n"); signal(SIGINT, sigint_handler); memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.port = 7681; info.mounts = &mount; info.error_document_404 = "/404.html"; info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT | LWS_SERVER_OPTION_EXPLICIT_VHOSTS; info.ssl_cert_filepath = "localhost-100y.cert"; info.ssl_private_key_filepath = "localhost-100y.key"; info.plugin_dirs = plugin_dirs; info.pvo = &pvo; if (lws_cmdline_option(argc, argv, "-h")) info.options |= LWS_SERVER_OPTION_VHOST_UPG_STRICT_HOST_CHECK; context = lws_create_context(&info); if (!context) { lwsl_err("lws init failed\n"); return 1; } info.headers = &pvo_hsbph[3]; if (!lws_create_vhost(context, &info)) { lwsl_err("lws init failed\n"); return 1; } while (n >= 0 && !interrupted) n = lws_service(context, 1000); lws_context_destroy(context); return 0; }
int main(int argc, const char **argv) { struct lws_context_creation_info info; struct lws_context *context; const char *p; int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE /* for LLL_ verbosity above NOTICE to be built into lws, * lws must have been configured and built with * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ /* | LLL_DEBUG */; signal(SIGINT, sigint_handler); if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS minimal ws client + permessage-deflate + multifragment bulk message\n"); lwsl_user(" needs minimal-ws-server-pmd-bulk running to communicate with\n"); lwsl_user(" %s [-n (no exts)] [-c (compressible)]\n", argv[0]); memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.port = CONTEXT_PORT_NO_LISTEN; info.protocols = protocols; info.pvo = &pvo; if (!lws_cmdline_option(argc, argv, "-n")) info.extensions = extensions; info.pt_serv_buf_size = 32 * 1024; if (lws_cmdline_option(argc, argv, "-c")) options |= 1; context = lws_create_context(&info); if (!context) { lwsl_err("lws init failed\n"); return 1; } while (n >= 0 && !interrupted) n = lws_service(context, 1000); lws_context_destroy(context); lwsl_user("Completed %s\n", interrupted == 2 ? "OK" : "failed"); return interrupted != 2; }
int main(int argc, const char **argv) { struct lws_context_creation_info info; struct lws_context *context; const char *p; int result = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE; if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS gencrypto apis tests\n"); memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.port = CONTEXT_PORT_NO_LISTEN; info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; context = lws_create_context(&info); if (!context) { lwsl_err("lws init failed\n"); return 1; } result |= test_genaes(context); result |= test_genec(context); lwsl_user("Completed: %s\n", result ? "FAIL" : "PASS"); lws_context_destroy(context); return result; }
int main(int argc, const char **argv) { struct lws_context_creation_info info; struct lws_context *context; const char *p; int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE /* for LLL_ verbosity above NOTICE to be built into lws, * lws must have been configured and built with * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ /* | LLL_DEBUG */; signal(SIGINT, sigint_handler); if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS minimal http server POST | visit http://localhost:7681\n"); memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.port = 7681; info.protocols = protocols; info.mounts = &mount; info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE; if (lws_cmdline_option(argc, argv, "-s")) { info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; info.ssl_cert_filepath = "localhost-100y.cert"; info.ssl_private_key_filepath = "localhost-100y.key"; } context = lws_create_context(&info); if (!context) { lwsl_err("lws init failed\n"); return 1; } while (n >= 0 && !interrupted) n = lws_service(context, 1000); lws_context_destroy(context); return 0; }
int main(int argc, const char **argv) { struct lws_context_creation_info info; struct lws_context *context; const char *p; int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE /* for LLL_ verbosity above NOTICE to be built into lws, * lws must have been configured and built with * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ /* | LLL_DEBUG */; signal(SIGINT, sigint_handler); if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS minimal raw file\n"); if (argc < 2) { lwsl_user("Usage: %s <file to monitor> " " eg, /dev/ttyUSB0 or /dev/input/event0 or " "/proc/self/fd/0\n", argv[0]); return 1; } signal(SIGINT, sigint_handler); memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.port = CONTEXT_PORT_NO_LISTEN_SERVER; /* no listen socket for demo */ info.protocols = protocols; lws_strncpy(filepath, argv[1], sizeof(filepath)); context = lws_create_context(&info); if (!context) { lwsl_err("lws init failed\n"); return 1; } while (n >= 0 && !interrupted) n = lws_service(context, 1000); lws_context_destroy(context); return 0; }
int main(int argc, const char **argv) { struct lws_context_creation_info info; struct lws_context *context; const char *p; int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE /* for LLL_ verbosity above NOTICE to be built into lws, * lws must have been configured and built with * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ /* | LLL_DEBUG */; signal(SIGINT, sigint_handler); if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS minimal ws server + threads | visit http://localhost:7681\n"); memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.port = 7681; info.mounts = &mount; info.protocols = protocols; info.pvo = &pvo; /* per-vhost options */ info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE; context = lws_create_context(&info); if (!context) { lwsl_err("lws init failed\n"); return 1; } /* start the threads that create content */ while (!interrupted) if (lws_service(context, 1000)) interrupted = 1; lws_context_destroy(context); return 0; }
int main(int argc, const char **argv) { struct lws_context_creation_info info; struct lws_context *context; const char *p; int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE /* for LLL_ verbosity above NOTICE to be built into lws, * lws must have been configured and built with * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ /* | LLL_DEBUG */; signal(SIGINT, sigint_handler); if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS minimal ws client tx\n"); lwsl_user(" Run minimal-ws-broker and browse to that\n"); memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */ info.protocols = protocols; context = lws_create_context(&info); if (!context) { lwsl_err("lws init failed\n"); return 1; } while (n >= 0 && !interrupted) n = lws_service(context, 1000); lws_context_destroy(context); lwsl_user("Completed\n"); return 0; }
static int callback_minimal_broker(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { struct pss *pss = (struct pss *)user; int n; switch (reason) { case LWS_CALLBACK_PROTOCOL_INIT: goto try; case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: lwsl_err("CLIENT_CONNECTION_ERROR: %s\n", in ? (char *)in : "(null)"); client_wsi = NULL; lws_timed_callback_vh_protocol(lws_get_vhost(wsi), lws_get_protocol(wsi), LWS_CALLBACK_USER, 1); break; /* --- client callbacks --- */ case LWS_CALLBACK_CLIENT_ESTABLISHED: lwsl_user("%s: established\n", __func__); lws_set_timer_usecs(wsi, 5 * LWS_USEC_PER_SEC); break; case LWS_CALLBACK_CLIENT_WRITEABLE: if (pss->send_a_ping) { uint8_t ping[LWS_PRE + 125]; int m; pss->send_a_ping = 0; n = 0; if (!zero_length_ping) n = lws_snprintf((char *)ping + LWS_PRE, 125, "ping body!"); lwsl_user("Sending PING %d...\n", n); m = lws_write(wsi, ping + LWS_PRE, n, LWS_WRITE_PING); if (m < n) { lwsl_err("sending ping failed: %d\n", m); return -1; } lws_callback_on_writable(wsi); } break; case LWS_CALLBACK_WS_CLIENT_DROP_PROTOCOL: client_wsi = NULL; lws_timed_callback_vh_protocol(lws_get_vhost(wsi), lws_get_protocol(wsi), LWS_CALLBACK_USER, 1); break; case LWS_CALLBACK_CLIENT_RECEIVE_PONG: lwsl_user("LWS_CALLBACK_CLIENT_RECEIVE_PONG\n"); lwsl_hexdump_notice(in, len); break; case LWS_CALLBACK_TIMER: /* we want to send a ws PING every few seconds */ pss->send_a_ping = 1; lws_callback_on_writable(wsi); lws_set_timer_usecs(wsi, 5 * LWS_USEC_PER_SEC); break; /* rate-limited client connect retries */ case LWS_CALLBACK_USER: lwsl_notice("%s: LWS_CALLBACK_USER\n", __func__); try: if (connect_client()) lws_timed_callback_vh_protocol(lws_get_vhost(wsi), lws_get_protocol(wsi), LWS_CALLBACK_USER, 1); break; default: break; } return lws_callback_http_dummy(wsi, reason, user, in, len); } static const struct lws_protocols protocols[] = { { "lws-ping-test", callback_minimal_broker, sizeof(struct pss), 0, }, { NULL, NULL, 0, 0 } }; static void sigint_handler(int sig) { interrupted = 1; } int main(int argc, const char **argv) { struct lws_context_creation_info info; const char *p; int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE /* for LLL_ verbosity above NOTICE to be built into lws, * lws must have been configured and built with * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ /* | LLL_DEBUG */; signal(SIGINT, sigint_handler); if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS minimal ws client PING\n"); memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */ info.protocols = protocols; #if defined(LWS_WITH_MBEDTLS) /* * OpenSSL uses the system trust store. mbedTLS has to be told which * CA to trust explicitly. */ info.client_ssl_ca_filepath = "./libwebsockets.org.cer"; #endif if (lws_cmdline_option(argc, argv, "-z")) zero_length_ping = 1; if ((p = lws_cmdline_option(argc, argv, "--protocol"))) pro = p; if ((p = lws_cmdline_option(argc, argv, "--server"))) { server_address = p; pro = "lws-minimal"; ssl_connection |= LCCSCF_ALLOW_SELFSIGNED; } if ((p = lws_cmdline_option(argc, argv, "--port"))) port = atoi(p); /* * since we know this lws context is only ever going to be used with * one client wsis / fds / sockets at a time, let lws know it doesn't * have to use the default allocations for fd tables up to ulimit -n. * It will just allocate for 1 internal and 1 (+ 1 http2 nwsi) that we * will use. */ info.fd_limit_per_thread = 1 + 1 + 1; context = lws_create_context(&info); if (!context) { lwsl_err("lws init failed\n"); return 1; } while (n >= 0 && !interrupted) n = lws_service(context, 1000); lws_context_destroy(context); lwsl_user("Completed\n"); return 0; }
int main(int argc, const char **argv) { struct lws_context_creation_info info; struct lws_context *context; const char *p; int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE /* for LLL_ verbosity above NOTICE to be built into lws, * lws must have been configured and built with * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ /* | LLL_DEBUG */; if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS minimal http server-multivhost | visit http://localhost:7681 / 7682\n"); signal(SIGINT, sigint_handler); memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS | LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE; /* * Because of LWS_SERVER_OPTION_EXPLICIT_VHOSTS, this only creates * the context and no longer creates a default vhost */ context = lws_create_context(&info); if (!context) { lwsl_err("lws init failed\n"); return 1; } /* it's our job now to create the vhosts we want: * * - "localhost1" listen on 7681 and serve ./mount-origin-localhost1/ * - "localhost2" listen on 7682 and serve ./mount-origin-localhost2/ * - "localhost3" share 7682 and serve ./mount-origin-localhost3/ * * Note lws supports dynamic vhost creation and destruction at runtime. * When using multi-vhost with your own protocols, you must provide a * pvo for each vhost naming each protocol you want enabled on it. * minimal-ws-server-threads demonstrates how to provide pvos. */ info.port = 7681; info.mounts = &mount_localhost1; info.error_document_404 = "/404.html"; info.vhost_name = "localhost1"; if (!lws_create_vhost(context, &info)) { lwsl_err("Failed to create first vhost\n"); goto bail; } info.port = 7682; info.mounts = &mount_localhost2; info.error_document_404 = "/404.html"; info.vhost_name = "localhost2"; if (!lws_create_vhost(context, &info)) { lwsl_err("Failed to create second vhost\n"); goto bail; } /* a second vhost listens on port 7682 */ info.mounts = &mount_localhost3; info.error_document_404 = "/404.html"; info.vhost_name = "localhost3"; info.finalize = vh_destruction_notification; info.finalize_arg = NULL; if (!lws_create_vhost(context, &info)) { lwsl_err("Failed to create third vhost\n"); goto bail; } if (lws_cmdline_option(argc, argv, "--die-after-vhost")) { lwsl_warn("bailing after creating vhosts\n"); goto bail; } while (n >= 0 && !interrupted) n = lws_service(context, 1000); bail: lws_context_destroy(context); return 0; }
int main(int argc, const char **argv) { struct lws_context_creation_info info; struct lws_context *context; const char *p; int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE /* for LLL_ verbosity above NOTICE to be built into lws, * lws must have been configured and built with * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ /* | LLL_DEBUG */; signal(SIGINT, sigint_handler); if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS minimal http server dynamic | visit http://localhost:7681\n"); memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT | LWS_SERVER_OPTION_EXPLICIT_VHOSTS; /* for testing ah queue, not useful in real world */ if (lws_cmdline_option(argc, argv, "--ah1")) info.max_http_header_pool = 1; context = lws_create_context(&info); if (!context) { lwsl_err("lws init failed\n"); return 1; } /* http on 7681 */ info.port = 7681; info.protocols = protocols; info.mounts = &mount; info.vhost_name = "http"; if (!lws_create_vhost(context, &info)) { lwsl_err("Failed to create tls vhost\n"); goto bail; } /* https on 7682 */ info.port = 7682; info.error_document_404 = "/404.html"; info.ssl_cert_filepath = "localhost-100y.cert"; info.ssl_private_key_filepath = "localhost-100y.key"; info.vhost_name = "localhost"; if (!lws_create_vhost(context, &info)) { lwsl_err("Failed to create tls vhost\n"); goto bail; } while (n >= 0 && !interrupted) n = lws_service(context, 1000); bail: lws_context_destroy(context); return 0; }
int main(int argc, const char **argv) { struct lws_context_creation_info info; struct lws_client_connect_info i; struct lws_context *context; const char *p; int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE /* * For LLL_ verbosity above NOTICE to be built into lws, * lws must have been configured and built with * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE * * | LLL_INFO | LLL_PARSER | LLL_HEADER | LLL_EXT | * LLL_CLIENT | LLL_LATENCY | LLL_DEBUG */ ; signal(SIGINT, sigint_handler); if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS minimal http client [-d<verbosity>] [-l] [--h1]\n"); memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */ info.protocols = protocols; #if defined(LWS_WITH_MBEDTLS) /* * OpenSSL uses the system trust store. mbedTLS has to be told which * CA to trust explicitly. */ info.client_ssl_ca_filepath = "./warmcat.com.cer"; #endif context = lws_create_context(&info); if (!context) { lwsl_err("lws init failed\n"); return 1; } memset(&i, 0, sizeof i); /* otherwise uninitialized garbage */ i.context = context; i.ssl_connection = LCCSCF_USE_SSL; if (lws_cmdline_option(argc, argv, "-l")) { i.port = 7681; i.address = "localhost"; i.ssl_connection |= LCCSCF_ALLOW_SELFSIGNED; } else { i.port = 443; i.address = "warmcat.com"; } if (lws_cmdline_option(argc, argv, "--h1")) i.alpn = "http/1.1"; i.path = "/"; i.host = i.address; i.origin = i.address; i.method = "GET"; i.protocol = protocols[0].name; i.pwsi = &client_wsi; lws_client_connect_via_info(&i); while (n >= 0 && client_wsi && !interrupted) n = lws_service(context, 1000); lws_context_destroy(context); lwsl_user("Completed: %s\n", bad ? "failed" : "OK"); return bad; }
int main(int argc, const char **argv) { const char *p; int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE /* for LLL_ verbosity above NOTICE to be built into lws, * lws must have been configured and built with * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ /* | LLL_DEBUG */; if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS minimal http server eventlib + foreign loop |" " visit http://localhost:7681\n"); /* * We prepare the info here, but don't use it until later in the * timer callback, to demonstrate the independence of the foreign loop * and lws. */ memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.port = 7681; info.mounts = &mount; info.error_document_404 = "/404.html"; info.pcontext = &context; if (lws_cmdline_option(argc, argv, "-s")) { info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; info.ssl_cert_filepath = "localhost-100y.cert"; info.ssl_private_key_filepath = "localhost-100y.key"; } if (lws_cmdline_option(argc, argv, "--uv")) info.options |= LWS_SERVER_OPTION_LIBUV; else if (lws_cmdline_option(argc, argv, "--event")) info.options |= LWS_SERVER_OPTION_LIBEVENT; else if (lws_cmdline_option(argc, argv, "--ev")) info.options |= LWS_SERVER_OPTION_LIBEV; else { lwsl_err("This app only makes sense when used\n"); lwsl_err(" with a foreign loop, --uv, --event, or --ev\n"); return 1; } lwsl_user(" This app creates a foreign event loop with a timer +\n"); lwsl_user(" signalhandler, and performs a test in three phases:\n"); lwsl_user("\n"); lwsl_user(" 1) 5s: Runs the loop with just the timer\n"); lwsl_user(" 2) 10s: create an lws context serving on localhost:7681\n"); lwsl_user(" using the same foreign loop. Destroy it after 10s.\n"); lwsl_user(" 3) 5s: Run the loop again with just the timer\n"); lwsl_user("\n"); lwsl_user(" Finally close only the timer and signalhandler and\n"); lwsl_user(" exit the loop cleanly\n"); lwsl_user("\n"); /* foreign loop specific startup and run */ #if defined(LWS_WITH_LIBUV) if (info.options & LWS_SERVER_OPTION_LIBUV) foreign_event_loop_init_and_run_libuv(); #endif #if defined(LWS_WITH_LIBEVENT) if (info.options & LWS_SERVER_OPTION_LIBEVENT) foreign_event_loop_init_and_run_libevent(); #endif #if defined(LWS_WITH_LIBEV) if (info.options & LWS_SERVER_OPTION_LIBEV) foreign_event_loop_init_and_run_libev(); #endif lws_context_destroy(context); /* foreign loop specific cleanup and exit */ #if defined(LWS_WITH_LIBUV) if (info.options & LWS_SERVER_OPTION_LIBUV) foreign_event_loop_cleanup_libuv(); #endif #if defined(LWS_WITH_LIBEVENT) if (info.options & LWS_SERVER_OPTION_LIBEVENT) foreign_event_loop_cleanup_libevent(); #endif #if defined(LWS_WITH_LIBEV) if (info.options & LWS_SERVER_OPTION_LIBEV) foreign_event_loop_cleanup_libev(); #endif lwsl_user("%s: exiting...\n", __func__); return 0; }
int main(int argc, const char **argv) { struct lws_context_creation_info info; const char *p; int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE /* for LLL_ verbosity above NOTICE to be built into lws, * lws must have been configured and built with * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ /* | LLL_DEBUG */; signal(SIGINT, sigint_handler); if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS minimal ws client SPAM\n"); memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */ info.protocols = protocols; #if defined(LWS_WITH_MBEDTLS) /* * OpenSSL uses the system trust store. mbedTLS has to be told which * CA to trust explicitly. */ info.client_ssl_ca_filepath = "./libwebsockets.org.cer"; #endif if ((p = lws_cmdline_option(argc, argv, "--server"))) { server_address = p; ssl_connection |= LCCSCF_ALLOW_SELFSIGNED; } if ((p = lws_cmdline_option(argc, argv, "--port"))) port = atoi(p); if ((p = lws_cmdline_option(argc, argv, "-l"))) limit = atoi(p); if ((p = lws_cmdline_option(argc, argv, "-c"))) concurrent = atoi(p); if (lws_cmdline_option(argc, argv, "-n")) { ssl_connection = 0; info.options = 0; } if (concurrent > (int)LWS_ARRAY_SIZE(clients)) { lwsl_err("%s: -c %d larger than max concurrency %d\n", __func__, concurrent, (int)LWS_ARRAY_SIZE(clients)); return 1; } /* * since we know this lws context is only ever going to be used with * one client wsis / fds / sockets at a time, let lws know it doesn't * have to use the default allocations for fd tables up to ulimit -n. * It will just allocate for 1 internal and n (+ 1 http2 nwsi) that we * will use. */ info.fd_limit_per_thread = 1 + concurrent + 1; context = lws_create_context(&info); if (!context) { lwsl_err("lws init failed\n"); return 1; } while (n >= 0 && !interrupted) n = lws_service(context, 1000); lws_context_destroy(context); if (tries == limit && closed == tries) { lwsl_user("Completed\n"); return 0; } lwsl_err("Failed\n"); return 1; }
int main(int argc, const char **argv) { struct lws_tokenize ts; lws_tokenize_elem e; const char *p; int n, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE /* for LLL_ verbosity above NOTICE to be built into lws, * lws must have been configured and built with * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ /* | LLL_DEBUG */; int fail = 0, ok = 0, flags = 0; if ((p = lws_cmdline_option(argc, argv, "-d"))) logs = atoi(p); lws_set_log_level(logs, NULL); lwsl_user("LWS API selftest: lws_tokenize\n"); if ((p = lws_cmdline_option(argc, argv, "-f"))) flags = atoi(p); p = lws_cmdline_option(argc, argv, "-s"); for (n = 0; n < (int)LWS_ARRAY_SIZE(tests); n++) { int m = 0, in_fail = fail; struct expected *exp = tests[n].exp; ts.start = tests[n].string; ts.len = strlen(ts.start); ts.flags = tests[n].flags; do { e = lws_tokenize(&ts); lwsl_info("{ %s, \"%.*s\", %d }\n", element_names[e + LWS_TOKZE_ERRS], (int)ts.token_len, ts.token, (int)ts.token_len); if (m == (int)tests[n].count) { lwsl_notice("fail: expected end earlier\n"); fail++; break; } if (e != exp->e) { lwsl_notice("fail... tok %s vs expected %s\n", element_names[e + LWS_TOKZE_ERRS], element_names[exp->e + LWS_TOKZE_ERRS]); fail++; break; } if (e > 0 && (ts.token_len != exp->len || memcmp(exp->value, ts.token, exp->len))) { lwsl_notice("fail token mismatch\n"); fail++; break; } m++; exp++; } while (e > 0); if (fail == in_fail) ok++; } if (p) { ts.start = p; ts.len = strlen(p); ts.flags = flags; printf("\t{\n\t\t\"%s\",\n" "\t\texpected%d, LWS_ARRAY_SIZE(expected%d),\n\t\t", p, (int)LWS_ARRAY_SIZE(tests) + 1, (int)LWS_ARRAY_SIZE(tests) + 1); if (!flags) printf("0\n\t},\n"); else { if (flags & LWS_TOKENIZE_F_MINUS_NONTERM) printf("LWS_TOKENIZE_F_MINUS_NONTERM"); if (flags & LWS_TOKENIZE_F_AGG_COLON) { if (flags & 1) printf(" | "); printf("LWS_TOKENIZE_F_AGG_COLON"); } if (flags & LWS_TOKENIZE_F_COMMA_SEP_LIST) { if (flags & 3) printf(" | "); printf("LWS_TOKENIZE_F_COMMA_SEP_LIST"); } if (flags & LWS_TOKENIZE_F_RFC7230_DELIMS) { if (flags & 7) printf(" | "); printf("LWS_TOKENIZE_F_RFC7230_DELIMS"); } if (flags & LWS_TOKENIZE_F_DOT_NONTERM) { if (flags & 15) printf(" | "); printf("LWS_TOKENIZE_F_DOT_NONTERM"); } if (flags & LWS_TOKENIZE_F_NO_FLOATS) { if (flags & 31) printf(" | "); printf("LWS_TOKENIZE_F_NO_FLOATS"); } printf("\n\t},\n"); } printf("\texpected%d[] = {\n", (int)LWS_ARRAY_SIZE(tests) + 1); do { e = lws_tokenize(&ts); printf("\t\t{ %s, \"%.*s\", %d },\n", element_names[e + LWS_TOKZE_ERRS], (int)ts.token_len, ts.token, (int)ts.token_len); } while (e > 0); printf("\t}\n"); } lwsl_user("Completed: PASS: %d, FAIL: %d\n", ok, fail); return !(ok && !fail); }