protocol_lws_status.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * libwebsockets-test-server - libwebsockets test implementation
  3. *
  4. * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. *
  9. * The person who associated a work with this deed has dedicated
  10. * the work to the public domain by waiving all of his or her rights
  11. * to the work worldwide under copyright law, including all related
  12. * and neighboring rights, to the extent allowed by law. You can copy,
  13. * modify, distribute and perform the work, even for commercial purposes,
  14. * all without asking permission.
  15. *
  16. * The test apps are intended to be adapted for use in your code, which
  17. * may be proprietary. So unlike the library itself, they are licensed
  18. * Public Domain.
  19. */
  20. #if !defined (LWS_PLUGIN_STATIC)
  21. #define LWS_DLL
  22. #define LWS_INTERNAL
  23. #include "../lib/libwebsockets.h"
  24. #endif
  25. #include <time.h>
  26. #include <string.h>
  27. #ifdef WIN32
  28. #include <io.h>
  29. #include <gettimeofday.h>
  30. #endif
  31. typedef enum {
  32. WALK_NONE,
  33. WALK_INITIAL,
  34. WALK_LIST,
  35. WALK_FINAL
  36. } e_walk;
  37. struct per_session_data__lws_status {
  38. struct per_session_data__lws_status *next;
  39. struct lws *wsi;
  40. time_t time_est;
  41. char user_agent[128];
  42. e_walk walk;
  43. struct per_session_data__lws_status *walk_next;
  44. unsigned char subsequent:1;
  45. unsigned char changed_partway:1;
  46. };
  47. struct per_vhost_data__lws_status {
  48. struct per_session_data__lws_status *live_pss_list;
  49. struct lws_context *context;
  50. struct lws_vhost *vhost;
  51. const struct lws_protocols *protocol;
  52. int count_live_pss;
  53. };
  54. static void
  55. trigger_resend(struct per_vhost_data__lws_status *vhd)
  56. {
  57. struct per_session_data__lws_status *pss = vhd->live_pss_list;
  58. while (pss) {
  59. if (pss->walk == WALK_NONE) {
  60. pss->subsequent = 0;
  61. pss->walk_next = vhd->live_pss_list;
  62. pss->walk = WALK_INITIAL;
  63. } else
  64. pss->changed_partway = 1;
  65. pss = pss->next;
  66. }
  67. lws_callback_on_writable_all_protocol(vhd->context, vhd->protocol);
  68. }
  69. /* lws-status protocol */
  70. int
  71. callback_lws_status(struct lws *wsi, enum lws_callback_reasons reason,
  72. void *user, void *in, size_t len)
  73. {
  74. struct per_session_data__lws_status *pss =
  75. (struct per_session_data__lws_status *)user,
  76. *pss1, *pss2;
  77. struct per_vhost_data__lws_status *vhd =
  78. (struct per_vhost_data__lws_status *)
  79. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  80. lws_get_protocol(wsi));
  81. char buf[LWS_PRE + 384], ip[24], *start = buf + LWS_PRE - 1, *p = start,
  82. *end = buf + sizeof(buf) - 1;
  83. int n, m;
  84. switch (reason) {
  85. case LWS_CALLBACK_PROTOCOL_INIT:
  86. vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
  87. lws_get_protocol(wsi),
  88. sizeof(struct per_vhost_data__lws_status));
  89. vhd->context = lws_get_context(wsi);
  90. vhd->protocol = lws_get_protocol(wsi);
  91. vhd->vhost = lws_get_vhost(wsi);
  92. break;
  93. case LWS_CALLBACK_ESTABLISHED:
  94. /*
  95. * This shows how to stage sending a single ws message in
  96. * multiple fragments. In this case, it lets us trade off
  97. * memory needed to make the data vs time to send it.
  98. */
  99. vhd->count_live_pss++;
  100. pss->next = vhd->live_pss_list;
  101. vhd->live_pss_list = pss;
  102. time(&pss->time_est);
  103. pss->wsi = wsi;
  104. strcpy(pss->user_agent, "unknown");
  105. lws_hdr_copy(wsi, pss->user_agent, sizeof(pss->user_agent),
  106. WSI_TOKEN_HTTP_USER_AGENT);
  107. trigger_resend(vhd);
  108. break;
  109. case LWS_CALLBACK_SERVER_WRITEABLE:
  110. switch (pss->walk) {
  111. case WALK_INITIAL:
  112. n = LWS_WRITE_TEXT | LWS_WRITE_NO_FIN;;
  113. p += lws_snprintf(p, end - p,
  114. "{ \"version\":\"%s\","
  115. " \"hostname\":\"%s\","
  116. " \"wsi\":\"%d\", \"conns\":[",
  117. lws_get_library_version(),
  118. lws_canonical_hostname(vhd->context),
  119. vhd->count_live_pss);
  120. pss->walk = WALK_LIST;
  121. pss->walk_next = vhd->live_pss_list;
  122. break;
  123. case WALK_LIST:
  124. n = LWS_WRITE_CONTINUATION | LWS_WRITE_NO_FIN;
  125. if (!pss->walk_next)
  126. goto walk_final;
  127. if (pss->subsequent)
  128. *p++ = ',';
  129. pss->subsequent = 1;
  130. m = 0;
  131. pss2 = vhd->live_pss_list;
  132. while (pss2) {
  133. if (pss2 == pss->walk_next) {
  134. m = 1;
  135. break;
  136. }
  137. pss2 = pss2->next;
  138. }
  139. if (!m) {
  140. /* our next guy went away */
  141. pss->walk = WALK_FINAL;
  142. pss->changed_partway = 1;
  143. break;
  144. }
  145. lws_get_peer_simple(pss->walk_next->wsi, ip, sizeof(ip));
  146. p += lws_snprintf(p, end - p,
  147. "{\"peer\":\"%s\",\"time\":\"%ld\","
  148. "\"ua\":\"%s\"}",
  149. ip, (unsigned long)pss->walk_next->time_est,
  150. pss->walk_next->user_agent);
  151. pss->walk_next = pss->walk_next->next;
  152. if (!pss->walk_next)
  153. pss->walk = WALK_FINAL;
  154. break;
  155. case WALK_FINAL:
  156. walk_final:
  157. n = LWS_WRITE_CONTINUATION;
  158. p += sprintf(p, "]}");
  159. if (pss->changed_partway) {
  160. pss->subsequent = 0;
  161. pss->walk_next = vhd->live_pss_list;
  162. pss->walk = WALK_INITIAL;
  163. } else
  164. pss->walk = WALK_NONE;
  165. break;
  166. default:
  167. return 0;
  168. }
  169. m = lws_write(wsi, (unsigned char *)start, p - start, n);
  170. if (m < 0) {
  171. lwsl_err("ERROR %d writing to di socket\n", m);
  172. return -1;
  173. }
  174. if (pss->walk != WALK_NONE)
  175. lws_callback_on_writable(wsi);
  176. break;
  177. case LWS_CALLBACK_RECEIVE:
  178. lwsl_notice("pmd test: RX len %d\n", (int)len);
  179. puts(in);
  180. break;
  181. case LWS_CALLBACK_CLOSED:
  182. pss1 = vhd->live_pss_list;
  183. pss2 = NULL;
  184. while (pss1) {
  185. if (pss1 == pss) {
  186. if (pss2)
  187. pss2->next = pss->next;
  188. else
  189. vhd->live_pss_list = pss->next;
  190. break;
  191. }
  192. pss2 = pss1;
  193. pss1 = pss1->next;
  194. }
  195. trigger_resend(vhd);
  196. break;
  197. default:
  198. break;
  199. }
  200. return 0;
  201. }
  202. #define LWS_PLUGIN_PROTOCOL_LWS_STATUS \
  203. { \
  204. "lws-status", \
  205. callback_lws_status, \
  206. sizeof(struct per_session_data__lws_status), \
  207. 512, /* rx buf size must be >= permessage-deflate rx size */ \
  208. }
  209. #if !defined (LWS_PLUGIN_STATIC)
  210. static const struct lws_protocols protocols[] = {
  211. LWS_PLUGIN_PROTOCOL_LWS_STATUS
  212. };
  213. LWS_EXTERN LWS_VISIBLE int
  214. init_protocol_lws_status(struct lws_context *context,
  215. struct lws_plugin_capability *c)
  216. {
  217. if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
  218. lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
  219. c->api_magic);
  220. return 1;
  221. }
  222. c->protocols = protocols;
  223. c->count_protocols = ARRAY_SIZE(protocols);
  224. c->extensions = NULL;
  225. c->count_extensions = 0;
  226. return 0;
  227. }
  228. LWS_EXTERN LWS_VISIBLE int
  229. destroy_protocol_lws_status(struct lws_context *context)
  230. {
  231. return 0;
  232. }
  233. #endif