1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
/*
* libwebsockets-test-server - libwebsockets test implementation
*
* Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
*
* 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"
#include <time.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 + LWS_PRE, *start = p;
char date[128];
time_t t;
struct tm *ptm;
#ifndef WIN32
struct tm tm;
#endif
p += lws_snprintf(p, 512, " { %s, \"wsi\":\"%d\", \"conns\":[",
server_info, live_wsi);
/* render the list */
while (*pp) {
t = (*pp)->tv_established.tv_sec;
#ifdef WIN32
ptm = localtime(&t);
if (!ptm)
#else
ptm = &tm;
if (!localtime_r(&t, &tm))
#endif
strcpy(date, "unknown");
else
#ifdef WIN32
strftime(date, sizeof(date), "%Y %H:%M %Z", ptm);
#else
strftime(date, sizeof(date), "%F %H:%M %Z", ptm);
#endif
if ((p - start) > (sizeof(cache) - 512))
break;
if (subsequent)
*p++ = ',';
subsequent = 1;
p += lws_snprintf(p, sizeof(cache) - (p - start) - 1,
"{\"peer\":\"%s\",\"time\":\"%s\","
"\"ua\":\"%s\"}",
(*pp)->ip, date, (*pp)->user_agent);
pp = &((*pp)->list);
}
p += sprintf(p, "]}");
cache_len = p - start;
lwsl_err("cache_len %d\n", cache_len);
*p = '\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 + LWS_PRE, 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;
}
|