protocol_lws_server_status.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * libwebsockets-test-server - libwebsockets test implementation
  3. *
  4. * Written in 2010-2019 by Andy Green <[email protected]>
  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. #define LWS_DLL
  21. #define LWS_INTERNAL
  22. #include <libwebsockets.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <stdio.h>
  29. struct lws_ss_filepath {
  30. struct lws_ss_filepath *next;
  31. char filepath[128];
  32. };
  33. struct lws_ss_dumps {
  34. char buf[32768];
  35. int length;
  36. };
  37. struct pss {
  38. int ver;
  39. int pos;
  40. };
  41. struct vhd {
  42. struct lws_context *context;
  43. struct lws_vhost *vhost;
  44. const struct lws_protocols *protocol;
  45. lws_sorted_usec_list_t sul;
  46. int hide_vhosts;
  47. int tow_flag;
  48. int period_s;
  49. int clients;
  50. struct lws_ss_dumps d;
  51. struct lws_ss_filepath *fp;
  52. };
  53. static const struct lws_protocols protocols[1];
  54. static void
  55. update(struct lws_sorted_usec_list *sul)
  56. {
  57. struct vhd *v = lws_container_of(sul, struct vhd, sul);
  58. struct lws_ss_filepath *fp;
  59. char contents[256], pure[256], *p = v->d.buf + LWS_PRE,
  60. *end = v->d.buf + sizeof(v->d.buf) - LWS_PRE - 1;
  61. int n, first = 1, fd;
  62. p += lws_snprintf(p, lws_ptr_diff(end, p), "{\"i\":");
  63. p += lws_json_dump_context(v->context, p, lws_ptr_diff(end, p),
  64. v->hide_vhosts);
  65. p += lws_snprintf(p, lws_ptr_diff(end, p), ", \"files\": [");
  66. fp = v->fp;
  67. while (fp) {
  68. if (!first)
  69. p += lws_snprintf(p, lws_ptr_diff(end, p), ",");
  70. strcpy(pure, "(unknown)");
  71. fd = lws_open(fp->filepath, LWS_O_RDONLY);
  72. if (fd >= 0) {
  73. n = read(fd, contents, sizeof(contents) - 1);
  74. close(fd);
  75. if (n >= 0) {
  76. contents[n] = '\0';
  77. lws_json_purify(pure, contents, sizeof(pure), NULL);
  78. }
  79. }
  80. p += lws_snprintf(p, lws_ptr_diff(end, p),
  81. "{\"path\":\"%s\",\"val\":\"%s\"}",
  82. fp->filepath, pure);
  83. first = 0;
  84. fp = fp->next;
  85. }
  86. p += lws_snprintf(p, lws_ptr_diff(end, p), "]}");
  87. v->d.length = p - (v->d.buf + LWS_PRE);
  88. lws_callback_on_writable_all_protocol(v->context, &protocols[0]);
  89. lws_sul_schedule(v->context, 0, &v->sul, update, v->period_s * LWS_US_PER_SEC);
  90. }
  91. static int
  92. callback_lws_server_status(struct lws *wsi, enum lws_callback_reasons reason,
  93. void *user, void *in, size_t len)
  94. {
  95. const struct lws_protocol_vhost_options *pvo =
  96. (const struct lws_protocol_vhost_options *)in;
  97. struct vhd *v = (struct vhd *)
  98. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  99. lws_get_protocol(wsi));
  100. struct lws_ss_filepath *fp, *fp1, **fp_old;
  101. int m;
  102. switch (reason) {
  103. case LWS_CALLBACK_ESTABLISHED:
  104. lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
  105. if (!v->clients++) {
  106. lws_sul_schedule(lws_get_context(wsi), 0, &v->sul, update, 1);
  107. lwsl_info("%s: starting updates\n", __func__);
  108. }
  109. break;
  110. case LWS_CALLBACK_CLOSED:
  111. if (!--v->clients)
  112. lwsl_notice("%s: stopping updates\n", __func__);
  113. break;
  114. case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
  115. if (v)
  116. break;
  117. lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
  118. lws_get_protocol(wsi),
  119. sizeof(struct vhd));
  120. v = (struct vhd *)lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  121. lws_get_protocol(wsi));
  122. fp_old = &v->fp;
  123. while (pvo) {
  124. if (!strcmp(pvo->name, "hide-vhosts"))
  125. v->hide_vhosts = atoi(pvo->value);
  126. if (!strcmp(pvo->name, "update-ms"))
  127. v->period_s = (atoi(pvo->value) + 500) / 1000;
  128. else
  129. v->period_s = 5;
  130. if (!strcmp(pvo->name, "filepath")) {
  131. fp = malloc(sizeof(*fp));
  132. if (!fp)
  133. return -1;
  134. fp->next = NULL;
  135. lws_snprintf(&fp->filepath[0],
  136. sizeof(fp->filepath), "%s",
  137. pvo->value);
  138. *fp_old = fp;
  139. fp_old = &fp->next;
  140. }
  141. pvo = pvo->next;
  142. }
  143. v->context = lws_get_context(wsi);
  144. v->vhost = lws_get_vhost(wsi);
  145. v->protocol = lws_get_protocol(wsi);
  146. lws_sul_schedule(lws_get_context(wsi), 0, &v->sul, update, 1);
  147. break;
  148. case LWS_CALLBACK_PROTOCOL_DESTROY: /* per vhost */
  149. if (!v)
  150. break;
  151. fp = v->fp;
  152. while (fp) {
  153. fp1= fp->next;
  154. free(fp);
  155. fp = fp1;
  156. }
  157. break;
  158. case LWS_CALLBACK_SERVER_WRITEABLE:
  159. m = lws_write(wsi, (unsigned char *)v->d.buf + LWS_PRE,
  160. v->d.length, LWS_WRITE_TEXT);
  161. if (m < 0)
  162. return -1;
  163. break;
  164. default:
  165. break;
  166. }
  167. return 0;
  168. }
  169. static const struct lws_protocols protocols[] = {
  170. {
  171. "lws-server-status",
  172. callback_lws_server_status,
  173. sizeof(struct pss),
  174. 1024,
  175. },
  176. };
  177. LWS_VISIBLE const lws_plugin_protocol_t lws_server_status = {
  178. .hdr = {
  179. "lws server status",
  180. "lws_protocol_plugin",
  181. LWS_PLUGIN_API_MAGIC
  182. },
  183. .protocols = protocols,
  184. .count_protocols = LWS_ARRAY_SIZE(protocols),
  185. .extensions = NULL,
  186. .count_extensions = 0,
  187. };