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"
55 * This demo server shows how to use libwebsockets for one or more
56 * websocket protocols in the same server
58 * It defines the following websocket protocols:
60 * dumb-increment-protocol: once the socket is opened, an incrementing
61 * ascii string is sent down it every 50ms.
62 * If you send "reset\n" on the websocket, then
63 * the incrementing number is reset to 0.
65 * lws-mirror-protocol: copies any received packet to every connection also
66 * using this protocol, including the sender
73 PROTOCOL_DUMB_INCREMENT,
81 /* list of supported protocols and callbacks */
83 static struct lws_protocols protocols[] = {
84 /* first protocol must always be HTTP handler */
87 "http-only", /* name */
88 callback_http, /* callback */
89 sizeof (struct per_session_data__http), /* per_session_data_size */
90 0, /* max frame size / rx buffer */
92 LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT,
93 LWS_PLUGIN_PROTOCOL_MIRROR,
94 LWS_PLUGIN_PROTOCOL_LWS_STATUS,
95 { NULL, NULL, 0, 0 } /* terminator */
98 static const struct lws_extension exts[] = {
100 "permessage-deflate",
101 lws_extension_callback_pm_deflate,
102 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
106 lws_extension_callback_pm_deflate,
109 { NULL, NULL, NULL /* terminator */ }
112 void signal_cb(uv_signal_t *watcher, int signum)
114 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
115 switch (watcher->signum) {
120 signal(SIGABRT, SIG_DFL);
124 lws_libuv_stop(context);
128 static struct option options[] = {
129 { "help", no_argument, NULL, 'h' },
130 { "debug", required_argument, NULL, 'd' },
131 { "port", required_argument, NULL, 'p' },
132 { "ssl", no_argument, NULL, 's' },
133 { "allow-non-ssl", no_argument, NULL, 'a' },
134 { "interface", required_argument, NULL, 'i' },
135 { "closetest", no_argument, NULL, 'c' },
136 { "libev", no_argument, NULL, 'e' },
137 { "foreign", no_argument, NULL, 'f' },
138 #ifndef LWS_NO_DAEMONIZE
139 { "daemonize", no_argument, NULL, 'D' },
141 { "resource_path", required_argument, NULL, 'r' },
145 #if UV_VERSION_MAJOR > 0
146 /* ----- this code is only needed for foreign / external libuv tests -----*/
153 static void timer_cb(uv_timer_t *t)
155 struct counter *c = t->data;
157 lwsl_notice(" timer %p cb, count %d, loop has %d handles\n",
158 t, c->cur, t->loop->active_handles);
160 if (c->cur++ == c->lim) {
161 lwsl_debug("stop loop from timer\n");
168 static void timer_close_cb(uv_handle_t *h)
170 lwsl_notice("timer close cb %p, loop has %d handles\n",
171 h, h->loop->active_handles);
174 void outer_signal_cb(uv_signal_t *s, int signum)
176 lwsl_notice("Foreign loop got signal %d\n", signum);
181 static void lws_uv_close_cb(uv_handle_t *handle)
183 //lwsl_err("%s\n", __func__);
186 static void lws_uv_walk_cb(uv_handle_t *handle, void *arg)
188 uv_close(handle, lws_uv_close_cb);
191 /* --- end of foreign test code ---- */
194 int main(int argc, char **argv)
196 struct lws_context_creation_info info;
197 char interface_name[128] = "";
198 #if UV_VERSION_MAJOR > 0
199 /* --- only needed for foreign loop test ---> */
201 uv_signal_t signal_outer;
202 uv_timer_t timer_outer;
204 int foreign_libuv_loop = 0;
205 /* <--- only needed for foreign loop test --- */
207 const char *iface = NULL;
208 char cert_path[1024];
214 int syslog_options = LOG_PID | LOG_PERROR;
216 #ifndef LWS_NO_DAEMONIZE
221 * take care to zero down the info struct, he contains random garbaage
222 * from the stack otherwise
224 memset(&info, 0, sizeof info);
228 n = getopt_long(argc, argv, "feci:hsap:d:Dr:", options, NULL);
233 #if UV_VERSION_MAJOR > 0
234 foreign_libuv_loop = 1;
238 opts |= LWS_SERVER_OPTION_LIBEV;
240 #ifndef LWS_NO_DAEMONIZE
244 syslog_options &= ~LOG_PERROR;
249 debug_level = atoi(optarg);
255 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
258 info.port = atoi(optarg);
261 strncpy(interface_name, optarg, sizeof interface_name);
262 interface_name[(sizeof interface_name) - 1] = '\0';
263 iface = interface_name;
267 fprintf(stderr, " Close testing mode -- closes on "
268 "client after 50 dumb increments"
269 "and suppresses lws_mirror spam\n");
272 resource_path = optarg;
273 printf("Setting resource path to \"%s\"\n", resource_path);
276 fprintf(stderr, "Usage: test-server "
277 "[--port=<p>] [--ssl] "
278 "[-d <log bitfield>] "
279 "[--resource_path <path>]\n");
285 #if !defined(LWS_NO_DAEMONIZE)
287 * normally lock path would be /var/lock/lwsts or similar, to
288 * simplify getting started without having to take care about
289 * permissions or running as root, set to /tmp/.lwsts-lock
291 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
292 fprintf(stderr, "Failed to daemonize\n");
297 /* we will only try to log things according to our debug_level */
298 setlogmask(LOG_UPTO (LOG_DEBUG));
299 openlog("lwsts", syslog_options, LOG_DAEMON);
302 /* tell the library what debug level to emit and to send it to syslog */
303 lws_set_log_level(debug_level, lwsl_emit_syslog);
305 lwsl_notice("libwebsockets test server libuv - license LGPL2.1+SLE\n");
306 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
308 lwsl_info("Using resource path \"%s\"\n", resource_path);
311 info.protocols = protocols;
312 info.extensions = exts;
314 info.ssl_cert_filepath = NULL;
315 info.ssl_private_key_filepath = NULL;
318 if (strlen(resource_path) > sizeof(cert_path) - 32) {
319 lwsl_err("resource path too long\n");
322 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
324 if (strlen(resource_path) > sizeof(key_path) - 32) {
325 lwsl_err("resource path too long\n");
328 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
331 info.ssl_cert_filepath = cert_path;
332 info.ssl_private_key_filepath = key_path;
333 opts |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
337 info.max_http_header_pool = 1;
338 info.timeout_secs = 5;
339 info.options = opts | LWS_SERVER_OPTION_LIBUV;
341 #if UV_VERSION_MAJOR > 0
342 if (foreign_libuv_loop) {
343 /* create the foreign loop */
346 /* run some timer on that loop just so loop is not 'clean' */
348 uv_signal_init(&loop, &signal_outer);
349 uv_signal_start(&signal_outer, outer_signal_cb, SIGINT);
351 uv_timer_init(&loop, &timer_outer);
352 timer_outer.data = &ctr;
354 ctr.lim = ctr.cur + 5;
356 uv_timer_start(&timer_outer, timer_cb, 0, 1000);
357 lwsl_notice("running loop without libwebsockets for %d s\n", ctr.lim);
359 uv_run(&loop, UV_RUN_DEFAULT);
361 /* timer will stop loop and we will get here */
365 context = lws_create_context(&info);
366 if (context == NULL) {
367 lwsl_err("libwebsocket init failed\n");
371 lws_uv_sigint_cfg(context, 1, signal_cb);
373 #if UV_VERSION_MAJOR > 0
374 if (foreign_libuv_loop)
375 /* we have our own uv loop outside of lws */
376 lws_uv_initloop(context, &loop, 0);
381 * lws will create his own libuv loop in the context
383 if (lws_uv_initloop(context, NULL, 0)) {
384 lwsl_err("lws_uv_initloop failed\n");
390 #if UV_VERSION_MAJOR > 0
391 if (foreign_libuv_loop) {
393 * prepare inner timer on loop, to run along with lws.
394 * Will exit after 5s while lws keeps running
396 struct counter ctr_inner = { 0, 3, 0 };
398 uv_timer_t timer_inner;
399 uv_timer_init(&loop, &timer_inner);
400 timer_inner.data = &ctr_inner;
401 uv_timer_start(&timer_inner, timer_cb, 200, 1000);
403 /* make this timer long-lived, should keep
404 * firing after lws exits */
406 ctr.lim = ctr.cur + 1000;
407 uv_timer_start(&timer_outer, timer_cb, 0, 1000);
409 uv_run(&loop, UV_RUN_DEFAULT);
411 /* we are here either because signal stopped us,
412 * or outer timer expired */
414 /* close short timer */
415 uv_timer_stop(&timer_inner);
416 uv_close((uv_handle_t*)&timer_inner, timer_close_cb);
419 lwsl_notice("Destroying lws context\n");
422 lws_context_destroy(context);
424 lwsl_notice("Please wait while the outer libuv test continues for 10s\n");
426 ctr.lim = ctr.cur + 10;
428 /* try and run outer timer for 10 more seconds,
429 * (or sigint outer handler) after lws has left the loop */
430 uv_run(&loop, UV_RUN_DEFAULT);
432 /* Clean up the foreign loop now */
434 /* PHASE 1: stop and close things we created
437 uv_timer_stop(&timer_outer);
438 uv_close((uv_handle_t*)&timer_outer, timer_close_cb);
439 uv_signal_stop(&signal_outer);
443 uv_run(&loop, UV_RUN_NOWAIT);
445 /* PHASE 2: close anything remaining */
447 uv_walk(&loop, lws_uv_walk_cb, NULL);
451 uv_run(&loop, UV_RUN_NOWAIT);
453 /* PHASE 3: close the UV loop itself */
455 e = uv_loop_close(&loop);
456 lwsl_notice("uv loop close rc %s\n",
457 e ? uv_strerror(e) : "ok");
459 /* PHASE 4: finalize context destruction */
461 lws_context_destroy2(context);
465 lws_libuv_run(context, 0);
468 lws_context_destroy(context);
469 lws_context_destroy2(context);
472 lwsl_notice("libwebsockets-test-server exited cleanly\n");