2010-11-08 17:12:19 +00:00
|
|
|
/*
|
|
|
|
* libwebsockets-test-server - libwebsockets test implementation
|
2010-11-13 10:03:47 +00:00
|
|
|
*
|
2011-01-23 16:50:33 +00:00
|
|
|
* Copyright (C) 2010-2011 Andy Green <andy@warmcat.com>
|
2010-11-08 17:12:19 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
2013-02-06 15:27:27 +09:00
|
|
|
#ifdef CMAKE_BUILD
|
|
|
|
#include "lws_config.h"
|
|
|
|
#endif
|
2010-11-08 17:12:19 +00:00
|
|
|
|
2010-10-29 14:15:22 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2010-10-31 07:40:33 +00:00
|
|
|
#include <getopt.h>
|
2010-10-31 11:57:17 +00:00
|
|
|
#include <string.h>
|
2011-01-27 06:26:52 +00:00
|
|
|
#include <sys/time.h>
|
2013-01-15 19:44:33 +08:00
|
|
|
#include <assert.h>
|
2013-02-06 15:26:58 +09:00
|
|
|
#ifndef WIN32
|
2013-01-19 11:32:18 +08:00
|
|
|
#include <syslog.h>
|
2013-02-06 15:26:58 +09:00
|
|
|
#endif
|
2013-01-30 08:02:26 +08:00
|
|
|
#include <signal.h>
|
2010-10-31 11:57:17 +00:00
|
|
|
|
2010-11-01 09:12:17 +00:00
|
|
|
#include "../lib/libwebsockets.h"
|
2010-10-29 14:15:22 +01:00
|
|
|
|
2011-03-07 07:08:07 +00:00
|
|
|
static int close_testing;
|
2013-01-20 20:51:14 +08:00
|
|
|
int max_poll_elements;
|
2013-01-15 19:44:33 +08:00
|
|
|
|
2013-01-20 20:51:14 +08:00
|
|
|
struct pollfd *pollfds;
|
|
|
|
int *fd_lookup;
|
2013-01-17 11:16:15 +08:00
|
|
|
int count_pollfds;
|
2013-01-30 08:02:26 +08:00
|
|
|
int force_exit = 0;
|
2013-01-19 11:11:42 +08:00
|
|
|
|
2010-11-12 13:10:40 +00:00
|
|
|
/*
|
|
|
|
* This demo server shows how to use libwebsockets for one or more
|
|
|
|
* websocket protocols in the same server
|
|
|
|
*
|
|
|
|
* It defines the following websocket protocols:
|
|
|
|
*
|
|
|
|
* dumb-increment-protocol: once the socket is opened, an incrementing
|
|
|
|
* ascii string is sent down it every 50ms.
|
2011-01-27 06:26:52 +00:00
|
|
|
* If you send "reset\n" on the websocket, then
|
|
|
|
* the incrementing number is reset to 0.
|
2010-11-12 14:12:13 +00:00
|
|
|
*
|
|
|
|
* lws-mirror-protocol: copies any received packet to every connection also
|
2011-01-27 06:26:52 +00:00
|
|
|
* using this protocol, including the sender
|
2010-11-12 13:10:40 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-18 15:29:04 +00:00
|
|
|
enum demo_protocols {
|
|
|
|
/* always first */
|
|
|
|
PROTOCOL_HTTP = 0,
|
|
|
|
|
|
|
|
PROTOCOL_DUMB_INCREMENT,
|
|
|
|
PROTOCOL_LWS_MIRROR,
|
|
|
|
|
|
|
|
/* always last */
|
|
|
|
DEMO_PROTOCOL_COUNT
|
|
|
|
};
|
|
|
|
|
2010-11-12 13:10:40 +00:00
|
|
|
|
2012-04-12 11:06:05 +08:00
|
|
|
#define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
|
2010-10-29 14:15:22 +01:00
|
|
|
|
2013-01-16 10:06:28 +08:00
|
|
|
/*
|
|
|
|
* We take a strict whitelist approach to stop ../ attacks
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct serveable {
|
|
|
|
const char *urlpath;
|
|
|
|
const char *mimetype;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct serveable whitelist[] = {
|
|
|
|
{ "/favicon.ico", "image/x-icon" },
|
|
|
|
{ "/libwebsockets.org-logo.png", "image/png" },
|
|
|
|
|
|
|
|
/* last one is the default served if no match */
|
|
|
|
{ "/test.html", "text/html" },
|
|
|
|
};
|
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
/* this protocol server (always the first one) just knows how to do HTTP */
|
2010-10-31 12:42:52 +00:00
|
|
|
|
2012-04-09 15:09:01 +08:00
|
|
|
static int callback_http(struct libwebsocket_context *context,
|
2011-02-14 09:14:25 +00:00
|
|
|
struct libwebsocket *wsi,
|
2010-11-13 10:03:47 +00:00
|
|
|
enum libwebsocket_callback_reasons reason, void *user,
|
2010-11-03 11:13:06 +00:00
|
|
|
void *in, size_t len)
|
2010-10-29 14:15:22 +01:00
|
|
|
{
|
2013-01-19 11:32:18 +08:00
|
|
|
#if 0
|
2011-02-13 08:40:37 +00:00
|
|
|
char client_name[128];
|
|
|
|
char client_ip[128];
|
2013-01-19 11:32:18 +08:00
|
|
|
#endif
|
2013-01-16 10:06:28 +08:00
|
|
|
char buf[256];
|
|
|
|
int n;
|
2013-01-15 12:39:48 +08:00
|
|
|
#ifdef EXTERNAL_POLL
|
2013-01-17 11:16:15 +08:00
|
|
|
int m;
|
2013-01-15 19:44:33 +08:00
|
|
|
int fd = (int)(long)user;
|
2013-01-15 12:39:48 +08:00
|
|
|
#endif
|
2011-02-13 08:40:37 +00:00
|
|
|
|
2010-10-29 14:15:22 +01:00
|
|
|
switch (reason) {
|
2010-10-31 11:57:17 +00:00
|
|
|
case LWS_CALLBACK_HTTP:
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2013-01-16 10:06:28 +08:00
|
|
|
for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++)
|
2013-02-01 08:42:15 +08:00
|
|
|
if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0)
|
2013-01-16 10:06:28 +08:00
|
|
|
break;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2013-01-16 10:06:28 +08:00
|
|
|
sprintf(buf, LOCAL_RESOURCE_PATH"%s", whitelist[n].urlpath);
|
2010-10-31 11:57:17 +00:00
|
|
|
|
2013-01-16 10:06:28 +08:00
|
|
|
if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype))
|
2013-01-22 07:20:08 +08:00
|
|
|
return 1; /* through completion or error, close the socket */
|
2013-01-13 09:53:18 +08:00
|
|
|
|
2013-01-15 13:40:23 +08:00
|
|
|
/*
|
|
|
|
* notice that the sending of the file completes asynchronously,
|
|
|
|
* we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when
|
|
|
|
* it's done
|
|
|
|
*/
|
|
|
|
|
2013-01-16 10:06:28 +08:00
|
|
|
break;
|
2013-01-15 13:40:23 +08:00
|
|
|
|
|
|
|
case LWS_CALLBACK_HTTP_FILE_COMPLETION:
|
2013-01-19 11:32:18 +08:00
|
|
|
// lwsl_info("LWS_CALLBACK_HTTP_FILE_COMPLETION seen\n");
|
2013-01-15 13:40:23 +08:00
|
|
|
/* kill the connection after we sent one file */
|
2013-01-13 09:53:18 +08:00
|
|
|
return 1;
|
2010-11-11 12:28:29 +00:00
|
|
|
|
2011-02-13 08:40:37 +00:00
|
|
|
/*
|
|
|
|
* callback for confirming to continue with client IP appear in
|
|
|
|
* protocol 0 callback since no websocket protocol has been agreed
|
|
|
|
* yet. You can just ignore this if you won't filter on client IP
|
|
|
|
* since the default uhandled callback return is 0 meaning let the
|
|
|
|
* connection continue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
|
2013-01-19 11:32:18 +08:00
|
|
|
#if 0
|
2013-01-30 08:12:20 +08:00
|
|
|
libwebsockets_get_peer_addresses(context, wsi, (int)(long)user, client_name,
|
2011-02-13 08:40:37 +00:00
|
|
|
sizeof(client_name), client_ip, sizeof(client_ip));
|
|
|
|
|
2013-01-19 11:32:18 +08:00
|
|
|
fprintf(stderr, "Received network connect from %s (%s)\n",
|
|
|
|
client_name, client_ip);
|
|
|
|
#endif
|
2011-02-13 08:40:37 +00:00
|
|
|
/* if we returned non-zero from here, we kill the connection */
|
|
|
|
break;
|
|
|
|
|
2013-01-15 12:39:48 +08:00
|
|
|
#ifdef EXTERNAL_POLL
|
|
|
|
/*
|
|
|
|
* callbacks for managing the external poll() array appear in
|
|
|
|
* protocol 0 callback
|
|
|
|
*/
|
|
|
|
|
|
|
|
case LWS_CALLBACK_ADD_POLL_FD:
|
2013-01-17 11:16:15 +08:00
|
|
|
|
2013-01-20 20:51:14 +08:00
|
|
|
if (count_pollfds >= max_poll_elements) {
|
2013-01-19 11:32:18 +08:00
|
|
|
lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n");
|
2013-01-15 12:39:48 +08:00
|
|
|
return 1;
|
2013-01-15 19:44:33 +08:00
|
|
|
}
|
|
|
|
|
2013-01-17 11:16:15 +08:00
|
|
|
fd_lookup[fd] = count_pollfds;
|
|
|
|
pollfds[count_pollfds].fd = fd;
|
|
|
|
pollfds[count_pollfds].events = (int)(long)len;
|
2013-01-15 12:39:48 +08:00
|
|
|
pollfds[count_pollfds++].revents = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_DEL_POLL_FD:
|
2013-01-17 11:16:15 +08:00
|
|
|
if (!--count_pollfds)
|
|
|
|
break;
|
|
|
|
m = fd_lookup[fd];
|
|
|
|
/* have the last guy take up the vacant slot */
|
|
|
|
pollfds[m] = pollfds[count_pollfds];
|
|
|
|
fd_lookup[pollfds[count_pollfds].fd] = m;
|
2013-01-15 12:39:48 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_SET_MODE_POLL_FD:
|
2013-01-17 11:16:15 +08:00
|
|
|
pollfds[fd_lookup[fd]].events |= (int)(long)len;
|
2013-01-15 12:39:48 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLEAR_MODE_POLL_FD:
|
2013-01-17 11:16:15 +08:00
|
|
|
pollfds[fd_lookup[fd]].events &= ~(int)(long)len;
|
2013-01-15 12:39:48 +08:00
|
|
|
break;
|
|
|
|
#endif
|
2013-01-17 11:16:15 +08:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-13 08:54:05 +00:00
|
|
|
/*
|
|
|
|
* this is just an example of parsing handshake headers, you don't need this
|
|
|
|
* in your code unless you will filter allowing connections by the header
|
|
|
|
* content
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_handshake_info(struct lws_tokens *lwst)
|
|
|
|
{
|
|
|
|
int n;
|
2011-05-23 10:00:03 +01:00
|
|
|
static const char *token_names[WSI_TOKEN_COUNT] = {
|
2011-03-02 22:03:47 +00:00
|
|
|
/*[WSI_TOKEN_GET_URI] =*/ "GET URI",
|
|
|
|
/*[WSI_TOKEN_HOST] =*/ "Host",
|
|
|
|
/*[WSI_TOKEN_CONNECTION] =*/ "Connection",
|
|
|
|
/*[WSI_TOKEN_KEY1] =*/ "key 1",
|
|
|
|
/*[WSI_TOKEN_KEY2] =*/ "key 2",
|
|
|
|
/*[WSI_TOKEN_PROTOCOL] =*/ "Protocol",
|
|
|
|
/*[WSI_TOKEN_UPGRADE] =*/ "Upgrade",
|
|
|
|
/*[WSI_TOKEN_ORIGIN] =*/ "Origin",
|
|
|
|
/*[WSI_TOKEN_DRAFT] =*/ "Draft",
|
|
|
|
/*[WSI_TOKEN_CHALLENGE] =*/ "Challenge",
|
2011-02-13 08:54:05 +00:00
|
|
|
|
|
|
|
/* new for 04 */
|
2011-03-02 22:03:47 +00:00
|
|
|
/*[WSI_TOKEN_KEY] =*/ "Key",
|
|
|
|
/*[WSI_TOKEN_VERSION] =*/ "Version",
|
|
|
|
/*[WSI_TOKEN_SWORIGIN] =*/ "Sworigin",
|
2011-02-13 08:54:05 +00:00
|
|
|
|
|
|
|
/* new for 05 */
|
2011-03-02 22:03:47 +00:00
|
|
|
/*[WSI_TOKEN_EXTENSIONS] =*/ "Extensions",
|
2011-02-13 08:54:05 +00:00
|
|
|
|
|
|
|
/* client receives these */
|
2011-03-02 22:03:47 +00:00
|
|
|
/*[WSI_TOKEN_ACCEPT] =*/ "Accept",
|
|
|
|
/*[WSI_TOKEN_NONCE] =*/ "Nonce",
|
|
|
|
/*[WSI_TOKEN_HTTP] =*/ "Http",
|
2011-05-23 10:00:03 +01:00
|
|
|
/*[WSI_TOKEN_MUXURL] =*/ "MuxURL",
|
2011-02-13 08:54:05 +00:00
|
|
|
};
|
2012-04-09 15:09:01 +08:00
|
|
|
|
2011-02-13 08:54:05 +00:00
|
|
|
for (n = 0; n < WSI_TOKEN_COUNT; n++) {
|
|
|
|
if (lwst[n].token == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fprintf(stderr, " %s = %s\n", token_names[n], lwst[n].token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
/* dumb_increment protocol */
|
|
|
|
|
2010-12-19 20:50:01 +00:00
|
|
|
/*
|
|
|
|
* one of these is auto-created for each connection and a pointer to the
|
|
|
|
* appropriate instance is passed to the callback in the user parameter
|
|
|
|
*
|
|
|
|
* for this example protocol we use it to individualize the count for each
|
|
|
|
* connection.
|
|
|
|
*/
|
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
struct per_session_data__dumb_increment {
|
|
|
|
int number;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2012-04-09 15:09:01 +08:00
|
|
|
callback_dumb_increment(struct libwebsocket_context *context,
|
2011-02-14 09:14:25 +00:00
|
|
|
struct libwebsocket *wsi,
|
2010-11-12 10:44:16 +00:00
|
|
|
enum libwebsocket_callback_reasons reason,
|
2010-11-13 10:03:47 +00:00
|
|
|
void *user, void *in, size_t len)
|
2010-11-12 10:44:16 +00:00
|
|
|
{
|
|
|
|
int n;
|
2011-01-27 06:26:52 +00:00
|
|
|
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
|
2010-11-12 10:44:16 +00:00
|
|
|
LWS_SEND_BUFFER_POST_PADDING];
|
2011-01-27 06:26:52 +00:00
|
|
|
unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
|
2013-02-01 08:42:15 +08:00
|
|
|
struct per_session_data__dumb_increment *pss = (struct per_session_data__dumb_increment *)user;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
switch (reason) {
|
2010-11-11 12:28:29 +00:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
case LWS_CALLBACK_ESTABLISHED:
|
2013-01-19 11:32:18 +08:00
|
|
|
lwsl_info("callback_dumb_increment: "
|
2012-04-09 15:09:01 +08:00
|
|
|
"LWS_CALLBACK_ESTABLISHED\n");
|
2010-11-12 10:44:16 +00:00
|
|
|
pss->number = 0;
|
|
|
|
break;
|
|
|
|
|
2013-01-29 17:57:39 +08:00
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
2011-01-27 06:26:52 +00:00
|
|
|
n = sprintf((char *)p, "%d", pss->number++);
|
2010-11-12 10:44:16 +00:00
|
|
|
n = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
|
|
|
|
if (n < 0) {
|
2013-01-19 11:32:18 +08:00
|
|
|
lwsl_err("ERROR %d writing to socket\n", n);
|
2010-11-11 12:52:28 +00:00
|
|
|
return 1;
|
2010-11-11 12:28:29 +00:00
|
|
|
}
|
2011-03-07 07:08:07 +00:00
|
|
|
if (close_testing && pss->number == 50) {
|
2013-01-19 11:32:18 +08:00
|
|
|
lwsl_info("close tesing limit, closing\n");
|
2011-03-07 07:08:07 +00:00
|
|
|
libwebsocket_close_and_free_session(context, wsi,
|
|
|
|
LWS_CLOSE_STATUS_NORMAL);
|
|
|
|
}
|
2010-11-12 10:44:16 +00:00
|
|
|
break;
|
2010-11-11 12:52:28 +00:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
case LWS_CALLBACK_RECEIVE:
|
2013-01-19 11:32:18 +08:00
|
|
|
// fprintf(stderr, "rx %d\n", (int)len);
|
2010-11-12 10:44:16 +00:00
|
|
|
if (len < 6)
|
|
|
|
break;
|
2013-02-01 08:42:15 +08:00
|
|
|
if (strcmp((const char *)in, "reset\n") == 0)
|
2010-11-12 10:44:16 +00:00
|
|
|
pss->number = 0;
|
|
|
|
break;
|
2011-02-13 08:54:05 +00:00
|
|
|
/*
|
|
|
|
* this just demonstrates how to use the protocol filter. If you won't
|
|
|
|
* study and reject connections based on header content, you don't need
|
|
|
|
* to handle this callback
|
|
|
|
*/
|
|
|
|
|
|
|
|
case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
|
|
|
|
dump_handshake_info((struct lws_tokens *)(long)user);
|
|
|
|
/* you could return non-zero here and kill the connection */
|
|
|
|
break;
|
2010-11-12 10:44:16 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2010-10-29 14:15:22 +01:00
|
|
|
}
|
2010-10-31 12:42:52 +00:00
|
|
|
|
2010-10-29 14:15:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
|
2010-11-12 13:10:40 +00:00
|
|
|
/* lws-mirror_protocol */
|
|
|
|
|
2013-01-20 17:08:31 +08:00
|
|
|
#define MAX_MESSAGE_QUEUE 128
|
2011-01-27 06:26:52 +00:00
|
|
|
|
|
|
|
struct per_session_data__lws_mirror {
|
|
|
|
struct libwebsocket *wsi;
|
|
|
|
int ringbuffer_tail;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct a_message {
|
|
|
|
void *payload;
|
|
|
|
size_t len;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct a_message ringbuffer[MAX_MESSAGE_QUEUE];
|
|
|
|
static int ringbuffer_head;
|
|
|
|
|
2013-01-17 16:50:35 +08:00
|
|
|
static struct libwebsocket *wsi_choked[20];
|
|
|
|
static int num_wsi_choked;
|
2011-01-27 06:26:52 +00:00
|
|
|
|
2010-11-12 13:10:40 +00:00
|
|
|
static int
|
2012-04-09 15:09:01 +08:00
|
|
|
callback_lws_mirror(struct libwebsocket_context *context,
|
2011-02-14 09:14:25 +00:00
|
|
|
struct libwebsocket *wsi,
|
2010-11-12 13:10:40 +00:00
|
|
|
enum libwebsocket_callback_reasons reason,
|
2010-11-13 10:03:47 +00:00
|
|
|
void *user, void *in, size_t len)
|
2010-11-12 13:10:40 +00:00
|
|
|
{
|
|
|
|
int n;
|
2013-02-01 08:42:15 +08:00
|
|
|
struct per_session_data__lws_mirror *pss = (struct per_session_data__lws_mirror *)user;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-12 13:10:40 +00:00
|
|
|
switch (reason) {
|
|
|
|
|
2011-01-27 06:26:52 +00:00
|
|
|
case LWS_CALLBACK_ESTABLISHED:
|
2013-01-19 11:32:18 +08:00
|
|
|
lwsl_info("callback_lws_mirror: "
|
2012-04-09 15:09:01 +08:00
|
|
|
"LWS_CALLBACK_ESTABLISHED\n");
|
2011-01-27 06:26:52 +00:00
|
|
|
pss->ringbuffer_tail = ringbuffer_head;
|
|
|
|
pss->wsi = wsi;
|
|
|
|
break;
|
|
|
|
|
2011-03-07 20:47:39 +00:00
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
2011-03-07 07:08:07 +00:00
|
|
|
if (close_testing)
|
|
|
|
break;
|
2013-01-20 17:08:31 +08:00
|
|
|
while (pss->ringbuffer_tail != ringbuffer_head) {
|
2011-01-27 06:26:52 +00:00
|
|
|
|
|
|
|
n = libwebsocket_write(wsi, (unsigned char *)
|
|
|
|
ringbuffer[pss->ringbuffer_tail].payload +
|
|
|
|
LWS_SEND_BUFFER_PRE_PADDING,
|
|
|
|
ringbuffer[pss->ringbuffer_tail].len,
|
|
|
|
LWS_WRITE_TEXT);
|
|
|
|
if (n < 0) {
|
2013-01-19 11:32:18 +08:00
|
|
|
lwsl_err("ERROR %d writing to socket\n", n);
|
2013-01-17 16:50:35 +08:00
|
|
|
return 1;
|
2011-01-27 06:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1))
|
|
|
|
pss->ringbuffer_tail = 0;
|
|
|
|
else
|
|
|
|
pss->ringbuffer_tail++;
|
|
|
|
|
2013-01-17 16:50:35 +08:00
|
|
|
if (((ringbuffer_head - pss->ringbuffer_tail) &
|
2013-01-20 17:08:31 +08:00
|
|
|
(MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15)) {
|
2013-01-17 16:50:35 +08:00
|
|
|
for (n = 0; n < num_wsi_choked; n++)
|
|
|
|
libwebsocket_rx_flow_control(wsi_choked[n], 1);
|
|
|
|
num_wsi_choked = 0;
|
|
|
|
}
|
2013-01-20 17:08:31 +08:00
|
|
|
// lwsl_debug("tx fifo %d\n", (ringbuffer_head - pss->ringbuffer_tail) & (MAX_MESSAGE_QUEUE - 1));
|
2013-01-17 16:50:35 +08:00
|
|
|
|
2013-01-20 17:08:31 +08:00
|
|
|
if (lws_send_pipe_choked(wsi)) {
|
|
|
|
libwebsocket_callback_on_writable(context, wsi);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-01-27 06:26:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-11-12 13:10:40 +00:00
|
|
|
case LWS_CALLBACK_RECEIVE:
|
2011-01-27 06:26:52 +00:00
|
|
|
|
2013-01-17 16:50:35 +08:00
|
|
|
if (((ringbuffer_head - pss->ringbuffer_tail) &
|
|
|
|
(MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 1)) {
|
2013-01-19 11:32:18 +08:00
|
|
|
lwsl_err("dropping!\n");
|
2013-01-17 16:50:35 +08:00
|
|
|
goto choke;
|
|
|
|
}
|
|
|
|
|
2011-01-27 06:26:52 +00:00
|
|
|
if (ringbuffer[ringbuffer_head].payload)
|
|
|
|
free(ringbuffer[ringbuffer_head].payload);
|
|
|
|
|
|
|
|
ringbuffer[ringbuffer_head].payload =
|
|
|
|
malloc(LWS_SEND_BUFFER_PRE_PADDING + len +
|
|
|
|
LWS_SEND_BUFFER_POST_PADDING);
|
|
|
|
ringbuffer[ringbuffer_head].len = len;
|
|
|
|
memcpy((char *)ringbuffer[ringbuffer_head].payload +
|
|
|
|
LWS_SEND_BUFFER_PRE_PADDING, in, len);
|
|
|
|
if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
|
|
|
|
ringbuffer_head = 0;
|
|
|
|
else
|
|
|
|
ringbuffer_head++;
|
|
|
|
|
2013-01-17 16:50:35 +08:00
|
|
|
if (((ringbuffer_head - pss->ringbuffer_tail) &
|
2013-01-20 17:08:31 +08:00
|
|
|
(MAX_MESSAGE_QUEUE - 1)) != (MAX_MESSAGE_QUEUE - 2))
|
2013-01-17 16:50:35 +08:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
choke:
|
|
|
|
if (num_wsi_choked < sizeof wsi_choked / sizeof wsi_choked[0]) {
|
2011-01-27 06:26:52 +00:00
|
|
|
libwebsocket_rx_flow_control(wsi, 0);
|
2013-01-17 16:50:35 +08:00
|
|
|
wsi_choked[num_wsi_choked++] = wsi;
|
|
|
|
}
|
2011-01-27 06:26:52 +00:00
|
|
|
|
2013-01-19 11:32:18 +08:00
|
|
|
// lwsl_debug("rx fifo %d\n", (ringbuffer_head - pss->ringbuffer_tail) & (MAX_MESSAGE_QUEUE - 1));
|
2013-01-17 16:50:35 +08:00
|
|
|
done:
|
2011-01-27 06:26:52 +00:00
|
|
|
libwebsocket_callback_on_writable_all_protocol(
|
|
|
|
libwebsockets_get_protocol(wsi));
|
2010-11-12 13:10:40 +00:00
|
|
|
break;
|
2013-01-17 16:50:35 +08:00
|
|
|
|
2011-02-13 08:54:05 +00:00
|
|
|
/*
|
|
|
|
* this just demonstrates how to use the protocol filter. If you won't
|
|
|
|
* study and reject connections based on header content, you don't need
|
|
|
|
* to handle this callback
|
|
|
|
*/
|
|
|
|
|
|
|
|
case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
|
|
|
|
dump_handshake_info((struct lws_tokens *)(long)user);
|
|
|
|
/* you could return non-zero here and kill the connection */
|
|
|
|
break;
|
2010-11-12 13:10:40 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
/* list of supported protocols and callbacks */
|
|
|
|
|
2010-12-18 15:13:50 +00:00
|
|
|
static struct libwebsocket_protocols protocols[] = {
|
2010-12-19 20:50:01 +00:00
|
|
|
/* first protocol must always be HTTP handler */
|
2011-03-02 22:03:47 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
"http-only", /* name */
|
|
|
|
callback_http, /* callback */
|
2013-02-06 21:10:16 +09:00
|
|
|
0, /* per_session_data_size */
|
|
|
|
0, /* max frame size / rx buffer */
|
2010-11-12 10:44:16 +00:00
|
|
|
},
|
2011-03-02 22:03:47 +00:00
|
|
|
{
|
|
|
|
"dumb-increment-protocol",
|
|
|
|
callback_dumb_increment,
|
|
|
|
sizeof(struct per_session_data__dumb_increment),
|
2013-02-06 21:10:16 +09:00
|
|
|
10,
|
2010-11-12 10:44:16 +00:00
|
|
|
},
|
2011-03-02 22:03:47 +00:00
|
|
|
{
|
|
|
|
"lws-mirror-protocol",
|
|
|
|
callback_lws_mirror,
|
2013-02-06 21:10:16 +09:00
|
|
|
sizeof(struct per_session_data__lws_mirror),
|
|
|
|
4096,
|
2010-11-12 13:10:40 +00:00
|
|
|
},
|
2013-02-06 21:10:16 +09:00
|
|
|
{ NULL, NULL, 0, 0 } /* terminator */
|
2010-11-12 10:44:16 +00:00
|
|
|
};
|
|
|
|
|
2013-01-30 08:02:26 +08:00
|
|
|
void sighandler(int sig)
|
|
|
|
{
|
|
|
|
force_exit = 1;
|
|
|
|
}
|
|
|
|
|
2010-10-31 07:40:33 +00:00
|
|
|
static struct option options[] = {
|
2011-01-27 06:26:52 +00:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2013-01-10 22:28:59 +08:00
|
|
|
{ "debug", required_argument, NULL, 'd' },
|
2011-01-27 06:26:52 +00:00
|
|
|
{ "port", required_argument, NULL, 'p' },
|
|
|
|
{ "ssl", no_argument, NULL, 's' },
|
2012-04-09 15:09:01 +08:00
|
|
|
{ "interface", required_argument, NULL, 'i' },
|
2011-03-07 07:08:07 +00:00
|
|
|
{ "closetest", no_argument, NULL, 'c' },
|
2013-01-21 12:58:04 +08:00
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
2013-01-19 11:11:42 +08:00
|
|
|
{ "daemonize", no_argument, NULL, 'D' },
|
|
|
|
#endif
|
2010-10-31 07:40:33 +00:00
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
2010-10-29 14:15:22 +01:00
|
|
|
|
2010-10-31 07:40:33 +00:00
|
|
|
int main(int argc, char **argv)
|
2010-10-29 14:15:22 +01:00
|
|
|
{
|
2010-10-31 07:40:33 +00:00
|
|
|
int n = 0;
|
2010-11-13 10:03:47 +00:00
|
|
|
const char *cert_path =
|
2010-11-08 20:20:42 +00:00
|
|
|
LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
|
2010-11-13 10:03:47 +00:00
|
|
|
const char *key_path =
|
2010-11-08 20:20:42 +00:00
|
|
|
LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
|
2010-12-19 20:50:01 +00:00
|
|
|
int port = 7681;
|
|
|
|
int use_ssl = 0;
|
2011-01-22 12:51:57 +00:00
|
|
|
struct libwebsocket_context *context;
|
2011-01-30 20:57:25 +00:00
|
|
|
int opts = 0;
|
2011-02-19 08:32:53 +00:00
|
|
|
char interface_name[128] = "";
|
2012-04-09 15:09:01 +08:00
|
|
|
const char *interface = NULL;
|
2013-02-06 15:26:58 +09:00
|
|
|
#ifndef WIN32
|
2013-01-19 11:11:42 +08:00
|
|
|
int syslog_options = LOG_PID | LOG_PERROR;
|
2013-02-06 15:26:58 +09:00
|
|
|
#endif
|
2011-01-27 06:26:52 +00:00
|
|
|
unsigned int oldus = 0;
|
2013-01-29 17:57:39 +08:00
|
|
|
|
2013-01-19 11:32:18 +08:00
|
|
|
int debug_level = 7;
|
2013-01-21 12:58:04 +08:00
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
2013-01-19 11:11:42 +08:00
|
|
|
int daemonize = 0;
|
|
|
|
#endif
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-10-31 07:40:33 +00:00
|
|
|
while (n >= 0) {
|
2013-01-21 09:53:35 +08:00
|
|
|
n = getopt_long(argc, argv, "ci:hsp:d:D", options, NULL);
|
2010-10-31 07:40:33 +00:00
|
|
|
if (n < 0)
|
|
|
|
continue;
|
|
|
|
switch (n) {
|
2013-01-21 12:58:04 +08:00
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
2013-01-19 11:11:42 +08:00
|
|
|
case 'D':
|
|
|
|
daemonize = 1;
|
2013-02-06 15:26:58 +09:00
|
|
|
#ifndef WIN32
|
2013-01-19 11:11:42 +08:00
|
|
|
syslog_options &= ~LOG_PERROR;
|
2013-02-06 15:26:58 +09:00
|
|
|
#endif
|
2013-01-19 11:11:42 +08:00
|
|
|
break;
|
|
|
|
#endif
|
2013-01-10 22:28:59 +08:00
|
|
|
case 'd':
|
2013-01-19 11:32:18 +08:00
|
|
|
debug_level = atoi(optarg);
|
2013-01-10 22:28:59 +08:00
|
|
|
break;
|
2010-11-08 17:03:03 +00:00
|
|
|
case 's':
|
|
|
|
use_ssl = 1;
|
|
|
|
break;
|
2010-10-31 07:40:33 +00:00
|
|
|
case 'p':
|
|
|
|
port = atoi(optarg);
|
|
|
|
break;
|
2011-02-19 08:32:53 +00:00
|
|
|
case 'i':
|
|
|
|
strncpy(interface_name, optarg, sizeof interface_name);
|
|
|
|
interface_name[(sizeof interface_name) - 1] = '\0';
|
|
|
|
interface = interface_name;
|
|
|
|
break;
|
2011-03-07 07:08:07 +00:00
|
|
|
case 'c':
|
|
|
|
close_testing = 1;
|
|
|
|
fprintf(stderr, " Close testing mode -- closes on "
|
|
|
|
"client after 50 dumb increments"
|
|
|
|
"and suppresses lws_mirror spam\n");
|
|
|
|
break;
|
2010-10-31 07:40:33 +00:00
|
|
|
case 'h':
|
2010-10-31 12:42:52 +00:00
|
|
|
fprintf(stderr, "Usage: test-server "
|
2013-01-10 22:28:59 +08:00
|
|
|
"[--port=<p>] [--ssl] "
|
|
|
|
"[-d <log bitfield>]\n");
|
2010-10-31 07:40:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2010-11-08 17:03:03 +00:00
|
|
|
|
2013-02-06 15:26:58 +09:00
|
|
|
#if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
|
2013-01-19 11:11:42 +08:00
|
|
|
/*
|
|
|
|
* normally lock path would be /var/lock/lwsts or similar, to
|
|
|
|
* simplify getting started without having to take care about
|
|
|
|
* permissions or running as root, set to /tmp/.lwsts-lock
|
|
|
|
*/
|
|
|
|
if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
|
|
|
|
fprintf(stderr, "Failed to daemonize\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2013-01-19 13:56:10 +08:00
|
|
|
#endif
|
2013-01-30 08:02:26 +08:00
|
|
|
|
|
|
|
signal(SIGINT, sighandler);
|
|
|
|
|
2013-02-06 15:26:58 +09:00
|
|
|
#ifndef WIN32
|
2013-01-19 11:11:42 +08:00
|
|
|
/* we will only try to log things according to our debug_level */
|
|
|
|
setlogmask(LOG_UPTO (LOG_DEBUG));
|
|
|
|
openlog("lwsts", syslog_options, LOG_DAEMON);
|
2013-02-06 15:26:58 +09:00
|
|
|
#endif
|
2013-01-19 11:32:18 +08:00
|
|
|
|
|
|
|
/* tell the library what debug level to emit and to send it to syslog */
|
|
|
|
lws_set_log_level(debug_level, lwsl_emit_syslog);
|
|
|
|
|
2013-01-19 11:11:42 +08:00
|
|
|
lwsl_notice("libwebsockets test server - "
|
|
|
|
"(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - "
|
2013-01-19 11:32:18 +08:00
|
|
|
"licensed under LGPL2.1\n");
|
2010-11-08 17:03:03 +00:00
|
|
|
if (!use_ssl)
|
|
|
|
cert_path = key_path = NULL;
|
2013-01-20 20:51:14 +08:00
|
|
|
#ifdef EXTERNAL_POLL
|
|
|
|
max_poll_elements = getdtablesize();
|
|
|
|
pollfds = malloc(max_poll_elements * sizeof (struct pollfd));
|
|
|
|
fd_lookup = malloc(max_poll_elements * sizeof (int));
|
|
|
|
if (pollfds == NULL || fd_lookup == NULL) {
|
|
|
|
lwsl_err("Out of memory pollfds=%d\n", max_poll_elements);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2011-02-19 08:32:53 +00:00
|
|
|
context = libwebsocket_create_context(port, interface, protocols,
|
2013-01-20 17:08:31 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
2011-03-06 13:15:32 +00:00
|
|
|
libwebsocket_internal_extensions,
|
2013-01-20 17:08:31 +08:00
|
|
|
#else
|
|
|
|
NULL,
|
|
|
|
#endif
|
2013-01-09 16:25:54 +08:00
|
|
|
cert_path, key_path, NULL, -1, -1, opts, NULL);
|
2011-01-22 12:51:57 +00:00
|
|
|
if (context == NULL) {
|
2013-01-19 11:32:18 +08:00
|
|
|
lwsl_err("libwebsocket init failed\n");
|
2010-10-29 14:15:22 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-07-20 12:58:38 +08:00
|
|
|
n = 0;
|
2013-01-30 08:02:26 +08:00
|
|
|
while (n >= 0 && !force_exit) {
|
2011-01-27 06:26:52 +00:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
2010-12-18 15:13:50 +00:00
|
|
|
|
|
|
|
/*
|
2013-01-29 17:57:39 +08:00
|
|
|
* This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
|
|
|
|
* live websocket connection using the DUMB_INCREMENT protocol,
|
|
|
|
* as soon as it can take more packets (usually immediately)
|
2010-12-18 15:13:50 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-27 06:26:52 +00:00
|
|
|
if (((unsigned int)tv.tv_usec - oldus) > 50000) {
|
2013-01-29 17:57:39 +08:00
|
|
|
libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_DUMB_INCREMENT]);
|
2011-01-27 06:26:52 +00:00
|
|
|
oldus = tv.tv_usec;
|
|
|
|
}
|
2011-01-19 13:11:55 +00:00
|
|
|
|
2013-01-15 12:39:48 +08:00
|
|
|
#ifdef EXTERNAL_POLL
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this represents an existing server's single poll action
|
|
|
|
* which also includes libwebsocket sockets
|
|
|
|
*/
|
2011-01-19 13:11:55 +00:00
|
|
|
|
2013-01-15 12:39:48 +08:00
|
|
|
n = poll(pollfds, count_pollfds, 50);
|
|
|
|
if (n < 0)
|
|
|
|
continue;
|
|
|
|
|
2013-01-15 13:40:23 +08:00
|
|
|
|
2013-01-15 12:39:48 +08:00
|
|
|
if (n)
|
|
|
|
for (n = 0; n < count_pollfds; n++)
|
|
|
|
if (pollfds[n].revents)
|
|
|
|
/*
|
|
|
|
* returns immediately if the fd does not
|
|
|
|
* match anything under libwebsockets
|
|
|
|
* control
|
|
|
|
*/
|
|
|
|
if (libwebsocket_service_fd(context,
|
|
|
|
&pollfds[n]) < 0)
|
|
|
|
goto done;
|
|
|
|
#else
|
2013-02-02 23:02:56 +08:00
|
|
|
/*
|
|
|
|
* If libwebsockets sockets are all we care about,
|
|
|
|
* you can use this api which takes care of the poll()
|
|
|
|
* and looping through finding who needed service.
|
|
|
|
*
|
|
|
|
* If no socket needs service, it'll return anyway after
|
|
|
|
* the number of ms in the second argument.
|
|
|
|
*/
|
|
|
|
|
2012-07-20 12:58:38 +08:00
|
|
|
n = libwebsocket_service(context, 50);
|
2013-01-15 12:39:48 +08:00
|
|
|
#endif
|
2010-12-18 15:13:50 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 12:39:48 +08:00
|
|
|
#ifdef EXTERNAL_POLL
|
|
|
|
done:
|
2011-01-20 10:23:50 +00:00
|
|
|
#endif
|
|
|
|
|
2011-01-23 16:50:33 +00:00
|
|
|
libwebsocket_context_destroy(context);
|
|
|
|
|
2013-01-19 11:32:18 +08:00
|
|
|
lwsl_notice("libwebsockets-test-server exited cleanly\n");
|
|
|
|
|
2013-02-06 15:26:58 +09:00
|
|
|
#ifndef WIN32
|
2013-01-19 11:32:18 +08:00
|
|
|
closelog();
|
2013-02-06 15:26:58 +09:00
|
|
|
#endif
|
2013-01-19 11:32:18 +08:00
|
|
|
|
2010-10-29 14:15:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|