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