2 * libwebsockets-test-server for libev - libwebsockets test implementation
4 * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include "test-server.h"
25 int max_poll_elements;
27 struct lws_context *context;
28 struct lws_plat_file_ops fops_plat;
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 /* singlethreaded version --> no locks */
36 void test_server_lock(int care)
39 void test_server_unlock(int care)
44 * This demo server shows how to use libwebsockets for one or more
45 * websocket protocols in the same server
47 * It defines the following websocket protocols:
49 * dumb-increment-protocol: once the socket is opened, an incrementing
50 * ascii string is sent down it every 50ms.
51 * If you send "reset\n" on the websocket, then
52 * the incrementing number is reset to 0.
54 * lws-mirror-protocol: copies any received packet to every connection also
55 * using this protocol, including the sender
62 PROTOCOL_DUMB_INCREMENT,
69 /* list of supported protocols and callbacks */
71 static struct lws_protocols protocols[] = {
72 /* first protocol must always be HTTP handler */
75 "http-only", /* name */
76 callback_http, /* callback */
77 sizeof (struct per_session_data__http), /* per_session_data_size */
78 0, /* max frame size / rx buffer */
81 "dumb-increment-protocol",
82 callback_dumb_increment,
83 sizeof(struct per_session_data__dumb_increment),
87 "lws-mirror-protocol",
89 sizeof(struct per_session_data__lws_mirror),
95 sizeof(struct per_session_data__lws_status),
98 { NULL, NULL, 0, 0 } /* terminator */
101 static const struct lws_extension exts[] = {
103 "permessage-deflate",
104 lws_extension_callback_pm_deflate,
105 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
109 lws_extension_callback_pm_deflate,
112 { NULL, NULL, NULL /* terminator */ }
115 void signal_cb(uv_signal_t *watcher, int revents)
117 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
118 switch (watcher->signum) {
123 signal(SIGABRT, SIG_DFL);
127 lws_libuv_stop(context);
131 uv_timeout_cb_dumb_increment(uv_timer_t *w)
133 lws_callback_on_writable_all_protocol(context,
134 &protocols[PROTOCOL_DUMB_INCREMENT]);
137 static struct option options[] = {
138 { "help", no_argument, NULL, 'h' },
139 { "debug", required_argument, NULL, 'd' },
140 { "port", required_argument, NULL, 'p' },
141 { "ssl", no_argument, NULL, 's' },
142 { "allow-non-ssl", no_argument, NULL, 'a' },
143 { "interface", required_argument, NULL, 'i' },
144 { "closetest", no_argument, NULL, 'c' },
145 { "libev", no_argument, NULL, 'e' },
146 #ifndef LWS_NO_DAEMONIZE
147 { "daemonize", no_argument, NULL, 'D' },
149 { "resource_path", required_argument, NULL, 'r' },
153 int main(int argc, char **argv)
155 struct lws_context_creation_info info;
156 char interface_name[128] = "";
157 uv_timer_t timeout_watcher;
158 const char *iface = NULL;
159 char cert_path[1024];
165 int syslog_options = LOG_PID | LOG_PERROR;
167 #ifndef LWS_NO_DAEMONIZE
172 * take care to zero down the info struct, he contains random garbaage
173 * from the stack otherwise
175 memset(&info, 0, sizeof info);
179 n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL);
184 opts |= LWS_SERVER_OPTION_LIBEV;
186 #ifndef LWS_NO_DAEMONIZE
190 syslog_options &= ~LOG_PERROR;
195 debug_level = atoi(optarg);
201 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
204 info.port = atoi(optarg);
207 strncpy(interface_name, optarg, sizeof interface_name);
208 interface_name[(sizeof interface_name) - 1] = '\0';
209 iface = interface_name;
213 fprintf(stderr, " Close testing mode -- closes on "
214 "client after 50 dumb increments"
215 "and suppresses lws_mirror spam\n");
218 resource_path = optarg;
219 printf("Setting resource path to \"%s\"\n", resource_path);
222 fprintf(stderr, "Usage: test-server "
223 "[--port=<p>] [--ssl] "
224 "[-d <log bitfield>] "
225 "[--resource_path <path>]\n");
230 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
232 * normally lock path would be /var/lock/lwsts or similar, to
233 * simplify getting started without having to take care about
234 * permissions or running as root, set to /tmp/.lwsts-lock
236 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
237 fprintf(stderr, "Failed to daemonize\n");
242 /* we will only try to log things according to our debug_level */
243 setlogmask(LOG_UPTO (LOG_DEBUG));
244 openlog("lwsts", syslog_options, LOG_DAEMON);
246 /* tell the library what debug level to emit and to send it to syslog */
247 lws_set_log_level(debug_level, lwsl_emit_syslog);
249 lwsl_notice("libwebsockets test server libuv - license LGPL2.1+SLE\n");
250 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
252 lwsl_info("Using resource path \"%s\"\n", resource_path);
255 info.protocols = protocols;
256 info.extensions = exts;
258 info.ssl_cert_filepath = NULL;
259 info.ssl_private_key_filepath = NULL;
262 if (strlen(resource_path) > sizeof(cert_path) - 32) {
263 lwsl_err("resource path too long\n");
266 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
268 if (strlen(resource_path) > sizeof(key_path) - 32) {
269 lwsl_err("resource path too long\n");
272 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
275 info.ssl_cert_filepath = cert_path;
276 info.ssl_private_key_filepath = key_path;
280 info.max_http_header_pool = 1;
281 info.timeout_secs = 5;
282 info.options = opts | LWS_SERVER_OPTION_LIBUV;
284 context = lws_create_context(&info);
285 if (context == NULL) {
286 lwsl_err("libwebsocket init failed\n");
290 lws_uv_initloop(context, NULL, signal_cb, 0);
292 uv_timer_init(lws_uv_getloop(context, 0), &timeout_watcher);
293 uv_timer_start(&timeout_watcher, uv_timeout_cb_dumb_increment, 50, 50);
295 lws_libuv_run(context, 0);
297 lws_context_destroy(context);
298 lwsl_notice("libwebsockets-test-server exited cleanly\n");