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 a handler for a section of the URL space
73 static const struct lws_http_mount mount_post = {
74 NULL, /* linked-list pointer to next*/
75 "/formtest", /* mountpoint in URL namespace on this vhost */
76 "protocol-post-demo", /* handler */
77 NULL, /* default filename if none given */
88 LWSMPRO_CALLBACK, /* origin points to a callback */
89 9, /* strlen("/formtest"), ie length of the mountpoint */
92 { NULL, NULL } // sentinel
96 * mount a filesystem directory into the URL space at /
97 * point it to our /usr/share directory with our assets in
98 * stuff from here is autoserved by the library
101 static const struct lws_http_mount mount = {
102 (struct lws_http_mount *)&mount_post, /* linked-list pointer to next*/
103 "/", /* mountpoint in URL namespace on this vhost */
104 LOCAL_RESOURCE_PATH, /* where to go on the filesystem for that */
105 "test.html", /* default filename if none given */
116 LWSMPRO_FILE, /* mount type is a directory in a filesystem */
117 1, /* strlen("/"), ie length of the mountpoint */
120 { NULL, NULL } // sentinel
124 * this sets a per-vhost, per-protocol option name:value pair
125 * the effect is to set this protocol to be the default one for the vhost,
126 * ie, selected if no Protocol: header is sent with the ws upgrade.
129 static const struct lws_protocol_vhost_options pvo_opt = {
137 * We must enable the plugin protocols we want into our vhost with a
138 * linked-list. We can also give the plugin per-vhost options here.
141 static const struct lws_protocol_vhost_options pvo_3 = {
144 "protocol-post-demo",
145 "" /* ignored, just matches the protocol name above */
148 static const struct lws_protocol_vhost_options pvo_2 = {
152 "" /* ignored, just matches the protocol name above */
155 static const struct lws_protocol_vhost_options pvo_1 = {
158 "lws-mirror-protocol",
162 static const struct lws_protocol_vhost_options pvo = {
165 "dumb-increment-protocol",
169 static void signal_cb(uv_signal_t *watcher, int signum)
171 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
172 switch (watcher->signum) {
177 signal(SIGABRT, SIG_DFL);
181 lws_libuv_stop(context);
184 static const struct option options[] = {
185 { "help", no_argument, NULL, 'h' },
186 { "debug", required_argument, NULL, 'd' },
187 { "port", required_argument, NULL, 'p' },
188 { "ssl", no_argument, NULL, 's' },
189 { "allow-non-ssl", no_argument, NULL, 'a' },
190 { "interface", required_argument, NULL, 'i' },
191 { "ssl-cert", required_argument, NULL, 'C' },
192 { "ssl-key", required_argument, NULL, 'K' },
193 { "ssl-ca", required_argument, NULL, 'A' },
194 #if defined(LWS_OPENSSL_SUPPORT)
195 { "ssl-verify-client", no_argument, NULL, 'v' },
196 #if defined(LWS_HAVE_SSL_CTX_set1_param)
197 { "ssl-crl", required_argument, NULL, 'R' },
200 #ifndef LWS_NO_DAEMONIZE
201 { "daemonize", no_argument, NULL, 'D' },
203 { "resource_path", required_argument, NULL, 'r' },
207 static const char * const plugin_dirs[] = {
208 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
212 int main(int argc, char **argv)
214 struct lws_context_creation_info info;
215 char interface_name[128] = "";
216 const char *iface = NULL;
217 char cert_path[1024] = "";
218 char key_path[1024] = "";
219 char ca_path[1024] = "";
220 int uid = -1, gid = -1;
225 int syslog_options = LOG_PID | LOG_PERROR;
227 #ifndef LWS_NO_DAEMONIZE
232 * take care to zero down the info struct, he contains random garbaage
233 * from the stack otherwise
235 memset(&info, 0, sizeof info);
239 n = getopt_long(argc, argv, "i:hsap:d:Dr:C:K:A:R:vu:g:",
240 (struct option *)options, NULL);
244 #ifndef LWS_NO_DAEMONIZE
248 syslog_options &= ~LOG_PERROR;
259 debug_level = atoi(optarg);
265 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
268 info.port = atoi(optarg);
271 strncpy(interface_name, optarg, sizeof interface_name);
272 interface_name[(sizeof interface_name) - 1] = '\0';
273 iface = interface_name;
276 resource_path = optarg;
277 printf("Setting resource path to \"%s\"\n", resource_path);
280 strncpy(cert_path, optarg, sizeof(cert_path) - 1);
281 cert_path[sizeof(cert_path) - 1] = '\0';
284 strncpy(key_path, optarg, sizeof(key_path) - 1);
285 key_path[sizeof(key_path) - 1] = '\0';
288 strncpy(ca_path, optarg, sizeof(ca_path) - 1);
289 ca_path[sizeof(ca_path) - 1] = '\0';
291 #if defined(LWS_OPENSSL_SUPPORT)
294 opts |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
297 #if defined(LWS_HAVE_SSL_CTX_set1_param)
299 strncpy(crl_path, optarg, sizeof(crl_path) - 1);
300 crl_path[sizeof(crl_path) - 1] = '\0';
305 fprintf(stderr, "Usage: test-server "
306 "[--port=<p>] [--ssl] "
307 "[-d <log bitfield>] "
308 "[--resource_path <path>]\n");
313 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
315 * normally lock path would be /var/lock/lwsts or similar, to
316 * simplify getting started without having to take care about
317 * permissions or running as root, set to /tmp/.lwsts-lock
319 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
320 fprintf(stderr, "Failed to daemonize\n");
325 signal(SIGINT, sighandler);
328 /* we will only try to log things according to our debug_level */
329 setlogmask(LOG_UPTO (LOG_DEBUG));
330 openlog("lwsts", syslog_options, LOG_DAEMON);
333 /* tell the library what debug level to emit and to send it to syslog */
334 lws_set_log_level(debug_level, lwsl_emit_syslog);
336 lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
337 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
339 lwsl_notice(" Using resource path \"%s\"\n", resource_path);
342 info.protocols = NULL; /* all protocols from lib / plugins */
343 info.ssl_cert_filepath = NULL;
344 info.ssl_private_key_filepath = NULL;
347 info.max_http_header_pool = 16;
348 info.options = opts |
349 LWS_SERVER_OPTION_VALIDATE_UTF8 |
350 LWS_SERVER_OPTION_LIBUV; /* plugins require this */
353 if (strlen(resource_path) > sizeof(cert_path) - 32) {
354 lwsl_err("resource path too long\n");
358 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
360 if (strlen(resource_path) > sizeof(key_path) - 32) {
361 lwsl_err("resource path too long\n");
365 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
368 info.ssl_cert_filepath = cert_path;
369 info.ssl_private_key_filepath = key_path;
371 info.ssl_ca_filepath = ca_path;
373 /* redirect guys coming on http */
374 info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS;
377 info.extensions = exts;
378 info.timeout_secs = 5;
379 info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
380 "ECDHE-RSA-AES256-GCM-SHA384:"
381 "DHE-RSA-AES256-GCM-SHA384:"
382 "ECDHE-RSA-AES256-SHA384:"
383 "HIGH:!aNULL:!eNULL:!EXPORT:"
384 "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
385 "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
386 "!DHE-RSA-AES128-SHA256:"
387 "!AES128-GCM-SHA256:"
389 "!DHE-RSA-AES256-SHA256:"
390 "!AES256-GCM-SHA384:"
393 /* tell lws to look for protocol plugins here */
394 info.plugin_dirs = plugin_dirs;
396 /* tell lws about our mount we want */
397 info.mounts = &mount;
399 * give it our linked-list of Per-Vhost Options, these control
400 * which protocols (from plugins) are allowed to be enabled on
406 * As it is, this creates the context and a single Vhost at the same
407 * time. You can use LWS_SERVER_OPTION_EXPLICIT_VHOSTS option above
408 * to just create the context, and call lws_create_vhost() afterwards
409 * multiple times with different info to get multiple listening vhosts.
411 context = lws_create_context(&info);
412 if (context == NULL) {
413 lwsl_err("libwebsocket init failed\n");
417 /* libuv event loop */
418 lws_uv_sigint_cfg(context, 1, signal_cb);
419 if (lws_uv_initloop(context, NULL, 0))
420 lwsl_err("lws_uv_initloop failed\n");
422 lws_libuv_run(context, 0);
424 /* when we decided to exit the event loop */
425 lws_context_destroy(context);
426 lwsl_notice("libwebsockets-test-server exited cleanly\n");