test server libuv support status protocol
[platform/upstream/libwebsockets.git] / test-server / test-server-libuv.c
1 /*
2  * libwebsockets-test-server for libev - libwebsockets test implementation
3  *
4  * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #include "test-server.h"
23
24 int close_testing;
25 int max_poll_elements;
26 int debug_level = 7;
27 struct lws_context *context;
28 struct lws_plat_file_ops fops_plat;
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 /* singlethreaded version --> no locks */
35
36 void test_server_lock(int care)
37 {
38 }
39 void test_server_unlock(int care)
40 {
41 }
42
43 /*
44  * This demo server shows how to use libwebsockets for one or more
45  * websocket protocols in the same server
46  *
47  * It defines the following websocket protocols:
48  *
49  *  dumb-increment-protocol:  once the socket is opened, an incrementing
50  *                              ascii string is sent down it every 50ms.
51  *                              If you send "reset\n" on the websocket, then
52  *                              the incrementing number is reset to 0.
53  *
54  *  lws-mirror-protocol: copies any received packet to every connection also
55  *                              using this protocol, including the sender
56  */
57
58 enum demo_protocols {
59         /* always first */
60         PROTOCOL_HTTP = 0,
61
62         PROTOCOL_DUMB_INCREMENT,
63         PROTOCOL_LWS_MIRROR,
64
65         /* always last */
66         DEMO_PROTOCOL_COUNT
67 };
68
69 /* list of supported protocols and callbacks */
70
71 static struct lws_protocols protocols[] = {
72         /* first protocol must always be HTTP handler */
73
74         {
75                 "http-only",            /* name */
76                 callback_http,          /* callback */
77                 sizeof (struct per_session_data__http), /* per_session_data_size */
78                 0,                      /* max frame size / rx buffer */
79         },
80         {
81                 "dumb-increment-protocol",
82                 callback_dumb_increment,
83                 sizeof(struct per_session_data__dumb_increment),
84                 10,
85         },
86         {
87                 "lws-mirror-protocol",
88                 callback_lws_mirror,
89                 sizeof(struct per_session_data__lws_mirror),
90                 128,
91         },
92         {
93                 "lws-status",
94                 callback_lws_status,
95                 sizeof(struct per_session_data__lws_status),
96                 128,
97         },
98         { NULL, NULL, 0, 0 } /* terminator */
99 };
100
101 static const struct lws_extension exts[] = {
102         {
103                 "permessage-deflate",
104                 lws_extension_callback_pm_deflate,
105                 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
106         },
107         {
108                 "deflate-frame",
109                 lws_extension_callback_pm_deflate,
110                 "deflate_frame"
111         },
112         { NULL, NULL, NULL /* terminator */ }
113 };
114
115 void signal_cb(uv_signal_t *watcher, int revents)
116 {
117         lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
118         switch (watcher->signum) {
119         case SIGTERM:
120         case SIGINT:
121                 break;
122         default:
123                 signal(SIGABRT, SIG_DFL);
124                 abort();
125                 break;
126         }
127         lws_libuv_stop(context);
128 }
129
130 static void
131 uv_timeout_cb_dumb_increment(uv_timer_t *w)
132 {
133         lws_callback_on_writable_all_protocol(context,
134                                         &protocols[PROTOCOL_DUMB_INCREMENT]);
135 }
136
137 static struct option options[] = {
138         { "help",       no_argument,            NULL, 'h' },
139         { "debug",      required_argument,      NULL, 'd' },
140         { "port",       required_argument,      NULL, 'p' },
141         { "ssl",        no_argument,            NULL, 's' },
142         { "allow-non-ssl",      no_argument,    NULL, 'a' },
143         { "interface",  required_argument,      NULL, 'i' },
144         { "closetest",  no_argument,            NULL, 'c' },
145         { "libev",  no_argument,                NULL, 'e' },
146 #ifndef LWS_NO_DAEMONIZE
147         { "daemonize",  no_argument,            NULL, 'D' },
148 #endif
149         { "resource_path", required_argument,   NULL, 'r' },
150         { NULL, 0, 0, 0 }
151 };
152
153 int main(int argc, char **argv)
154 {
155         struct lws_context_creation_info info;
156         char interface_name[128] = "";
157         uv_timer_t timeout_watcher;
158         const char *iface = NULL;
159         char cert_path[1024];
160         char key_path[1024];
161         int use_ssl = 0;
162         int opts = 0;
163         int n = 0;
164 #ifndef _WIN32
165         int syslog_options = LOG_PID | LOG_PERROR;
166 #endif
167 #ifndef LWS_NO_DAEMONIZE
168         int daemonize = 0;
169 #endif
170
171         /*
172          * take care to zero down the info struct, he contains random garbaage
173          * from the stack otherwise
174          */
175         memset(&info, 0, sizeof info);
176         info.port = 7681;
177
178         while (n >= 0) {
179                 n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL);
180                 if (n < 0)
181                         continue;
182                 switch (n) {
183                 case 'e':
184                         opts |= LWS_SERVER_OPTION_LIBEV;
185                         break;
186 #ifndef LWS_NO_DAEMONIZE
187                 case 'D':
188                         daemonize = 1;
189                         #ifndef _WIN32
190                         syslog_options &= ~LOG_PERROR;
191                         #endif
192                         break;
193 #endif
194                 case 'd':
195                         debug_level = atoi(optarg);
196                         break;
197                 case 's':
198                         use_ssl = 1;
199                         break;
200                 case 'a':
201                         opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
202                         break;
203                 case 'p':
204                         info.port = atoi(optarg);
205                         break;
206                 case 'i':
207                         strncpy(interface_name, optarg, sizeof interface_name);
208                         interface_name[(sizeof interface_name) - 1] = '\0';
209                         iface = interface_name;
210                         break;
211                 case 'c':
212                         close_testing = 1;
213                         fprintf(stderr, " Close testing mode -- closes on "
214                                            "client after 50 dumb increments"
215                                            "and suppresses lws_mirror spam\n");
216                         break;
217                 case 'r':
218                         resource_path = optarg;
219                         printf("Setting resource path to \"%s\"\n", resource_path);
220                         break;
221                 case 'h':
222                         fprintf(stderr, "Usage: test-server "
223                                         "[--port=<p>] [--ssl] "
224                                         "[-d <log bitfield>] "
225                                         "[--resource_path <path>]\n");
226                         exit(1);
227                 }
228         }
229
230 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
231         /*
232          * normally lock path would be /var/lock/lwsts or similar, to
233          * simplify getting started without having to take care about
234          * permissions or running as root, set to /tmp/.lwsts-lock
235          */
236         if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
237                 fprintf(stderr, "Failed to daemonize\n");
238                 return 1;
239         }
240 #endif
241
242         /* we will only try to log things according to our debug_level */
243         setlogmask(LOG_UPTO (LOG_DEBUG));
244         openlog("lwsts", syslog_options, LOG_DAEMON);
245
246         /* tell the library what debug level to emit and to send it to syslog */
247         lws_set_log_level(debug_level, lwsl_emit_syslog);
248
249         lwsl_notice("libwebsockets test server libuv - license LGPL2.1+SLE\n");
250         lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
251
252         lwsl_info("Using resource path \"%s\"\n", resource_path);
253
254         info.iface = iface;
255         info.protocols = protocols;
256         info.extensions = exts;
257
258         info.ssl_cert_filepath = NULL;
259         info.ssl_private_key_filepath = NULL;
260
261         if (use_ssl) {
262                 if (strlen(resource_path) > sizeof(cert_path) - 32) {
263                         lwsl_err("resource path too long\n");
264                         return -1;
265                 }
266                 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
267                         resource_path);
268                 if (strlen(resource_path) > sizeof(key_path) - 32) {
269                         lwsl_err("resource path too long\n");
270                         return -1;
271                 }
272                 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
273                         resource_path);
274
275                 info.ssl_cert_filepath = cert_path;
276                 info.ssl_private_key_filepath = key_path;
277         }
278         info.gid = -1;
279         info.uid = -1;
280         info.max_http_header_pool = 1;
281         info.timeout_secs = 5;
282         info.options = opts | LWS_SERVER_OPTION_LIBUV;
283
284         context = lws_create_context(&info);
285         if (context == NULL) {
286                 lwsl_err("libwebsocket init failed\n");
287                 return -1;
288         }
289
290         lws_uv_initloop(context, NULL, signal_cb, 0);
291
292         uv_timer_init(lws_uv_getloop(context, 0), &timeout_watcher);
293         uv_timer_start(&timeout_watcher, uv_timeout_cb_dumb_increment, 50, 50);
294
295         lws_libuv_run(context, 0);
296
297         lws_context_destroy(context);
298         lwsl_notice("libwebsockets-test-server exited cleanly\n");
299
300         return 0;
301 }