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>
26 struct lws_context *context;
28 /* http server gets files from this path */
29 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
30 char *resource_path = LOCAL_RESOURCE_PATH;
31 #if defined(LWS_USE_POLARSSL)
33 #if defined(LWS_USE_MBEDTLS)
35 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
36 char crl_path[1024] = "";
42 * This test server is ONLY this .c file, it's radically simpler than the
43 * pre-v2.0 test servers. For example it has no user callback content or
44 * defines any protocols.
46 * To achieve that, it uses the LWS protocol plugins. Those in turn
47 * use libuv. So you must configure with LWS_WITH_PLUGINS (which implies
48 * libuv) to get this to build.
50 * You can find the individual protocol plugin sources in ../plugins
53 void sighandler(int sig)
55 lws_cancel_service(context);
58 static const struct lws_extension exts[] = {
61 lws_extension_callback_pm_deflate,
66 lws_extension_callback_pm_deflate,
69 { NULL, NULL, NULL /* terminator */ }
73 * mount a handler for a section of the URL space
76 static const struct lws_http_mount mount_post = {
77 NULL, /* linked-list pointer to next*/
78 "/formtest", /* mountpoint in URL namespace on this vhost */
79 "protocol-post-demo", /* handler */
80 NULL, /* default filename if none given */
87 LWSMPRO_CALLBACK, /* origin points to a callback */
88 9, /* strlen("/formtest"), ie length of the mountpoint */
92 * mount a filesystem directory into the URL space at /
93 * point it to our /usr/share directory with our assets in
94 * stuff from here is autoserved by the library
97 static const struct lws_http_mount mount = {
98 (struct lws_http_mount *)&mount_post, /* linked-list pointer to next*/
99 "/", /* mountpoint in URL namespace on this vhost */
100 LOCAL_RESOURCE_PATH, /* where to go on the filesystem for that */
101 "test.html", /* default filename if none given */
108 LWSMPRO_FILE, /* mount type is a directory in a filesystem */
109 1, /* strlen("/"), ie length of the mountpoint */
113 * this sets a per-vhost, per-protocol option name:value pair
114 * the effect is to set this protocol to be the default one for the vhost,
115 * ie, selected if no Protocol: header is sent with the ws upgrade.
118 static const struct lws_protocol_vhost_options pvo_opt = {
126 * We must enable the plugin protocols we want into our vhost with a
127 * linked-list. We can also give the plugin per-vhost options here.
130 static const struct lws_protocol_vhost_options pvo_3 = {
133 "protocol-post-demo",
134 "" /* ignored, just matches the protocol name above */
137 static const struct lws_protocol_vhost_options pvo_2 = {
141 "" /* ignored, just matches the protocol name above */
144 static const struct lws_protocol_vhost_options pvo_1 = {
147 "lws-mirror-protocol",
151 static const struct lws_protocol_vhost_options pvo = {
154 "dumb-increment-protocol",
158 static void signal_cb(uv_signal_t *watcher, int signum)
160 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
161 switch (watcher->signum) {
166 signal(SIGABRT, SIG_DFL);
170 lws_libuv_stop(context);
173 static const struct option options[] = {
174 { "help", no_argument, NULL, 'h' },
175 { "debug", required_argument, NULL, 'd' },
176 { "port", required_argument, NULL, 'p' },
177 { "ssl", no_argument, NULL, 's' },
178 { "allow-non-ssl", no_argument, NULL, 'a' },
179 { "interface", required_argument, NULL, 'i' },
180 { "ssl-cert", required_argument, NULL, 'C' },
181 { "ssl-key", required_argument, NULL, 'K' },
182 { "ssl-ca", required_argument, NULL, 'A' },
183 #if defined(LWS_OPENSSL_SUPPORT)
184 { "ssl-verify-client", no_argument, NULL, 'v' },
185 #if defined(LWS_HAVE_SSL_CTX_set1_param)
186 { "ssl-crl", required_argument, NULL, 'R' },
189 #ifndef LWS_NO_DAEMONIZE
190 { "daemonize", no_argument, NULL, 'D' },
192 { "resource_path", required_argument, NULL, 'r' },
196 static const char * const plugin_dirs[] = {
197 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
201 int main(int argc, char **argv)
203 struct lws_context_creation_info info;
204 char interface_name[128] = "";
205 const char *iface = NULL;
206 char cert_path[1024] = "";
207 char key_path[1024] = "";
208 char ca_path[1024] = "";
209 int uid = -1, gid = -1;
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, "i:hsap:d:Dr:C:K:A:R:vu:g:", options, NULL);
232 #ifndef LWS_NO_DAEMONIZE
236 syslog_options &= ~LOG_PERROR;
247 debug_level = atoi(optarg);
253 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
256 info.port = atoi(optarg);
259 strncpy(interface_name, optarg, sizeof interface_name);
260 interface_name[(sizeof interface_name) - 1] = '\0';
261 iface = interface_name;
264 resource_path = optarg;
265 printf("Setting resource path to \"%s\"\n", resource_path);
268 strncpy(cert_path, optarg, sizeof(cert_path) - 1);
269 cert_path[sizeof(cert_path) - 1] = '\0';
272 strncpy(key_path, optarg, sizeof(key_path) - 1);
273 key_path[sizeof(key_path) - 1] = '\0';
276 strncpy(ca_path, optarg, sizeof(ca_path) - 1);
277 ca_path[sizeof(ca_path) - 1] = '\0';
279 #if defined(LWS_OPENSSL_SUPPORT)
282 opts |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
284 #if defined(LWS_USE_POLARSSL)
286 #if defined(LWS_USE_MBEDTLS)
288 #if defined(LWS_HAVE_SSL_CTX_set1_param)
290 strncpy(crl_path, optarg, sizeof(crl_path) - 1);
291 crl_path[sizeof(crl_path) - 1] = '\0';
298 fprintf(stderr, "Usage: test-server "
299 "[--port=<p>] [--ssl] "
300 "[-d <log bitfield>] "
301 "[--resource_path <path>]\n");
306 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
308 * normally lock path would be /var/lock/lwsts or similar, to
309 * simplify getting started without having to take care about
310 * permissions or running as root, set to /tmp/.lwsts-lock
312 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
313 fprintf(stderr, "Failed to daemonize\n");
318 signal(SIGINT, sighandler);
321 /* we will only try to log things according to our debug_level */
322 setlogmask(LOG_UPTO (LOG_DEBUG));
323 openlog("lwsts", syslog_options, LOG_DAEMON);
326 /* tell the library what debug level to emit and to send it to syslog */
327 lws_set_log_level(debug_level, lwsl_emit_syslog);
329 lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
330 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
332 lwsl_notice(" Using resource path \"%s\"\n", resource_path);
335 info.protocols = NULL; /* all protocols from lib / plugins */
336 info.ssl_cert_filepath = NULL;
337 info.ssl_private_key_filepath = NULL;
340 info.max_http_header_pool = 16;
341 info.options = opts |
342 LWS_SERVER_OPTION_VALIDATE_UTF8 |
343 LWS_SERVER_OPTION_LIBUV; /* plugins require this */
346 if (strlen(resource_path) > sizeof(cert_path) - 32) {
347 lwsl_err("resource path too long\n");
351 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
353 if (strlen(resource_path) > sizeof(key_path) - 32) {
354 lwsl_err("resource path too long\n");
358 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
361 info.ssl_cert_filepath = cert_path;
362 info.ssl_private_key_filepath = key_path;
364 info.ssl_ca_filepath = ca_path;
366 /* redirect guys coming on http */
367 info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS;
370 info.extensions = exts;
371 info.timeout_secs = 5;
372 info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
373 "ECDHE-RSA-AES256-GCM-SHA384:"
374 "DHE-RSA-AES256-GCM-SHA384:"
375 "ECDHE-RSA-AES256-SHA384:"
376 "HIGH:!aNULL:!eNULL:!EXPORT:"
377 "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
378 "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
379 "!DHE-RSA-AES128-SHA256:"
380 "!AES128-GCM-SHA256:"
382 "!DHE-RSA-AES256-SHA256:"
383 "!AES256-GCM-SHA384:"
386 /* tell lws to look for protocol plugins here */
387 info.plugin_dirs = plugin_dirs;
389 /* tell lws about our mount we want */
390 info.mounts = &mount;
392 * give it our linked-list of Per-Vhost Options, these control
393 * which protocols (from plugins) are allowed to be enabled on
399 * As it is, this creates the context and a single Vhost at the same
400 * time. You can use LWS_SERVER_OPTION_EXPLICIT_VHOSTS option above
401 * to just create the context, and call lws_create_vhost() afterwards
402 * multiple times with different info to get multiple listening vhosts.
404 context = lws_create_context(&info);
405 if (context == NULL) {
406 lwsl_err("libwebsocket init failed\n");
410 /* libuv event loop */
411 lws_uv_sigint_cfg(context, 1, signal_cb);
412 if (lws_uv_initloop(context, NULL, 0))
413 lwsl_err("lws_uv_initloop failed\n");
415 lws_libuv_run(context, 0);
417 /* when we decided to exit the event loop */
418 lws_context_destroy(context);
419 lwsl_notice("libwebsockets-test-server exited cleanly\n");