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>
29 struct lws_context *context;
31 /* http server gets files from this path */
32 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
33 char *resource_path = LOCAL_RESOURCE_PATH;
35 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
36 char crl_path[1024] = "";
40 * This test server is ONLY this .c file, it's radically simpler than the
41 * pre-v2.0 test servers. For example it has no user callback content or
42 * defines any protocols.
44 * To achieve that, it uses the LWS protocol plugins. Those in turn
45 * use libuv. So you must configure with LWS_WITH_PLUGINS (which implies
46 * libuv) to get this to build.
48 * You can find the individual protocol plugin sources in ../plugins
51 void sighandler(int sig)
53 lws_cancel_service(context);
56 static const struct lws_extension exts[] = {
59 lws_extension_callback_pm_deflate,
64 lws_extension_callback_pm_deflate,
67 { NULL, NULL, NULL /* terminator */ }
71 * mount handlers for sections of the URL space
74 static const struct lws_http_mount mount_ziptest = {
75 NULL, /* linked-list pointer to next*/
76 "/ziptest", /* mountpoint in URL namespace on this vhost */
77 LOCAL_RESOURCE_PATH"/candide.zip", /* handler */
78 NULL, /* default filename if none given */
89 LWSMPRO_FILE, /* origin points to a callback */
90 8, /* strlen("/ziptest"), ie length of the mountpoint */
93 { NULL, NULL } // sentinel
96 static const struct lws_http_mount mount_post = {
97 (struct lws_http_mount *)&mount_ziptest, /* linked-list pointer to next*/
98 "/formtest", /* mountpoint in URL namespace on this vhost */
99 "protocol-post-demo", /* handler */
100 NULL, /* default filename if none given */
111 LWSMPRO_CALLBACK, /* origin points to a callback */
112 9, /* strlen("/formtest"), ie length of the mountpoint */
115 { NULL, NULL } // sentinel
119 * mount a filesystem directory into the URL space at /
120 * point it to our /usr/share directory with our assets in
121 * stuff from here is autoserved by the library
124 static const struct lws_http_mount mount = {
125 (struct lws_http_mount *)&mount_post, /* linked-list pointer to next*/
126 "/", /* mountpoint in URL namespace on this vhost */
127 LOCAL_RESOURCE_PATH, /* where to go on the filesystem for that */
128 "test.html", /* default filename if none given */
139 LWSMPRO_FILE, /* mount type is a directory in a filesystem */
140 1, /* strlen("/"), ie length of the mountpoint */
143 { NULL, NULL } // sentinel
147 * this sets a per-vhost, per-protocol option name:value pair
148 * the effect is to set this protocol to be the default one for the vhost,
149 * ie, selected if no Protocol: header is sent with the ws upgrade.
152 static const struct lws_protocol_vhost_options pvo_opt = {
160 static const struct lws_protocol_vhost_options pvo_opt4a = {
163 "raw", /* indicate we are the protocol that gets raw connections */
167 static const struct lws_protocol_vhost_options pvo_opt4 = {
170 "fifo-path", /* tell the raw test plugin to open a raw file here */
175 * We must enable the plugin protocols we want into our vhost with a
176 * linked-list. We can also give the plugin per-vhost options here.
179 static const struct lws_protocol_vhost_options pvo_4 = {
181 &pvo_opt4, /* set us as the protocol who gets raw connections */
182 "protocol-lws-raw-test",
183 "" /* ignored, just matches the protocol name above */
186 static const struct lws_protocol_vhost_options pvo_3 = {
189 "protocol-post-demo",
190 "" /* ignored, just matches the protocol name above */
193 static const struct lws_protocol_vhost_options pvo_2 = {
197 "" /* ignored, just matches the protocol name above */
200 static const struct lws_protocol_vhost_options pvo_1 = {
203 "lws-mirror-protocol",
207 static const struct lws_protocol_vhost_options pvo = {
210 "dumb-increment-protocol",
214 static void signal_cb(uv_signal_t *watcher, int signum)
216 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
217 switch (watcher->signum) {
222 signal(SIGABRT, SIG_DFL);
226 lws_libuv_stop(context);
229 static const struct option options[] = {
230 { "help", no_argument, NULL, 'h' },
231 { "debug", required_argument, NULL, 'd' },
232 { "port", required_argument, NULL, 'p' },
233 { "ssl", no_argument, NULL, 's' },
234 { "ssl-alerts", no_argument, NULL, 'S' },
235 { "allow-non-ssl", no_argument, NULL, 'a' },
236 { "interface", required_argument, NULL, 'i' },
237 { "ssl-cert", required_argument, NULL, 'C' },
238 { "ssl-key", required_argument, NULL, 'K' },
239 { "ssl-ca", required_argument, NULL, 'A' },
240 #if defined(LWS_OPENSSL_SUPPORT)
241 { "ssl-verify-client", no_argument, NULL, 'v' },
242 #if defined(LWS_HAVE_SSL_CTX_set1_param)
243 { "ssl-crl", required_argument, NULL, 'R' },
246 #ifndef LWS_NO_DAEMONIZE
247 { "daemonize", no_argument, NULL, 'D' },
249 { "resource_path", required_argument, NULL, 'r' },
253 static const char * const plugin_dirs[] = {
254 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
258 int main(int argc, char **argv)
260 struct lws_context_creation_info info;
261 char interface_name[128] = "";
262 const char *iface = NULL;
263 char cert_path[1024] = "";
264 char key_path[1024] = "";
265 char ca_path[1024] = "";
266 int uid = -1, gid = -1;
271 int syslog_options = LOG_PID | LOG_PERROR;
273 #ifndef LWS_NO_DAEMONIZE
278 * take care to zero down the info struct, he contains random garbaage
279 * from the stack otherwise
281 memset(&info, 0, sizeof info);
285 n = getopt_long(argc, argv, "i:hsap:d:Dr:C:K:A:R:vu:g:S",
286 (struct option *)options, NULL);
290 #ifndef LWS_NO_DAEMONIZE
294 syslog_options &= ~LOG_PERROR;
305 debug_level = atoi(optarg);
311 #if defined(LWS_OPENSSL_SUPPORT)
312 info.ssl_info_event_mask |= SSL_CB_ALERT;
316 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
319 info.port = atoi(optarg);
322 strncpy(interface_name, optarg, sizeof interface_name);
323 interface_name[(sizeof interface_name) - 1] = '\0';
324 iface = interface_name;
327 resource_path = optarg;
328 printf("Setting resource path to \"%s\"\n", resource_path);
331 strncpy(cert_path, optarg, sizeof(cert_path) - 1);
332 cert_path[sizeof(cert_path) - 1] = '\0';
335 strncpy(key_path, optarg, sizeof(key_path) - 1);
336 key_path[sizeof(key_path) - 1] = '\0';
339 strncpy(ca_path, optarg, sizeof(ca_path) - 1);
340 ca_path[sizeof(ca_path) - 1] = '\0';
342 #if defined(LWS_OPENSSL_SUPPORT)
345 opts |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
348 #if defined(LWS_HAVE_SSL_CTX_set1_param)
350 strncpy(crl_path, optarg, sizeof(crl_path) - 1);
351 crl_path[sizeof(crl_path) - 1] = '\0';
356 fprintf(stderr, "Usage: test-server "
357 "[--port=<p>] [--ssl] "
358 "[-d <log bitfield>] "
359 "[--resource_path <path>]\n");
364 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
366 * normally lock path would be /var/lock/lwsts or similar, to
367 * simplify getting started without having to take care about
368 * permissions or running as root, set to /tmp/.lwsts-lock
370 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
371 fprintf(stderr, "Failed to daemonize\n");
376 signal(SIGINT, sighandler);
379 /* we will only try to log things according to our debug_level */
380 setlogmask(LOG_UPTO (LOG_DEBUG));
381 openlog("lwsts", syslog_options, LOG_DAEMON);
384 /* tell the library what debug level to emit and to send it to syslog */
385 lws_set_log_level(debug_level, lwsl_emit_syslog);
387 lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
388 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
390 lwsl_notice(" Using resource path \"%s\"\n", resource_path);
393 info.protocols = NULL; /* all protocols from lib / plugins */
394 info.ssl_cert_filepath = NULL;
395 info.ssl_private_key_filepath = NULL;
398 info.max_http_header_pool = 16;
399 info.options = opts | LWS_SERVER_OPTION_FALLBACK_TO_RAW |
400 LWS_SERVER_OPTION_VALIDATE_UTF8 |
401 LWS_SERVER_OPTION_LIBUV; /* plugins require this */
404 if (strlen(resource_path) > sizeof(cert_path) - 32) {
405 lwsl_err("resource path too long\n");
409 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
411 if (strlen(resource_path) > sizeof(key_path) - 32) {
412 lwsl_err("resource path too long\n");
416 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
419 info.ssl_cert_filepath = cert_path;
420 info.ssl_private_key_filepath = key_path;
422 info.ssl_ca_filepath = ca_path;
424 /* redirect guys coming on http */
425 info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS;
428 info.extensions = exts;
429 info.timeout_secs = 5;
430 info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
431 "ECDHE-RSA-AES256-GCM-SHA384:"
432 "DHE-RSA-AES256-GCM-SHA384:"
433 "ECDHE-RSA-AES256-SHA384:"
434 "HIGH:!aNULL:!eNULL:!EXPORT:"
435 "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
436 "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
437 "!DHE-RSA-AES128-SHA256:"
438 "!AES128-GCM-SHA256:"
440 "!DHE-RSA-AES256-SHA256:"
441 "!AES256-GCM-SHA384:"
444 /* tell lws to look for protocol plugins here */
445 info.plugin_dirs = plugin_dirs;
447 /* tell lws about our mount we want */
448 info.mounts = &mount;
450 * give it our linked-list of Per-Vhost Options, these control
451 * which protocols (from plugins) are allowed to be enabled on
457 * As it is, this creates the context and a single Vhost at the same
458 * time. You can use LWS_SERVER_OPTION_EXPLICIT_VHOSTS option above
459 * to just create the context, and call lws_create_vhost() afterwards
460 * multiple times with different info to get multiple listening vhosts.
462 context = lws_create_context(&info);
463 if (context == NULL) {
464 lwsl_err("libwebsocket init failed\n");
468 /* libuv event loop */
469 lws_uv_sigint_cfg(context, 1, signal_cb);
470 if (lws_uv_initloop(context, NULL, 0))
471 lwsl_err("lws_uv_initloop failed\n");
473 lws_libuv_run(context, 0);
475 /* when we decided to exit the event loop */
476 lws_context_destroy(context);
477 lwsl_notice("libwebsockets-test-server exited cleanly\n");