test servers: convert to static inclusion of plugins
[platform/upstream/libwebsockets.git] / test-server / test-server-libev.c
1 /*
2  * libwebsockets-test-server - libwebsockets test implementation
3  *
4  * Copyright (C) 2011-2016 Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
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.
15  *
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
18  * Public Domain.
19  */
20 #include "test-server.h"
21
22 int close_testing;
23 int max_poll_elements;
24 int debug_level = 7;
25 volatile int force_exit = 0;
26 struct lws_context *context;
27 struct lws_plat_file_ops fops_plat;
28
29 /* http server gets files from this path */
30 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
31 char *resource_path = LOCAL_RESOURCE_PATH;
32
33 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
34 char crl_path[1024] = "";
35 #endif
36
37 #define LWS_PLUGIN_STATIC
38 #include "../plugins/protocol_lws_mirror.c"
39 #include "../plugins/protocol_lws_status.c"
40
41 /* singlethreaded version --> no locks */
42
43 void test_server_lock(int care)
44 {
45 }
46 void test_server_unlock(int care)
47 {
48 }
49
50 /*
51  * This demo server shows how to use libwebsockets for one or more
52  * websocket protocols in the same server
53  *
54  * It defines the following websocket protocols:
55  *
56  *  dumb-increment-protocol:  once the socket is opened, an incrementing
57  *                              ascii string is sent down it every 50ms.
58  *                              If you send "reset\n" on the websocket, then
59  *                              the incrementing number is reset to 0.
60  *
61  *  lws-mirror-protocol: copies any received packet to every connection also
62  *                              using this protocol, including the sender
63  */
64
65 enum demo_protocols {
66         /* always first */
67         PROTOCOL_HTTP = 0,
68
69         PROTOCOL_DUMB_INCREMENT,
70         PROTOCOL_LWS_MIRROR,
71         PROTOCOL_LWS_STATUS,
72
73         /* always last */
74         DEMO_PROTOCOL_COUNT
75 };
76
77 /* list of supported protocols and callbacks */
78
79 static struct lws_protocols protocols[] = {
80         /* first protocol must always be HTTP handler */
81
82         {
83                 "http-only",            /* name */
84                 callback_http,          /* callback */
85                 sizeof (struct per_session_data__http), /* per_session_data_size */
86                 0,                      /* max frame size / rx buffer */
87         },
88         {
89                 "dumb-increment-protocol",
90                 callback_dumb_increment,
91                 sizeof(struct per_session_data__dumb_increment),
92                 10, /* rx buf size must be >= permessage-deflate rx size
93                      * dumb-increment only sends very small packets, so we set
94                      * this accordingly.  If your protocol will send bigger
95                      * things, adjust this to match */
96         },
97         LWS_PLUGIN_PROTOCOL_MIRROR,
98         LWS_PLUGIN_PROTOCOL_LWS_STATUS,
99         { NULL, NULL, 0, 0 } /* terminator */
100 };
101
102 static const struct lws_extension exts[] = {
103         {
104                 "permessage-deflate",
105                 lws_extension_callback_pm_deflate,
106                 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
107         },
108         {
109                 "deflate-frame",
110                 lws_extension_callback_pm_deflate,
111                 "deflate_frame"
112         },
113         { NULL, NULL, NULL /* terminator */ }
114 };
115
116 /* this shows how to override the lws file operations.  You don't need
117  * to do any of this unless you have a reason (eg, want to serve
118  * compressed files without decompressing the whole archive)
119  */
120 static lws_fop_fd_t
121 test_server_fops_open(const struct lws_plat_file_ops *fops,
122                       const char *vfs_path, const char *vpath,
123                       lws_fop_flags_t *flags)
124 {
125         lws_fop_fd_t n;
126
127         /* call through to original platform implementation */
128         n = fops_plat.open(fops, vfs_path, vpath, flags);
129
130         lwsl_notice("%s: opening %s, ret %p\n", __func__, vfs_path, n);
131
132         return n;
133 }
134
135 void signal_cb(struct ev_loop *loop, struct ev_signal* watcher, int revents)
136 {
137         lwsl_notice("Signal caught, exiting...\n");
138         force_exit = 1;
139         switch (watcher->signum) {
140         case SIGTERM:
141         case SIGINT:
142                 ev_break(loop, EVBREAK_ALL);
143                 break;
144         default:
145                 signal(SIGABRT, SIG_DFL);
146                 abort();
147                 break;
148         }
149 }
150
151 static void
152 ev_timeout_cb (EV_P_ ev_timer *w, int revents)
153 {
154         lws_callback_on_writable_all_protocol(context,
155                                         &protocols[PROTOCOL_DUMB_INCREMENT]);
156 }
157
158 static struct option options[] = {
159         { "help",       no_argument,            NULL, 'h' },
160         { "debug",      required_argument,      NULL, 'd' },
161         { "port",       required_argument,      NULL, 'p' },
162         { "ssl",        no_argument,            NULL, 's' },
163         { "allow-non-ssl",      no_argument,    NULL, 'a' },
164         { "interface",  required_argument,      NULL, 'i' },
165         { "closetest",  no_argument,            NULL, 'c' },
166         { "libev",  no_argument,                NULL, 'e' },
167 #ifndef LWS_NO_DAEMONIZE
168         { "daemonize",  no_argument,            NULL, 'D' },
169 #endif
170         { "resource_path", required_argument,   NULL, 'r' },
171         { NULL, 0, 0, 0 }
172 };
173
174 int main(int argc, char **argv)
175 {
176         int sigs[] = { SIGINT, SIGKILL, SIGTERM, SIGSEGV, SIGFPE };
177         struct ev_signal signals[ARRAY_SIZE(sigs)];
178         struct ev_loop *loop = ev_default_loop(0);
179         struct lws_context_creation_info info;
180         char interface_name[128] = "";
181         const char *iface = NULL;
182         ev_timer timeout_watcher;
183         char cert_path[1024];
184         char key_path[1024];
185         int use_ssl = 0;
186         int opts = 0;
187         int n = 0;
188 #ifndef _WIN32
189         int syslog_options = LOG_PID | LOG_PERROR;
190 #endif
191 #ifndef LWS_NO_DAEMONIZE
192         int daemonize = 0;
193 #endif
194
195         /*
196          * take care to zero down the info struct, he contains random garbaage
197          * from the stack otherwise
198          */
199         memset(&info, 0, sizeof info);
200         info.port = 7681;
201
202         while (n >= 0) {
203                 n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL);
204                 if (n < 0)
205                         continue;
206                 switch (n) {
207                 case 'e':
208                         opts |= LWS_SERVER_OPTION_LIBEV;
209                         break;
210 #ifndef LWS_NO_DAEMONIZE
211                 case 'D':
212                         daemonize = 1;
213                         #ifndef _WIN32
214                         syslog_options &= ~LOG_PERROR;
215                         #endif
216                         break;
217 #endif
218                 case 'd':
219                         debug_level = atoi(optarg);
220                         break;
221                 case 's':
222                         use_ssl = 1;
223                         break;
224                 case 'a':
225                         opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
226                         break;
227                 case 'p':
228                         info.port = atoi(optarg);
229                         break;
230                 case 'i':
231                         strncpy(interface_name, optarg, sizeof interface_name);
232                         interface_name[(sizeof interface_name) - 1] = '\0';
233                         iface = interface_name;
234                         break;
235                 case 'c':
236                         close_testing = 1;
237                         fprintf(stderr, " Close testing mode -- closes on "
238                                            "client after 50 dumb increments"
239                                            "and suppresses lws_mirror spam\n");
240                         break;
241                 case 'r':
242                         resource_path = optarg;
243                         printf("Setting resource path to \"%s\"\n", resource_path);
244                         break;
245                 case 'h':
246                         fprintf(stderr, "Usage: test-server "
247                                         "[--port=<p>] [--ssl] "
248                                         "[-d <log bitfield>] "
249                                         "[--resource_path <path>]\n");
250                         exit(1);
251                 }
252         }
253
254 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
255         /*
256          * normally lock path would be /var/lock/lwsts or similar, to
257          * simplify getting started without having to take care about
258          * permissions or running as root, set to /tmp/.lwsts-lock
259          */
260         if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
261                 fprintf(stderr, "Failed to daemonize\n");
262                 return 1;
263         }
264 #endif
265
266         for (n = 0; n < ARRAY_SIZE(sigs); n++) {
267                 ev_init(&signals[n], signal_cb);
268                 ev_signal_set(&signals[n], sigs[n]);
269                 ev_signal_start(loop, &signals[n]);
270         }
271
272 #ifndef _WIN32
273         /* we will only try to log things according to our debug_level */
274         setlogmask(LOG_UPTO (LOG_DEBUG));
275         openlog("lwsts", syslog_options, LOG_DAEMON);
276 #endif
277
278         /* tell the library what debug level to emit and to send it to syslog */
279         lws_set_log_level(debug_level, lwsl_emit_syslog);
280
281         lwsl_notice("libwebsockets test server libev - license LGPL2.1+SLE\n");
282         lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
283
284         printf("Using resource path \"%s\"\n", resource_path);
285
286         info.iface = iface;
287         info.protocols = protocols;
288         info.extensions = exts;
289
290         info.ssl_cert_filepath = NULL;
291         info.ssl_private_key_filepath = NULL;
292
293         if (use_ssl) {
294                 if (strlen(resource_path) > sizeof(cert_path) - 32) {
295                         lwsl_err("resource path too long\n");
296                         return -1;
297                 }
298                 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
299                                                                 resource_path);
300                 if (strlen(resource_path) > sizeof(key_path) - 32) {
301                         lwsl_err("resource path too long\n");
302                         return -1;
303                 }
304                 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
305                                                                 resource_path);
306
307                 info.ssl_cert_filepath = cert_path;
308                 info.ssl_private_key_filepath = key_path;
309
310                 opts |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
311         }
312         info.gid = -1;
313         info.uid = -1;
314         info.max_http_header_pool = 1;
315         info.options = opts | LWS_SERVER_OPTION_LIBEV;
316
317         context = lws_create_context(&info);
318         if (context == NULL) {
319                 lwsl_err("libwebsocket init failed\n");
320                 return -1;
321         }
322
323         /*
324          * this shows how to override the lws file operations.  You don't need
325          * to do any of this unless you have a reason (eg, want to serve
326          * compressed files without decompressing the whole archive)
327          */
328         /* stash original platform fops */
329         fops_plat = *(lws_get_fops(context));
330         /* override the active fops */
331         lws_get_fops(context)->open = test_server_fops_open;
332
333         lws_ev_initloop(context, loop, 0);
334
335         ev_timer_init(&timeout_watcher, ev_timeout_cb, 0.05, 0.05);
336         ev_timer_start(loop, &timeout_watcher);
337         ev_run(loop, 0);
338
339         lws_context_destroy(context);
340         lwsl_notice("libwebsockets-test-server exited cleanly\n");
341
342 #ifndef _WIN32
343         closelog();
344 #endif
345
346         return 0;
347 }