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; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 32 | static int use_ssl = 0; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 33 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 34 | /* 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] | 35 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 36 | static int callback_http(struct libwebsocket * wsi, |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 37 | enum libwebsocket_callback_reasons reason, void * user, |
| 38 | void *in, size_t len) |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 39 | { |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 40 | switch (reason) { |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 41 | case LWS_CALLBACK_HTTP: |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 42 | fprintf(stderr, "serving HTTP URI %s\n", in); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 43 | |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 44 | if (in && strcmp(in, "/favicon.ico") == 0) { |
Andy Green | 3138e44 | 2010-11-01 09:20:48 +0000 | [diff] [blame] | 45 | if (libwebsockets_serve_http_file(wsi, |
| 46 | LOCAL_RESOURCE_PATH"/favicon.ico", "image/x-icon")) |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 47 | fprintf(stderr, "Failed to send favicon\n"); |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 48 | break; |
| 49 | } |
| 50 | |
| 51 | /* send the script... when it runs it'll start websockets */ |
| 52 | |
Andy Green | 3138e44 | 2010-11-01 09:20:48 +0000 | [diff] [blame] | 53 | if (libwebsockets_serve_http_file(wsi, |
| 54 | LOCAL_RESOURCE_PATH"/test.html", "text/html")) |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 55 | fprintf(stderr, "Failed to send HTTP file\n"); |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 56 | break; |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 57 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 58 | default: |
| 59 | break; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 60 | } |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 61 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 65 | /* dumb_increment protocol */ |
| 66 | |
| 67 | struct per_session_data__dumb_increment { |
| 68 | int number; |
| 69 | }; |
| 70 | |
| 71 | static int |
| 72 | callback_dumb_increment(struct libwebsocket * wsi, |
| 73 | enum libwebsocket_callback_reasons reason, |
| 74 | void * user, void *in, size_t len) |
| 75 | { |
| 76 | int n; |
| 77 | char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 + |
| 78 | LWS_SEND_BUFFER_POST_PADDING]; |
| 79 | unsigned char *p = (unsigned char *)&buf[LWS_SEND_BUFFER_PRE_PADDING]; |
| 80 | struct per_session_data__dumb_increment * pss = user; |
| 81 | |
| 82 | switch (reason) { |
| 83 | |
| 84 | case LWS_CALLBACK_ESTABLISHED: |
| 85 | pss->number = 0; |
| 86 | break; |
| 87 | |
| 88 | case LWS_CALLBACK_SEND: |
| 89 | n = sprintf(p, "%d", pss->number++); |
| 90 | n = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT); |
| 91 | if (n < 0) { |
| 92 | fprintf(stderr, "ERROR writing to socket"); |
| 93 | return 1; |
| 94 | } |
| 95 | break; |
| 96 | |
| 97 | case LWS_CALLBACK_RECEIVE: |
| 98 | if (len < 6) |
| 99 | break; |
| 100 | if (strcmp(in, "reset\n") == 0) |
| 101 | pss->number = 0; |
| 102 | break; |
| 103 | |
| 104 | default: |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /* list of supported protocols and callbacks */ |
| 113 | |
| 114 | static const struct libwebsocket_protocols protocols[] = { |
| 115 | { |
| 116 | .name = "http-only", |
| 117 | .callback = callback_http, |
| 118 | .per_session_data_size = 0, |
| 119 | }, |
| 120 | { |
| 121 | .name = "dumb-increment-protocol", |
| 122 | .callback = callback_dumb_increment, |
| 123 | .per_session_data_size = |
| 124 | sizeof(struct per_session_data__dumb_increment), |
| 125 | }, |
| 126 | { /* end of list */ |
| 127 | .callback = NULL |
| 128 | } |
| 129 | }; |
| 130 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 131 | static struct option options[] = { |
| 132 | { "help", no_argument, NULL, 'h' }, |
| 133 | { "port", required_argument, NULL, 'p' }, |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 134 | { "ssl", no_argument, NULL, 's' }, |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 135 | { NULL, 0, 0, 0 } |
| 136 | }; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 137 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 138 | int main(int argc, char **argv) |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 139 | { |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 140 | int n = 0; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 141 | const char * cert_path = |
| 142 | LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem"; |
| 143 | const char * key_path = |
| 144 | LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem"; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 145 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 146 | fprintf(stderr, "libwebsockets test server\n" |
Andy Green | 6452f1e | 2010-11-11 09:22:22 +0000 | [diff] [blame] | 147 | "(C) Copyright 2010 Andy Green <andy@warmcat.com> " |
| 148 | "licensed under LGPL2.1\n"); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 149 | |
| 150 | while (n >= 0) { |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 151 | n = getopt_long(argc, argv, "hp:", options, NULL); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 152 | if (n < 0) |
| 153 | continue; |
| 154 | switch (n) { |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 155 | case 's': |
| 156 | use_ssl = 1; |
| 157 | break; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 158 | case 'p': |
| 159 | port = atoi(optarg); |
| 160 | break; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 161 | case 'h': |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 162 | fprintf(stderr, "Usage: test-server " |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 163 | "[--port=<p>] [--ssl]\n"); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 164 | exit(1); |
| 165 | } |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 166 | } |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 167 | |
| 168 | if (!use_ssl) |
| 169 | cert_path = key_path = NULL; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 170 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 171 | if (libwebsocket_create_server(port, protocols, |
| 172 | cert_path, key_path, -1, -1) < 0) { |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 173 | fprintf(stderr, "libwebsocket init failed\n"); |
| 174 | return -1; |
| 175 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 176 | |
| 177 | return 0; |
| 178 | } |