test client have u option also reject deflate stream
[platform/upstream/libwebsockets.git] / test-server / test-client.c
1 /*
2  * libwebsockets-test-client - libwebsockets test implementation
3  *
4  * Copyright (C) 2011 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 <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <string.h>
27 #include <signal.h>
28
29 #ifdef CMAKE_BUILD
30 #include "lws_config.h"
31 #endif
32
33 #include "../lib/libwebsockets.h"
34
35 static unsigned int opts;
36 static int was_closed;
37 static int deny_deflate;
38 static int deny_mux;
39 static struct libwebsocket *wsi_mirror;
40 static int mirror_lifetime = 0;
41 static int force_exit = 0;
42
43 /*
44  * This demo shows how to connect multiple websockets simultaneously to a
45  * websocket server (there is no restriction on their having to be the same
46  * server just it simplifies the demo).
47  *
48  *  dumb-increment-protocol:  we connect to the server and print the number
49  *                              we are given
50  *
51  *  lws-mirror-protocol: draws random circles, which are mirrored on to every
52  *                              client (see them being drawn in every browser
53  *                              session also using the test server)
54  */
55
56 enum demo_protocols {
57
58         PROTOCOL_DUMB_INCREMENT,
59         PROTOCOL_LWS_MIRROR,
60
61         /* always last */
62         DEMO_PROTOCOL_COUNT
63 };
64
65
66 /* dumb_increment protocol */
67
68 static int
69 callback_dumb_increment(struct libwebsocket_context *this,
70                         struct libwebsocket *wsi,
71                         enum libwebsocket_callback_reasons reason,
72                                                void *user, void *in, size_t len)
73 {
74         switch (reason) {
75
76         case LWS_CALLBACK_CLOSED:
77                 fprintf(stderr, "LWS_CALLBACK_CLOSED\n");
78                 was_closed = 1;
79                 break;
80
81         case LWS_CALLBACK_CLIENT_RECEIVE:
82                 ((char *)in)[len] = '\0';
83                 fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
84                 break;
85
86         /* because we are protocols[0] ... */
87
88         case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
89                 if ((strcmp(in, "deflate-stream") == 0) && deny_deflate) {
90                         fprintf(stderr, "denied deflate-stream extension\n");
91                         return 1;
92                 }
93                 if ((strcmp(in, "deflate-frame") == 0) && deny_deflate) {
94                         fprintf(stderr, "denied deflate-frame extension\n");
95                         return 1;
96                 }
97                 if ((strcmp(in, "x-google-mux") == 0) && deny_mux) {
98                         fprintf(stderr, "denied x-google-mux extension\n");
99                         return 1;
100                 }
101
102                 break;
103
104         default:
105                 break;
106         }
107
108         return 0;
109 }
110
111
112 /* lws-mirror_protocol */
113
114
115 static int
116 callback_lws_mirror(struct libwebsocket_context *context,
117                         struct libwebsocket *wsi,
118                         enum libwebsocket_callback_reasons reason,
119                                                void *user, void *in, size_t len)
120 {
121         unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
122                                                   LWS_SEND_BUFFER_POST_PADDING];
123         int l = 0;
124         int n;
125
126         switch (reason) {
127
128         case LWS_CALLBACK_CLOSED:
129                 fprintf(stderr, "mirror: LWS_CALLBACK_CLOSED mirror_lifetime=%d\n", mirror_lifetime);
130                 wsi_mirror = NULL;
131                 break;
132
133         case LWS_CALLBACK_CLIENT_ESTABLISHED:
134
135                 /*
136                  * start the ball rolling,
137                  * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
138                  */
139
140                 libwebsocket_callback_on_writable(context, wsi);
141                 break;
142
143         case LWS_CALLBACK_CLIENT_RECEIVE:
144 /*              fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
145                 break;
146
147         case LWS_CALLBACK_CLIENT_WRITEABLE:
148
149                 for (n = 0; n < 1; n++)
150                         l += sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING + l],
151                                         "c #%06X %d %d %d;",
152                                         (int)random() & 0xffffff,
153                                         (int)random() % 500,
154                                         (int)random() % 250,
155                                         (int)random() % 24);
156
157                 n = libwebsocket_write(wsi,
158                    &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT);
159
160                 if (n < 0)
161                         return -1;
162                 if (n < l) {
163                         lwsl_err("Partial write LWS_CALLBACK_CLIENT_WRITEABLE\n");
164                         return -1;
165                 }
166
167                 mirror_lifetime--;
168                 if (!mirror_lifetime) {
169                         fprintf(stderr, "closing mirror session\n");
170                         return -1;
171                 } else
172                         /* get notified as soon as we can write again */
173                         libwebsocket_callback_on_writable(context, wsi);
174                 break;
175
176         default:
177                 break;
178         }
179
180         return 0;
181 }
182
183
184 /* list of supported protocols and callbacks */
185
186 static struct libwebsocket_protocols protocols[] = {
187         {
188                 "dumb-increment-protocol",
189                 callback_dumb_increment,
190                 0,
191                 20,
192         },
193         {
194                 "lws-mirror-protocol",
195                 callback_lws_mirror,
196                 0,
197                 128,
198         },
199         { NULL, NULL, 0, 0 } /* end */
200 };
201
202 void sighandler(int sig)
203 {
204         force_exit = 1;
205 }
206
207 static struct option options[] = {
208         { "help",       no_argument,            NULL, 'h' },
209         { "debug",      required_argument,      NULL, 'd' },
210         { "port",       required_argument,      NULL, 'p' },
211         { "ssl",        no_argument,            NULL, 's' },
212         { "version",    required_argument,      NULL, 'v' },
213         { "undeflated", no_argument,            NULL, 'u' },
214         { "nomux",      no_argument,            NULL, 'n' },
215         { "longlived",  no_argument,            NULL, 'l' },
216         { NULL, 0, 0, 0 }
217 };
218
219
220 int main(int argc, char **argv)
221 {
222         int n = 0;
223         int ret = 0;
224         int port = 7681;
225         int use_ssl = 0;
226         struct libwebsocket_context *context;
227         const char *address;
228         struct libwebsocket *wsi_dumb;
229         int ietf_version = -1; /* latest */
230         int longlived = 0;
231         struct lws_context_creation_info info;
232
233         memset(&info, 0, sizeof info);
234
235         fprintf(stderr, "libwebsockets test client\n"
236                         "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> "
237                                                     "licensed under LGPL2.1\n");
238
239         if (argc < 2)
240                 goto usage;
241
242         while (n >= 0) {
243                 n = getopt_long(argc, argv, "nuv:hsp:d:l", options, NULL);
244                 if (n < 0)
245                         continue;
246                 switch (n) {
247                 case 'd':
248                         lws_set_log_level(atoi(optarg), NULL);
249                         break;
250                 case 's':
251                         use_ssl = 2; /* 2 = allow selfsigned */
252                         break;
253                 case 'p':
254                         port = atoi(optarg);
255                         break;
256                 case 'l':
257                         longlived = 1;
258                         break;
259                 case 'v':
260                         ietf_version = atoi(optarg);
261                         break;
262                 case 'u':
263                         deny_deflate = 1;
264                         break;
265                 case 'n':
266                         deny_mux = 1;
267                         break;
268                 case 'h':
269                         goto usage;
270                 }
271         }
272
273         if (optind >= argc)
274                 goto usage;
275
276         signal(SIGINT, sighandler);
277
278         address = argv[optind];
279
280         /*
281          * create the websockets context.  This tracks open connections and
282          * knows how to route any traffic and which protocol version to use,
283          * and if each connection is client or server side.
284          *
285          * For this client-only demo, we tell it to not listen on any port.
286          */
287
288         info.port = CONTEXT_PORT_NO_LISTEN;
289         info.protocols = protocols;
290 #ifndef LWS_NO_EXTENSIONS
291         info.extensions = libwebsocket_get_internal_extensions();
292 #endif
293         info.gid = -1;
294         info.uid = -1;
295
296         context = libwebsocket_create_context(&info);
297         if (context == NULL) {
298                 fprintf(stderr, "Creating libwebsocket context failed\n");
299                 return 1;
300         }
301
302         /* create a client websocket using dumb increment protocol */
303
304         wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
305                         "/", argv[optind], argv[optind],
306                          protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version);
307
308         if (wsi_dumb == NULL) {
309                 fprintf(stderr, "libwebsocket dumb connect failed\n");
310                 ret = 1;
311                 goto bail;
312         }
313
314         fprintf(stderr, "Websocket connections opened\n");
315
316         /*
317          * sit there servicing the websocket context to handle incoming
318          * packets, and drawing random circles on the mirror protocol websocket
319          */
320
321         n = 0;
322         while (n >= 0 && !was_closed && !force_exit) {
323                 n = libwebsocket_service(context, 10);
324
325                 if (n < 0)
326                         continue;
327
328                 if (wsi_mirror)
329                         continue;
330
331                 /* create a client websocket using mirror protocol */
332
333                 wsi_mirror = libwebsocket_client_connect(context,
334                         address, port, use_ssl,  "/",
335                         argv[optind], argv[optind],
336                         protocols[PROTOCOL_LWS_MIRROR].name, ietf_version);
337
338                 if (wsi_mirror == NULL) {
339                         fprintf(stderr, "libwebsocket "
340                                               "dumb connect failed\n");
341                         ret = 1;
342                         goto bail;
343                 }
344
345                 mirror_lifetime = 10 + (random() & 1023);
346                 /* useful to test single connection stability */
347                 if (longlived)
348                         mirror_lifetime += 50000;
349
350                 fprintf(stderr, "opened mirror connection with "
351                                      "%d lifetime\n", mirror_lifetime);
352
353                 /*
354                  * mirror_lifetime is decremented each send, when it reaches
355                  * zero the connection is closed in the send callback.
356                  * When the close callback comes, wsi_mirror is set to NULL
357                  * so a new connection will be opened
358                  */
359         }
360
361 bail:
362         fprintf(stderr, "Exiting\n");
363
364         libwebsocket_context_destroy(context);
365
366         return ret;
367
368 usage:
369         fprintf(stderr, "Usage: libwebsockets-test-client "
370                                 "<server address> [--port=<p>] "
371                                 "[--ssl] [-k] [-v <ver>] "
372                                 "[-d <log bitfield>] [-l]\n");
373         return 1;
374 }