blob: 50a497585927b97dcdc601aa06f6a58a74a7e2e3 [file] [log] [blame]
Andy Greena0da8a82010-11-08 17:12:19 +00001/*
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 Green775c0dd2010-10-29 14:15:22 +010022#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
Andy Greenea71ed12010-10-31 07:40:33 +000025#include <getopt.h>
Andy Green5fd8a5e2010-10-31 11:57:17 +000026#include <string.h>
27
Andy Green7310e9c2010-11-01 09:12:17 +000028#include "../lib/libwebsockets.h"
Andy Green775c0dd2010-10-29 14:15:22 +010029
Andy Green3138e442010-11-01 09:20:48 +000030#define LOCAL_RESOURCE_PATH "/usr/share/libwebsockets-test-server"
Andy Greenea71ed12010-10-31 07:40:33 +000031static int port = 7681;
Andy Green3faa9c72010-11-08 17:03:03 +000032static int use_ssl = 0;
Andy Green775c0dd2010-10-29 14:15:22 +010033
Andy Green4f3943a2010-11-12 10:44:16 +000034/* this protocol server (always the first one) just knows how to do HTTP */
Andy Green251f6fa2010-11-03 11:13:06 +000035
Andy Green4f3943a2010-11-12 10:44:16 +000036static int callback_http(struct libwebsocket * wsi,
Andy Green251f6fa2010-11-03 11:13:06 +000037 enum libwebsocket_callback_reasons reason, void * user,
38 void *in, size_t len)
Andy Green775c0dd2010-10-29 14:15:22 +010039{
Andy Green775c0dd2010-10-29 14:15:22 +010040 switch (reason) {
Andy Green5fd8a5e2010-10-31 11:57:17 +000041 case LWS_CALLBACK_HTTP:
Andy Greena2b0ab02010-11-11 12:28:29 +000042 fprintf(stderr, "serving HTTP URI %s\n", in);
Andy Green3faa9c72010-11-08 17:03:03 +000043
Andy Greena2b0ab02010-11-11 12:28:29 +000044 if (in && strcmp(in, "/favicon.ico") == 0) {
Andy Green3138e442010-11-01 09:20:48 +000045 if (libwebsockets_serve_http_file(wsi,
46 LOCAL_RESOURCE_PATH"/favicon.ico", "image/x-icon"))
Andy Greenab990e42010-10-31 12:42:52 +000047 fprintf(stderr, "Failed to send favicon\n");
Andy Green5fd8a5e2010-10-31 11:57:17 +000048 break;
49 }
50
51 /* send the script... when it runs it'll start websockets */
52
Andy Green3138e442010-11-01 09:20:48 +000053 if (libwebsockets_serve_http_file(wsi,
54 LOCAL_RESOURCE_PATH"/test.html", "text/html"))
Andy Green5fd8a5e2010-10-31 11:57:17 +000055 fprintf(stderr, "Failed to send HTTP file\n");
Andy Green5fd8a5e2010-10-31 11:57:17 +000056 break;
Andy Greena2b0ab02010-11-11 12:28:29 +000057
Andy Green4f3943a2010-11-12 10:44:16 +000058 default:
59 break;
Andy Green775c0dd2010-10-29 14:15:22 +010060 }
Andy Greenab990e42010-10-31 12:42:52 +000061
Andy Green775c0dd2010-10-29 14:15:22 +010062 return 0;
63}
64
Andy Green4f3943a2010-11-12 10:44:16 +000065/* dumb_increment protocol */
66
67struct per_session_data__dumb_increment {
68 int number;
69};
70
71static int
72callback_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
114static 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 Greenea71ed12010-10-31 07:40:33 +0000131static struct option options[] = {
132 { "help", no_argument, NULL, 'h' },
133 { "port", required_argument, NULL, 'p' },
Andy Green3faa9c72010-11-08 17:03:03 +0000134 { "ssl", no_argument, NULL, 's' },
Andy Greenea71ed12010-10-31 07:40:33 +0000135 { NULL, 0, 0, 0 }
136};
Andy Green775c0dd2010-10-29 14:15:22 +0100137
Andy Greenea71ed12010-10-31 07:40:33 +0000138int main(int argc, char **argv)
Andy Green775c0dd2010-10-29 14:15:22 +0100139{
Andy Greenea71ed12010-10-31 07:40:33 +0000140 int n = 0;
Andy Green7c212cc2010-11-08 20:20:42 +0000141 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 Greenea71ed12010-10-31 07:40:33 +0000145
Andy Greenab990e42010-10-31 12:42:52 +0000146 fprintf(stderr, "libwebsockets test server\n"
Andy Green6452f1e2010-11-11 09:22:22 +0000147 "(C) Copyright 2010 Andy Green <andy@warmcat.com> "
148 "licensed under LGPL2.1\n");
Andy Greenea71ed12010-10-31 07:40:33 +0000149
150 while (n >= 0) {
Andy Green4f3943a2010-11-12 10:44:16 +0000151 n = getopt_long(argc, argv, "hp:", options, NULL);
Andy Greenea71ed12010-10-31 07:40:33 +0000152 if (n < 0)
153 continue;
154 switch (n) {
Andy Green3faa9c72010-11-08 17:03:03 +0000155 case 's':
156 use_ssl = 1;
157 break;
Andy Greenea71ed12010-10-31 07:40:33 +0000158 case 'p':
159 port = atoi(optarg);
160 break;
Andy Greenea71ed12010-10-31 07:40:33 +0000161 case 'h':
Andy Greenab990e42010-10-31 12:42:52 +0000162 fprintf(stderr, "Usage: test-server "
Andy Green4f3943a2010-11-12 10:44:16 +0000163 "[--port=<p>] [--ssl]\n");
Andy Greenea71ed12010-10-31 07:40:33 +0000164 exit(1);
165 }
Andy Greenea71ed12010-10-31 07:40:33 +0000166 }
Andy Green3faa9c72010-11-08 17:03:03 +0000167
168 if (!use_ssl)
169 cert_path = key_path = NULL;
Andy Greenea71ed12010-10-31 07:40:33 +0000170
Andy Green4f3943a2010-11-12 10:44:16 +0000171 if (libwebsocket_create_server(port, protocols,
172 cert_path, key_path, -1, -1) < 0) {
Andy Green775c0dd2010-10-29 14:15:22 +0100173 fprintf(stderr, "libwebsocket init failed\n");
174 return -1;
175 }
Andy Green775c0dd2010-10-29 14:15:22 +0100176
177 return 0;
178}