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