2 * libwebsockets-test-server for libev - libwebsockets test implementation
4 * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #define DI_HANDLED_BY_PLUGIN
23 #include "test-server.h"
27 int max_poll_elements;
29 struct lws_context *context;
30 struct lws_plat_file_ops fops_plat;
32 /* http server gets files from this path */
33 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
34 char *resource_path = LOCAL_RESOURCE_PATH;
36 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
37 char crl_path[1024] = "";
40 /* singlethreaded version --> no locks */
42 void test_server_lock(int care)
45 void test_server_unlock(int care)
49 #define LWS_PLUGIN_STATIC
50 #include "../plugins/protocol_dumb_increment.c"
51 #include "../plugins/protocol_lws_mirror.c"
52 #include "../plugins/protocol_lws_status.c"
53 #include "../plugins/protocol_lws_meta.c"
56 * This demo server shows how to use libwebsockets for one or more
57 * websocket protocols in the same server
59 * It defines the following websocket protocols:
61 * dumb-increment-protocol: once the socket is opened, an incrementing
62 * ascii string is sent down it every 50ms.
63 * If you send "reset\n" on the websocket, then
64 * the incrementing number is reset to 0.
66 * lws-mirror-protocol: copies any received packet to every connection also
67 * using this protocol, including the sender
74 PROTOCOL_DUMB_INCREMENT,
83 /* list of supported protocols and callbacks */
85 static struct lws_protocols protocols[] = {
86 /* first protocol must always be HTTP handler */
89 "http-only", /* name */
90 callback_http, /* callback */
91 sizeof (struct per_session_data__http), /* per_session_data_size */
92 0, /* max frame size / rx buffer */
94 LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT,
95 LWS_PLUGIN_PROTOCOL_MIRROR,
96 LWS_PLUGIN_PROTOCOL_LWS_STATUS,
97 LWS_PLUGIN_PROTOCOL_LWS_META,
98 { NULL, NULL, 0, 0 } /* terminator */
101 static const struct lws_extension exts[] = {
103 "permessage-deflate",
104 lws_extension_callback_pm_deflate,
105 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
109 lws_extension_callback_pm_deflate,
112 { NULL, NULL, NULL /* terminator */ }
115 void signal_cb(uv_signal_t *watcher, int signum)
117 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
118 switch (watcher->signum) {
123 signal(SIGABRT, SIG_DFL);
127 lws_libuv_stop(context);
131 static struct option options[] = {
132 { "help", no_argument, NULL, 'h' },
133 { "debug", required_argument, NULL, 'd' },
134 { "port", required_argument, NULL, 'p' },
135 { "ssl", no_argument, NULL, 's' },
136 { "allow-non-ssl", no_argument, NULL, 'a' },
137 { "interface", required_argument, NULL, 'i' },
138 { "closetest", no_argument, NULL, 'c' },
139 { "libev", no_argument, NULL, 'e' },
140 { "foreign", no_argument, NULL, 'f' },
141 #ifndef LWS_NO_DAEMONIZE
142 { "daemonize", no_argument, NULL, 'D' },
144 { "resource_path", required_argument, NULL, 'r' },
148 #if UV_VERSION_MAJOR > 0
149 /* ----- this code is only needed for foreign / external libuv tests -----*/
156 static void timer_cb(uv_timer_t *t)
158 struct counter *c = t->data;
160 lwsl_notice(" timer %p cb, count %d, loop has %d handles\n",
161 t, c->cur, t->loop->active_handles);
163 if (c->cur++ == c->lim) {
164 lwsl_debug("stop loop from timer\n");
171 static void timer_close_cb(uv_handle_t *h)
173 lwsl_notice("timer close cb %p, loop has %d handles\n",
174 h, h->loop->active_handles);
177 void outer_signal_cb(uv_signal_t *s, int signum)
179 lwsl_notice("Foreign loop got signal %d\n", signum);
184 static void lws_uv_close_cb(uv_handle_t *handle)
186 //lwsl_err("%s\n", __func__);
189 static void lws_uv_walk_cb(uv_handle_t *handle, void *arg)
191 uv_close(handle, lws_uv_close_cb);
194 /* --- end of foreign test code ---- */
197 int main(int argc, char **argv)
199 struct lws_context_creation_info info;
200 char interface_name[128] = "";
201 #if UV_VERSION_MAJOR > 0
202 /* --- only needed for foreign loop test ---> */
204 uv_signal_t signal_outer;
205 uv_timer_t timer_outer;
207 int foreign_libuv_loop = 0;
208 /* <--- only needed for foreign loop test --- */
210 const char *iface = NULL;
211 char cert_path[1024];
217 int syslog_options = LOG_PID | LOG_PERROR;
219 #ifndef LWS_NO_DAEMONIZE
224 * take care to zero down the info struct, he contains random garbaage
225 * from the stack otherwise
227 memset(&info, 0, sizeof info);
231 n = getopt_long(argc, argv, "feci:hsap:d:Dr:", options, NULL);
236 #if UV_VERSION_MAJOR > 0
237 foreign_libuv_loop = 1;
241 opts |= LWS_SERVER_OPTION_LIBEV;
243 #ifndef LWS_NO_DAEMONIZE
247 syslog_options &= ~LOG_PERROR;
252 debug_level = atoi(optarg);
258 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
261 info.port = atoi(optarg);
264 strncpy(interface_name, optarg, sizeof interface_name);
265 interface_name[(sizeof interface_name) - 1] = '\0';
266 iface = interface_name;
270 fprintf(stderr, " Close testing mode -- closes on "
271 "client after 50 dumb increments"
272 "and suppresses lws_mirror spam\n");
275 resource_path = optarg;
276 printf("Setting resource path to \"%s\"\n", resource_path);
279 fprintf(stderr, "Usage: test-server "
280 "[--port=<p>] [--ssl] "
281 "[-d <log bitfield>] "
282 "[--resource_path <path>]\n");
288 #if !defined(LWS_NO_DAEMONIZE)
290 * normally lock path would be /var/lock/lwsts or similar, to
291 * simplify getting started without having to take care about
292 * permissions or running as root, set to /tmp/.lwsts-lock
294 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
295 fprintf(stderr, "Failed to daemonize\n");
300 /* we will only try to log things according to our debug_level */
301 setlogmask(LOG_UPTO (LOG_DEBUG));
302 openlog("lwsts", syslog_options, LOG_DAEMON);
305 /* tell the library what debug level to emit and to send it to syslog */
306 lws_set_log_level(debug_level, lwsl_emit_syslog);
308 lwsl_notice("libwebsockets test server libuv - license LGPL2.1+SLE\n");
309 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
311 lwsl_info("Using resource path \"%s\"\n", resource_path);
314 info.protocols = protocols;
315 info.extensions = exts;
317 info.ssl_cert_filepath = NULL;
318 info.ssl_private_key_filepath = NULL;
321 if (strlen(resource_path) > sizeof(cert_path) - 32) {
322 lwsl_err("resource path too long\n");
325 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
327 if (strlen(resource_path) > sizeof(key_path) - 32) {
328 lwsl_err("resource path too long\n");
331 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
334 info.ssl_cert_filepath = cert_path;
335 info.ssl_private_key_filepath = key_path;
336 opts |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
340 info.max_http_header_pool = 1;
341 info.timeout_secs = 5;
342 info.options = opts | LWS_SERVER_OPTION_LIBUV;
344 #if UV_VERSION_MAJOR > 0
345 if (foreign_libuv_loop) {
346 /* create the foreign loop */
349 /* run some timer on that loop just so loop is not 'clean' */
351 uv_signal_init(&loop, &signal_outer);
352 uv_signal_start(&signal_outer, outer_signal_cb, SIGINT);
354 uv_timer_init(&loop, &timer_outer);
355 timer_outer.data = &ctr;
357 ctr.lim = ctr.cur + 5;
359 uv_timer_start(&timer_outer, timer_cb, 0, 1000);
360 lwsl_notice("running loop without libwebsockets for %d s\n", ctr.lim);
362 uv_run(&loop, UV_RUN_DEFAULT);
364 /* timer will stop loop and we will get here */
368 context = lws_create_context(&info);
369 if (context == NULL) {
370 lwsl_err("libwebsocket init failed\n");
374 lws_uv_sigint_cfg(context, 1, signal_cb);
376 #if UV_VERSION_MAJOR > 0
377 if (foreign_libuv_loop)
378 /* we have our own uv loop outside of lws */
379 lws_uv_initloop(context, &loop, 0);
384 * lws will create his own libuv loop in the context
386 if (lws_uv_initloop(context, NULL, 0)) {
387 lwsl_err("lws_uv_initloop failed\n");
393 #if UV_VERSION_MAJOR > 0
394 if (foreign_libuv_loop) {
396 * prepare inner timer on loop, to run along with lws.
397 * Will exit after 5s while lws keeps running
399 struct counter ctr_inner = { 0, 3, 0 };
401 uv_timer_t timer_inner;
402 uv_timer_init(&loop, &timer_inner);
403 timer_inner.data = &ctr_inner;
404 uv_timer_start(&timer_inner, timer_cb, 200, 1000);
406 /* make this timer long-lived, should keep
407 * firing after lws exits */
409 ctr.lim = ctr.cur + 1000;
410 uv_timer_start(&timer_outer, timer_cb, 0, 1000);
412 uv_run(&loop, UV_RUN_DEFAULT);
414 /* we are here either because signal stopped us,
415 * or outer timer expired */
417 /* close short timer */
418 uv_timer_stop(&timer_inner);
419 uv_close((uv_handle_t*)&timer_inner, timer_close_cb);
422 lwsl_notice("Destroying lws context\n");
425 lws_context_destroy(context);
427 lwsl_notice("Please wait while the outer libuv test continues for 10s\n");
429 ctr.lim = ctr.cur + 10;
431 /* try and run outer timer for 10 more seconds,
432 * (or sigint outer handler) after lws has left the loop */
433 uv_run(&loop, UV_RUN_DEFAULT);
435 /* Clean up the foreign loop now */
437 /* PHASE 1: stop and close things we created
440 uv_timer_stop(&timer_outer);
441 uv_close((uv_handle_t*)&timer_outer, timer_close_cb);
442 uv_signal_stop(&signal_outer);
446 uv_run(&loop, UV_RUN_NOWAIT);
448 /* PHASE 2: close anything remaining */
450 uv_walk(&loop, lws_uv_walk_cb, NULL);
454 uv_run(&loop, UV_RUN_NOWAIT);
456 /* PHASE 3: close the UV loop itself */
458 e = uv_loop_close(&loop);
459 lwsl_notice("uv loop close rc %s\n",
460 e ? uv_strerror(e) : "ok");
462 /* PHASE 4: finalize context destruction */
464 lws_context_destroy2(context);
468 lws_libuv_run(context, 0);
471 lws_context_destroy(context);
472 lws_context_destroy2(context);
475 lwsl_notice("libwebsockets-test-server exited cleanly\n");