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