From 4038721baf7a8ab4f87e0d382782206543fe4a8a Mon Sep 17 00:00:00 2001 From: Andy Green Date: Sun, 21 Feb 2016 13:44:07 +0800 Subject: [PATCH] test server add lws_status Signed-off-by: Andy Green --- CMakeLists.txt | 20 +++-- changelog | 7 ++ test-server/test-server-status.c | 154 +++++++++++++++++++++++++++++++ test-server/test-server.c | 7 ++ test-server/test-server.h | 14 +++ test-server/test.html | 189 +++++++++++++++++++++++++++++++-------- 6 files changed, 351 insertions(+), 40 deletions(-) create mode 100644 test-server/test-server-status.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 34fe1ba..ad7c7f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -748,7 +748,7 @@ if (NOT LWS_WITHOUT_TESTAPPS) # # Helper function for adding a test app. # - macro(create_test_app TEST_NAME MAIN_SRC S2 S3 S4 S5) + macro(create_test_app TEST_NAME MAIN_SRC S2 S3 S4 S5 S6) set(TEST_SRCS ${MAIN_SRC}) set(TEST_HDR) @@ -768,6 +768,10 @@ if (NOT LWS_WITHOUT_TESTAPPS) else() list(APPEND TEST_SRCS ${S5}) endif() + if ("${S6}" STREQUAL "") + else() + list(APPEND TEST_SRCS ${S6}) + endif() if (WIN32) list(APPEND TEST_SRCS ${WIN32_HELPERS_PATH}/getopt.c @@ -829,12 +833,14 @@ if (NOT LWS_WITHOUT_TESTAPPS) "test-server/test-server-http.c" "test-server/test-server-dumb-increment.c" "test-server/test-server-mirror.c" + "test-server/test-server-status.c" "test-server/test-server-echogen.c") if (UNIX) create_test_app(test-fuzxy "test-server/fuzxy.c" "" "" "" + "" "") endif() if (UNIX AND NOT ((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))) @@ -843,6 +849,7 @@ if (NOT LWS_WITHOUT_TESTAPPS) "test-server/test-server-http.c" "test-server/test-server-dumb-increment.c" "test-server/test-server-mirror.c" + "test-server/test-server-status.c" "test-server/test-server-echogen.c") endif() if (UNIX AND NOT ((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) @@ -852,6 +859,7 @@ if (NOT LWS_WITHOUT_TESTAPPS) "test-server/test-server-http.c" "test-server/test-server-dumb-increment.c" "test-server/test-server-mirror.c" + "test-server/test-server-status.c" "test-server/test-server-echogen.c") endif() if (UNIX AND NOT ((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) @@ -861,6 +869,7 @@ if (NOT LWS_WITHOUT_TESTAPPS) "test-server/test-server-http.c" "test-server/test-server-dumb-increment.c" "test-server/test-server-mirror.c" + "test-server/test-server-status.c" "test-server/test-server-echogen.c") endif() endif() @@ -873,6 +882,7 @@ if (NOT LWS_WITHOUT_TESTAPPS) "test-server/test-server-http.c" "test-server/test-server-dumb-increment.c" "test-server/test-server-mirror.c" + "test-server/test-server-status.c" "test-server/test-server-echogen.c") # Set defines for this executable only. set_property( @@ -978,27 +988,27 @@ if (NOT LWS_WITHOUT_TESTAPPS) # test-client # if (NOT LWS_WITHOUT_TEST_CLIENT) - create_test_app(test-client "test-server/test-client.c" "" "" "" "") + create_test_app(test-client "test-server/test-client.c" "" "" "" "" "") endif() # # test-fraggle # if (NOT LWS_WITHOUT_TEST_FRAGGLE) - create_test_app(test-fraggle "test-server/test-fraggle.c" "" "" "" "") + create_test_app(test-fraggle "test-server/test-fraggle.c" "" "" "" "" "") endif() # # test-ping # if (NOT LWS_WITHOUT_TEST_PING) - create_test_app(test-ping "test-server/test-ping.c" "" "" "" "") + create_test_app(test-ping "test-server/test-ping.c" "" "" "" "" "") endif() # # test-echo # if (NOT LWS_WITHOUT_TEST_ECHO) - create_test_app(test-echo "test-server/test-echo.c" "" "" "" "") + create_test_app(test-echo "test-server/test-echo.c" "" "" "" "" "") endif() endif(NOT LWS_WITHOUT_CLIENT) diff --git a/changelog b/changelog index a961aa6..b7a31ef 100644 --- a/changelog +++ b/changelog @@ -6,6 +6,13 @@ Fixes 1) libuv one-per-session valgrind leak fixed +Changes +------- + +1) test server html is updated with tabs and a new live server monitoring +feature. Input sanitization added to the js. + + v1.7.1 ====== diff --git a/test-server/test-server-status.c b/test-server/test-server-status.c new file mode 100644 index 0000000..292d68d --- /dev/null +++ b/test-server/test-server-status.c @@ -0,0 +1,154 @@ +/* + * libwebsockets-test-server - libwebsockets test implementation + * + * Copyright (C) 2010-2016 Andy Green + * + * This file is made available under the Creative Commons CC0 1.0 + * Universal Public Domain Dedication. + * + * The person who associated a work with this deed has dedicated + * the work to the public domain by waiving all of his or her rights + * to the work worldwide under copyright law, including all related + * and neighboring rights, to the extent allowed by law. You can copy, + * modify, distribute and perform the work, even for commercial purposes, + * all without asking permission. + * + * The test apps are intended to be adapted for use in your code, which + * may be proprietary. So unlike the library itself, they are licensed + * Public Domain. + */ +#include "test-server.h" + +static unsigned char server_info[1024]; +static int server_info_len; +static int current; +static char cache[16384]; +static int cache_len; +static struct per_session_data__lws_status *list; +static int live_wsi; + + +static void +update_status(struct lws *wsi, struct per_session_data__lws_status *pss) +{ + struct per_session_data__lws_status **pp = &list; + int subsequent = 0; + char *p = cache; + char date[128]; + time_t t; + struct tm tm; + + p += snprintf(p, 512, " { %s, \"wsi\":\"%d\", \"conns\":[", + server_info, live_wsi); + + /* render the list */ + while (*pp) { + t = (*pp)->tv_established.tv_sec; + if (!localtime_r(&t, &tm)) + strcpy(date, "unknown"); + else + strftime(date, sizeof(date), "%F %H:%M %Z", &tm); + if ((p - cache) > (sizeof(cache) - 512)) + break; + if (subsequent) + *p++ = ','; + subsequent = 1; + p += snprintf(p, sizeof(cache) - (p - cache) - 1, + "{\"peer\":\"%s\",\"time\":\"%s\"," + "\"ua\":\"%s\"}", + (*pp)->ip, date, (*pp)->user_agent); + pp = &((*pp)->list); + } + + p += sprintf(p, "]}"); + cache_len = p - cache; + lwsl_err("cache_len %d\n", cache_len); + cache[cache_len] = '\0'; + + /* since we changed the list, increment the 'version' */ + current++; + /* update everyone */ + lws_callback_on_writable_all_protocol(lws_get_context(wsi), + lws_get_protocol(wsi)); +} + + +/* lws-status protocol */ + +int +callback_lws_status(struct lws *wsi, enum lws_callback_reasons reason, + void *user, void *in, size_t len) +{ + struct per_session_data__lws_status *pss = + (struct per_session_data__lws_status *)user, + **pp; + char name[128], rip[128]; + int m; + + switch (reason) { + + case LWS_CALLBACK_PROTOCOL_INIT: + /* + * Prepare the static server info + */ + server_info_len = sprintf((char *)server_info, + "\"version\":\"%s\"," + " \"hostname\":\"%s\"", + lws_get_library_version(), + lws_canonical_hostname( + lws_get_context(wsi))); + break; + + case LWS_CALLBACK_ESTABLISHED: + /* + * we keep a linked list of live pss, so we can walk it + */ + pss->last = 0; + pss->list = list; + list = pss; + live_wsi++; + lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, + sizeof(name), rip, sizeof(rip)); + sprintf(pss->ip, "%s (%s)", name, rip); + gettimeofday(&pss->tv_established, NULL); + strcpy(pss->user_agent, "unknown"); + lws_hdr_copy(wsi, pss->user_agent, sizeof(pss->user_agent), + WSI_TOKEN_HTTP_USER_AGENT); + update_status(wsi, pss); + break; + + case LWS_CALLBACK_SERVER_WRITEABLE: + m = lws_write(wsi, (unsigned char *)cache, cache_len, + LWS_WRITE_TEXT); + if (m < server_info_len) { + lwsl_err("ERROR %d writing to di socket\n", m); + return -1; + } + break; + + case LWS_CALLBACK_CLOSED: + /* + * remove ourselves from live pss list + */ + lwsl_err("CLOSING pss %p ********\n", pss); + + pp = &list; + while (*pp) { + if (*pp == pss) { + *pp = pss->list; + pss->list = NULL; + live_wsi--; + break; + } + pp = &((*pp)->list); + } + + update_status(wsi, pss); + break; + + default: + break; + } + + return 0; +} diff --git a/test-server/test-server.c b/test-server/test-server.c index 6062383..09f7046 100644 --- a/test-server/test-server.c +++ b/test-server/test-server.c @@ -68,6 +68,7 @@ enum demo_protocols { PROTOCOL_DUMB_INCREMENT, PROTOCOL_LWS_MIRROR, PROTOCOL_LWS_ECHOGEN, + PROTOCOL_LWS_STATUS, /* always last */ DEMO_PROTOCOL_COUNT @@ -102,6 +103,12 @@ static struct lws_protocols protocols[] = { sizeof(struct per_session_data__echogen), 128, }, + { + "lws-status", + callback_lws_status, + sizeof(struct per_session_data__lws_status), + 128, + }, { NULL, NULL, 0, 0 } /* terminator */ }; diff --git a/test-server/test-server.h b/test-server/test-server.h index 0055651..cb69d50 100644 --- a/test-server/test-server.h +++ b/test-server/test-server.h @@ -94,6 +94,16 @@ struct per_session_data__echogen { int wr; }; +struct per_session_data__lws_status { + struct per_session_data__lws_status *list; + struct timeval tv_established; + int last; + char ip[270]; + char user_agent[512]; + const char *pos; + int len; +}; + extern int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len); @@ -106,6 +116,10 @@ callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason, extern int callback_lws_echogen(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len); +extern int +callback_lws_status(struct lws *wsi, enum lws_callback_reasons reason, + void *user, void *in, size_t len); + extern void dump_handshake_info(struct lws *wsi); diff --git a/test-server/test.html b/test-server/test.html index afd5bc8..eff2ba1 100644 --- a/test-server/test.html +++ b/test-server/test.html @@ -4,7 +4,7 @@ Minimal Websocket test app