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;
34 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
35 char crl_path[1024] = "";
39 * This test server is ONLY this .c file, it's radically simpler than the
40 * pre-v2.0 test servers. For example it has no user callback content or
41 * defines any protocols.
43 * To achieve that, it uses the LWS protocol plugins. Those in turn
44 * use libuv. So you must configure with LWS_WITH_PLUGINS (which implies
45 * libuv) to get this to build.
47 * You can find the individual protocol plugin sources in ../plugins
50 void sighandler(int sig)
52 lws_cancel_service(context);
55 static const struct lws_extension exts[] = {
58 lws_extension_callback_pm_deflate,
63 lws_extension_callback_pm_deflate,
66 { NULL, NULL, NULL /* terminator */ }
70 * mount handlers for sections of the URL space
73 static const struct lws_http_mount mount_ziptest = {
74 NULL, /* linked-list pointer to next*/
75 "/ziptest", /* mountpoint in URL namespace on this vhost */
76 LOCAL_RESOURCE_PATH"/candide.zip", /* handler */
77 NULL, /* default filename if none given */
88 LWSMPRO_FILE, /* origin points to a callback */
89 8, /* strlen("/ziptest"), ie length of the mountpoint */
92 { NULL, NULL } // sentinel
95 static const struct lws_http_mount mount_post = {
96 (struct lws_http_mount *)&mount_ziptest, /* linked-list pointer to next*/
97 "/formtest", /* mountpoint in URL namespace on this vhost */
98 "protocol-post-demo", /* handler */
99 NULL, /* default filename if none given */
110 LWSMPRO_CALLBACK, /* origin points to a callback */
111 9, /* strlen("/formtest"), ie length of the mountpoint */
114 { NULL, NULL } // sentinel
118 * mount a filesystem directory into the URL space at /
119 * point it to our /usr/share directory with our assets in
120 * stuff from here is autoserved by the library
123 static const struct lws_http_mount mount = {
124 (struct lws_http_mount *)&mount_post, /* linked-list pointer to next*/
125 "/", /* mountpoint in URL namespace on this vhost */
126 LOCAL_RESOURCE_PATH, /* where to go on the filesystem for that */
127 "test.html", /* default filename if none given */
138 LWSMPRO_FILE, /* mount type is a directory in a filesystem */
139 1, /* strlen("/"), ie length of the mountpoint */
142 { NULL, NULL } // sentinel
146 * this sets a per-vhost, per-protocol option name:value pair
147 * the effect is to set this protocol to be the default one for the vhost,
148 * ie, selected if no Protocol: header is sent with the ws upgrade.
151 static const struct lws_protocol_vhost_options pvo_opt = {
159 * We must enable the plugin protocols we want into our vhost with a
160 * linked-list. We can also give the plugin per-vhost options here.
163 static const struct lws_protocol_vhost_options pvo_3 = {
166 "protocol-post-demo",
167 "" /* ignored, just matches the protocol name above */
170 static const struct lws_protocol_vhost_options pvo_2 = {
174 "" /* ignored, just matches the protocol name above */
177 static const struct lws_protocol_vhost_options pvo_1 = {
180 "lws-mirror-protocol",
184 static const struct lws_protocol_vhost_options pvo = {
187 "dumb-increment-protocol",
191 static void signal_cb(uv_signal_t *watcher, int signum)
193 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
194 switch (watcher->signum) {
199 signal(SIGABRT, SIG_DFL);
203 lws_libuv_stop(context);
206 static const struct option options[] = {
207 { "help", no_argument, NULL, 'h' },
208 { "debug", required_argument, NULL, 'd' },
209 { "port", required_argument, NULL, 'p' },
210 { "ssl", no_argument, NULL, 's' },
211 { "allow-non-ssl", no_argument, NULL, 'a' },
212 { "interface", required_argument, NULL, 'i' },
213 { "ssl-cert", required_argument, NULL, 'C' },
214 { "ssl-key", required_argument, NULL, 'K' },
215 { "ssl-ca", required_argument, NULL, 'A' },
216 #if defined(LWS_OPENSSL_SUPPORT)
217 { "ssl-verify-client", no_argument, NULL, 'v' },
218 #if defined(LWS_HAVE_SSL_CTX_set1_param)
219 { "ssl-crl", required_argument, NULL, 'R' },
222 #ifndef LWS_NO_DAEMONIZE
223 { "daemonize", no_argument, NULL, 'D' },
225 { "resource_path", required_argument, NULL, 'r' },
229 static const char * const plugin_dirs[] = {
230 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
234 int main(int argc, char **argv)
236 struct lws_context_creation_info info;
237 char interface_name[128] = "";
238 const char *iface = NULL;
239 char cert_path[1024] = "";
240 char key_path[1024] = "";
241 char ca_path[1024] = "";
242 int uid = -1, gid = -1;
247 int syslog_options = LOG_PID | LOG_PERROR;
249 #ifndef LWS_NO_DAEMONIZE
254 * take care to zero down the info struct, he contains random garbaage
255 * from the stack otherwise
257 memset(&info, 0, sizeof info);
261 n = getopt_long(argc, argv, "i:hsap:d:Dr:C:K:A:R:vu:g:",
262 (struct option *)options, NULL);
266 #ifndef LWS_NO_DAEMONIZE
270 syslog_options &= ~LOG_PERROR;
281 debug_level = atoi(optarg);
287 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
290 info.port = atoi(optarg);
293 strncpy(interface_name, optarg, sizeof interface_name);
294 interface_name[(sizeof interface_name) - 1] = '\0';
295 iface = interface_name;
298 resource_path = optarg;
299 printf("Setting resource path to \"%s\"\n", resource_path);
302 strncpy(cert_path, optarg, sizeof(cert_path) - 1);
303 cert_path[sizeof(cert_path) - 1] = '\0';
306 strncpy(key_path, optarg, sizeof(key_path) - 1);
307 key_path[sizeof(key_path) - 1] = '\0';
310 strncpy(ca_path, optarg, sizeof(ca_path) - 1);
311 ca_path[sizeof(ca_path) - 1] = '\0';
313 #if defined(LWS_OPENSSL_SUPPORT)
316 opts |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
319 #if defined(LWS_HAVE_SSL_CTX_set1_param)
321 strncpy(crl_path, optarg, sizeof(crl_path) - 1);
322 crl_path[sizeof(crl_path) - 1] = '\0';
327 fprintf(stderr, "Usage: test-server "
328 "[--port=<p>] [--ssl] "
329 "[-d <log bitfield>] "
330 "[--resource_path <path>]\n");
335 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
337 * normally lock path would be /var/lock/lwsts or similar, to
338 * simplify getting started without having to take care about
339 * permissions or running as root, set to /tmp/.lwsts-lock
341 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
342 fprintf(stderr, "Failed to daemonize\n");
347 signal(SIGINT, sighandler);
350 /* we will only try to log things according to our debug_level */
351 setlogmask(LOG_UPTO (LOG_DEBUG));
352 openlog("lwsts", syslog_options, LOG_DAEMON);
355 /* tell the library what debug level to emit and to send it to syslog */
356 lws_set_log_level(debug_level, lwsl_emit_syslog);
358 lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
359 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
361 lwsl_notice(" Using resource path \"%s\"\n", resource_path);
364 info.protocols = NULL; /* all protocols from lib / plugins */
365 info.ssl_cert_filepath = NULL;
366 info.ssl_private_key_filepath = NULL;
369 info.max_http_header_pool = 16;
370 info.options = opts |
371 LWS_SERVER_OPTION_VALIDATE_UTF8 |
372 LWS_SERVER_OPTION_LIBUV; /* plugins require this */
375 if (strlen(resource_path) > sizeof(cert_path) - 32) {
376 lwsl_err("resource path too long\n");
380 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
382 if (strlen(resource_path) > sizeof(key_path) - 32) {
383 lwsl_err("resource path too long\n");
387 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
390 info.ssl_cert_filepath = cert_path;
391 info.ssl_private_key_filepath = key_path;
393 info.ssl_ca_filepath = ca_path;
395 /* redirect guys coming on http */
396 info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS;
399 info.extensions = exts;
400 info.timeout_secs = 5;
401 info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
402 "ECDHE-RSA-AES256-GCM-SHA384:"
403 "DHE-RSA-AES256-GCM-SHA384:"
404 "ECDHE-RSA-AES256-SHA384:"
405 "HIGH:!aNULL:!eNULL:!EXPORT:"
406 "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
407 "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
408 "!DHE-RSA-AES128-SHA256:"
409 "!AES128-GCM-SHA256:"
411 "!DHE-RSA-AES256-SHA256:"
412 "!AES256-GCM-SHA384:"
415 /* tell lws to look for protocol plugins here */
416 info.plugin_dirs = plugin_dirs;
418 /* tell lws about our mount we want */
419 info.mounts = &mount;
421 * give it our linked-list of Per-Vhost Options, these control
422 * which protocols (from plugins) are allowed to be enabled on
428 * As it is, this creates the context and a single Vhost at the same
429 * time. You can use LWS_SERVER_OPTION_EXPLICIT_VHOSTS option above
430 * to just create the context, and call lws_create_vhost() afterwards
431 * multiple times with different info to get multiple listening vhosts.
433 context = lws_create_context(&info);
434 if (context == NULL) {
435 lwsl_err("libwebsocket init failed\n");
439 /* libuv event loop */
440 lws_uv_sigint_cfg(context, 1, signal_cb);
441 if (lws_uv_initloop(context, NULL, 0))
442 lwsl_err("lws_uv_initloop failed\n");
444 lws_libuv_run(context, 0);
446 /* when we decided to exit the event loop */
447 lws_context_destroy(context);
448 lwsl_notice("libwebsockets-test-server exited cleanly\n");