fix %3d handling in path part and add attack.sh
[platform/upstream/libwebsockets.git] / test-server / test-server-libev.c
1 /*
2  * libwebsockets-test-server - libwebsockets test implementation
3  *
4  * Copyright (C) 2011-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 #include "test-server.h"
21
22 int close_testing;
23 int max_poll_elements;
24 int debug_level = 7;
25 volatile int force_exit = 0;
26 struct lws_context *context;
27 struct lws_plat_file_ops fops_plat;
28
29 /* http server gets files from this path */
30 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
31 char *resource_path = LOCAL_RESOURCE_PATH;
32
33 #if defined(LWS_USE_POLARSSL)
34 #else
35 #if defined(LWS_USE_MBEDTLS)
36 #else
37 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
38 char crl_path[1024] = "";
39 #endif
40 #endif
41 #endif
42
43 /*
44  * libev dumps their hygiene problems on their users blaming compiler
45  * http://lists.schmorp.de/pipermail/libev/2008q4/000442.html
46  */
47
48 #if EV_MINPRI == EV_MAXPRI
49 # define _ev_set_priority(ev, pri) ((ev), (pri))
50 #else
51 # define _ev_set_priority(ev, pri) { \
52         ev_watcher *evw = (ev_watcher *)(void *)ev; \
53         evw->priority = pri; \
54 }
55 #endif
56
57 #define _ev_init(ev,cb_) {  \
58         ev_watcher *evw = (ev_watcher *)(void *)ev; \
59 \
60         evw->active = evw->pending = 0; \
61         _ev_set_priority((ev), 0); \
62         ev_set_cb((ev), cb_); \
63 }
64
65 #define _ev_timer_init(ev, cb, after, _repeat) { \
66         ev_watcher_time *evwt = (ev_watcher_time *)(void *)ev; \
67 \
68         _ev_init(ev, cb); \
69         evwt->at = after; \
70         (ev)->repeat = _repeat; \
71 }
72
73 /* singlethreaded version --> no locks */
74
75 void test_server_lock(int care)
76 {
77 }
78 void test_server_unlock(int care)
79 {
80 }
81
82 /*
83  * This demo server shows how to use libwebsockets for one or more
84  * websocket protocols in the same server
85  *
86  * It defines the following websocket protocols:
87  *
88  *  dumb-increment-protocol:  once the socket is opened, an incrementing
89  *                              ascii string is sent down it every 50ms.
90  *                              If you send "reset\n" on the websocket, then
91  *                              the incrementing number is reset to 0.
92  *
93  *  lws-mirror-protocol: copies any received packet to every connection also
94  *                              using this protocol, including the sender
95  */
96
97 enum demo_protocols {
98         /* always first */
99         PROTOCOL_HTTP = 0,
100
101         PROTOCOL_DUMB_INCREMENT,
102         PROTOCOL_LWS_MIRROR,
103
104         /* always last */
105         DEMO_PROTOCOL_COUNT
106 };
107
108 /* list of supported protocols and callbacks */
109
110 static struct lws_protocols protocols[] = {
111         /* first protocol must always be HTTP handler */
112
113         {
114                 "http-only",            /* name */
115                 callback_http,          /* callback */
116                 sizeof (struct per_session_data__http), /* per_session_data_size */
117                 0,                      /* max frame size / rx buffer */
118         },
119         {
120                 "dumb-increment-protocol",
121                 callback_dumb_increment,
122                 sizeof(struct per_session_data__dumb_increment),
123                 10,
124         },
125         {
126                 "lws-mirror-protocol",
127                 callback_lws_mirror,
128                 sizeof(struct per_session_data__lws_mirror),
129                 128,
130         },
131         { NULL, NULL, 0, 0 } /* terminator */
132 };
133
134 static const struct lws_extension exts[] = {
135         {
136                 "permessage-deflate",
137                 lws_extension_callback_pm_deflate,
138                 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
139         },
140         {
141                 "deflate-frame",
142                 lws_extension_callback_pm_deflate,
143                 "deflate_frame"
144         },
145         { NULL, NULL, NULL /* terminator */ }
146 };
147
148 /* this shows how to override the lws file operations.  You don't need
149  * to do any of this unless you have a reason (eg, want to serve
150  * compressed files without decompressing the whole archive)
151  */
152 static lws_filefd_type
153 test_server_fops_open(struct lws *wsi, const char *filename,
154                       unsigned long *filelen, int flags)
155 {
156         lws_filefd_type n;
157
158         /* call through to original platform implementation */
159         n = fops_plat.open(wsi, filename, filelen, flags);
160
161         lwsl_notice("%s: opening %s, ret %ld, len %lu\n", __func__, filename,
162                         (long)n, *filelen);
163
164         return n;
165 }
166
167 void signal_cb(struct ev_loop *loop, struct ev_signal* watcher, int revents)
168 {
169         lwsl_notice("Signal caught, exiting...\n");
170         force_exit = 1;
171         switch (watcher->signum) {
172         case SIGTERM:
173         case SIGINT:
174                 ev_break(loop, EVBREAK_ALL);
175                 break;
176         default:
177                 signal(SIGABRT, SIG_DFL);
178                 abort();
179                 break;
180         }
181 }
182
183 static void
184 ev_timeout_cb (EV_P_ ev_timer *w, int revents)
185 {
186         lws_callback_on_writable_all_protocol(context,
187                                         &protocols[PROTOCOL_DUMB_INCREMENT]);
188 }
189
190 static struct option options[] = {
191         { "help",       no_argument,            NULL, 'h' },
192         { "debug",      required_argument,      NULL, 'd' },
193         { "port",       required_argument,      NULL, 'p' },
194         { "ssl",        no_argument,            NULL, 's' },
195         { "allow-non-ssl",      no_argument,    NULL, 'a' },
196         { "interface",  required_argument,      NULL, 'i' },
197         { "closetest",  no_argument,            NULL, 'c' },
198         { "libev",  no_argument,                NULL, 'e' },
199 #ifndef LWS_NO_DAEMONIZE
200         { "daemonize",  no_argument,            NULL, 'D' },
201 #endif
202         { "resource_path", required_argument,   NULL, 'r' },
203         { NULL, 0, 0, 0 }
204 };
205
206 int main(int argc, char **argv)
207 {
208         int sigs[] = { SIGINT, SIGKILL, SIGTERM, SIGSEGV, SIGFPE };
209         struct ev_signal signals[ARRAY_SIZE(sigs)];
210         struct ev_loop *loop = ev_default_loop(0);
211         struct lws_context_creation_info info;
212         char interface_name[128] = "";
213         const char *iface = NULL;
214         ev_timer timeout_watcher;
215         char cert_path[1024];
216         char key_path[1024];
217         int use_ssl = 0;
218         int opts = 0;
219         int n = 0;
220 #ifndef _WIN32
221         int syslog_options = LOG_PID | LOG_PERROR;
222 #endif
223 #ifndef LWS_NO_DAEMONIZE
224         int daemonize = 0;
225 #endif
226
227         /*
228          * take care to zero down the info struct, he contains random garbaage
229          * from the stack otherwise
230          */
231         memset(&info, 0, sizeof info);
232         info.port = 7681;
233
234         while (n >= 0) {
235                 n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL);
236                 if (n < 0)
237                         continue;
238                 switch (n) {
239                 case 'e':
240                         opts |= LWS_SERVER_OPTION_LIBEV;
241                         break;
242 #ifndef LWS_NO_DAEMONIZE
243                 case 'D':
244                         daemonize = 1;
245                         #ifndef _WIN32
246                         syslog_options &= ~LOG_PERROR;
247                         #endif
248                         break;
249 #endif
250                 case 'd':
251                         debug_level = atoi(optarg);
252                         break;
253                 case 's':
254                         use_ssl = 1;
255                         break;
256                 case 'a':
257                         opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
258                         break;
259                 case 'p':
260                         info.port = atoi(optarg);
261                         break;
262                 case 'i':
263                         strncpy(interface_name, optarg, sizeof interface_name);
264                         interface_name[(sizeof interface_name) - 1] = '\0';
265                         iface = interface_name;
266                         break;
267                 case 'c':
268                         close_testing = 1;
269                         fprintf(stderr, " Close testing mode -- closes on "
270                                            "client after 50 dumb increments"
271                                            "and suppresses lws_mirror spam\n");
272                         break;
273                 case 'r':
274                         resource_path = optarg;
275                         printf("Setting resource path to \"%s\"\n", resource_path);
276                         break;
277                 case 'h':
278                         fprintf(stderr, "Usage: test-server "
279                                         "[--port=<p>] [--ssl] "
280                                         "[-d <log bitfield>] "
281                                         "[--resource_path <path>]\n");
282                         exit(1);
283                 }
284         }
285
286 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
287         /*
288          * normally lock path would be /var/lock/lwsts or similar, to
289          * simplify getting started without having to take care about
290          * permissions or running as root, set to /tmp/.lwsts-lock
291          */
292         if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
293                 fprintf(stderr, "Failed to daemonize\n");
294                 return 1;
295         }
296 #endif
297
298         for (n = 0; n < ARRAY_SIZE(sigs); n++) {
299                 _ev_init(&signals[n], signal_cb);
300                 ev_signal_set(&signals[n], sigs[n]);
301                 ev_signal_start(loop, &signals[n]);
302         }
303
304 #ifndef _WIN32
305         /* we will only try to log things according to our debug_level */
306         setlogmask(LOG_UPTO (LOG_DEBUG));
307         openlog("lwsts", syslog_options, LOG_DAEMON);
308 #endif
309
310         /* tell the library what debug level to emit and to send it to syslog */
311         lws_set_log_level(debug_level, lwsl_emit_syslog);
312
313         lwsl_notice("libwebsockets test server libev - license LGPL2.1+SLE\n");
314         lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
315
316         printf("Using resource path \"%s\"\n", resource_path);
317
318         info.iface = iface;
319         info.protocols = protocols;
320         info.extensions = exts;
321
322         info.ssl_cert_filepath = NULL;
323         info.ssl_private_key_filepath = NULL;
324
325         if (use_ssl) {
326                 if (strlen(resource_path) > sizeof(cert_path) - 32) {
327                         lwsl_err("resource path too long\n");
328                         return -1;
329                 }
330                 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
331                                                                 resource_path);
332                 if (strlen(resource_path) > sizeof(key_path) - 32) {
333                         lwsl_err("resource path too long\n");
334                         return -1;
335                 }
336                 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
337                                                                 resource_path);
338
339                 info.ssl_cert_filepath = cert_path;
340                 info.ssl_private_key_filepath = key_path;
341
342                 opts |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
343         }
344         info.gid = -1;
345         info.uid = -1;
346         info.max_http_header_pool = 1;
347         info.options = opts | LWS_SERVER_OPTION_LIBEV;
348
349         context = lws_create_context(&info);
350         if (context == NULL) {
351                 lwsl_err("libwebsocket init failed\n");
352                 return -1;
353         }
354
355         /*
356          * this shows how to override the lws file operations.  You don't need
357          * to do any of this unless you have a reason (eg, want to serve
358          * compressed files without decompressing the whole archive)
359          */
360         /* stash original platform fops */
361         fops_plat = *(lws_get_fops(context));
362         /* override the active fops */
363         lws_get_fops(context)->open = test_server_fops_open;
364
365         lws_ev_initloop(context, loop, 0);
366
367         _ev_timer_init(&timeout_watcher, ev_timeout_cb, 0.05, 0.05);
368         ev_timer_start(loop, &timeout_watcher);
369         ev_run(loop, 0);
370
371         lws_context_destroy(context);
372         lwsl_notice("libwebsockets-test-server exited cleanly\n");
373
374 #ifndef _WIN32
375         closelog();
376 #endif
377
378         return 0;
379 }