test server add lws_status
[platform/upstream/libwebsockets.git] / test-server / test-server-libuv.c
1 /*
2  * libwebsockets-test-server for libev - libwebsockets test implementation
3  *
4  * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
5  *
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.
10  *
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.
15  *
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,
19  *  MA  02110-1301  USA
20  */
21
22 #include "test-server.h"
23
24 int close_testing;
25 int max_poll_elements;
26 int debug_level = 7;
27 struct lws_context *context;
28 struct lws_plat_file_ops fops_plat;
29
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;
33
34 /* singlethreaded version --> no locks */
35
36 void test_server_lock(int care)
37 {
38 }
39 void test_server_unlock(int care)
40 {
41 }
42
43 /*
44  * This demo server shows how to use libwebsockets for one or more
45  * websocket protocols in the same server
46  *
47  * It defines the following websocket protocols:
48  *
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.
53  *
54  *  lws-mirror-protocol: copies any received packet to every connection also
55  *                              using this protocol, including the sender
56  */
57
58 enum demo_protocols {
59         /* always first */
60         PROTOCOL_HTTP = 0,
61
62         PROTOCOL_DUMB_INCREMENT,
63         PROTOCOL_LWS_MIRROR,
64
65         /* always last */
66         DEMO_PROTOCOL_COUNT
67 };
68
69 /* list of supported protocols and callbacks */
70
71 static struct lws_protocols protocols[] = {
72         /* first protocol must always be HTTP handler */
73
74         {
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 */
79         },
80         {
81                 "dumb-increment-protocol",
82                 callback_dumb_increment,
83                 sizeof(struct per_session_data__dumb_increment),
84                 10,
85         },
86         {
87                 "lws-mirror-protocol",
88                 callback_lws_mirror,
89                 sizeof(struct per_session_data__lws_mirror),
90                 128,
91         },
92         { NULL, NULL, 0, 0 } /* terminator */
93 };
94
95 static const struct lws_extension exts[] = {
96         {
97                 "permessage-deflate",
98                 lws_extension_callback_pm_deflate,
99                 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
100         },
101         {
102                 "deflate-frame",
103                 lws_extension_callback_pm_deflate,
104                 "deflate_frame"
105         },
106         { NULL, NULL, NULL /* terminator */ }
107 };
108
109 void signal_cb(uv_signal_t *watcher, int revents)
110 {
111         lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
112         switch (watcher->signum) {
113         case SIGTERM:
114         case SIGINT:
115                 break;
116         default:
117                 signal(SIGABRT, SIG_DFL);
118                 abort();
119                 break;
120         }
121         lws_libuv_stop(context);
122 }
123
124 static void
125 uv_timeout_cb(uv_timer_t *w)
126 {
127         lwsl_info("%s\n", __func__);
128         lws_callback_on_writable_all_protocol(context,
129                                         &protocols[PROTOCOL_DUMB_INCREMENT]);
130 }
131
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' },
143 #endif
144         { "resource_path", required_argument,   NULL, 'r' },
145         { NULL, 0, 0, 0 }
146 };
147
148 int main(int argc, char **argv)
149 {
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];
155         char key_path[1024];
156         int use_ssl = 0;
157         int opts = 0;
158         int n = 0;
159 #ifndef _WIN32
160         int syslog_options = LOG_PID | LOG_PERROR;
161 #endif
162 #ifndef LWS_NO_DAEMONIZE
163         int daemonize = 0;
164 #endif
165
166         /*
167          * take care to zero down the info struct, he contains random garbaage
168          * from the stack otherwise
169          */
170         memset(&info, 0, sizeof info);
171         info.port = 7681;
172
173         while (n >= 0) {
174                 n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL);
175                 if (n < 0)
176                         continue;
177                 switch (n) {
178                 case 'e':
179                         opts |= LWS_SERVER_OPTION_LIBEV;
180                         break;
181 #ifndef LWS_NO_DAEMONIZE
182                 case 'D':
183                         daemonize = 1;
184                         #ifndef _WIN32
185                         syslog_options &= ~LOG_PERROR;
186                         #endif
187                         break;
188 #endif
189                 case 'd':
190                         debug_level = atoi(optarg);
191                         break;
192                 case 's':
193                         use_ssl = 1;
194                         break;
195                 case 'a':
196                         opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
197                         break;
198                 case 'p':
199                         info.port = atoi(optarg);
200                         break;
201                 case 'i':
202                         strncpy(interface_name, optarg, sizeof interface_name);
203                         interface_name[(sizeof interface_name) - 1] = '\0';
204                         iface = interface_name;
205                         break;
206                 case 'c':
207                         close_testing = 1;
208                         fprintf(stderr, " Close testing mode -- closes on "
209                                            "client after 50 dumb increments"
210                                            "and suppresses lws_mirror spam\n");
211                         break;
212                 case 'r':
213                         resource_path = optarg;
214                         printf("Setting resource path to \"%s\"\n", resource_path);
215                         break;
216                 case 'h':
217                         fprintf(stderr, "Usage: test-server "
218                                         "[--port=<p>] [--ssl] "
219                                         "[-d <log bitfield>] "
220                                         "[--resource_path <path>]\n");
221                         exit(1);
222                 }
223         }
224
225 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
226         /*
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
230          */
231         if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
232                 fprintf(stderr, "Failed to daemonize\n");
233                 return 1;
234         }
235 #endif
236
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);
240
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);
243
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");
246
247         lwsl_info("Using resource path \"%s\"\n", resource_path);
248
249         info.iface = iface;
250         info.protocols = protocols;
251         info.extensions = exts;
252
253         info.ssl_cert_filepath = NULL;
254         info.ssl_private_key_filepath = NULL;
255
256         if (use_ssl) {
257                 if (strlen(resource_path) > sizeof(cert_path) - 32) {
258                         lwsl_err("resource path too long\n");
259                         return -1;
260                 }
261                 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
262                         resource_path);
263                 if (strlen(resource_path) > sizeof(key_path) - 32) {
264                         lwsl_err("resource path too long\n");
265                         return -1;
266                 }
267                 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
268                         resource_path);
269
270                 info.ssl_cert_filepath = cert_path;
271                 info.ssl_private_key_filepath = key_path;
272         }
273         info.gid = -1;
274         info.uid = -1;
275         info.max_http_header_pool = 1;
276         info.options = opts | LWS_SERVER_OPTION_LIBUV;
277
278         context = lws_create_context(&info);
279         if (context == NULL) {
280                 lwsl_err("libwebsocket init failed\n");
281                 return -1;
282         }
283
284         lws_uv_initloop(context, NULL, signal_cb, 0);
285
286         uv_timer_init(lws_uv_getloop(context, 0), &timeout_watcher);
287         uv_timer_start(&timeout_watcher, uv_timeout_cb, 50, 50);
288
289         lws_libuv_run(context, 0);
290
291         lws_context_destroy(context);
292         lwsl_notice("libwebsockets-test-server exited cleanly\n");
293
294         return 0;
295 }