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