win fix warnings from appveyor
[platform/upstream/libwebsockets.git] / test-server / test-server-v2.0.c
1 /*
2  * libwebsockets-test-server-v2.0 - libwebsockets test implementation
3  *
4  * Copyright (C) 2010-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
21 #include <libwebsockets.h>
22 #include <getopt.h>
23 #ifndef WIN32
24 #include <syslog.h>
25 #endif
26
27 int debug_level = 7;
28 struct lws_context *context;
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 #if defined(LWS_USE_POLARSSL)
34 #else
35 #if defined(LWS_USE_MBEDTLS)
36 #else
37 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
38 char crl_path[1024] = "";
39 #endif
40 #endif
41 #endif
42
43 /*
44  * This test server is ONLY this .c file, it's radically simpler than the
45  * pre-v2.0 test servers.  For example it has no user callback content or
46  * defines any protocols.
47  *
48  * To achieve that, it uses the LWS protocol plugins.  Those in turn
49  * use libuv.  So you must configure with LWS_WITH_PLUGINS (which implies
50  * libuv) to get this to build.
51  *
52  * You can find the individual protocol plugin sources in ../plugins
53  */
54
55 void sighandler(int sig)
56 {
57         lws_cancel_service(context);
58 }
59
60 static const struct lws_extension exts[] = {
61         {
62                 "permessage-deflate",
63                 lws_extension_callback_pm_deflate,
64                 "permessage-deflate"
65         },
66         {
67                 "deflate-frame",
68                 lws_extension_callback_pm_deflate,
69                 "deflate_frame"
70         },
71         { NULL, NULL, NULL /* terminator */ }
72 };
73
74 /*
75  * mount a handler for a section of the URL space
76  */
77
78 static const struct lws_http_mount mount_post = {
79         NULL,           /* linked-list pointer to next*/
80         "/formtest",            /* mountpoint in URL namespace on this vhost */
81         "protocol-post-demo",   /* handler */
82         NULL,   /* default filename if none given */
83         NULL,
84         NULL,
85         0,
86         0,
87         0,
88         0,
89         0,
90         LWSMPRO_CALLBACK,       /* origin points to a callback */
91         9,                      /* strlen("/formtest"), ie length of the mountpoint */
92 };
93
94 /*
95  * mount a filesystem directory into the URL space at /
96  * point it to our /usr/share directory with our assets in
97  * stuff from here is autoserved by the library
98  */
99
100 static const struct lws_http_mount mount = {
101         (struct lws_http_mount *)&mount_post,           /* linked-list pointer to next*/
102         "/",            /* mountpoint in URL namespace on this vhost */
103         LOCAL_RESOURCE_PATH, /* where to go on the filesystem for that */
104         "test.html",    /* default filename if none given */
105         NULL,
106         NULL,
107         0,
108         0,
109         0,
110         0,
111         0,
112         LWSMPRO_FILE,   /* mount type is a directory in a filesystem */
113         1,              /* strlen("/"), ie length of the mountpoint */
114 };
115
116 /*
117  * this sets a per-vhost, per-protocol option name:value pair
118  * the effect is to set this protocol to be the default one for the vhost,
119  * ie, selected if no Protocol: header is sent with the ws upgrade.
120  */
121
122 static const struct lws_protocol_vhost_options pvo_opt = {
123         NULL,
124         NULL,
125         "default",
126         "1"
127 };
128
129 /*
130  * We must enable the plugin protocols we want into our vhost with a
131  * linked-list.  We can also give the plugin per-vhost options here.
132  */
133
134 static const struct lws_protocol_vhost_options pvo_3 = {
135         NULL,
136         NULL,
137         "protocol-post-demo",
138         "" /* ignored, just matches the protocol name above */
139 };
140
141 static const struct lws_protocol_vhost_options pvo_2 = {
142         &pvo_3,
143         NULL,
144         "lws-status",
145         "" /* ignored, just matches the protocol name above */
146 };
147
148 static const struct lws_protocol_vhost_options pvo_1 = {
149         &pvo_2,
150         NULL,
151         "lws-mirror-protocol",
152         ""
153 };
154
155 static const struct lws_protocol_vhost_options pvo = {
156         &pvo_1,
157         &pvo_opt,
158         "dumb-increment-protocol",
159         ""
160 };
161
162 static void signal_cb(uv_signal_t *watcher, int signum)
163 {
164         lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
165         switch (watcher->signum) {
166         case SIGTERM:
167         case SIGINT:
168                 break;
169         default:
170                 signal(SIGABRT, SIG_DFL);
171                 abort();
172                 break;
173         }
174         lws_libuv_stop(context);
175 }
176
177 static const struct option options[] = {
178         { "help",       no_argument,            NULL, 'h' },
179         { "debug",      required_argument,      NULL, 'd' },
180         { "port",       required_argument,      NULL, 'p' },
181         { "ssl",        no_argument,            NULL, 's' },
182         { "allow-non-ssl",      no_argument,    NULL, 'a' },
183         { "interface",  required_argument,      NULL, 'i' },
184         { "ssl-cert",  required_argument,       NULL, 'C' },
185         { "ssl-key",  required_argument,        NULL, 'K' },
186         { "ssl-ca",  required_argument,         NULL, 'A' },
187 #if defined(LWS_OPENSSL_SUPPORT)
188         { "ssl-verify-client",  no_argument,            NULL, 'v' },
189 #if defined(LWS_HAVE_SSL_CTX_set1_param)
190         { "ssl-crl",  required_argument,                NULL, 'R' },
191 #endif
192 #endif
193 #ifndef LWS_NO_DAEMONIZE
194         { "daemonize",  no_argument,            NULL, 'D' },
195 #endif
196         { "resource_path", required_argument,   NULL, 'r' },
197         { NULL, 0, 0, 0 }
198 };
199
200 static const char * const plugin_dirs[] = {
201                 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
202                 NULL
203 };
204
205 int main(int argc, char **argv)
206 {
207         struct lws_context_creation_info info;
208         char interface_name[128] = "";
209         const char *iface = NULL;
210         char cert_path[1024] = "";
211         char key_path[1024] = "";
212         char ca_path[1024] = "";
213         int uid = -1, gid = -1;
214         int use_ssl = 0;
215         int opts = 0;
216         int n = 0;
217 #ifndef _WIN32
218         int syslog_options = LOG_PID | LOG_PERROR;
219 #endif
220 #ifndef LWS_NO_DAEMONIZE
221         int daemonize = 0;
222 #endif
223
224         /*
225          * take care to zero down the info struct, he contains random garbaage
226          * from the stack otherwise
227          */
228         memset(&info, 0, sizeof info);
229         info.port = 7681;
230
231         while (n >= 0) {
232                 n = getopt_long(argc, argv, "i:hsap:d:Dr:C:K:A:R:vu:g:",
233                                 (struct option *)options, NULL);
234                 if (n < 0)
235                         continue;
236                 switch (n) {
237 #ifndef LWS_NO_DAEMONIZE
238                 case 'D':
239                         daemonize = 1;
240                         #ifndef _WIN32
241                         syslog_options &= ~LOG_PERROR;
242                         #endif
243                         break;
244 #endif
245                 case 'u':
246                         uid = atoi(optarg);
247                         break;
248                 case 'g':
249                         gid = atoi(optarg);
250                         break;
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 'r':
269                         resource_path = optarg;
270                         printf("Setting resource path to \"%s\"\n", resource_path);
271                         break;
272                 case 'C':
273                         strncpy(cert_path, optarg, sizeof(cert_path) - 1);
274                         cert_path[sizeof(cert_path) - 1] = '\0';
275                         break;
276                 case 'K':
277                         strncpy(key_path, optarg, sizeof(key_path) - 1);
278                         key_path[sizeof(key_path) - 1] = '\0';
279                         break;
280                 case 'A':
281                         strncpy(ca_path, optarg, sizeof(ca_path) - 1);
282                         ca_path[sizeof(ca_path) - 1] = '\0';
283                         break;
284 #if defined(LWS_OPENSSL_SUPPORT)
285                 case 'v':
286                         use_ssl = 1;
287                         opts |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
288                         break;
289 #if defined(LWS_USE_POLARSSL)
290 #else
291 #if defined(LWS_USE_MBEDTLS)
292 #else
293 #if defined(LWS_HAVE_SSL_CTX_set1_param)
294                 case 'R':
295                         strncpy(crl_path, optarg, sizeof(crl_path) - 1);
296                         crl_path[sizeof(crl_path) - 1] = '\0';
297                         break;
298 #endif
299 #endif
300 #endif
301 #endif
302                 case 'h':
303                         fprintf(stderr, "Usage: test-server "
304                                         "[--port=<p>] [--ssl] "
305                                         "[-d <log bitfield>] "
306                                         "[--resource_path <path>]\n");
307                         exit(1);
308                 }
309         }
310
311 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
312         /*
313          * normally lock path would be /var/lock/lwsts or similar, to
314          * simplify getting started without having to take care about
315          * permissions or running as root, set to /tmp/.lwsts-lock
316          */
317         if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
318                 fprintf(stderr, "Failed to daemonize\n");
319                 return 10;
320         }
321 #endif
322
323         signal(SIGINT, sighandler);
324
325 #ifndef _WIN32
326         /* we will only try to log things according to our debug_level */
327         setlogmask(LOG_UPTO (LOG_DEBUG));
328         openlog("lwsts", syslog_options, LOG_DAEMON);
329 #endif
330
331         /* tell the library what debug level to emit and to send it to syslog */
332         lws_set_log_level(debug_level, lwsl_emit_syslog);
333
334         lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
335         lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
336
337         lwsl_notice(" Using resource path \"%s\"\n", resource_path);
338
339         info.iface = iface;
340         info.protocols = NULL; /* all protocols from lib / plugins */
341         info.ssl_cert_filepath = NULL;
342         info.ssl_private_key_filepath = NULL;
343         info.gid = gid;
344         info.uid = uid;
345         info.max_http_header_pool = 16;
346         info.options = opts |
347                         LWS_SERVER_OPTION_VALIDATE_UTF8 |
348                         LWS_SERVER_OPTION_LIBUV; /* plugins require this */
349
350         if (use_ssl) {
351                 if (strlen(resource_path) > sizeof(cert_path) - 32) {
352                         lwsl_err("resource path too long\n");
353                         return -1;
354                 }
355                 if (!cert_path[0])
356                         sprintf(cert_path, "%s/libwebsockets-test-server.pem",
357                                 resource_path);
358                 if (strlen(resource_path) > sizeof(key_path) - 32) {
359                         lwsl_err("resource path too long\n");
360                         return -1;
361                 }
362                 if (!key_path[0])
363                         sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
364                                 resource_path);
365
366                 info.ssl_cert_filepath = cert_path;
367                 info.ssl_private_key_filepath = key_path;
368                 if (ca_path[0])
369                         info.ssl_ca_filepath = ca_path;
370
371                 /* redirect guys coming on http */
372                 info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS;
373         }
374
375         info.extensions = exts;
376         info.timeout_secs = 5;
377         info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
378                                "ECDHE-RSA-AES256-GCM-SHA384:"
379                                "DHE-RSA-AES256-GCM-SHA384:"
380                                "ECDHE-RSA-AES256-SHA384:"
381                                "HIGH:!aNULL:!eNULL:!EXPORT:"
382                                "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
383                                "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
384                                "!DHE-RSA-AES128-SHA256:"
385                                "!AES128-GCM-SHA256:"
386                                "!AES128-SHA256:"
387                                "!DHE-RSA-AES256-SHA256:"
388                                "!AES256-GCM-SHA384:"
389                                "!AES256-SHA256";
390
391         /* tell lws to look for protocol plugins here */
392         info.plugin_dirs = plugin_dirs;
393
394         /* tell lws about our mount we want */
395         info.mounts = &mount;
396         /*
397          *  give it our linked-list of Per-Vhost Options, these control
398          * which protocols (from plugins) are allowed to be enabled on
399          * our vhost
400          */
401         info.pvo = &pvo;
402
403         /*
404          * As it is, this creates the context and a single Vhost at the same
405          * time.  You can use LWS_SERVER_OPTION_EXPLICIT_VHOSTS option above
406          * to just create the context, and call lws_create_vhost() afterwards
407          * multiple times with different info to get multiple listening vhosts.
408          */
409         context = lws_create_context(&info);
410         if (context == NULL) {
411                 lwsl_err("libwebsocket init failed\n");
412                 return -1;
413         }
414
415         /* libuv event loop */
416         lws_uv_sigint_cfg(context, 1, signal_cb);
417         if (lws_uv_initloop(context, NULL, 0))
418                 lwsl_err("lws_uv_initloop failed\n");
419         else
420                 lws_libuv_run(context, 0);
421
422         /* when we decided to exit the event loop */
423         lws_context_destroy(context);
424         lwsl_notice("libwebsockets-test-server exited cleanly\n");
425
426 #ifndef _WIN32
427         closelog();
428 #endif
429
430         return 0;
431 }