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),
92 { NULL, NULL, 0, 0 } /* terminator */
95 static const struct lws_extension exts[] = {
98 lws_extension_callback_pm_deflate,
99 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
103 lws_extension_callback_pm_deflate,
106 { NULL, NULL, NULL /* terminator */ }
109 void signal_cb(uv_signal_t *watcher, int revents)
111 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
112 switch (watcher->signum) {
117 signal(SIGABRT, SIG_DFL);
121 lws_libuv_stop(context);
125 uv_timeout_cb(uv_timer_t *w)
127 lwsl_info("%s\n", __func__);
128 lws_callback_on_writable_all_protocol(context,
129 &protocols[PROTOCOL_DUMB_INCREMENT]);
132 static struct option options[] = {
133 { "help", no_argument, NULL, 'h' },
134 { "debug", required_argument, NULL, 'd' },
135 { "port", required_argument, NULL, 'p' },
136 { "ssl", no_argument, NULL, 's' },
137 { "allow-non-ssl", no_argument, NULL, 'a' },
138 { "interface", required_argument, NULL, 'i' },
139 { "closetest", no_argument, NULL, 'c' },
140 { "libev", no_argument, NULL, 'e' },
141 #ifndef LWS_NO_DAEMONIZE
142 { "daemonize", no_argument, NULL, 'D' },
144 { "resource_path", required_argument, NULL, 'r' },
148 int main(int argc, char **argv)
150 struct lws_context_creation_info info;
151 char interface_name[128] = "";
152 uv_timer_t timeout_watcher;
153 const char *iface = NULL;
154 char cert_path[1024];
160 int syslog_options = LOG_PID | LOG_PERROR;
162 #ifndef LWS_NO_DAEMONIZE
167 * take care to zero down the info struct, he contains random garbaage
168 * from the stack otherwise
170 memset(&info, 0, sizeof info);
174 n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL);
179 opts |= LWS_SERVER_OPTION_LIBEV;
181 #ifndef LWS_NO_DAEMONIZE
185 syslog_options &= ~LOG_PERROR;
190 debug_level = atoi(optarg);
196 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
199 info.port = atoi(optarg);
202 strncpy(interface_name, optarg, sizeof interface_name);
203 interface_name[(sizeof interface_name) - 1] = '\0';
204 iface = interface_name;
208 fprintf(stderr, " Close testing mode -- closes on "
209 "client after 50 dumb increments"
210 "and suppresses lws_mirror spam\n");
213 resource_path = optarg;
214 printf("Setting resource path to \"%s\"\n", resource_path);
217 fprintf(stderr, "Usage: test-server "
218 "[--port=<p>] [--ssl] "
219 "[-d <log bitfield>] "
220 "[--resource_path <path>]\n");
225 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
227 * normally lock path would be /var/lock/lwsts or similar, to
228 * simplify getting started without having to take care about
229 * permissions or running as root, set to /tmp/.lwsts-lock
231 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
232 fprintf(stderr, "Failed to daemonize\n");
237 /* we will only try to log things according to our debug_level */
238 setlogmask(LOG_UPTO (LOG_DEBUG));
239 openlog("lwsts", syslog_options, LOG_DAEMON);
241 /* tell the library what debug level to emit and to send it to syslog */
242 lws_set_log_level(debug_level, lwsl_emit_syslog);
244 lwsl_notice("libwebsockets test server libuv - license LGPL2.1+SLE\n");
245 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
247 lwsl_info("Using resource path \"%s\"\n", resource_path);
250 info.protocols = protocols;
251 info.extensions = exts;
253 info.ssl_cert_filepath = NULL;
254 info.ssl_private_key_filepath = NULL;
257 if (strlen(resource_path) > sizeof(cert_path) - 32) {
258 lwsl_err("resource path too long\n");
261 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
263 if (strlen(resource_path) > sizeof(key_path) - 32) {
264 lwsl_err("resource path too long\n");
267 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
270 info.ssl_cert_filepath = cert_path;
271 info.ssl_private_key_filepath = key_path;
275 info.max_http_header_pool = 1;
276 info.options = opts | LWS_SERVER_OPTION_LIBUV;
278 context = lws_create_context(&info);
279 if (context == NULL) {
280 lwsl_err("libwebsocket init failed\n");
284 lws_uv_initloop(context, NULL, signal_cb, 0);
286 uv_timer_init(lws_uv_getloop(context, 0), &timeout_watcher);
287 uv_timer_start(&timeout_watcher, uv_timeout_cb, 50, 50);
289 lws_libuv_run(context, 0);
291 lws_context_destroy(context);
292 lwsl_notice("libwebsockets-test-server exited cleanly\n");