test-server-v2.0: disable setting default protocol
[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         { "allow-non-ssl",      no_argument,    NULL, 'a' },
235         { "interface",  required_argument,      NULL, 'i' },
236         { "ssl-cert",  required_argument,       NULL, 'C' },
237         { "ssl-key",  required_argument,        NULL, 'K' },
238         { "ssl-ca",  required_argument,         NULL, 'A' },
239 #if defined(LWS_OPENSSL_SUPPORT)
240         { "ssl-verify-client",  no_argument,            NULL, 'v' },
241 #if defined(LWS_HAVE_SSL_CTX_set1_param)
242         { "ssl-crl",  required_argument,                NULL, 'R' },
243 #endif
244 #endif
245 #ifndef LWS_NO_DAEMONIZE
246         { "daemonize",  no_argument,            NULL, 'D' },
247 #endif
248         { "resource_path", required_argument,   NULL, 'r' },
249         { NULL, 0, 0, 0 }
250 };
251
252 static const char * const plugin_dirs[] = {
253                 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
254                 NULL
255 };
256
257 int main(int argc, char **argv)
258 {
259         struct lws_context_creation_info info;
260         char interface_name[128] = "";
261         const char *iface = NULL;
262         char cert_path[1024] = "";
263         char key_path[1024] = "";
264         char ca_path[1024] = "";
265         int uid = -1, gid = -1;
266         int use_ssl = 0;
267         int opts = 0;
268         int n = 0;
269 #ifndef _WIN32
270         int syslog_options = LOG_PID | LOG_PERROR;
271 #endif
272 #ifndef LWS_NO_DAEMONIZE
273         int daemonize = 0;
274 #endif
275
276         /*
277          * take care to zero down the info struct, he contains random garbaage
278          * from the stack otherwise
279          */
280         memset(&info, 0, sizeof info);
281         info.port = 7681;
282
283         while (n >= 0) {
284                 n = getopt_long(argc, argv, "i:hsap:d:Dr:C:K:A:R:vu:g:",
285                                 (struct option *)options, NULL);
286                 if (n < 0)
287                         continue;
288                 switch (n) {
289 #ifndef LWS_NO_DAEMONIZE
290                 case 'D':
291                         daemonize = 1;
292                         #ifndef _WIN32
293                         syslog_options &= ~LOG_PERROR;
294                         #endif
295                         break;
296 #endif
297                 case 'u':
298                         uid = atoi(optarg);
299                         break;
300                 case 'g':
301                         gid = atoi(optarg);
302                         break;
303                 case 'd':
304                         debug_level = atoi(optarg);
305                         break;
306                 case 's':
307                         use_ssl = 1;
308                         break;
309                 case 'a':
310                         opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
311                         break;
312                 case 'p':
313                         info.port = atoi(optarg);
314                         break;
315                 case 'i':
316                         strncpy(interface_name, optarg, sizeof interface_name);
317                         interface_name[(sizeof interface_name) - 1] = '\0';
318                         iface = interface_name;
319                         break;
320                 case 'r':
321                         resource_path = optarg;
322                         printf("Setting resource path to \"%s\"\n", resource_path);
323                         break;
324                 case 'C':
325                         strncpy(cert_path, optarg, sizeof(cert_path) - 1);
326                         cert_path[sizeof(cert_path) - 1] = '\0';
327                         break;
328                 case 'K':
329                         strncpy(key_path, optarg, sizeof(key_path) - 1);
330                         key_path[sizeof(key_path) - 1] = '\0';
331                         break;
332                 case 'A':
333                         strncpy(ca_path, optarg, sizeof(ca_path) - 1);
334                         ca_path[sizeof(ca_path) - 1] = '\0';
335                         break;
336 #if defined(LWS_OPENSSL_SUPPORT)
337                 case 'v':
338                         use_ssl = 1;
339                         opts |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
340                         break;
341
342 #if defined(LWS_HAVE_SSL_CTX_set1_param)
343                 case 'R':
344                         strncpy(crl_path, optarg, sizeof(crl_path) - 1);
345                         crl_path[sizeof(crl_path) - 1] = '\0';
346                         break;
347 #endif
348 #endif
349                 case 'h':
350                         fprintf(stderr, "Usage: test-server "
351                                         "[--port=<p>] [--ssl] "
352                                         "[-d <log bitfield>] "
353                                         "[--resource_path <path>]\n");
354                         exit(1);
355                 }
356         }
357
358 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
359         /*
360          * normally lock path would be /var/lock/lwsts or similar, to
361          * simplify getting started without having to take care about
362          * permissions or running as root, set to /tmp/.lwsts-lock
363          */
364         if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
365                 fprintf(stderr, "Failed to daemonize\n");
366                 return 10;
367         }
368 #endif
369
370         signal(SIGINT, sighandler);
371
372 #ifndef _WIN32
373         /* we will only try to log things according to our debug_level */
374         setlogmask(LOG_UPTO (LOG_DEBUG));
375         openlog("lwsts", syslog_options, LOG_DAEMON);
376 #endif
377
378         /* tell the library what debug level to emit and to send it to syslog */
379         lws_set_log_level(debug_level, lwsl_emit_syslog);
380
381         lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
382         lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
383
384         lwsl_notice(" Using resource path \"%s\"\n", resource_path);
385
386         info.iface = iface;
387         info.protocols = NULL; /* all protocols from lib / plugins */
388         info.ssl_cert_filepath = NULL;
389         info.ssl_private_key_filepath = NULL;
390         info.gid = gid;
391         info.uid = uid;
392         info.max_http_header_pool = 16;
393         info.options = opts | LWS_SERVER_OPTION_FALLBACK_TO_RAW |
394                         LWS_SERVER_OPTION_VALIDATE_UTF8 |
395                         LWS_SERVER_OPTION_LIBUV; /* plugins require this */
396
397         if (use_ssl) {
398                 if (strlen(resource_path) > sizeof(cert_path) - 32) {
399                         lwsl_err("resource path too long\n");
400                         return -1;
401                 }
402                 if (!cert_path[0])
403                         sprintf(cert_path, "%s/libwebsockets-test-server.pem",
404                                 resource_path);
405                 if (strlen(resource_path) > sizeof(key_path) - 32) {
406                         lwsl_err("resource path too long\n");
407                         return -1;
408                 }
409                 if (!key_path[0])
410                         sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
411                                 resource_path);
412
413                 info.ssl_cert_filepath = cert_path;
414                 info.ssl_private_key_filepath = key_path;
415                 if (ca_path[0])
416                         info.ssl_ca_filepath = ca_path;
417
418                 /* redirect guys coming on http */
419                 info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS;
420         }
421
422         info.extensions = exts;
423         info.timeout_secs = 5;
424         info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
425                                "ECDHE-RSA-AES256-GCM-SHA384:"
426                                "DHE-RSA-AES256-GCM-SHA384:"
427                                "ECDHE-RSA-AES256-SHA384:"
428                                "HIGH:!aNULL:!eNULL:!EXPORT:"
429                                "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
430                                "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
431                                "!DHE-RSA-AES128-SHA256:"
432                                "!AES128-GCM-SHA256:"
433                                "!AES128-SHA256:"
434                                "!DHE-RSA-AES256-SHA256:"
435                                "!AES256-GCM-SHA384:"
436                                "!AES256-SHA256";
437
438         /* tell lws to look for protocol plugins here */
439         info.plugin_dirs = plugin_dirs;
440
441         /* tell lws about our mount we want */
442         info.mounts = &mount;
443         /*
444          *  give it our linked-list of Per-Vhost Options, these control
445          * which protocols (from plugins) are allowed to be enabled on
446          * our vhost
447          */
448         info.pvo = &pvo;
449
450         /*
451          * As it is, this creates the context and a single Vhost at the same
452          * time.  You can use LWS_SERVER_OPTION_EXPLICIT_VHOSTS option above
453          * to just create the context, and call lws_create_vhost() afterwards
454          * multiple times with different info to get multiple listening vhosts.
455          */
456         context = lws_create_context(&info);
457         if (context == NULL) {
458                 lwsl_err("libwebsocket init failed\n");
459                 return -1;
460         }
461
462         /* libuv event loop */
463         lws_uv_sigint_cfg(context, 1, signal_cb);
464         if (lws_uv_initloop(context, NULL, 0))
465                 lwsl_err("lws_uv_initloop failed\n");
466         else
467                 lws_libuv_run(context, 0);
468
469         /* when we decided to exit the event loop */
470         lws_context_destroy(context);
471         lwsl_notice("libwebsockets-test-server exited cleanly\n");
472
473 #ifndef _WIN32
474         closelog();
475 #endif
476
477         return 0;
478 }