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 | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 4 | * Copyright (C) 2010-2011 Andy Green <andy@warmcat.com> |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 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 | */ |
Joakim Soderberg | d2edfec | 2013-02-06 15:27:27 +0900 | [diff] [blame] | 21 | #ifdef CMAKE_BUILD |
| 22 | #include "lws_config.h" |
| 23 | #endif |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 24 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 28 | #include <getopt.h> |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 29 | #include <string.h> |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 30 | #include <sys/time.h> |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <fcntl.h> |
Andy Green | 5fc460c | 2013-01-15 19:44:33 +0800 | [diff] [blame] | 33 | #include <assert.h> |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 34 | #ifdef WIN32 |
| 35 | |
| 36 | #ifdef EXTERNAL_POLL |
| 37 | #ifndef WIN32_LEAN_AND_MEAN |
| 38 | #define WIN32_LEAN_AND_MEAN |
| 39 | #endif |
| 40 | #include <winsock2.h> |
| 41 | #include <ws2tcpip.h> |
| 42 | #include <stddef.h> |
| 43 | |
| 44 | #include "websock-w32.h" |
| 45 | #endif |
| 46 | |
| 47 | #else // NOT WIN32 |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 48 | #include <syslog.h> |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 49 | #endif |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 50 | |
Andy Green | 08f2c01 | 2013-01-30 08:02:26 +0800 | [diff] [blame] | 51 | #include <signal.h> |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 52 | |
Andy Green | 7310e9c | 2010-11-01 09:12:17 +0000 | [diff] [blame] | 53 | #include "../lib/libwebsockets.h" |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 54 | |
Andy Green | 69758fa | 2011-03-07 07:08:07 +0000 | [diff] [blame] | 55 | static int close_testing; |
Edwin van den Oetelaar | 596b220 | 2013-01-20 20:51:14 +0800 | [diff] [blame] | 56 | int max_poll_elements; |
Andy Green | 5fc460c | 2013-01-15 19:44:33 +0800 | [diff] [blame] | 57 | |
Edwin van den Oetelaar | 596b220 | 2013-01-20 20:51:14 +0800 | [diff] [blame] | 58 | struct pollfd *pollfds; |
| 59 | int *fd_lookup; |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 60 | int count_pollfds; |
Andy Green | 08f2c01 | 2013-01-30 08:02:26 +0800 | [diff] [blame] | 61 | int force_exit = 0; |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 62 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 63 | /* |
| 64 | * This demo server shows how to use libwebsockets for one or more |
| 65 | * websocket protocols in the same server |
| 66 | * |
| 67 | * It defines the following websocket protocols: |
| 68 | * |
| 69 | * dumb-increment-protocol: once the socket is opened, an incrementing |
| 70 | * ascii string is sent down it every 50ms. |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 71 | * If you send "reset\n" on the websocket, then |
| 72 | * the incrementing number is reset to 0. |
Andy Green | 5195968 | 2010-11-12 14:12:13 +0000 | [diff] [blame] | 73 | * |
| 74 | * lws-mirror-protocol: copies any received packet to every connection also |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 75 | * using this protocol, including the sender |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 76 | */ |
| 77 | |
Andy Green | c87fc2f | 2011-01-18 15:29:04 +0000 | [diff] [blame] | 78 | enum demo_protocols { |
| 79 | /* always first */ |
| 80 | PROTOCOL_HTTP = 0, |
| 81 | |
| 82 | PROTOCOL_DUMB_INCREMENT, |
| 83 | PROTOCOL_LWS_MIRROR, |
| 84 | |
| 85 | /* always last */ |
| 86 | DEMO_PROTOCOL_COUNT |
| 87 | }; |
| 88 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 89 | |
Andy Green | cce2a81 | 2012-04-12 11:06:05 +0800 | [diff] [blame] | 90 | #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server" |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 91 | char *resource_path = LOCAL_RESOURCE_PATH; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 92 | |
Andy Green | bb2dc8a | 2013-01-16 10:06:28 +0800 | [diff] [blame] | 93 | /* |
| 94 | * We take a strict whitelist approach to stop ../ attacks |
| 95 | */ |
| 96 | |
| 97 | struct serveable { |
| 98 | const char *urlpath; |
| 99 | const char *mimetype; |
| 100 | }; |
| 101 | |
| 102 | static const struct serveable whitelist[] = { |
| 103 | { "/favicon.ico", "image/x-icon" }, |
| 104 | { "/libwebsockets.org-logo.png", "image/png" }, |
| 105 | |
| 106 | /* last one is the default served if no match */ |
| 107 | { "/test.html", "text/html" }, |
| 108 | }; |
| 109 | |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 110 | struct per_session_data__http { |
| 111 | int fd; |
| 112 | }; |
| 113 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 114 | /* 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] | 115 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 116 | static int callback_http(struct libwebsocket_context *context, |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 117 | struct libwebsocket *wsi, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 118 | enum libwebsocket_callback_reasons reason, void *user, |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 119 | void *in, size_t len) |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 120 | { |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 121 | #if 0 |
Andy Green | 863d4d2 | 2011-02-13 08:40:37 +0000 | [diff] [blame] | 122 | char client_name[128]; |
| 123 | char client_ip[128]; |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 124 | #endif |
Andy Green | bb2dc8a | 2013-01-16 10:06:28 +0800 | [diff] [blame] | 125 | char buf[256]; |
Andy Green | d91561c | 2013-04-30 06:51:10 +0800 | [diff] [blame] | 126 | char leaf_path[1024]; |
Andy Green | 6cd8880 | 2013-11-09 11:06:29 +0800 | [diff] [blame^] | 127 | char b64[64]; |
| 128 | struct timeval tv; |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 129 | int n, m; |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 130 | unsigned char *p; |
Andy Green | 6cd8880 | 2013-11-09 11:06:29 +0800 | [diff] [blame^] | 131 | char *other_headers; |
Andy Green | 73a820a | 2013-02-18 11:32:49 +0800 | [diff] [blame] | 132 | static unsigned char buffer[4096]; |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 133 | struct stat stat_buf; |
Andy Green | d91561c | 2013-04-30 06:51:10 +0800 | [diff] [blame] | 134 | struct per_session_data__http *pss = |
| 135 | (struct per_session_data__http *)user; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 136 | #ifdef EXTERNAL_POLL |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 137 | int fd = (int)(long)in; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 138 | #endif |
Andy Green | 863d4d2 | 2011-02-13 08:40:37 +0000 | [diff] [blame] | 139 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 140 | switch (reason) { |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 141 | case LWS_CALLBACK_HTTP: |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 142 | |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 143 | /* check for the "send a big file by hand" example case */ |
| 144 | |
| 145 | if (!strcmp((const char *)in, "/leaf.jpg")) { |
Andy Green | d91561c | 2013-04-30 06:51:10 +0800 | [diff] [blame] | 146 | if (strlen(resource_path) > sizeof(leaf_path) - 10) |
| 147 | return -1; |
| 148 | sprintf(leaf_path, "%s/leaf.jpg", resource_path); |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 149 | |
| 150 | /* well, let's demonstrate how to send the hard way */ |
| 151 | |
| 152 | p = buffer; |
| 153 | |
Michel Archambault | e8c00aa | 2013-03-14 11:45:49 -0400 | [diff] [blame] | 154 | #ifdef WIN32 |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 155 | pss->fd = open(leaf_path, O_RDONLY | _O_BINARY); |
Michel Archambault | e8c00aa | 2013-03-14 11:45:49 -0400 | [diff] [blame] | 156 | #else |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 157 | pss->fd = open(leaf_path, O_RDONLY); |
Michel Archambault | e8c00aa | 2013-03-14 11:45:49 -0400 | [diff] [blame] | 158 | #endif |
| 159 | |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 160 | if (pss->fd < 0) |
| 161 | return -1; |
| 162 | |
| 163 | fstat(pss->fd, &stat_buf); |
| 164 | |
| 165 | /* |
| 166 | * we will send a big jpeg file, but it could be |
| 167 | * anything. Set the Content-Type: appropriately |
| 168 | * so the browser knows what to do with it. |
| 169 | */ |
| 170 | |
| 171 | p += sprintf((char *)p, |
| 172 | "HTTP/1.0 200 OK\x0d\x0a" |
| 173 | "Server: libwebsockets\x0d\x0a" |
Luc | e46043c | 2013-02-23 11:01:21 +0800 | [diff] [blame] | 174 | "Content-Type: image/jpeg\x0d\x0a" |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 175 | "Content-Length: %u\x0d\x0a\x0d\x0a", |
| 176 | (unsigned int)stat_buf.st_size); |
| 177 | |
| 178 | /* |
| 179 | * send the http headers... |
| 180 | * this won't block since it's the first payload sent |
| 181 | * on the connection since it was established |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 182 | * (too small for partial) |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 183 | */ |
| 184 | |
| 185 | n = libwebsocket_write(wsi, buffer, |
| 186 | p - buffer, LWS_WRITE_HTTP); |
| 187 | |
| 188 | if (n < 0) { |
| 189 | close(pss->fd); |
| 190 | return -1; |
| 191 | } |
| 192 | /* |
| 193 | * book us a LWS_CALLBACK_HTTP_WRITEABLE callback |
| 194 | */ |
| 195 | libwebsocket_callback_on_writable(context, wsi); |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | /* if not, send a file the easy way */ |
| 200 | |
Andy Green | bb2dc8a | 2013-01-16 10:06:28 +0800 | [diff] [blame] | 201 | for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++) |
Andy Green | 36eb70d | 2013-02-01 08:42:15 +0800 | [diff] [blame] | 202 | if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0) |
Andy Green | bb2dc8a | 2013-01-16 10:06:28 +0800 | [diff] [blame] | 203 | break; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 204 | |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 205 | sprintf(buf, "%s%s", resource_path, whitelist[n].urlpath); |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 206 | |
Andy Green | 6cd8880 | 2013-11-09 11:06:29 +0800 | [diff] [blame^] | 207 | /* demostrates how to set a cookie on / */ |
| 208 | |
| 209 | other_headers = NULL; |
| 210 | if (!strcmp((const char *)in, "/") && |
| 211 | !lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COOKIE)) { |
| 212 | /* this isn't very unguessable but it'll do for us */ |
| 213 | gettimeofday(&tv, NULL); |
| 214 | sprintf(b64, "LWS_%u_%u_COOKIE", |
| 215 | (unsigned int)tv.tv_sec, |
| 216 | (unsigned int)tv.tv_usec); |
| 217 | |
| 218 | sprintf(leaf_path, |
| 219 | "Set-Cookie: test=LWS_%u_%u_COOKIE;Max-Age=360000\x0d\x0a", |
| 220 | (unsigned int)tv.tv_sec, (unsigned int)tv.tv_usec); |
| 221 | other_headers = leaf_path; |
| 222 | lwsl_err(other_headers); |
| 223 | } |
| 224 | |
| 225 | if (libwebsockets_serve_http_file(context, wsi, buf, |
| 226 | whitelist[n].mimetype, other_headers)) |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 227 | return -1; /* through completion or error, close the socket */ |
Andy Green | 24b588b | 2013-01-13 09:53:18 +0800 | [diff] [blame] | 228 | |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 229 | /* |
| 230 | * notice that the sending of the file completes asynchronously, |
| 231 | * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when |
| 232 | * it's done |
| 233 | */ |
| 234 | |
Andy Green | bb2dc8a | 2013-01-16 10:06:28 +0800 | [diff] [blame] | 235 | break; |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 236 | |
| 237 | case LWS_CALLBACK_HTTP_FILE_COMPLETION: |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 238 | // lwsl_info("LWS_CALLBACK_HTTP_FILE_COMPLETION seen\n"); |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 239 | /* kill the connection after we sent one file */ |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 240 | return -1; |
| 241 | |
| 242 | case LWS_CALLBACK_HTTP_WRITEABLE: |
| 243 | /* |
| 244 | * we can send more of whatever it is we were sending |
| 245 | */ |
| 246 | |
| 247 | do { |
| 248 | n = read(pss->fd, buffer, sizeof buffer); |
| 249 | /* problem reading, close conn */ |
| 250 | if (n < 0) |
| 251 | goto bail; |
| 252 | /* sent it all, close conn */ |
| 253 | if (n == 0) |
| 254 | goto bail; |
| 255 | /* |
| 256 | * because it's HTTP and not websocket, don't need to take |
| 257 | * care about pre and postamble |
| 258 | */ |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 259 | m = libwebsocket_write(wsi, buffer, n, LWS_WRITE_HTTP); |
| 260 | if (m < 0) |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 261 | /* write failed, close conn */ |
| 262 | goto bail; |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 263 | if (m != n) |
| 264 | /* partial write, adjust */ |
| 265 | lseek(pss->fd, m - n, SEEK_CUR); |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 266 | |
| 267 | } while (!lws_send_pipe_choked(wsi)); |
| 268 | libwebsocket_callback_on_writable(context, wsi); |
| 269 | break; |
| 270 | |
| 271 | bail: |
| 272 | close(pss->fd); |
| 273 | return -1; |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 274 | |
Andy Green | 863d4d2 | 2011-02-13 08:40:37 +0000 | [diff] [blame] | 275 | /* |
| 276 | * callback for confirming to continue with client IP appear in |
| 277 | * protocol 0 callback since no websocket protocol has been agreed |
| 278 | * yet. You can just ignore this if you won't filter on client IP |
| 279 | * since the default uhandled callback return is 0 meaning let the |
| 280 | * connection continue. |
| 281 | */ |
| 282 | |
| 283 | case LWS_CALLBACK_FILTER_NETWORK_CONNECTION: |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 284 | #if 0 |
Edwin van den Oetelaar | 8c8a8e1 | 2013-02-20 20:56:59 +0800 | [diff] [blame] | 285 | libwebsockets_get_peer_addresses(context, wsi, (int)(long)in, client_name, |
Andy Green | 863d4d2 | 2011-02-13 08:40:37 +0000 | [diff] [blame] | 286 | sizeof(client_name), client_ip, sizeof(client_ip)); |
| 287 | |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 288 | fprintf(stderr, "Received network connect from %s (%s)\n", |
| 289 | client_name, client_ip); |
| 290 | #endif |
Andy Green | 863d4d2 | 2011-02-13 08:40:37 +0000 | [diff] [blame] | 291 | /* if we returned non-zero from here, we kill the connection */ |
| 292 | break; |
| 293 | |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 294 | #ifdef EXTERNAL_POLL |
| 295 | /* |
| 296 | * callbacks for managing the external poll() array appear in |
| 297 | * protocol 0 callback |
| 298 | */ |
| 299 | |
| 300 | case LWS_CALLBACK_ADD_POLL_FD: |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 301 | |
Edwin van den Oetelaar | 596b220 | 2013-01-20 20:51:14 +0800 | [diff] [blame] | 302 | if (count_pollfds >= max_poll_elements) { |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 303 | lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n"); |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 304 | return 1; |
Andy Green | 5fc460c | 2013-01-15 19:44:33 +0800 | [diff] [blame] | 305 | } |
Andy Green | 5fc460c | 2013-01-15 19:44:33 +0800 | [diff] [blame] | 306 | |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 307 | fd_lookup[fd] = count_pollfds; |
| 308 | pollfds[count_pollfds].fd = fd; |
| 309 | pollfds[count_pollfds].events = (int)(long)len; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 310 | pollfds[count_pollfds++].revents = 0; |
| 311 | break; |
| 312 | |
| 313 | case LWS_CALLBACK_DEL_POLL_FD: |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 314 | if (!--count_pollfds) |
| 315 | break; |
| 316 | m = fd_lookup[fd]; |
| 317 | /* have the last guy take up the vacant slot */ |
| 318 | pollfds[m] = pollfds[count_pollfds]; |
| 319 | fd_lookup[pollfds[count_pollfds].fd] = m; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 320 | break; |
| 321 | |
| 322 | case LWS_CALLBACK_SET_MODE_POLL_FD: |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 323 | pollfds[fd_lookup[fd]].events |= (int)(long)len; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 324 | break; |
| 325 | |
| 326 | case LWS_CALLBACK_CLEAR_MODE_POLL_FD: |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 327 | pollfds[fd_lookup[fd]].events &= ~(int)(long)len; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 328 | break; |
| 329 | #endif |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 330 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 331 | default: |
| 332 | break; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 333 | } |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 334 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 338 | /* |
| 339 | * this is just an example of parsing handshake headers, you don't need this |
| 340 | * in your code unless you will filter allowing connections by the header |
| 341 | * content |
| 342 | */ |
| 343 | |
| 344 | static void |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 345 | dump_handshake_info(struct libwebsocket *wsi) |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 346 | { |
| 347 | int n; |
Andy Green | 6cd8880 | 2013-11-09 11:06:29 +0800 | [diff] [blame^] | 348 | static const char *token_names[] = { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 349 | /*[WSI_TOKEN_GET_URI] =*/ "GET URI", |
| 350 | /*[WSI_TOKEN_HOST] =*/ "Host", |
| 351 | /*[WSI_TOKEN_CONNECTION] =*/ "Connection", |
| 352 | /*[WSI_TOKEN_KEY1] =*/ "key 1", |
| 353 | /*[WSI_TOKEN_KEY2] =*/ "key 2", |
| 354 | /*[WSI_TOKEN_PROTOCOL] =*/ "Protocol", |
| 355 | /*[WSI_TOKEN_UPGRADE] =*/ "Upgrade", |
| 356 | /*[WSI_TOKEN_ORIGIN] =*/ "Origin", |
| 357 | /*[WSI_TOKEN_DRAFT] =*/ "Draft", |
| 358 | /*[WSI_TOKEN_CHALLENGE] =*/ "Challenge", |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 359 | |
| 360 | /* new for 04 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 361 | /*[WSI_TOKEN_KEY] =*/ "Key", |
| 362 | /*[WSI_TOKEN_VERSION] =*/ "Version", |
| 363 | /*[WSI_TOKEN_SWORIGIN] =*/ "Sworigin", |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 364 | |
| 365 | /* new for 05 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 366 | /*[WSI_TOKEN_EXTENSIONS] =*/ "Extensions", |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 367 | |
| 368 | /* client receives these */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 369 | /*[WSI_TOKEN_ACCEPT] =*/ "Accept", |
| 370 | /*[WSI_TOKEN_NONCE] =*/ "Nonce", |
| 371 | /*[WSI_TOKEN_HTTP] =*/ "Http", |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 372 | |
| 373 | "Accept:", |
| 374 | "If-Modified-Since:", |
| 375 | "Accept-Encoding:", |
| 376 | "Accept-Language:", |
| 377 | "Pragma:", |
| 378 | "Cache-Control:", |
| 379 | "Authorization:", |
| 380 | "Cookie:", |
| 381 | "Content-Type:", |
| 382 | "Date:", |
| 383 | "Range:", |
| 384 | "Referer:" |
| 385 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 386 | /*[WSI_TOKEN_MUXURL] =*/ "MuxURL", |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 387 | }; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 388 | char buf[256]; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 389 | |
Andy Green | 6cd8880 | 2013-11-09 11:06:29 +0800 | [diff] [blame^] | 390 | for (n = 0; n < sizeof(token_names) / sizeof(token_names[0]); n++) { |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 391 | if (!lws_hdr_total_length(wsi, n)) |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 392 | continue; |
| 393 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 394 | lws_hdr_copy(wsi, buf, sizeof buf, n); |
| 395 | |
| 396 | fprintf(stderr, " %s = %s\n", token_names[n], buf); |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 400 | /* dumb_increment protocol */ |
| 401 | |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 402 | /* |
| 403 | * one of these is auto-created for each connection and a pointer to the |
| 404 | * appropriate instance is passed to the callback in the user parameter |
| 405 | * |
| 406 | * for this example protocol we use it to individualize the count for each |
| 407 | * connection. |
| 408 | */ |
| 409 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 410 | struct per_session_data__dumb_increment { |
| 411 | int number; |
| 412 | }; |
| 413 | |
| 414 | static int |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 415 | callback_dumb_increment(struct libwebsocket_context *context, |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 416 | struct libwebsocket *wsi, |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 417 | enum libwebsocket_callback_reasons reason, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 418 | void *user, void *in, size_t len) |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 419 | { |
Andy Green | 3390efd | 2013-03-23 09:46:18 +0800 | [diff] [blame] | 420 | int n, m; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 421 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 + |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 422 | LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 423 | unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING]; |
Andy Green | 36eb70d | 2013-02-01 08:42:15 +0800 | [diff] [blame] | 424 | struct per_session_data__dumb_increment *pss = (struct per_session_data__dumb_increment *)user; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 425 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 426 | switch (reason) { |
| 427 | |
| 428 | case LWS_CALLBACK_ESTABLISHED: |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 429 | lwsl_info("callback_dumb_increment: " |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 430 | "LWS_CALLBACK_ESTABLISHED\n"); |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 431 | pss->number = 0; |
| 432 | break; |
| 433 | |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 434 | case LWS_CALLBACK_SERVER_WRITEABLE: |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 435 | n = sprintf((char *)p, "%d", pss->number++); |
Andy Green | 3390efd | 2013-03-23 09:46:18 +0800 | [diff] [blame] | 436 | m = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT); |
| 437 | if (m < n) { |
Andy Green | 51b20ee | 2013-02-17 09:14:08 +0800 | [diff] [blame] | 438 | lwsl_err("ERROR %d writing to di socket\n", n); |
| 439 | return -1; |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 440 | } |
Andy Green | 69758fa | 2011-03-07 07:08:07 +0000 | [diff] [blame] | 441 | if (close_testing && pss->number == 50) { |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 442 | lwsl_info("close tesing limit, closing\n"); |
Andy Green | 508946c | 2013-02-12 10:19:08 +0800 | [diff] [blame] | 443 | return -1; |
Andy Green | 69758fa | 2011-03-07 07:08:07 +0000 | [diff] [blame] | 444 | } |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 445 | break; |
| 446 | |
| 447 | case LWS_CALLBACK_RECEIVE: |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 448 | // fprintf(stderr, "rx %d\n", (int)len); |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 449 | if (len < 6) |
| 450 | break; |
Andy Green | 36eb70d | 2013-02-01 08:42:15 +0800 | [diff] [blame] | 451 | if (strcmp((const char *)in, "reset\n") == 0) |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 452 | pss->number = 0; |
| 453 | break; |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 454 | /* |
| 455 | * this just demonstrates how to use the protocol filter. If you won't |
| 456 | * study and reject connections based on header content, you don't need |
| 457 | * to handle this callback |
| 458 | */ |
| 459 | |
| 460 | case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 461 | dump_handshake_info(wsi); |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 462 | /* you could return non-zero here and kill the connection */ |
| 463 | break; |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 464 | |
| 465 | default: |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 473 | /* lws-mirror_protocol */ |
| 474 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 475 | #define MAX_MESSAGE_QUEUE 32 |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 476 | |
| 477 | struct per_session_data__lws_mirror { |
| 478 | struct libwebsocket *wsi; |
| 479 | int ringbuffer_tail; |
| 480 | }; |
| 481 | |
| 482 | struct a_message { |
| 483 | void *payload; |
| 484 | size_t len; |
| 485 | }; |
| 486 | |
| 487 | static struct a_message ringbuffer[MAX_MESSAGE_QUEUE]; |
| 488 | static int ringbuffer_head; |
| 489 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 490 | static int |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 491 | callback_lws_mirror(struct libwebsocket_context *context, |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 492 | struct libwebsocket *wsi, |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 493 | enum libwebsocket_callback_reasons reason, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 494 | void *user, void *in, size_t len) |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 495 | { |
| 496 | int n; |
Andy Green | 36eb70d | 2013-02-01 08:42:15 +0800 | [diff] [blame] | 497 | struct per_session_data__lws_mirror *pss = (struct per_session_data__lws_mirror *)user; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 498 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 499 | switch (reason) { |
| 500 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 501 | case LWS_CALLBACK_ESTABLISHED: |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 502 | lwsl_info("callback_lws_mirror: LWS_CALLBACK_ESTABLISHED\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 503 | pss->ringbuffer_tail = ringbuffer_head; |
| 504 | pss->wsi = wsi; |
| 505 | break; |
| 506 | |
Andy Green | a7109e6 | 2013-02-11 12:05:54 +0800 | [diff] [blame] | 507 | case LWS_CALLBACK_PROTOCOL_DESTROY: |
| 508 | lwsl_notice("mirror protocol cleaning up\n"); |
| 509 | for (n = 0; n < sizeof ringbuffer / sizeof ringbuffer[0]; n++) |
| 510 | if (ringbuffer[n].payload) |
| 511 | free(ringbuffer[n].payload); |
| 512 | break; |
| 513 | |
Andy Green | 9e4c2b6 | 2011-03-07 20:47:39 +0000 | [diff] [blame] | 514 | case LWS_CALLBACK_SERVER_WRITEABLE: |
Andy Green | 69758fa | 2011-03-07 07:08:07 +0000 | [diff] [blame] | 515 | if (close_testing) |
| 516 | break; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 517 | while (pss->ringbuffer_tail != ringbuffer_head) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 518 | |
| 519 | n = libwebsocket_write(wsi, (unsigned char *) |
| 520 | ringbuffer[pss->ringbuffer_tail].payload + |
| 521 | LWS_SEND_BUFFER_PRE_PADDING, |
| 522 | ringbuffer[pss->ringbuffer_tail].len, |
| 523 | LWS_WRITE_TEXT); |
Andy Green | 3390efd | 2013-03-23 09:46:18 +0800 | [diff] [blame] | 524 | if (n < ringbuffer[pss->ringbuffer_tail].len) { |
Andy Green | 51b20ee | 2013-02-17 09:14:08 +0800 | [diff] [blame] | 525 | lwsl_err("ERROR %d writing to mirror socket\n", n); |
| 526 | return -1; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 527 | } |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 528 | if (n < ringbuffer[pss->ringbuffer_tail].len) |
| 529 | lwsl_err("mirror partial write %d vs %d\n", |
| 530 | n, ringbuffer[pss->ringbuffer_tail].len); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 531 | |
| 532 | if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1)) |
| 533 | pss->ringbuffer_tail = 0; |
| 534 | else |
| 535 | pss->ringbuffer_tail++; |
| 536 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 537 | if (((ringbuffer_head - pss->ringbuffer_tail) & |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 538 | (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15)) |
| 539 | libwebsocket_rx_flow_allow_all_protocol( |
| 540 | libwebsockets_get_protocol(wsi)); |
| 541 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 542 | // lwsl_debug("tx fifo %d\n", (ringbuffer_head - pss->ringbuffer_tail) & (MAX_MESSAGE_QUEUE - 1)); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 543 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 544 | if (lws_send_pipe_choked(wsi)) { |
| 545 | libwebsocket_callback_on_writable(context, wsi); |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 546 | break; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 547 | } |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 548 | /* |
| 549 | * for tests with chrome on same machine as client and |
| 550 | * server, this is needed to stop chrome choking |
| 551 | */ |
| 552 | usleep(1); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 553 | } |
| 554 | break; |
| 555 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 556 | case LWS_CALLBACK_RECEIVE: |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 557 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 558 | if (((ringbuffer_head - pss->ringbuffer_tail) & |
| 559 | (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 1)) { |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 560 | lwsl_err("dropping!\n"); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 561 | goto choke; |
| 562 | } |
| 563 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 564 | if (ringbuffer[ringbuffer_head].payload) |
| 565 | free(ringbuffer[ringbuffer_head].payload); |
| 566 | |
| 567 | ringbuffer[ringbuffer_head].payload = |
| 568 | malloc(LWS_SEND_BUFFER_PRE_PADDING + len + |
| 569 | LWS_SEND_BUFFER_POST_PADDING); |
| 570 | ringbuffer[ringbuffer_head].len = len; |
| 571 | memcpy((char *)ringbuffer[ringbuffer_head].payload + |
| 572 | LWS_SEND_BUFFER_PRE_PADDING, in, len); |
| 573 | if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1)) |
| 574 | ringbuffer_head = 0; |
| 575 | else |
| 576 | ringbuffer_head++; |
| 577 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 578 | if (((ringbuffer_head - pss->ringbuffer_tail) & |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 579 | (MAX_MESSAGE_QUEUE - 1)) != (MAX_MESSAGE_QUEUE - 2)) |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 580 | goto done; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 581 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 582 | choke: |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 583 | lwsl_debug("LWS_CALLBACK_RECEIVE: throttling %p\n", wsi); |
| 584 | libwebsocket_rx_flow_control(wsi, 0); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 585 | |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 586 | // lwsl_debug("rx fifo %d\n", (ringbuffer_head - pss->ringbuffer_tail) & (MAX_MESSAGE_QUEUE - 1)); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 587 | done: |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 588 | libwebsocket_callback_on_writable_all_protocol( |
| 589 | libwebsockets_get_protocol(wsi)); |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 590 | break; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 591 | |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 592 | /* |
| 593 | * this just demonstrates how to use the protocol filter. If you won't |
| 594 | * study and reject connections based on header content, you don't need |
| 595 | * to handle this callback |
| 596 | */ |
| 597 | |
| 598 | case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 599 | dump_handshake_info(wsi); |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 600 | /* you could return non-zero here and kill the connection */ |
| 601 | break; |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 602 | |
| 603 | default: |
| 604 | break; |
| 605 | } |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 611 | /* list of supported protocols and callbacks */ |
| 612 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 613 | static struct libwebsocket_protocols protocols[] = { |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 614 | /* first protocol must always be HTTP handler */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 615 | |
| 616 | { |
| 617 | "http-only", /* name */ |
| 618 | callback_http, /* callback */ |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 619 | sizeof (struct per_session_data__http), /* per_session_data_size */ |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 620 | 0, /* max frame size / rx buffer */ |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 621 | }, |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 622 | { |
| 623 | "dumb-increment-protocol", |
| 624 | callback_dumb_increment, |
| 625 | sizeof(struct per_session_data__dumb_increment), |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 626 | 10, |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 627 | }, |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 628 | { |
| 629 | "lws-mirror-protocol", |
| 630 | callback_lws_mirror, |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 631 | sizeof(struct per_session_data__lws_mirror), |
Andy Green | e7c97e8 | 2013-02-09 14:07:32 +0800 | [diff] [blame] | 632 | 128, |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 633 | }, |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 634 | { NULL, NULL, 0, 0 } /* terminator */ |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 635 | }; |
| 636 | |
Andy Green | 08f2c01 | 2013-01-30 08:02:26 +0800 | [diff] [blame] | 637 | void sighandler(int sig) |
| 638 | { |
| 639 | force_exit = 1; |
| 640 | } |
| 641 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 642 | static struct option options[] = { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 643 | { "help", no_argument, NULL, 'h' }, |
Andy Green | 46ef0cf | 2013-01-10 22:28:59 +0800 | [diff] [blame] | 644 | { "debug", required_argument, NULL, 'd' }, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 645 | { "port", required_argument, NULL, 'p' }, |
| 646 | { "ssl", no_argument, NULL, 's' }, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 647 | { "interface", required_argument, NULL, 'i' }, |
Andy Green | 69758fa | 2011-03-07 07:08:07 +0000 | [diff] [blame] | 648 | { "closetest", no_argument, NULL, 'c' }, |
Andy Green | 5c54d62 | 2013-01-21 12:58:04 +0800 | [diff] [blame] | 649 | #ifndef LWS_NO_DAEMONIZE |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 650 | { "daemonize", no_argument, NULL, 'D' }, |
| 651 | #endif |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 652 | { "resource_path", required_argument, NULL, 'r' }, |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 653 | { NULL, 0, 0, 0 } |
| 654 | }; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 655 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 656 | int main(int argc, char **argv) |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 657 | { |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 658 | char cert_path[1024]; |
| 659 | char key_path[1024]; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 660 | int n = 0; |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 661 | int use_ssl = 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 662 | struct libwebsocket_context *context; |
Andy Green | 8014b29 | 2011-01-30 20:57:25 +0000 | [diff] [blame] | 663 | int opts = 0; |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 664 | char interface_name[128] = ""; |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 665 | const char *iface = NULL; |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 666 | #ifndef WIN32 |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 667 | int syslog_options = LOG_PID | LOG_PERROR; |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 668 | #endif |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 669 | unsigned int oldus = 0; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 670 | struct lws_context_creation_info info; |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 671 | |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 672 | int debug_level = 7; |
Andy Green | 5c54d62 | 2013-01-21 12:58:04 +0800 | [diff] [blame] | 673 | #ifndef LWS_NO_DAEMONIZE |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 674 | int daemonize = 0; |
| 675 | #endif |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 676 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 677 | memset(&info, 0, sizeof info); |
| 678 | info.port = 7681; |
| 679 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 680 | while (n >= 0) { |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 681 | n = getopt_long(argc, argv, "ci:hsp:d:Dr:", options, NULL); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 682 | if (n < 0) |
| 683 | continue; |
| 684 | switch (n) { |
Andy Green | 5c54d62 | 2013-01-21 12:58:04 +0800 | [diff] [blame] | 685 | #ifndef LWS_NO_DAEMONIZE |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 686 | case 'D': |
| 687 | daemonize = 1; |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 688 | #ifndef WIN32 |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 689 | syslog_options &= ~LOG_PERROR; |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 690 | #endif |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 691 | break; |
| 692 | #endif |
Andy Green | 46ef0cf | 2013-01-10 22:28:59 +0800 | [diff] [blame] | 693 | case 'd': |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 694 | debug_level = atoi(optarg); |
Andy Green | 46ef0cf | 2013-01-10 22:28:59 +0800 | [diff] [blame] | 695 | break; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 696 | case 's': |
| 697 | use_ssl = 1; |
| 698 | break; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 699 | case 'p': |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 700 | info.port = atoi(optarg); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 701 | break; |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 702 | case 'i': |
| 703 | strncpy(interface_name, optarg, sizeof interface_name); |
| 704 | interface_name[(sizeof interface_name) - 1] = '\0'; |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 705 | iface = interface_name; |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 706 | break; |
Andy Green | 69758fa | 2011-03-07 07:08:07 +0000 | [diff] [blame] | 707 | case 'c': |
| 708 | close_testing = 1; |
| 709 | fprintf(stderr, " Close testing mode -- closes on " |
| 710 | "client after 50 dumb increments" |
| 711 | "and suppresses lws_mirror spam\n"); |
| 712 | break; |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 713 | case 'r': |
| 714 | resource_path = optarg; |
| 715 | printf("Setting resource path to \"%s\"\n", resource_path); |
| 716 | break; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 717 | case 'h': |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 718 | fprintf(stderr, "Usage: test-server " |
Andy Green | 46ef0cf | 2013-01-10 22:28:59 +0800 | [diff] [blame] | 719 | "[--port=<p>] [--ssl] " |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 720 | "[-d <log bitfield>] " |
| 721 | "[--resource_path <path>]\n"); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 722 | exit(1); |
| 723 | } |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 724 | } |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 725 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 726 | #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32) |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 727 | /* |
| 728 | * normally lock path would be /var/lock/lwsts or similar, to |
| 729 | * simplify getting started without having to take care about |
| 730 | * permissions or running as root, set to /tmp/.lwsts-lock |
| 731 | */ |
| 732 | if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) { |
| 733 | fprintf(stderr, "Failed to daemonize\n"); |
| 734 | return 1; |
| 735 | } |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 736 | #endif |
Andy Green | 08f2c01 | 2013-01-30 08:02:26 +0800 | [diff] [blame] | 737 | |
| 738 | signal(SIGINT, sighandler); |
| 739 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 740 | #ifndef WIN32 |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 741 | /* we will only try to log things according to our debug_level */ |
| 742 | setlogmask(LOG_UPTO (LOG_DEBUG)); |
| 743 | openlog("lwsts", syslog_options, LOG_DAEMON); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 744 | #endif |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 745 | |
| 746 | /* tell the library what debug level to emit and to send it to syslog */ |
| 747 | lws_set_log_level(debug_level, lwsl_emit_syslog); |
| 748 | |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 749 | lwsl_notice("libwebsockets test server - " |
| 750 | "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - " |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 751 | "licensed under LGPL2.1\n"); |
Edwin van den Oetelaar | 596b220 | 2013-01-20 20:51:14 +0800 | [diff] [blame] | 752 | #ifdef EXTERNAL_POLL |
| 753 | max_poll_elements = getdtablesize(); |
| 754 | pollfds = malloc(max_poll_elements * sizeof (struct pollfd)); |
| 755 | fd_lookup = malloc(max_poll_elements * sizeof (int)); |
| 756 | if (pollfds == NULL || fd_lookup == NULL) { |
| 757 | lwsl_err("Out of memory pollfds=%d\n", max_poll_elements); |
| 758 | return -1; |
| 759 | } |
| 760 | #endif |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 761 | |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 762 | info.iface = iface; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 763 | info.protocols = protocols; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 764 | #ifndef LWS_NO_EXTENSIONS |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 765 | info.extensions = libwebsocket_get_internal_extensions(); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 766 | #endif |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 767 | if (!use_ssl) { |
| 768 | info.ssl_cert_filepath = NULL; |
| 769 | info.ssl_private_key_filepath = NULL; |
| 770 | } else { |
Andy Green | 6c72070 | 2013-05-02 08:15:53 +0800 | [diff] [blame] | 771 | if (strlen(resource_path) > sizeof(cert_path) - 32) { |
| 772 | lwsl_err("resource path too long\n"); |
| 773 | return -1; |
| 774 | } |
| 775 | sprintf(cert_path, "%s/libwebsockets-test-server.pem", |
| 776 | resource_path); |
| 777 | if (strlen(resource_path) > sizeof(key_path) - 32) { |
| 778 | lwsl_err("resource path too long\n"); |
| 779 | return -1; |
| 780 | } |
| 781 | sprintf(key_path, "%s/libwebsockets-test-server.key.pem", |
| 782 | resource_path); |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 783 | |
| 784 | info.ssl_cert_filepath = cert_path; |
| 785 | info.ssl_private_key_filepath = key_path; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 786 | } |
| 787 | info.gid = -1; |
| 788 | info.uid = -1; |
| 789 | info.options = opts; |
| 790 | |
| 791 | context = libwebsocket_create_context(&info); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 792 | if (context == NULL) { |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 793 | lwsl_err("libwebsocket init failed\n"); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 794 | return -1; |
| 795 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 796 | |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 797 | n = 0; |
Andy Green | 08f2c01 | 2013-01-30 08:02:26 +0800 | [diff] [blame] | 798 | while (n >= 0 && !force_exit) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 799 | struct timeval tv; |
| 800 | |
| 801 | gettimeofday(&tv, NULL); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 802 | |
| 803 | /* |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 804 | * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every |
| 805 | * live websocket connection using the DUMB_INCREMENT protocol, |
| 806 | * as soon as it can take more packets (usually immediately) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 807 | */ |
| 808 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 809 | if (((unsigned int)tv.tv_usec - oldus) > 50000) { |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 810 | libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_DUMB_INCREMENT]); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 811 | oldus = tv.tv_usec; |
| 812 | } |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 813 | |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 814 | #ifdef EXTERNAL_POLL |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 815 | |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 816 | /* |
| 817 | * this represents an existing server's single poll action |
| 818 | * which also includes libwebsocket sockets |
| 819 | */ |
| 820 | |
| 821 | n = poll(pollfds, count_pollfds, 50); |
| 822 | if (n < 0) |
| 823 | continue; |
| 824 | |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 825 | |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 826 | if (n) |
| 827 | for (n = 0; n < count_pollfds; n++) |
| 828 | if (pollfds[n].revents) |
| 829 | /* |
| 830 | * returns immediately if the fd does not |
| 831 | * match anything under libwebsockets |
| 832 | * control |
| 833 | */ |
| 834 | if (libwebsocket_service_fd(context, |
| 835 | &pollfds[n]) < 0) |
| 836 | goto done; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 837 | #else |
Andy Green | 0d49c8d | 2013-02-02 23:02:56 +0800 | [diff] [blame] | 838 | /* |
| 839 | * If libwebsockets sockets are all we care about, |
| 840 | * you can use this api which takes care of the poll() |
| 841 | * and looping through finding who needed service. |
| 842 | * |
| 843 | * If no socket needs service, it'll return anyway after |
| 844 | * the number of ms in the second argument. |
| 845 | */ |
| 846 | |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 847 | n = libwebsocket_service(context, 50); |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 848 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 851 | #ifdef EXTERNAL_POLL |
| 852 | done: |
| 853 | #endif |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 854 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 855 | libwebsocket_context_destroy(context); |
| 856 | |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 857 | lwsl_notice("libwebsockets-test-server exited cleanly\n"); |
| 858 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 859 | #ifndef WIN32 |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 860 | closelog(); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 861 | #endif |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 862 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 863 | return 0; |
| 864 | } |