2 * libwebsockets-test-server - libwebsockets test implementation
4 * Copyright (C) 2011-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
20 #include "test-server.h"
23 int max_poll_elements;
25 volatile int force_exit = 0;
26 struct lws_context *context;
27 struct lws_plat_file_ops fops_plat;
29 /* http server gets files from this path */
30 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
31 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 * libev dumps their hygiene problems on their users blaming compiler
45 * http://lists.schmorp.de/pipermail/libev/2008q4/000442.html
48 #if EV_MINPRI == EV_MAXPRI
49 # define _ev_set_priority(ev, pri) ((ev), (pri))
51 # define _ev_set_priority(ev, pri) { \
52 ev_watcher *evw = (ev_watcher *)(void *)ev; \
53 evw->priority = pri; \
57 #define _ev_init(ev,cb_) { \
58 ev_watcher *evw = (ev_watcher *)(void *)ev; \
60 evw->active = evw->pending = 0; \
61 _ev_set_priority((ev), 0); \
62 ev_set_cb((ev), cb_); \
65 #define _ev_timer_init(ev, cb, after, _repeat) { \
66 ev_watcher_time *evwt = (ev_watcher_time *)(void *)ev; \
70 (ev)->repeat = _repeat; \
73 /* singlethreaded version --> no locks */
75 void test_server_lock(int care)
78 void test_server_unlock(int care)
83 * This demo server shows how to use libwebsockets for one or more
84 * websocket protocols in the same server
86 * It defines the following websocket protocols:
88 * dumb-increment-protocol: once the socket is opened, an incrementing
89 * ascii string is sent down it every 50ms.
90 * If you send "reset\n" on the websocket, then
91 * the incrementing number is reset to 0.
93 * lws-mirror-protocol: copies any received packet to every connection also
94 * using this protocol, including the sender
101 PROTOCOL_DUMB_INCREMENT,
108 /* list of supported protocols and callbacks */
110 static struct lws_protocols protocols[] = {
111 /* first protocol must always be HTTP handler */
114 "http-only", /* name */
115 callback_http, /* callback */
116 sizeof (struct per_session_data__http), /* per_session_data_size */
117 0, /* max frame size / rx buffer */
120 "dumb-increment-protocol",
121 callback_dumb_increment,
122 sizeof(struct per_session_data__dumb_increment),
126 "lws-mirror-protocol",
128 sizeof(struct per_session_data__lws_mirror),
131 { NULL, NULL, 0, 0 } /* terminator */
134 static const struct lws_extension exts[] = {
136 "permessage-deflate",
137 lws_extension_callback_pm_deflate,
138 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
142 lws_extension_callback_pm_deflate,
145 { NULL, NULL, NULL /* terminator */ }
148 /* this shows how to override the lws file operations. You don't need
149 * to do any of this unless you have a reason (eg, want to serve
150 * compressed files without decompressing the whole archive)
152 static lws_filefd_type
153 test_server_fops_open(struct lws *wsi, const char *filename,
154 unsigned long *filelen, int flags)
158 /* call through to original platform implementation */
159 n = fops_plat.open(wsi, filename, filelen, flags);
161 lwsl_notice("%s: opening %s, ret %ld, len %lu\n", __func__, filename,
167 void signal_cb(struct ev_loop *loop, struct ev_signal* watcher, int revents)
169 lwsl_notice("Signal caught, exiting...\n");
171 switch (watcher->signum) {
174 ev_break(loop, EVBREAK_ALL);
177 signal(SIGABRT, SIG_DFL);
184 ev_timeout_cb (EV_P_ ev_timer *w, int revents)
186 lws_callback_on_writable_all_protocol(context,
187 &protocols[PROTOCOL_DUMB_INCREMENT]);
190 static struct option options[] = {
191 { "help", no_argument, NULL, 'h' },
192 { "debug", required_argument, NULL, 'd' },
193 { "port", required_argument, NULL, 'p' },
194 { "ssl", no_argument, NULL, 's' },
195 { "allow-non-ssl", no_argument, NULL, 'a' },
196 { "interface", required_argument, NULL, 'i' },
197 { "closetest", no_argument, NULL, 'c' },
198 { "libev", no_argument, NULL, 'e' },
199 #ifndef LWS_NO_DAEMONIZE
200 { "daemonize", no_argument, NULL, 'D' },
202 { "resource_path", required_argument, NULL, 'r' },
206 int main(int argc, char **argv)
208 int sigs[] = { SIGINT, SIGKILL, SIGTERM, SIGSEGV, SIGFPE };
209 struct ev_signal signals[ARRAY_SIZE(sigs)];
210 struct ev_loop *loop = ev_default_loop(0);
211 struct lws_context_creation_info info;
212 char interface_name[128] = "";
213 const char *iface = NULL;
214 ev_timer timeout_watcher;
215 char cert_path[1024];
221 int syslog_options = LOG_PID | LOG_PERROR;
223 #ifndef LWS_NO_DAEMONIZE
228 * take care to zero down the info struct, he contains random garbaage
229 * from the stack otherwise
231 memset(&info, 0, sizeof info);
235 n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL);
240 opts |= LWS_SERVER_OPTION_LIBEV;
242 #ifndef LWS_NO_DAEMONIZE
246 syslog_options &= ~LOG_PERROR;
251 debug_level = atoi(optarg);
257 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
260 info.port = atoi(optarg);
263 strncpy(interface_name, optarg, sizeof interface_name);
264 interface_name[(sizeof interface_name) - 1] = '\0';
265 iface = interface_name;
269 fprintf(stderr, " Close testing mode -- closes on "
270 "client after 50 dumb increments"
271 "and suppresses lws_mirror spam\n");
274 resource_path = optarg;
275 printf("Setting resource path to \"%s\"\n", resource_path);
278 fprintf(stderr, "Usage: test-server "
279 "[--port=<p>] [--ssl] "
280 "[-d <log bitfield>] "
281 "[--resource_path <path>]\n");
286 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
288 * normally lock path would be /var/lock/lwsts or similar, to
289 * simplify getting started without having to take care about
290 * permissions or running as root, set to /tmp/.lwsts-lock
292 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
293 fprintf(stderr, "Failed to daemonize\n");
298 for (n = 0; n < ARRAY_SIZE(sigs); n++) {
299 _ev_init(&signals[n], signal_cb);
300 ev_signal_set(&signals[n], sigs[n]);
301 ev_signal_start(loop, &signals[n]);
305 /* we will only try to log things according to our debug_level */
306 setlogmask(LOG_UPTO (LOG_DEBUG));
307 openlog("lwsts", syslog_options, LOG_DAEMON);
310 /* tell the library what debug level to emit and to send it to syslog */
311 lws_set_log_level(debug_level, lwsl_emit_syslog);
313 lwsl_notice("libwebsockets test server libev - license LGPL2.1+SLE\n");
314 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
316 printf("Using resource path \"%s\"\n", resource_path);
319 info.protocols = protocols;
320 info.extensions = exts;
322 info.ssl_cert_filepath = NULL;
323 info.ssl_private_key_filepath = NULL;
326 if (strlen(resource_path) > sizeof(cert_path) - 32) {
327 lwsl_err("resource path too long\n");
330 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
332 if (strlen(resource_path) > sizeof(key_path) - 32) {
333 lwsl_err("resource path too long\n");
336 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
339 info.ssl_cert_filepath = cert_path;
340 info.ssl_private_key_filepath = key_path;
342 opts |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
346 info.max_http_header_pool = 1;
347 info.options = opts | LWS_SERVER_OPTION_LIBEV;
349 context = lws_create_context(&info);
350 if (context == NULL) {
351 lwsl_err("libwebsocket init failed\n");
356 * this shows how to override the lws file operations. You don't need
357 * to do any of this unless you have a reason (eg, want to serve
358 * compressed files without decompressing the whole archive)
360 /* stash original platform fops */
361 fops_plat = *(lws_get_fops(context));
362 /* override the active fops */
363 lws_get_fops(context)->open = test_server_fops_open;
365 lws_ev_initloop(context, loop, 0);
367 _ev_timer_init(&timeout_watcher, ev_timeout_cb, 0.05, 0.05);
368 ev_timer_start(loop, &timeout_watcher);
371 lws_context_destroy(context);
372 lwsl_notice("libwebsockets-test-server exited cleanly\n");