blob: 79737fcf05bfabcc0287c10ce417cccb47e1be90 [file] [log] [blame]
Andy Greena0da8a82010-11-08 17:12:19 +00001/*
2 * libwebsockets-test-server - libwebsockets test implementation
Andy Greene77ddd82010-11-13 10:03:47 +00003 *
Andy Greena0da8a82010-11-08 17:12:19 +00004 * Copyright (C) 2010 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
Andy Green775c0dd2010-10-29 14:15:22 +010022#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
Andy Greenea71ed12010-10-31 07:40:33 +000025#include <getopt.h>
Andy Green5fd8a5e2010-10-31 11:57:17 +000026#include <string.h>
27
Andy Green7310e9c2010-11-01 09:12:17 +000028#include "../lib/libwebsockets.h"
Andy Green775c0dd2010-10-29 14:15:22 +010029
Andy Greenfe2a0d22010-11-12 13:10:40 +000030/*
31 * This demo server shows how to use libwebsockets for one or more
32 * websocket protocols in the same server
33 *
34 * It defines the following websocket protocols:
35 *
36 * dumb-increment-protocol: once the socket is opened, an incrementing
37 * ascii string is sent down it every 50ms.
38 * If you send "reset\n" on the websocket, then
39 * the incrementing number is reset to 0.
Andy Green51959682010-11-12 14:12:13 +000040 *
41 * lws-mirror-protocol: copies any received packet to every connection also
42 * using this protocol, including the sender
Andy Greenfe2a0d22010-11-12 13:10:40 +000043 */
44
45
Andy Green3138e442010-11-01 09:20:48 +000046#define LOCAL_RESOURCE_PATH "/usr/share/libwebsockets-test-server"
Andy Greenea71ed12010-10-31 07:40:33 +000047static int port = 7681;
Andy Greene77ddd82010-11-13 10:03:47 +000048static int use_ssl;
Andy Green775c0dd2010-10-29 14:15:22 +010049
Andy Green4f3943a2010-11-12 10:44:16 +000050/* this protocol server (always the first one) just knows how to do HTTP */
Andy Green251f6fa2010-11-03 11:13:06 +000051
Andy Greene77ddd82010-11-13 10:03:47 +000052static int callback_http(struct libwebsocket *wsi,
53 enum libwebsocket_callback_reasons reason, void *user,
Andy Green251f6fa2010-11-03 11:13:06 +000054 void *in, size_t len)
Andy Green775c0dd2010-10-29 14:15:22 +010055{
Andy Green775c0dd2010-10-29 14:15:22 +010056 switch (reason) {
Andy Green5fd8a5e2010-10-31 11:57:17 +000057 case LWS_CALLBACK_HTTP:
Andy Greena2b0ab02010-11-11 12:28:29 +000058 fprintf(stderr, "serving HTTP URI %s\n", in);
Andy Greene77ddd82010-11-13 10:03:47 +000059
Andy Greena2b0ab02010-11-11 12:28:29 +000060 if (in && strcmp(in, "/favicon.ico") == 0) {
Andy Green3138e442010-11-01 09:20:48 +000061 if (libwebsockets_serve_http_file(wsi,
62 LOCAL_RESOURCE_PATH"/favicon.ico", "image/x-icon"))
Andy Greenab990e42010-10-31 12:42:52 +000063 fprintf(stderr, "Failed to send favicon\n");
Andy Green5fd8a5e2010-10-31 11:57:17 +000064 break;
65 }
Andy Greene77ddd82010-11-13 10:03:47 +000066
Andy Green5fd8a5e2010-10-31 11:57:17 +000067 /* send the script... when it runs it'll start websockets */
68
Andy Green3138e442010-11-01 09:20:48 +000069 if (libwebsockets_serve_http_file(wsi,
70 LOCAL_RESOURCE_PATH"/test.html", "text/html"))
Andy Green5fd8a5e2010-10-31 11:57:17 +000071 fprintf(stderr, "Failed to send HTTP file\n");
Andy Green5fd8a5e2010-10-31 11:57:17 +000072 break;
Andy Greena2b0ab02010-11-11 12:28:29 +000073
Andy Green4f3943a2010-11-12 10:44:16 +000074 default:
75 break;
Andy Green775c0dd2010-10-29 14:15:22 +010076 }
Andy Greenab990e42010-10-31 12:42:52 +000077
Andy Green775c0dd2010-10-29 14:15:22 +010078 return 0;
79}
80
Andy Green4f3943a2010-11-12 10:44:16 +000081/* dumb_increment protocol */
82
83struct per_session_data__dumb_increment {
84 int number;
85};
86
87static int
Andy Greene77ddd82010-11-13 10:03:47 +000088callback_dumb_increment(struct libwebsocket *wsi,
Andy Green4f3943a2010-11-12 10:44:16 +000089 enum libwebsocket_callback_reasons reason,
Andy Greene77ddd82010-11-13 10:03:47 +000090 void *user, void *in, size_t len)
Andy Green4f3943a2010-11-12 10:44:16 +000091{
92 int n;
93 char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
94 LWS_SEND_BUFFER_POST_PADDING];
95 unsigned char *p = (unsigned char *)&buf[LWS_SEND_BUFFER_PRE_PADDING];
Andy Greene77ddd82010-11-13 10:03:47 +000096 struct per_session_data__dumb_increment *pss = user;
97
Andy Green4f3943a2010-11-12 10:44:16 +000098 switch (reason) {
99
100 case LWS_CALLBACK_ESTABLISHED:
101 pss->number = 0;
102 break;
103
Andy Greene77ddd82010-11-13 10:03:47 +0000104 case LWS_CALLBACK_SEND:
Andy Green4f3943a2010-11-12 10:44:16 +0000105 n = sprintf(p, "%d", pss->number++);
106 n = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
107 if (n < 0) {
108 fprintf(stderr, "ERROR writing to socket");
109 return 1;
110 }
111 break;
112
113 case LWS_CALLBACK_RECEIVE:
Andy Greenfe2a0d22010-11-12 13:10:40 +0000114 fprintf(stderr, "rx %d\n", len);
Andy Green4f3943a2010-11-12 10:44:16 +0000115 if (len < 6)
116 break;
117 if (strcmp(in, "reset\n") == 0)
118 pss->number = 0;
119 break;
120
121 default:
122 break;
123 }
124
125 return 0;
126}
127
128
Andy Greenfe2a0d22010-11-12 13:10:40 +0000129/* lws-mirror_protocol */
130
131#define MAX_MESSAGE_QUEUE 64
Andy Greenfe2a0d22010-11-12 13:10:40 +0000132
133struct per_session_data__lws_mirror {
Andy Greene77ddd82010-11-13 10:03:47 +0000134 struct libwebsocket *wsi;
Andy Greenfe2a0d22010-11-12 13:10:40 +0000135 int ringbuffer_tail;
136};
137
138struct a_message {
Andy Greene77ddd82010-11-13 10:03:47 +0000139 void *payload;
Andy Greenfe2a0d22010-11-12 13:10:40 +0000140 size_t len;
141};
142
143static struct a_message ringbuffer[MAX_MESSAGE_QUEUE];
144static int ringbuffer_head;
145
146
Andy Greenfe2a0d22010-11-12 13:10:40 +0000147static int
Andy Greene77ddd82010-11-13 10:03:47 +0000148callback_lws_mirror(struct libwebsocket *wsi,
Andy Greenfe2a0d22010-11-12 13:10:40 +0000149 enum libwebsocket_callback_reasons reason,
Andy Greene77ddd82010-11-13 10:03:47 +0000150 void *user, void *in, size_t len)
Andy Greenfe2a0d22010-11-12 13:10:40 +0000151{
152 int n;
153 char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
154 LWS_SEND_BUFFER_POST_PADDING];
155 unsigned char *p = (unsigned char *)&buf[LWS_SEND_BUFFER_PRE_PADDING];
Andy Greene77ddd82010-11-13 10:03:47 +0000156 struct per_session_data__lws_mirror *pss = user;
157
Andy Greenfe2a0d22010-11-12 13:10:40 +0000158 switch (reason) {
159
160 case LWS_CALLBACK_ESTABLISHED:
161 pss->wsi = wsi;
162 pss->ringbuffer_tail = ringbuffer_head;
163 break;
164
Andy Greene77ddd82010-11-13 10:03:47 +0000165 case LWS_CALLBACK_SEND:
Andy Greenfe2a0d22010-11-12 13:10:40 +0000166 /* send everything that's pending */
167 while (pss->ringbuffer_tail != ringbuffer_head) {
168
Andy Greene77ddd82010-11-13 10:03:47 +0000169 n = libwebsocket_write(wsi, (unsigned char *)
170 ringbuffer[pss->ringbuffer_tail].payload +
171 LWS_SEND_BUFFER_PRE_PADDING,
Andy Greenfe2a0d22010-11-12 13:10:40 +0000172 ringbuffer[pss->ringbuffer_tail].len,
Andy Greene77ddd82010-11-13 10:03:47 +0000173 LWS_WRITE_TEXT);
Andy Greenfe2a0d22010-11-12 13:10:40 +0000174 if (n < 0) {
175 fprintf(stderr, "ERROR writing to socket");
176 exit(1);
177 }
178
179 if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1))
180 pss->ringbuffer_tail = 0;
181 else
182 pss->ringbuffer_tail++;
183 }
184 break;
185
186 case LWS_CALLBACK_RECEIVE:
Andy Green51959682010-11-12 14:12:13 +0000187 if (ringbuffer[ringbuffer_head].payload)
Andy Greene77ddd82010-11-13 10:03:47 +0000188 free(ringbuffer[ringbuffer_head].payload);
Andy Green51959682010-11-12 14:12:13 +0000189
Andy Greenfe2a0d22010-11-12 13:10:40 +0000190 ringbuffer[ringbuffer_head].payload =
191 malloc(LWS_SEND_BUFFER_PRE_PADDING + len +
192 LWS_SEND_BUFFER_POST_PADDING);
193 ringbuffer[ringbuffer_head].len = len;
Andy Greenfe2a0d22010-11-12 13:10:40 +0000194 memcpy(ringbuffer[ringbuffer_head].payload +
195 LWS_SEND_BUFFER_PRE_PADDING, in, len);
196 if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
197 ringbuffer_head = 0;
198 else
199 ringbuffer_head++;
200 break;
201
202 default:
203 break;
204 }
205
206 return 0;
207}
208
209
Andy Green4f3943a2010-11-12 10:44:16 +0000210/* list of supported protocols and callbacks */
211
212static const struct libwebsocket_protocols protocols[] = {
213 {
214 .name = "http-only",
215 .callback = callback_http,
216 .per_session_data_size = 0,
217 },
218 {
219 .name = "dumb-increment-protocol",
220 .callback = callback_dumb_increment,
221 .per_session_data_size =
222 sizeof(struct per_session_data__dumb_increment),
223 },
Andy Greenfe2a0d22010-11-12 13:10:40 +0000224 {
225 .name = "lws-mirror-protocol",
226 .callback = callback_lws_mirror,
227 .per_session_data_size =
228 sizeof(struct per_session_data__lws_mirror),
229 },
Andy Green4f3943a2010-11-12 10:44:16 +0000230 { /* end of list */
231 .callback = NULL
232 }
233};
234
Andy Greenea71ed12010-10-31 07:40:33 +0000235static struct option options[] = {
236 { "help", no_argument, NULL, 'h' },
237 { "port", required_argument, NULL, 'p' },
Andy Green3faa9c72010-11-08 17:03:03 +0000238 { "ssl", no_argument, NULL, 's' },
Andy Greenea71ed12010-10-31 07:40:33 +0000239 { NULL, 0, 0, 0 }
240};
Andy Green775c0dd2010-10-29 14:15:22 +0100241
Andy Greenea71ed12010-10-31 07:40:33 +0000242int main(int argc, char **argv)
Andy Green775c0dd2010-10-29 14:15:22 +0100243{
Andy Greenea71ed12010-10-31 07:40:33 +0000244 int n = 0;
Andy Greene77ddd82010-11-13 10:03:47 +0000245 const char *cert_path =
Andy Green7c212cc2010-11-08 20:20:42 +0000246 LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
Andy Greene77ddd82010-11-13 10:03:47 +0000247 const char *key_path =
Andy Green7c212cc2010-11-08 20:20:42 +0000248 LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
Andy Greenea71ed12010-10-31 07:40:33 +0000249
Andy Greenab990e42010-10-31 12:42:52 +0000250 fprintf(stderr, "libwebsockets test server\n"
Andy Green6452f1e2010-11-11 09:22:22 +0000251 "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
252 "licensed under LGPL2.1\n");
Andy Greene77ddd82010-11-13 10:03:47 +0000253
Andy Greenea71ed12010-10-31 07:40:33 +0000254 while (n >= 0) {
Andy Green4f3943a2010-11-12 10:44:16 +0000255 n = getopt_long(argc, argv, "hp:", options, NULL);
Andy Greenea71ed12010-10-31 07:40:33 +0000256 if (n < 0)
257 continue;
258 switch (n) {
Andy Green3faa9c72010-11-08 17:03:03 +0000259 case 's':
260 use_ssl = 1;
261 break;
Andy Greenea71ed12010-10-31 07:40:33 +0000262 case 'p':
263 port = atoi(optarg);
264 break;
Andy Greenea71ed12010-10-31 07:40:33 +0000265 case 'h':
Andy Greenab990e42010-10-31 12:42:52 +0000266 fprintf(stderr, "Usage: test-server "
Andy Green4f3943a2010-11-12 10:44:16 +0000267 "[--port=<p>] [--ssl]\n");
Andy Greenea71ed12010-10-31 07:40:33 +0000268 exit(1);
269 }
Andy Greenea71ed12010-10-31 07:40:33 +0000270 }
Andy Green3faa9c72010-11-08 17:03:03 +0000271
272 if (!use_ssl)
273 cert_path = key_path = NULL;
Andy Greene77ddd82010-11-13 10:03:47 +0000274
Andy Green4f3943a2010-11-12 10:44:16 +0000275 if (libwebsocket_create_server(port, protocols,
276 cert_path, key_path, -1, -1) < 0) {
Andy Green775c0dd2010-10-29 14:15:22 +0100277 fprintf(stderr, "libwebsocket init failed\n");
278 return -1;
279 }
Andy Green775c0dd2010-10-29 14:15:22 +0100280
281 return 0;
282}