test-server-libuv: add lws_meta
[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 #define DI_HANDLED_BY_PLUGIN
23 #include "test-server.h"
24 #include <uv.h>
25
26 int close_testing;
27 int max_poll_elements;
28 int debug_level = 7;
29 struct lws_context *context;
30 struct lws_plat_file_ops fops_plat;
31
32 /* http server gets files from this path */
33 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
34 char *resource_path = LOCAL_RESOURCE_PATH;
35
36 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
37 char crl_path[1024] = "";
38 #endif
39
40 /* singlethreaded version --> no locks */
41
42 void test_server_lock(int care)
43 {
44 }
45 void test_server_unlock(int care)
46 {
47 }
48
49 #define LWS_PLUGIN_STATIC
50 #include "../plugins/protocol_dumb_increment.c"
51 #include "../plugins/protocol_lws_mirror.c"
52 #include "../plugins/protocol_lws_status.c"
53 #include "../plugins/protocol_lws_meta.c"
54
55 /*
56  * This demo server shows how to use libwebsockets for one or more
57  * websocket protocols in the same server
58  *
59  * It defines the following websocket protocols:
60  *
61  *  dumb-increment-protocol:  once the socket is opened, an incrementing
62  *                              ascii string is sent down it every 50ms.
63  *                              If you send "reset\n" on the websocket, then
64  *                              the incrementing number is reset to 0.
65  *
66  *  lws-mirror-protocol: copies any received packet to every connection also
67  *                              using this protocol, including the sender
68  */
69
70 enum demo_protocols {
71         /* always first */
72         PROTOCOL_HTTP = 0,
73
74         PROTOCOL_DUMB_INCREMENT,
75         PROTOCOL_LWS_MIRROR,
76         PROTOCOL_LWS_STATUS,
77         PROTOCOL_LWS_META,
78
79         /* always last */
80         DEMO_PROTOCOL_COUNT
81 };
82
83 /* list of supported protocols and callbacks */
84
85 static struct lws_protocols protocols[] = {
86         /* first protocol must always be HTTP handler */
87
88         {
89                 "http-only",            /* name */
90                 callback_http,          /* callback */
91                 sizeof (struct per_session_data__http), /* per_session_data_size */
92                 0,                      /* max frame size / rx buffer */
93         },
94         LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT,
95         LWS_PLUGIN_PROTOCOL_MIRROR,
96         LWS_PLUGIN_PROTOCOL_LWS_STATUS,
97         LWS_PLUGIN_PROTOCOL_LWS_META,
98         { NULL, NULL, 0, 0 } /* terminator */
99 };
100
101 static const struct lws_extension exts[] = {
102         {
103                 "permessage-deflate",
104                 lws_extension_callback_pm_deflate,
105                 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
106         },
107         {
108                 "deflate-frame",
109                 lws_extension_callback_pm_deflate,
110                 "deflate_frame"
111         },
112         { NULL, NULL, NULL /* terminator */ }
113 };
114
115 void signal_cb(uv_signal_t *watcher, int signum)
116 {
117         lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
118         switch (watcher->signum) {
119         case SIGTERM:
120         case SIGINT:
121                 break;
122         default:
123                 signal(SIGABRT, SIG_DFL);
124                 abort();
125                 break;
126         }
127         lws_libuv_stop(context);
128 }
129
130
131 static struct option options[] = {
132         { "help",       no_argument,            NULL, 'h' },
133         { "debug",      required_argument,      NULL, 'd' },
134         { "port",       required_argument,      NULL, 'p' },
135         { "ssl",        no_argument,            NULL, 's' },
136         { "allow-non-ssl",      no_argument,    NULL, 'a' },
137         { "interface",  required_argument,      NULL, 'i' },
138         { "closetest",  no_argument,            NULL, 'c' },
139         { "libev",  no_argument,                NULL, 'e' },
140         { "foreign",  no_argument,              NULL, 'f' },
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 #if UV_VERSION_MAJOR > 0
149 /* ----- this code is only needed for foreign / external libuv tests -----*/
150 struct counter
151 {
152         int cur, lim;
153         int stop_loop;
154 };
155
156 static void timer_cb(uv_timer_t *t)
157 {
158         struct counter *c = t->data;
159
160         lwsl_notice("  timer %p cb, count %d, loop has %d handles\n",
161                     t, c->cur, t->loop->active_handles);
162
163         if (c->cur++ == c->lim) {
164                 lwsl_debug("stop loop from timer\n");
165                 uv_timer_stop(t);
166                 if (c->stop_loop)
167                         uv_stop(t->loop);
168         }
169 }
170
171 static void timer_close_cb(uv_handle_t *h)
172 {
173         lwsl_notice("timer close cb %p, loop has %d handles\n",
174                     h, h->loop->active_handles);
175 }
176
177 void outer_signal_cb(uv_signal_t *s, int signum)
178 {
179         lwsl_notice("Foreign loop got signal %d\n", signum);
180         uv_signal_stop(s);
181         uv_stop(s->loop);
182 }
183
184 static void lws_uv_close_cb(uv_handle_t *handle)
185 {
186         //lwsl_err("%s\n", __func__);
187 }
188
189 static void lws_uv_walk_cb(uv_handle_t *handle, void *arg)
190 {
191         uv_close(handle, lws_uv_close_cb);
192 }
193
194 /* --- end of foreign test code ---- */
195 #endif
196
197 int main(int argc, char **argv)
198 {
199         struct lws_context_creation_info info;
200         char interface_name[128] = "";
201 #if UV_VERSION_MAJOR > 0
202 /* --- only needed for foreign loop test ---> */
203         uv_loop_t loop;
204         uv_signal_t signal_outer;
205         uv_timer_t timer_outer;
206         struct counter ctr;
207         int foreign_libuv_loop = 0;
208 /* <--- only needed for foreign loop test --- */
209 #endif
210         const char *iface = NULL;
211         char cert_path[1024];
212         char key_path[1024];
213         int use_ssl = 0;
214         int opts = 0;
215         int n = 0;
216 #ifndef _WIN32
217         int syslog_options = LOG_PID | LOG_PERROR;
218 #endif
219 #ifndef LWS_NO_DAEMONIZE
220         int daemonize = 0;
221 #endif
222
223         /*
224          * take care to zero down the info struct, he contains random garbaage
225          * from the stack otherwise
226          */
227         memset(&info, 0, sizeof info);
228         info.port = 7681;
229
230         while (n >= 0) {
231                 n = getopt_long(argc, argv, "feci:hsap:d:Dr:", options, NULL);
232                 if (n < 0)
233                         continue;
234                 switch (n) {
235                 case 'f':
236 #if UV_VERSION_MAJOR > 0
237                         foreign_libuv_loop = 1;
238 #endif
239                         break;
240                 case 'e':
241                         opts |= LWS_SERVER_OPTION_LIBEV;
242                         break;
243 #ifndef LWS_NO_DAEMONIZE
244                 case 'D':
245                         daemonize = 1;
246                         #ifndef _WIN32
247                         syslog_options &= ~LOG_PERROR;
248                         #endif
249                         break;
250 #endif
251                 case 'd':
252                         debug_level = atoi(optarg);
253                         break;
254                 case 's':
255                         use_ssl = 1;
256                         break;
257                 case 'a':
258                         opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
259                         break;
260                 case 'p':
261                         info.port = atoi(optarg);
262                         break;
263                 case 'i':
264                         strncpy(interface_name, optarg, sizeof interface_name);
265                         interface_name[(sizeof interface_name) - 1] = '\0';
266                         iface = interface_name;
267                         break;
268                 case 'c':
269                         close_testing = 1;
270                         fprintf(stderr, " Close testing mode -- closes on "
271                                            "client after 50 dumb increments"
272                                            "and suppresses lws_mirror spam\n");
273                         break;
274                 case 'r':
275                         resource_path = optarg;
276                         printf("Setting resource path to \"%s\"\n", resource_path);
277                         break;
278                 case 'h':
279                         fprintf(stderr, "Usage: test-server "
280                                         "[--port=<p>] [--ssl] "
281                                         "[-d <log bitfield>] "
282                                         "[--resource_path <path>]\n");
283                         exit(1);
284                 }
285         }
286
287 #if !defined(WIN32)
288 #if !defined(LWS_NO_DAEMONIZE)
289         /*
290          * normally lock path would be /var/lock/lwsts or similar, to
291          * simplify getting started without having to take care about
292          * permissions or running as root, set to /tmp/.lwsts-lock
293          */
294         if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
295                 fprintf(stderr, "Failed to daemonize\n");
296                 return 1;
297         }
298 #endif
299
300         /* we will only try to log things according to our debug_level */
301         setlogmask(LOG_UPTO (LOG_DEBUG));
302         openlog("lwsts", syslog_options, LOG_DAEMON);
303 #endif
304
305         /* tell the library what debug level to emit and to send it to syslog */
306         lws_set_log_level(debug_level, lwsl_emit_syslog);
307
308         lwsl_notice("libwebsockets test server libuv - license LGPL2.1+SLE\n");
309         lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
310
311         lwsl_info("Using resource path \"%s\"\n", resource_path);
312
313         info.iface = iface;
314         info.protocols = protocols;
315         info.extensions = exts;
316
317         info.ssl_cert_filepath = NULL;
318         info.ssl_private_key_filepath = NULL;
319
320         if (use_ssl) {
321                 if (strlen(resource_path) > sizeof(cert_path) - 32) {
322                         lwsl_err("resource path too long\n");
323                         return -1;
324                 }
325                 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
326                         resource_path);
327                 if (strlen(resource_path) > sizeof(key_path) - 32) {
328                         lwsl_err("resource path too long\n");
329                         return -1;
330                 }
331                 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
332                         resource_path);
333
334                 info.ssl_cert_filepath = cert_path;
335                 info.ssl_private_key_filepath = key_path;
336                 opts |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
337         }
338         info.gid = -1;
339         info.uid = -1;
340         info.max_http_header_pool = 1;
341         info.timeout_secs = 5;
342         info.options = opts | LWS_SERVER_OPTION_LIBUV;
343
344 #if UV_VERSION_MAJOR > 0
345         if (foreign_libuv_loop) {
346                 /* create the foreign loop */
347                 uv_loop_init(&loop);
348
349                 /* run some timer on that loop just so loop is not 'clean' */
350
351                 uv_signal_init(&loop, &signal_outer);
352                 uv_signal_start(&signal_outer, outer_signal_cb, SIGINT);
353
354                 uv_timer_init(&loop, &timer_outer);
355                 timer_outer.data = &ctr;
356                 ctr.cur = 0;
357                 ctr.lim = ctr.cur + 5;
358                 ctr.stop_loop = 1;
359                 uv_timer_start(&timer_outer, timer_cb, 0, 1000);
360                 lwsl_notice("running loop without libwebsockets for %d s\n", ctr.lim);
361
362                 uv_run(&loop, UV_RUN_DEFAULT);
363
364                 /* timer will stop loop and we will get here */
365         }
366 #endif
367
368         context = lws_create_context(&info);
369         if (context == NULL) {
370                 lwsl_err("libwebsocket init failed\n");
371                 return -1;
372         }
373
374         lws_uv_sigint_cfg(context, 1, signal_cb);
375
376 #if UV_VERSION_MAJOR > 0
377         if (foreign_libuv_loop)
378                 /* we have our own uv loop outside of lws */
379                 lws_uv_initloop(context, &loop, 0);
380         else
381 #endif
382         {
383                 /*
384                  * lws will create his own libuv loop in the context
385                  */
386                 if (lws_uv_initloop(context, NULL, 0)) {
387                         lwsl_err("lws_uv_initloop failed\n");
388
389                         goto bail;
390                 }
391         }
392
393 #if UV_VERSION_MAJOR > 0
394         if (foreign_libuv_loop) {
395                 /*
396                  * prepare inner timer on loop, to run along with lws.
397                  * Will exit after 5s while lws keeps running
398                  */
399                 struct counter ctr_inner = { 0, 3, 0 };
400                 int e;
401                 uv_timer_t timer_inner;
402                 uv_timer_init(&loop, &timer_inner);
403                 timer_inner.data = &ctr_inner;
404                 uv_timer_start(&timer_inner, timer_cb, 200, 1000);
405
406                 /* make this timer long-lived, should keep
407                  * firing after lws exits */
408                 ctr.cur = 0;
409                 ctr.lim = ctr.cur + 1000;
410                 uv_timer_start(&timer_outer, timer_cb, 0, 1000);
411
412                 uv_run(&loop, UV_RUN_DEFAULT);
413
414                 /* we are here either because signal stopped us,
415                  * or outer timer expired */
416
417                 /* close short timer */
418                 uv_timer_stop(&timer_inner);
419                 uv_close((uv_handle_t*)&timer_inner, timer_close_cb);
420
421
422                 lwsl_notice("Destroying lws context\n");
423
424                 /* detach lws */
425                 lws_context_destroy(context);
426
427                 lwsl_notice("Please wait while the outer libuv test continues for 10s\n");
428
429                 ctr.lim = ctr.cur + 10;
430
431                 /* try and run outer timer for 10 more seconds,
432                  * (or sigint outer handler) after lws has left the loop */
433                 uv_run(&loop, UV_RUN_DEFAULT);
434
435                 /* Clean up the foreign loop now */
436
437                 /* PHASE 1: stop and close things we created
438                  *          outside of lws */
439
440                 uv_timer_stop(&timer_outer);
441                 uv_close((uv_handle_t*)&timer_outer, timer_close_cb);
442                 uv_signal_stop(&signal_outer);
443
444                 e = 100;
445                 while (e--)
446                         uv_run(&loop, UV_RUN_NOWAIT);
447
448                 /* PHASE 2: close anything remaining */
449
450                 uv_walk(&loop, lws_uv_walk_cb, NULL);
451
452                 e = 100;
453                 while (e--)
454                         uv_run(&loop, UV_RUN_NOWAIT);
455
456                 /* PHASE 3: close the UV loop itself */
457
458                 e = uv_loop_close(&loop);
459                 lwsl_notice("uv loop close rc %s\n",
460                             e ? uv_strerror(e) : "ok");
461
462                 /* PHASE 4: finalize context destruction */
463
464                 lws_context_destroy2(context);
465         } else
466 #endif
467         {
468                 lws_libuv_run(context, 0);
469
470 bail:
471                 lws_context_destroy(context);
472                 lws_context_destroy2(context);
473         }
474
475         lwsl_notice("libwebsockets-test-server exited cleanly\n");
476
477         context = NULL;
478
479         return 0;
480 }