Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets-test-server - libwebsockets test implementation |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 3 | * |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 4 | * 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 Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <unistd.h> |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 25 | #include <getopt.h> |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 26 | #include <string.h> |
| 27 | |
Andy Green | 7310e9c | 2010-11-01 09:12:17 +0000 | [diff] [blame] | 28 | #include "../lib/libwebsockets.h" |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 29 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 30 | /* |
| 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 Green | 5195968 | 2010-11-12 14:12:13 +0000 | [diff] [blame] | 40 | * |
| 41 | * lws-mirror-protocol: copies any received packet to every connection also |
| 42 | * using this protocol, including the sender |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 43 | */ |
| 44 | |
| 45 | |
Andy Green | 3138e44 | 2010-11-01 09:20:48 +0000 | [diff] [blame] | 46 | #define LOCAL_RESOURCE_PATH "/usr/share/libwebsockets-test-server" |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 47 | static int port = 7681; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 48 | static int use_ssl; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 49 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 50 | /* this protocol server (always the first one) just knows how to do HTTP */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 51 | |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 52 | static int callback_http(struct libwebsocket *wsi, |
| 53 | enum libwebsocket_callback_reasons reason, void *user, |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 54 | void *in, size_t len) |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 55 | { |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 56 | switch (reason) { |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 57 | case LWS_CALLBACK_HTTP: |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 58 | fprintf(stderr, "serving HTTP URI %s\n", in); |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 59 | |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 60 | if (in && strcmp(in, "/favicon.ico") == 0) { |
Andy Green | 3138e44 | 2010-11-01 09:20:48 +0000 | [diff] [blame] | 61 | if (libwebsockets_serve_http_file(wsi, |
| 62 | LOCAL_RESOURCE_PATH"/favicon.ico", "image/x-icon")) |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 63 | fprintf(stderr, "Failed to send favicon\n"); |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 64 | break; |
| 65 | } |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 66 | |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 67 | /* send the script... when it runs it'll start websockets */ |
| 68 | |
Andy Green | 3138e44 | 2010-11-01 09:20:48 +0000 | [diff] [blame] | 69 | if (libwebsockets_serve_http_file(wsi, |
| 70 | LOCAL_RESOURCE_PATH"/test.html", "text/html")) |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 71 | fprintf(stderr, "Failed to send HTTP file\n"); |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 72 | break; |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 73 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 74 | default: |
| 75 | break; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 76 | } |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 77 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 81 | /* dumb_increment protocol */ |
| 82 | |
| 83 | struct per_session_data__dumb_increment { |
| 84 | int number; |
| 85 | }; |
| 86 | |
| 87 | static int |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 88 | callback_dumb_increment(struct libwebsocket *wsi, |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 89 | enum libwebsocket_callback_reasons reason, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 90 | void *user, void *in, size_t len) |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 91 | { |
| 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 Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 96 | struct per_session_data__dumb_increment *pss = user; |
| 97 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 98 | switch (reason) { |
| 99 | |
| 100 | case LWS_CALLBACK_ESTABLISHED: |
| 101 | pss->number = 0; |
| 102 | break; |
| 103 | |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 104 | case LWS_CALLBACK_SEND: |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 105 | 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 Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 114 | fprintf(stderr, "rx %d\n", len); |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 115 | 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 Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 129 | /* lws-mirror_protocol */ |
| 130 | |
| 131 | #define MAX_MESSAGE_QUEUE 64 |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 132 | |
| 133 | struct per_session_data__lws_mirror { |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 134 | struct libwebsocket *wsi; |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 135 | int ringbuffer_tail; |
| 136 | }; |
| 137 | |
| 138 | struct a_message { |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 139 | void *payload; |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 140 | size_t len; |
| 141 | }; |
| 142 | |
| 143 | static struct a_message ringbuffer[MAX_MESSAGE_QUEUE]; |
| 144 | static int ringbuffer_head; |
| 145 | |
| 146 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 147 | static int |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 148 | callback_lws_mirror(struct libwebsocket *wsi, |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 149 | enum libwebsocket_callback_reasons reason, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 150 | void *user, void *in, size_t len) |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 151 | { |
| 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 Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 156 | struct per_session_data__lws_mirror *pss = user; |
| 157 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 158 | switch (reason) { |
| 159 | |
| 160 | case LWS_CALLBACK_ESTABLISHED: |
| 161 | pss->wsi = wsi; |
| 162 | pss->ringbuffer_tail = ringbuffer_head; |
| 163 | break; |
| 164 | |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 165 | case LWS_CALLBACK_SEND: |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 166 | /* send everything that's pending */ |
| 167 | while (pss->ringbuffer_tail != ringbuffer_head) { |
| 168 | |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 169 | n = libwebsocket_write(wsi, (unsigned char *) |
| 170 | ringbuffer[pss->ringbuffer_tail].payload + |
| 171 | LWS_SEND_BUFFER_PRE_PADDING, |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 172 | ringbuffer[pss->ringbuffer_tail].len, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 173 | LWS_WRITE_TEXT); |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 174 | 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 Green | 5195968 | 2010-11-12 14:12:13 +0000 | [diff] [blame] | 187 | if (ringbuffer[ringbuffer_head].payload) |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 188 | free(ringbuffer[ringbuffer_head].payload); |
Andy Green | 5195968 | 2010-11-12 14:12:13 +0000 | [diff] [blame] | 189 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 190 | 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 Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 194 | 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 Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 210 | /* list of supported protocols and callbacks */ |
| 211 | |
| 212 | static 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 Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 224 | { |
| 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 Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 230 | { /* end of list */ |
| 231 | .callback = NULL |
| 232 | } |
| 233 | }; |
| 234 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 235 | static struct option options[] = { |
| 236 | { "help", no_argument, NULL, 'h' }, |
| 237 | { "port", required_argument, NULL, 'p' }, |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 238 | { "ssl", no_argument, NULL, 's' }, |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 239 | { NULL, 0, 0, 0 } |
| 240 | }; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 241 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 242 | int main(int argc, char **argv) |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 243 | { |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 244 | int n = 0; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 245 | const char *cert_path = |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 246 | LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem"; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 247 | const char *key_path = |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 248 | LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem"; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 249 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 250 | fprintf(stderr, "libwebsockets test server\n" |
Andy Green | 6452f1e | 2010-11-11 09:22:22 +0000 | [diff] [blame] | 251 | "(C) Copyright 2010 Andy Green <andy@warmcat.com> " |
| 252 | "licensed under LGPL2.1\n"); |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 253 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 254 | while (n >= 0) { |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 255 | n = getopt_long(argc, argv, "hp:", options, NULL); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 256 | if (n < 0) |
| 257 | continue; |
| 258 | switch (n) { |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 259 | case 's': |
| 260 | use_ssl = 1; |
| 261 | break; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 262 | case 'p': |
| 263 | port = atoi(optarg); |
| 264 | break; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 265 | case 'h': |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 266 | fprintf(stderr, "Usage: test-server " |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 267 | "[--port=<p>] [--ssl]\n"); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 268 | exit(1); |
| 269 | } |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 270 | } |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 271 | |
| 272 | if (!use_ssl) |
| 273 | cert_path = key_path = NULL; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame^] | 274 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 275 | if (libwebsocket_create_server(port, protocols, |
| 276 | cert_path, key_path, -1, -1) < 0) { |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 277 | fprintf(stderr, "libwebsocket init failed\n"); |
| 278 | return -1; |
| 279 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 280 | |
| 281 | return 0; |
| 282 | } |