2 * libwebsockets-test-servet - 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 "test-server.h"
24 int max_poll_elements;
28 struct lws_pollfd *pollfds;
32 volatile int force_exit = 0;
33 struct lws_context *context;
34 struct lws_plat_file_ops fops_plat;
36 /* http server gets files from this path */
37 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
38 char *resource_path = LOCAL_RESOURCE_PATH;
40 /* singlethreaded version --> no locks */
42 void test_server_lock(int care)
45 void test_server_unlock(int care)
50 * This demo server shows how to use libwebsockets for one or more
51 * websocket protocols in the same server
53 * It defines the following websocket protocols:
55 * dumb-increment-protocol: once the socket is opened, an incrementing
56 * ascii string is sent down it every 50ms.
57 * If you send "reset\n" on the websocket, then
58 * the incrementing number is reset to 0.
60 * lws-mirror-protocol: copies any received packet to every connection also
61 * using this protocol, including the sender
68 PROTOCOL_DUMB_INCREMENT,
76 /* list of supported protocols and callbacks */
78 static struct lws_protocols protocols[] = {
79 /* first protocol must always be HTTP handler */
82 "http-only", /* name */
83 callback_http, /* callback */
84 sizeof (struct per_session_data__http), /* per_session_data_size */
85 0, /* max frame size / rx buffer */
88 "dumb-increment-protocol",
89 callback_dumb_increment,
90 sizeof(struct per_session_data__dumb_increment),
94 "lws-mirror-protocol",
96 sizeof(struct per_session_data__lws_mirror),
101 callback_lws_echogen,
102 sizeof(struct per_session_data__echogen),
105 { NULL, NULL, 0, 0 } /* terminator */
109 /* this shows how to override the lws file operations. You don't need
110 * to do any of this unless you have a reason (eg, want to serve
111 * compressed files without decompressing the whole archive)
113 static lws_filefd_type
114 test_server_fops_open(struct lws *wsi, const char *filename,
115 unsigned long *filelen, int flags)
119 /* call through to original platform implementation */
120 n = fops_plat.open(wsi, filename, filelen, flags);
122 lwsl_notice("%s: opening %s, ret %ld, len %lu\n", __func__, filename,
128 void sighandler(int sig)
131 lws_cancel_service(context);
134 static const struct lws_extension exts[] = {
136 "permessage-deflate",
137 lws_extension_callback_pm_deflate,
142 lws_extension_callback_pm_deflate,
145 { NULL, NULL, NULL /* terminator */ }
150 static struct option options[] = {
151 { "help", no_argument, NULL, 'h' },
152 { "debug", required_argument, NULL, 'd' },
153 { "port", required_argument, NULL, 'p' },
154 { "ssl", no_argument, NULL, 's' },
155 { "allow-non-ssl", no_argument, NULL, 'a' },
156 { "interface", required_argument, NULL, 'i' },
157 { "closetest", no_argument, NULL, 'c' },
158 { "libev", no_argument, NULL, 'e' },
159 #ifndef LWS_NO_DAEMONIZE
160 { "daemonize", no_argument, NULL, 'D' },
162 { "resource_path", required_argument, NULL, 'r' },
166 int main(int argc, char **argv)
168 struct lws_context_creation_info info;
169 char interface_name[128] = "";
170 unsigned int ms, oldms = 0;
171 const char *iface = NULL;
172 char cert_path[1024];
178 int syslog_options = LOG_PID | LOG_PERROR;
180 #ifndef LWS_NO_DAEMONIZE
185 * take care to zero down the info struct, he contains random garbaage
186 * from the stack otherwise
188 memset(&info, 0, sizeof info);
192 n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL);
197 opts |= LWS_SERVER_OPTION_LIBEV;
199 #ifndef LWS_NO_DAEMONIZE
203 syslog_options &= ~LOG_PERROR;
208 debug_level = atoi(optarg);
214 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
217 info.port = atoi(optarg);
220 strncpy(interface_name, optarg, sizeof interface_name);
221 interface_name[(sizeof interface_name) - 1] = '\0';
222 iface = interface_name;
226 fprintf(stderr, " Close testing mode -- closes on "
227 "client after 50 dumb increments"
228 "and suppresses lws_mirror spam\n");
231 resource_path = optarg;
232 printf("Setting resource path to \"%s\"\n", resource_path);
235 fprintf(stderr, "Usage: test-server "
236 "[--port=<p>] [--ssl] "
237 "[-d <log bitfield>] "
238 "[--resource_path <path>]\n");
243 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
245 * normally lock path would be /var/lock/lwsts or similar, to
246 * simplify getting started without having to take care about
247 * permissions or running as root, set to /tmp/.lwsts-lock
249 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
250 fprintf(stderr, "Failed to daemonize\n");
255 signal(SIGINT, sighandler);
258 /* we will only try to log things according to our debug_level */
259 setlogmask(LOG_UPTO (LOG_DEBUG));
260 openlog("lwsts", syslog_options, LOG_DAEMON);
263 /* tell the library what debug level to emit and to send it to syslog */
264 lws_set_log_level(debug_level, lwsl_emit_syslog);
266 lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
267 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
269 printf("Using resource path \"%s\"\n", resource_path);
271 max_poll_elements = getdtablesize();
272 pollfds = malloc(max_poll_elements * sizeof (struct lws_pollfd));
273 fd_lookup = malloc(max_poll_elements * sizeof (int));
274 if (pollfds == NULL || fd_lookup == NULL) {
275 lwsl_err("Out of memory pollfds=%d\n", max_poll_elements);
281 info.protocols = protocols;
282 info.ssl_cert_filepath = NULL;
283 info.ssl_private_key_filepath = NULL;
286 if (strlen(resource_path) > sizeof(cert_path) - 32) {
287 lwsl_err("resource path too long\n");
290 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
292 if (strlen(resource_path) > sizeof(key_path) - 32) {
293 lwsl_err("resource path too long\n");
296 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
299 info.ssl_cert_filepath = cert_path;
300 info.ssl_private_key_filepath = key_path;
304 info.max_http_header_pool = 1;
305 info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8;
306 info.extensions = exts;
307 context = lws_create_context(&info);
308 if (context == NULL) {
309 lwsl_err("libwebsocket init failed\n");
313 /* this shows how to override the lws file operations. You don't need
314 * to do any of this unless you have a reason (eg, want to serve
315 * compressed files without decompressing the whole archive)
317 /* stash original platform fops */
318 fops_plat = *(lws_get_fops(context));
319 /* override the active fops */
320 lws_get_fops(context)->open = test_server_fops_open;
323 while (n >= 0 && !force_exit) {
326 gettimeofday(&tv, NULL);
329 * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
330 * live websocket connection using the DUMB_INCREMENT protocol,
331 * as soon as it can take more packets (usually immediately)
334 ms = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
335 if ((ms - oldms) > 50) {
336 lws_callback_on_writable_all_protocol(context,
337 &protocols[PROTOCOL_DUMB_INCREMENT]);
343 * this represents an existing server's single poll action
344 * which also includes libwebsocket sockets
347 n = poll(pollfds, count_pollfds, 50);
352 for (n = 0; n < count_pollfds; n++)
353 if (pollfds[n].revents)
355 * returns immediately if the fd does not
356 * match anything under libwebsockets
359 if (lws_service_fd(context,
364 * If libwebsockets sockets are all we care about,
365 * you can use this api which takes care of the poll()
366 * and looping through finding who needed service.
368 * If no socket needs service, it'll return anyway after
369 * the number of ms in the second argument.
372 n = lws_service(context, 50);
380 lws_context_destroy(context);
382 lwsl_notice("libwebsockets-test-server exited cleanly\n");