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 struct lws_context *context;
30 /* http server gets files from this path */
31 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
32 char *resource_path = LOCAL_RESOURCE_PATH;
33 #if defined(LWS_USE_POLARSSL)
35 #if defined(LWS_USE_MBEDTLS)
37 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
38 char crl_path[1024] = "";
44 * This test server is ONLY this .c file, it's radically simpler than the
45 * pre-v2.0 test servers. For example it has no user callback content or
46 * defines any protocols.
48 * To achieve that, it uses the LWS protocol plugins. Those in turn
49 * use libuv. So you must configure with LWS_WITH_PLUGINS (which implies
50 * libuv) to get this to build.
52 * You can find the individual protocol plugin sources in ../plugins
55 void sighandler(int sig)
57 lws_cancel_service(context);
60 static const struct lws_extension exts[] = {
63 lws_extension_callback_pm_deflate,
68 lws_extension_callback_pm_deflate,
71 { NULL, NULL, NULL /* terminator */ }
75 * mount a handler for a section of the URL space
78 static const struct lws_http_mount mount_post = {
79 NULL, /* linked-list pointer to next*/
80 "/formtest", /* mountpoint in URL namespace on this vhost */
81 "protocol-post-demo", /* handler */
82 NULL, /* default filename if none given */
90 LWSMPRO_CALLBACK, /* origin points to a callback */
91 9, /* strlen("/formtest"), ie length of the mountpoint */
95 * mount a filesystem directory into the URL space at /
96 * point it to our /usr/share directory with our assets in
97 * stuff from here is autoserved by the library
100 static const struct lws_http_mount mount = {
101 (struct lws_http_mount *)&mount_post, /* linked-list pointer to next*/
102 "/", /* mountpoint in URL namespace on this vhost */
103 LOCAL_RESOURCE_PATH, /* where to go on the filesystem for that */
104 "test.html", /* default filename if none given */
112 LWSMPRO_FILE, /* mount type is a directory in a filesystem */
113 1, /* strlen("/"), ie length of the mountpoint */
117 * this sets a per-vhost, per-protocol option name:value pair
118 * the effect is to set this protocol to be the default one for the vhost,
119 * ie, selected if no Protocol: header is sent with the ws upgrade.
122 static const struct lws_protocol_vhost_options pvo_opt = {
130 * We must enable the plugin protocols we want into our vhost with a
131 * linked-list. We can also give the plugin per-vhost options here.
134 static const struct lws_protocol_vhost_options pvo_3 = {
137 "protocol-post-demo",
138 "" /* ignored, just matches the protocol name above */
141 static const struct lws_protocol_vhost_options pvo_2 = {
145 "" /* ignored, just matches the protocol name above */
148 static const struct lws_protocol_vhost_options pvo_1 = {
151 "lws-mirror-protocol",
155 static const struct lws_protocol_vhost_options pvo = {
158 "dumb-increment-protocol",
162 static void signal_cb(uv_signal_t *watcher, int signum)
164 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
165 switch (watcher->signum) {
170 signal(SIGABRT, SIG_DFL);
174 lws_libuv_stop(context);
177 static const struct option options[] = {
178 { "help", no_argument, NULL, 'h' },
179 { "debug", required_argument, NULL, 'd' },
180 { "port", required_argument, NULL, 'p' },
181 { "ssl", no_argument, NULL, 's' },
182 { "allow-non-ssl", no_argument, NULL, 'a' },
183 { "interface", required_argument, NULL, 'i' },
184 { "ssl-cert", required_argument, NULL, 'C' },
185 { "ssl-key", required_argument, NULL, 'K' },
186 { "ssl-ca", required_argument, NULL, 'A' },
187 #if defined(LWS_OPENSSL_SUPPORT)
188 { "ssl-verify-client", no_argument, NULL, 'v' },
189 #if defined(LWS_HAVE_SSL_CTX_set1_param)
190 { "ssl-crl", required_argument, NULL, 'R' },
193 #ifndef LWS_NO_DAEMONIZE
194 { "daemonize", no_argument, NULL, 'D' },
196 { "resource_path", required_argument, NULL, 'r' },
200 static const char * const plugin_dirs[] = {
201 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
205 int main(int argc, char **argv)
207 struct lws_context_creation_info info;
208 char interface_name[128] = "";
209 const char *iface = NULL;
210 char cert_path[1024] = "";
211 char key_path[1024] = "";
212 char ca_path[1024] = "";
213 int uid = -1, gid = -1;
218 int syslog_options = LOG_PID | LOG_PERROR;
220 #ifndef LWS_NO_DAEMONIZE
225 * take care to zero down the info struct, he contains random garbaage
226 * from the stack otherwise
228 memset(&info, 0, sizeof info);
232 n = getopt_long(argc, argv, "i:hsap:d:Dr:C:K:A:R:vu:g:",
233 (struct option *)options, NULL);
237 #ifndef LWS_NO_DAEMONIZE
241 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;
269 resource_path = optarg;
270 printf("Setting resource path to \"%s\"\n", resource_path);
273 strncpy(cert_path, optarg, sizeof(cert_path) - 1);
274 cert_path[sizeof(cert_path) - 1] = '\0';
277 strncpy(key_path, optarg, sizeof(key_path) - 1);
278 key_path[sizeof(key_path) - 1] = '\0';
281 strncpy(ca_path, optarg, sizeof(ca_path) - 1);
282 ca_path[sizeof(ca_path) - 1] = '\0';
284 #if defined(LWS_OPENSSL_SUPPORT)
287 opts |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
289 #if defined(LWS_USE_POLARSSL)
291 #if defined(LWS_USE_MBEDTLS)
293 #if defined(LWS_HAVE_SSL_CTX_set1_param)
295 strncpy(crl_path, optarg, sizeof(crl_path) - 1);
296 crl_path[sizeof(crl_path) - 1] = '\0';
303 fprintf(stderr, "Usage: test-server "
304 "[--port=<p>] [--ssl] "
305 "[-d <log bitfield>] "
306 "[--resource_path <path>]\n");
311 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
313 * normally lock path would be /var/lock/lwsts or similar, to
314 * simplify getting started without having to take care about
315 * permissions or running as root, set to /tmp/.lwsts-lock
317 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
318 fprintf(stderr, "Failed to daemonize\n");
323 signal(SIGINT, sighandler);
326 /* we will only try to log things according to our debug_level */
327 setlogmask(LOG_UPTO (LOG_DEBUG));
328 openlog("lwsts", syslog_options, LOG_DAEMON);
331 /* tell the library what debug level to emit and to send it to syslog */
332 lws_set_log_level(debug_level, lwsl_emit_syslog);
334 lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
335 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
337 lwsl_notice(" Using resource path \"%s\"\n", resource_path);
340 info.protocols = NULL; /* all protocols from lib / plugins */
341 info.ssl_cert_filepath = NULL;
342 info.ssl_private_key_filepath = NULL;
345 info.max_http_header_pool = 16;
346 info.options = opts |
347 LWS_SERVER_OPTION_VALIDATE_UTF8 |
348 LWS_SERVER_OPTION_LIBUV; /* plugins require this */
351 if (strlen(resource_path) > sizeof(cert_path) - 32) {
352 lwsl_err("resource path too long\n");
356 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
358 if (strlen(resource_path) > sizeof(key_path) - 32) {
359 lwsl_err("resource path too long\n");
363 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
366 info.ssl_cert_filepath = cert_path;
367 info.ssl_private_key_filepath = key_path;
369 info.ssl_ca_filepath = ca_path;
371 /* redirect guys coming on http */
372 info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS;
375 info.extensions = exts;
376 info.timeout_secs = 5;
377 info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
378 "ECDHE-RSA-AES256-GCM-SHA384:"
379 "DHE-RSA-AES256-GCM-SHA384:"
380 "ECDHE-RSA-AES256-SHA384:"
381 "HIGH:!aNULL:!eNULL:!EXPORT:"
382 "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
383 "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
384 "!DHE-RSA-AES128-SHA256:"
385 "!AES128-GCM-SHA256:"
387 "!DHE-RSA-AES256-SHA256:"
388 "!AES256-GCM-SHA384:"
391 /* tell lws to look for protocol plugins here */
392 info.plugin_dirs = plugin_dirs;
394 /* tell lws about our mount we want */
395 info.mounts = &mount;
397 * give it our linked-list of Per-Vhost Options, these control
398 * which protocols (from plugins) are allowed to be enabled on
404 * As it is, this creates the context and a single Vhost at the same
405 * time. You can use LWS_SERVER_OPTION_EXPLICIT_VHOSTS option above
406 * to just create the context, and call lws_create_vhost() afterwards
407 * multiple times with different info to get multiple listening vhosts.
409 context = lws_create_context(&info);
410 if (context == NULL) {
411 lwsl_err("libwebsocket init failed\n");
415 /* libuv event loop */
416 lws_uv_sigint_cfg(context, 1, signal_cb);
417 if (lws_uv_initloop(context, NULL, 0))
418 lwsl_err("lws_uv_initloop failed\n");
420 lws_libuv_run(context, 0);
422 /* when we decided to exit the event loop */
423 lws_context_destroy(context);
424 lwsl_notice("libwebsockets-test-server exited cleanly\n");