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_4 = {
231 &pvo_opt4, /* set us as the protocol who gets raw connections */
232 "protocol-lws-raw-test",
233 "" /* ignored, just matches the protocol name above */
236 static const struct lws_protocol_vhost_options pvo_3 = {
239 "protocol-post-demo",
240 "" /* ignored, just matches the protocol name above */
243 static const struct lws_protocol_vhost_options pvo_2 = {
247 "" /* ignored, just matches the protocol name above */
250 static const struct lws_protocol_vhost_options pvo_1 = {
253 "lws-mirror-protocol",
257 static const struct lws_protocol_vhost_options pvo = {
260 "dumb-increment-protocol",
264 static void signal_cb(uv_signal_t *watcher, int signum)
266 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
267 switch (watcher->signum) {
272 signal(SIGABRT, SIG_DFL);
276 lws_libuv_stop(context);
279 static const struct option options[] = {
280 { "help", no_argument, NULL, 'h' },
281 { "debug", required_argument, NULL, 'd' },
282 { "port", required_argument, NULL, 'p' },
283 { "ssl", no_argument, NULL, 's' },
284 { "ssl-alerts", no_argument, NULL, 'S' },
285 { "allow-non-ssl", no_argument, NULL, 'a' },
286 { "interface", required_argument, NULL, 'i' },
287 { "ssl-cert", required_argument, NULL, 'C' },
288 { "ssl-key", required_argument, NULL, 'K' },
289 { "ssl-ca", required_argument, NULL, 'A' },
290 #if defined(LWS_OPENSSL_SUPPORT)
291 { "ssl-verify-client", no_argument, NULL, 'v' },
292 #if defined(LWS_HAVE_SSL_CTX_set1_param)
293 { "ssl-crl", required_argument, NULL, 'R' },
296 #ifndef LWS_NO_DAEMONIZE
297 { "daemonize", no_argument, NULL, 'D' },
299 { "resource_path", required_argument, NULL, 'r' },
303 static const char * const plugin_dirs[] = {
304 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
308 int main(int argc, char **argv)
310 struct lws_vhost *vhost;
311 char interface_name[128] = "";
312 const char *iface = NULL;
313 char cert_path[1024] = "";
314 char key_path[1024] = "";
315 char ca_path[1024] = "";
316 int uid = -1, gid = -1;
321 int syslog_options = LOG_PID | LOG_PERROR;
323 #ifndef LWS_NO_DAEMONIZE
328 * take care to zero down the info struct, he contains random garbaage
329 * from the stack otherwise
331 memset(&info, 0, sizeof info);
335 n = getopt_long(argc, argv, "i:hsap:d:Dr:C:K:A:R:vu:g:S",
336 (struct option *)options, NULL);
340 #ifndef LWS_NO_DAEMONIZE
344 syslog_options &= ~LOG_PERROR;
355 debug_level = atoi(optarg);
361 #if defined(LWS_OPENSSL_SUPPORT)
362 info.ssl_info_event_mask |= SSL_CB_ALERT;
366 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
369 info.port = atoi(optarg);
372 strncpy(interface_name, optarg, sizeof interface_name);
373 interface_name[(sizeof interface_name) - 1] = '\0';
374 iface = interface_name;
377 resource_path = optarg;
378 printf("Setting resource path to \"%s\"\n", resource_path);
381 strncpy(cert_path, optarg, sizeof(cert_path) - 1);
382 cert_path[sizeof(cert_path) - 1] = '\0';
385 strncpy(key_path, optarg, sizeof(key_path) - 1);
386 key_path[sizeof(key_path) - 1] = '\0';
389 strncpy(ca_path, optarg, sizeof(ca_path) - 1);
390 ca_path[sizeof(ca_path) - 1] = '\0';
392 #if defined(LWS_OPENSSL_SUPPORT)
395 opts |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
398 #if defined(LWS_HAVE_SSL_CTX_set1_param)
400 strncpy(crl_path, optarg, sizeof(crl_path) - 1);
401 crl_path[sizeof(crl_path) - 1] = '\0';
406 fprintf(stderr, "Usage: test-server "
407 "[--port=<p>] [--ssl] "
408 "[-d <log bitfield>] "
409 "[--resource_path <path>]\n");
414 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
416 * normally lock path would be /var/lock/lwsts or similar, to
417 * simplify getting started without having to take care about
418 * permissions or running as root, set to /tmp/.lwsts-lock
420 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
421 fprintf(stderr, "Failed to daemonize\n");
426 signal(SIGINT, sighandler);
427 #if defined(TEST_DYNAMIC_VHOST)
428 signal(SIGUSR1, sighandler_USR1);
432 /* we will only try to log things according to our debug_level */
433 setlogmask(LOG_UPTO (LOG_DEBUG));
434 openlog("lwsts", syslog_options, LOG_DAEMON);
437 /* tell the library what debug level to emit and to send it to syslog */
438 lws_set_log_level(debug_level, lwsl_emit_syslog);
440 lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
441 lwsl_notice("(C) Copyright 2010-2017 Andy Green <andy@warmcat.com>\n");
443 lwsl_notice(" Using resource path \"%s\"\n", resource_path);
446 info.protocols = NULL; /* all protocols from lib / plugins */
447 info.ssl_cert_filepath = NULL;
448 info.ssl_private_key_filepath = NULL;
451 info.max_http_header_pool = 16;
452 info.options = opts | LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
453 LWS_SERVER_OPTION_FALLBACK_TO_RAW |
454 LWS_SERVER_OPTION_VALIDATE_UTF8 |
455 LWS_SERVER_OPTION_LIBUV; /* plugins require this */
458 if (strlen(resource_path) > sizeof(cert_path) - 32) {
459 lwsl_err("resource path too long\n");
463 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
465 if (strlen(resource_path) > sizeof(key_path) - 32) {
466 lwsl_err("resource path too long\n");
470 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
473 info.ssl_cert_filepath = cert_path;
474 info.ssl_private_key_filepath = key_path;
476 info.ssl_ca_filepath = ca_path;
478 /* redirect guys coming on http */
479 info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS;
482 info.extensions = exts;
483 info.timeout_secs = 5;
484 info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
485 "ECDHE-RSA-AES256-GCM-SHA384:"
486 "DHE-RSA-AES256-GCM-SHA384:"
487 "ECDHE-RSA-AES256-SHA384:"
488 "HIGH:!aNULL:!eNULL:!EXPORT:"
489 "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
490 "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
491 "!DHE-RSA-AES128-SHA256:"
492 "!AES128-GCM-SHA256:"
494 "!DHE-RSA-AES256-SHA256:"
495 "!AES256-GCM-SHA384:"
498 /* tell lws to look for protocol plugins here */
499 info.plugin_dirs = plugin_dirs;
501 /* tell lws about our mount we want */
502 info.mounts = &mount;
504 * give it our linked-list of Per-Vhost Options, these control
505 * which protocols (from plugins) are allowed to be enabled on
511 * Since we used LWS_SERVER_OPTION_EXPLICIT_VHOSTS, this only creates
512 * the context. We can modify info and create as many vhosts as we
515 context = lws_create_context(&info);
516 if (context == NULL) {
517 lwsl_err("libwebsocket init failed\n");
522 * normally we would adapt at least info.name to reflect the
523 * external hostname for this server.
525 vhost = lws_create_vhost(context, &info);
527 lwsl_err("vhost creation failed\n");
531 #if defined(TEST_DYNAMIC_VHOST)
532 /* our dynamic vhost is on port + 1 */
536 /* libuv event loop */
537 lws_uv_sigint_cfg(context, 1, signal_cb);
538 if (lws_uv_initloop(context, NULL, 0)) {
539 lwsl_err("lws_uv_initloop failed\n");
543 #if defined(TEST_DYNAMIC_VHOST)
544 uv_timer_init(lws_uv_getloop(context, 0), &timeout_watcher);
546 lws_libuv_run(context, 0);
548 #if defined(TEST_DYNAMIC_VHOST)
549 uv_timer_stop(&timeout_watcher);
550 uv_close((uv_handle_t *)&timeout_watcher, NULL);
554 /* when we decided to exit the event loop */
555 lws_context_destroy(context);
556 lws_context_destroy2(context);
557 lwsl_notice("libwebsockets-test-server exited cleanly\n");