2 * libwebsockets-test-server-v2.0 - libwebsockets test implementation
4 * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
9 * The person who associated a work with this deed has dedicated
10 * the work to the public domain by waiving all of his or her rights
11 * to the work worldwide under copyright law, including all related
12 * and neighboring rights, to the extent allowed by law. You can copy,
13 * modify, distribute and perform the work, even for commercial purposes,
14 * all without asking permission.
16 * The test apps are intended to be adapted for use in your code, which
17 * may be proprietary. So unlike the library itself, they are licensed
21 #include <libwebsockets.h>
28 /* windows has no SIGUSR1 */
29 #if !defined(WIN32) && !defined(_WIN32)
30 #define TEST_DYNAMIC_VHOST
33 struct lws_context_creation_info info;
35 struct lws_context *context;
37 #if defined(TEST_DYNAMIC_VHOST)
38 volatile int dynamic_vhost_enable = 0;
39 struct lws_vhost *dynamic_vhost;
40 uv_timer_t timeout_watcher;
43 /* http server gets files from this path */
44 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
45 char *resource_path = LOCAL_RESOURCE_PATH;
47 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
48 char crl_path[1024] = "";
52 * This test server is ONLY this .c file, it's radically simpler than the
53 * pre-v2.0 test servers. For example it has no user callback content or
54 * defines any protocols.
56 * To achieve that, it uses the LWS protocol plugins. Those in turn
57 * use libuv. So you must configure with LWS_WITH_PLUGINS (which implies
58 * libuv) to get this to build.
60 * You can find the individual protocol plugin sources in ../plugins
63 #if defined(TEST_DYNAMIC_VHOST)
66 * to test dynamic vhost creation, fire a SIGUSR1 at the test server.
67 * It will toggle the existence of a second identical vhost at port + 1
69 * To synchronize with the event loop, it uses a libuv timer with 0 delay
70 * to get the business end called as the next event.
74 uv_timeout_dynamic_vhost_toggle(uv_timer_t *w
75 #if UV_VERSION_MAJOR == 0
80 if (dynamic_vhost_enable && !dynamic_vhost) {
81 lwsl_notice("creating dynamic vhost...\n");
82 dynamic_vhost = lws_create_vhost(context, &info);
84 if (!dynamic_vhost_enable && dynamic_vhost) {
85 lwsl_notice("destroying dynamic vhost...\n");
86 lws_vhost_destroy(dynamic_vhost);
91 void sighandler_USR1(int sig)
93 dynamic_vhost_enable ^= 1;
94 lwsl_notice("SIGUSR1: dynamic_vhost_enable: %d\n",
95 dynamic_vhost_enable);
96 uv_timer_start(&timeout_watcher,
97 uv_timeout_dynamic_vhost_toggle, 0, 0);
101 void sighandler(int sig)
103 lws_cancel_service(context);
106 static const struct lws_extension exts[] = {
108 "permessage-deflate",
109 lws_extension_callback_pm_deflate,
114 lws_extension_callback_pm_deflate,
117 { NULL, NULL, NULL /* terminator */ }
121 * mount handlers for sections of the URL space
124 static const struct lws_http_mount mount_ziptest = {
125 NULL, /* linked-list pointer to next*/
126 "/ziptest", /* mountpoint in URL namespace on this vhost */
127 LOCAL_RESOURCE_PATH"/candide.zip", /* handler */
128 NULL, /* default filename if none given */
139 LWSMPRO_FILE, /* origin points to a callback */
140 8, /* strlen("/ziptest"), ie length of the mountpoint */
143 { NULL, NULL } // sentinel
146 static const struct lws_http_mount mount_post = {
147 (struct lws_http_mount *)&mount_ziptest, /* linked-list pointer to next*/
148 "/formtest", /* mountpoint in URL namespace on this vhost */
149 "protocol-post-demo", /* handler */
150 NULL, /* default filename if none given */
161 LWSMPRO_CALLBACK, /* origin points to a callback */
162 9, /* strlen("/formtest"), ie length of the mountpoint */
165 { NULL, NULL } // sentinel
169 * mount a filesystem directory into the URL space at /
170 * point it to our /usr/share directory with our assets in
171 * stuff from here is autoserved by the library
174 static const struct lws_http_mount mount = {
175 (struct lws_http_mount *)&mount_post, /* linked-list pointer to next*/
176 "/", /* mountpoint in URL namespace on this vhost */
177 LOCAL_RESOURCE_PATH, /* where to go on the filesystem for that */
178 "test.html", /* default filename if none given */
189 LWSMPRO_FILE, /* mount type is a directory in a filesystem */
190 1, /* strlen("/"), ie length of the mountpoint */
193 { NULL, NULL } // sentinel
197 * this sets a per-vhost, per-protocol option name:value pair
198 * the effect is to set this protocol to be the default one for the vhost,
199 * ie, selected if no Protocol: header is sent with the ws upgrade.
202 static const struct lws_protocol_vhost_options pvo_opt = {
210 static const struct lws_protocol_vhost_options pvo_opt4a = {
213 "raw", /* indicate we are the protocol that gets raw connections */
217 static const struct lws_protocol_vhost_options pvo_opt4 = {
220 "fifo-path", /* tell the raw test plugin to open a raw file here */
225 * We must enable the plugin protocols we want into our vhost with a
226 * linked-list. We can also give the plugin per-vhost options here.
229 static const struct lws_protocol_vhost_options pvo_5 = {
233 "" /* ignored, just matches the protocol name above */
236 static const struct lws_protocol_vhost_options pvo_4 = {
238 &pvo_opt4, /* set us as the protocol who gets raw connections */
239 "protocol-lws-raw-test",
240 "" /* ignored, just matches the protocol name above */
243 static const struct lws_protocol_vhost_options pvo_3 = {
246 "protocol-post-demo",
247 "" /* ignored, just matches the protocol name above */
250 static const struct lws_protocol_vhost_options pvo_2 = {
254 "" /* ignored, just matches the protocol name above */
257 static const struct lws_protocol_vhost_options pvo_1 = {
260 "lws-mirror-protocol",
264 static const struct lws_protocol_vhost_options pvo = {
267 "dumb-increment-protocol",
271 static void signal_cb(uv_signal_t *watcher, int signum)
273 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
274 switch (watcher->signum) {
279 signal(SIGABRT, SIG_DFL);
283 lws_libuv_stop(context);
286 static const struct option options[] = {
287 { "help", no_argument, NULL, 'h' },
288 { "debug", required_argument, NULL, 'd' },
289 { "port", required_argument, NULL, 'p' },
290 { "ssl", no_argument, NULL, 's' },
291 { "ssl-alerts", no_argument, NULL, 'S' },
292 { "allow-non-ssl", no_argument, NULL, 'a' },
293 { "interface", required_argument, NULL, 'i' },
294 { "ssl-cert", required_argument, NULL, 'C' },
295 { "ssl-key", required_argument, NULL, 'K' },
296 { "ssl-ca", required_argument, NULL, 'A' },
297 #if defined(LWS_OPENSSL_SUPPORT)
298 { "ssl-verify-client", no_argument, NULL, 'v' },
299 #if defined(LWS_HAVE_SSL_CTX_set1_param)
300 { "ssl-crl", required_argument, NULL, 'R' },
303 #ifndef LWS_NO_DAEMONIZE
304 { "daemonize", no_argument, NULL, 'D' },
306 { "resource_path", required_argument, NULL, 'r' },
310 static const char * const plugin_dirs[] = {
311 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
315 int main(int argc, char **argv)
317 struct lws_vhost *vhost;
318 char interface_name[128] = "";
319 const char *iface = NULL;
320 char cert_path[1024] = "";
321 char key_path[1024] = "";
322 char ca_path[1024] = "";
323 int uid = -1, gid = -1;
328 int syslog_options = LOG_PID | LOG_PERROR;
330 #ifndef LWS_NO_DAEMONIZE
335 * take care to zero down the info struct, he contains random garbaage
336 * from the stack otherwise
338 memset(&info, 0, sizeof info);
342 n = getopt_long(argc, argv, "i:hsap:d:Dr:C:K:A:R:vu:g:S",
343 (struct option *)options, NULL);
347 #ifndef LWS_NO_DAEMONIZE
351 syslog_options &= ~LOG_PERROR;
362 debug_level = atoi(optarg);
368 #if defined(LWS_OPENSSL_SUPPORT)
369 info.ssl_info_event_mask |= SSL_CB_ALERT;
373 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
376 info.port = atoi(optarg);
379 strncpy(interface_name, optarg, sizeof interface_name);
380 interface_name[(sizeof interface_name) - 1] = '\0';
381 iface = interface_name;
384 resource_path = optarg;
385 printf("Setting resource path to \"%s\"\n", resource_path);
388 strncpy(cert_path, optarg, sizeof(cert_path) - 1);
389 cert_path[sizeof(cert_path) - 1] = '\0';
392 strncpy(key_path, optarg, sizeof(key_path) - 1);
393 key_path[sizeof(key_path) - 1] = '\0';
396 strncpy(ca_path, optarg, sizeof(ca_path) - 1);
397 ca_path[sizeof(ca_path) - 1] = '\0';
399 #if defined(LWS_OPENSSL_SUPPORT)
402 opts |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
405 #if defined(LWS_HAVE_SSL_CTX_set1_param)
407 strncpy(crl_path, optarg, sizeof(crl_path) - 1);
408 crl_path[sizeof(crl_path) - 1] = '\0';
413 fprintf(stderr, "Usage: test-server "
414 "[--port=<p>] [--ssl] "
415 "[-d <log bitfield>] "
416 "[--resource_path <path>]\n");
421 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
423 * normally lock path would be /var/lock/lwsts or similar, to
424 * simplify getting started without having to take care about
425 * permissions or running as root, set to /tmp/.lwsts-lock
427 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
428 fprintf(stderr, "Failed to daemonize\n");
433 signal(SIGINT, sighandler);
434 #if defined(TEST_DYNAMIC_VHOST)
435 signal(SIGUSR1, sighandler_USR1);
439 /* we will only try to log things according to our debug_level */
440 setlogmask(LOG_UPTO (LOG_DEBUG));
441 openlog("lwsts", syslog_options, LOG_DAEMON);
444 /* tell the library what debug level to emit and to send it to syslog */
445 lws_set_log_level(debug_level, lwsl_emit_syslog);
447 lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
448 lwsl_notice("(C) Copyright 2010-2017 Andy Green <andy@warmcat.com>\n");
450 lwsl_notice(" Using resource path \"%s\"\n", resource_path);
453 info.protocols = NULL; /* all protocols from lib / plugins */
454 info.ssl_cert_filepath = NULL;
455 info.ssl_private_key_filepath = NULL;
458 info.max_http_header_pool = 16;
459 info.options = opts | LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
460 LWS_SERVER_OPTION_FALLBACK_TO_RAW |
461 LWS_SERVER_OPTION_VALIDATE_UTF8 |
462 LWS_SERVER_OPTION_LIBUV; /* plugins require this */
465 if (strlen(resource_path) > sizeof(cert_path) - 32) {
466 lwsl_err("resource path too long\n");
470 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
472 if (strlen(resource_path) > sizeof(key_path) - 32) {
473 lwsl_err("resource path too long\n");
477 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
480 info.ssl_cert_filepath = cert_path;
481 info.ssl_private_key_filepath = key_path;
483 info.ssl_ca_filepath = ca_path;
485 /* redirect guys coming on http */
486 info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS;
489 info.extensions = exts;
490 info.timeout_secs = 5;
491 info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
492 "ECDHE-RSA-AES256-GCM-SHA384:"
493 "DHE-RSA-AES256-GCM-SHA384:"
494 "ECDHE-RSA-AES256-SHA384:"
495 "HIGH:!aNULL:!eNULL:!EXPORT:"
496 "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
497 "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
498 "!DHE-RSA-AES128-SHA256:"
499 "!AES128-GCM-SHA256:"
501 "!DHE-RSA-AES256-SHA256:"
502 "!AES256-GCM-SHA384:"
505 /* tell lws to look for protocol plugins here */
506 info.plugin_dirs = plugin_dirs;
508 /* tell lws about our mount we want */
509 info.mounts = &mount;
511 * give it our linked-list of Per-Vhost Options, these control
512 * which protocols (from plugins) are allowed to be enabled on
518 * Since we used LWS_SERVER_OPTION_EXPLICIT_VHOSTS, this only creates
519 * the context. We can modify info and create as many vhosts as we
522 context = lws_create_context(&info);
523 if (context == NULL) {
524 lwsl_err("libwebsocket init failed\n");
529 * normally we would adapt at least info.name to reflect the
530 * external hostname for this server.
532 vhost = lws_create_vhost(context, &info);
534 lwsl_err("vhost creation failed\n");
538 #if defined(TEST_DYNAMIC_VHOST)
539 /* our dynamic vhost is on port + 1 */
543 /* libuv event loop */
544 lws_uv_sigint_cfg(context, 1, signal_cb);
545 if (lws_uv_initloop(context, NULL, 0)) {
546 lwsl_err("lws_uv_initloop failed\n");
550 #if defined(TEST_DYNAMIC_VHOST)
551 uv_timer_init(lws_uv_getloop(context, 0), &timeout_watcher);
553 lws_libuv_run(context, 0);
555 #if defined(TEST_DYNAMIC_VHOST)
556 uv_timer_stop(&timeout_watcher);
557 uv_close((uv_handle_t *)&timeout_watcher, NULL);
561 /* when we decided to exit the event loop */
562 lws_context_destroy(context);
563 lws_context_destroy2(context);
564 lwsl_notice("libwebsockets-test-server exited cleanly\n");