Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets-test-server - libwebsockets test implementation |
| 3 | * |
| 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 | 3138e44 | 2010-11-01 09:20:48 +0000 | [diff] [blame] | 30 | #define LOCAL_RESOURCE_PATH "/usr/share/libwebsockets-test-server" |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 31 | static int port = 7681; |
| 32 | static int ws_protocol = 76; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 33 | static int use_ssl = 0; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 34 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 35 | struct per_session_data { |
| 36 | int number; |
| 37 | }; |
| 38 | |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 39 | /** |
| 40 | * libwebsocket_callback() - User server actions |
| 41 | * @wsi: Opaque websocket instance pointer |
| 42 | * @reason: The reason for the call |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 43 | * @user: Pointer to per-session user data allocated by library |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 44 | * @in: Pointer used for some callback reasons |
| 45 | * @len: Length set for some callback reasons |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 46 | * |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 47 | * This callback is the way the user controls what is served. All the |
| 48 | * protocol detail is hidden and handled by the library. |
| 49 | * |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 50 | * For each connection / session there is user data allocated that is |
| 51 | * pointed to by "user". You set the size of this user data area when |
| 52 | * the library is initialized with libwebsocket_create_server. |
| 53 | * |
| 54 | * You get an opportunity to initialize user data when called back with |
| 55 | * LWS_CALLBACK_ESTABLISHED reason. |
| 56 | * |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 57 | * LWS_CALLBACK_ESTABLISHED: after successful websocket handshake |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame^] | 58 | * |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 59 | * LWS_CALLBACK_CLOSED: when the websocket session ends |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame^] | 60 | * |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 61 | * LWS_CALLBACK_SEND: opportunity to send to client (you would use |
| 62 | * libwebsocket_write() taking care about the |
| 63 | * special buffer requirements |
| 64 | * LWS_CALLBACK_RECEIVE: data has appeared for the server, it can be |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame^] | 65 | * found at *in and is len bytes long |
| 66 | * |
| 67 | * LWS_CALLBACK_HTTP: an http request has come from a client that is not |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 68 | * asking to upgrade the connection to a websocket |
| 69 | * one. This is a chance to serve http content, |
| 70 | * for example, to send a script to the client |
| 71 | * which will then open the websockets connection. |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame^] | 72 | * @in points to the URI path requested and |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 73 | * libwebsockets_serve_http_file() makes it very |
| 74 | * simple to send back a file to the client. |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame^] | 75 | * |
| 76 | * LWS_CALLBACK_PROTOCOL_FILTER: before the confirmation handshake is sent |
| 77 | * the user callback is given a chance to confirm |
| 78 | * it's OK with the protocol that was requested |
| 79 | * from the client. The protocol string (which |
| 80 | * may be NULL if no protocol header was sent) |
| 81 | * can be found at parameter @in. Return 0 from |
| 82 | * the callback to allow the connection or nonzero |
| 83 | * to abort the connection. |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 84 | */ |
| 85 | |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 86 | static int websocket_callback(struct libwebsocket * wsi, |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 87 | enum libwebsocket_callback_reasons reason, void * user, |
| 88 | void *in, size_t len) |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 89 | { |
| 90 | int n; |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 91 | char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 + |
| 92 | LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 93 | char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING]; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 94 | struct per_session_data * pss = user; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 95 | |
| 96 | switch (reason) { |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 97 | /* |
| 98 | * Websockets session handshake completed and is established |
| 99 | */ |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 100 | case LWS_CALLBACK_ESTABLISHED: |
| 101 | fprintf(stderr, "Websocket connection established\n"); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 102 | pss->number = 0; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 103 | break; |
| 104 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 105 | /* |
| 106 | * Websockets session is closed |
| 107 | */ |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 108 | case LWS_CALLBACK_CLOSED: |
| 109 | fprintf(stderr, "Websocket connection closed\n"); |
| 110 | break; |
| 111 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 112 | /* |
| 113 | * Opportunity for us to send something on the connection |
| 114 | */ |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 115 | case LWS_CALLBACK_SEND: |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 116 | n = sprintf(p, "%d", pss->number++); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 117 | n = libwebsocket_write(wsi, (unsigned char *)p, n, |
| 118 | LWS_WRITE_TEXT); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 119 | if (n < 0) { |
| 120 | fprintf(stderr, "ERROR writing to socket"); |
| 121 | exit(1); |
| 122 | } |
| 123 | break; |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 124 | /* |
| 125 | * Something has arrived for us on the connection, it's len bytes long |
| 126 | * and is available at *in |
| 127 | */ |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 128 | case LWS_CALLBACK_RECEIVE: |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 129 | fprintf(stderr, "Received %d bytes payload\n", (int)len); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 130 | break; |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * The client has asked us for something in normal HTTP mode, |
| 134 | * not websockets mode. Normally it means we want to send |
| 135 | * our script / html to the client, and when that script runs |
| 136 | * it will start up separate websocket connections. |
| 137 | * |
| 138 | * Interpret the URI string to figure out what is needed to send |
| 139 | */ |
| 140 | |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 141 | case LWS_CALLBACK_HTTP: |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 142 | |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame^] | 143 | fprintf(stderr, "serving HTTP URI %s\n", in); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 144 | |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame^] | 145 | if (in && strcmp(in, "/favicon.ico") == 0) { |
Andy Green | 3138e44 | 2010-11-01 09:20:48 +0000 | [diff] [blame] | 146 | if (libwebsockets_serve_http_file(wsi, |
| 147 | LOCAL_RESOURCE_PATH"/favicon.ico", "image/x-icon")) |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 148 | fprintf(stderr, "Failed to send favicon\n"); |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 149 | break; |
| 150 | } |
| 151 | |
| 152 | /* send the script... when it runs it'll start websockets */ |
| 153 | |
Andy Green | 3138e44 | 2010-11-01 09:20:48 +0000 | [diff] [blame] | 154 | if (libwebsockets_serve_http_file(wsi, |
| 155 | LOCAL_RESOURCE_PATH"/test.html", "text/html")) |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 156 | fprintf(stderr, "Failed to send HTTP file\n"); |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 157 | break; |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame^] | 158 | |
| 159 | /* |
| 160 | * This is our chance to choose if we support one of the requested |
| 161 | * protocols or not. in points to the protocol string. Nonzero return |
| 162 | * aborts the connection handshake |
| 163 | */ |
| 164 | |
| 165 | case LWS_CALLBACK_PROTOCOL_FILTER: |
| 166 | if (in == NULL) { |
| 167 | fprintf(stderr, "Client did not request protocol\n"); |
| 168 | /* accept it */ |
| 169 | return 0; |
| 170 | } |
| 171 | fprintf(stderr, "Client requested protocol '%s'\n", in); |
| 172 | /* accept it */ |
| 173 | return 0; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 174 | } |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 175 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 179 | static struct option options[] = { |
| 180 | { "help", no_argument, NULL, 'h' }, |
| 181 | { "port", required_argument, NULL, 'p' }, |
| 182 | { "protocol", required_argument, NULL, 'r' }, |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 183 | { "ssl", no_argument, NULL, 's' }, |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 184 | { NULL, 0, 0, 0 } |
| 185 | }; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 186 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 187 | int main(int argc, char **argv) |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 188 | { |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 189 | int n = 0; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 190 | const char * cert_path = |
| 191 | LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem"; |
| 192 | const char * key_path = |
| 193 | LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem"; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 194 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 195 | fprintf(stderr, "libwebsockets test server\n" |
Andy Green | 6452f1e | 2010-11-11 09:22:22 +0000 | [diff] [blame] | 196 | "(C) Copyright 2010 Andy Green <andy@warmcat.com> " |
| 197 | "licensed under LGPL2.1\n"); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 198 | |
| 199 | while (n >= 0) { |
| 200 | n = getopt_long(argc, argv, "hp:r:", options, NULL); |
| 201 | if (n < 0) |
| 202 | continue; |
| 203 | switch (n) { |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 204 | case 's': |
| 205 | use_ssl = 1; |
| 206 | break; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 207 | case 'p': |
| 208 | port = atoi(optarg); |
| 209 | break; |
| 210 | case 'r': |
| 211 | ws_protocol = atoi(optarg); |
| 212 | break; |
| 213 | case 'h': |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 214 | fprintf(stderr, "Usage: test-server " |
| 215 | "[--port=<p>] [--protocol=<v>]\n"); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 216 | exit(1); |
| 217 | } |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 218 | } |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 219 | |
| 220 | if (!use_ssl) |
| 221 | cert_path = key_path = NULL; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 222 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 223 | if (libwebsocket_create_server(port, websocket_callback, ws_protocol, |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 224 | sizeof(struct per_session_data), |
| 225 | cert_path, key_path, -1, -1) < 0) { |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 226 | fprintf(stderr, "libwebsocket init failed\n"); |
| 227 | return -1; |
| 228 | } |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 229 | |
| 230 | /* just sit there until killed */ |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 231 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 232 | while (1) |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 233 | sleep(10); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 234 | |
| 235 | return 0; |
| 236 | } |