2 * libwebsockets-test-server - libwebsockets test implementation
4 * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
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.
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
23 #include "../lib/libwebsockets.h"
27 struct lws_ss_load_sample {
32 struct lws_ss_filepath {
33 struct lws_ss_filepath *next;
41 struct lws_ss_load_sample load[64];
46 struct per_session_data__server_status {
51 struct per_vhost_data__lws_server_status {
52 uv_timer_t timeout_watcher;
53 struct lws_context *context;
56 struct lws_ss_dumps d;
57 struct lws_ss_filepath *fp;
60 static const struct lws_protocols protocols[1];
63 uv_timeout_cb_server_status(uv_timer_t *w
64 #if UV_VERSION_MAJOR == 0
69 struct per_vhost_data__lws_server_status *v = lws_container_of(w,
70 struct per_vhost_data__lws_server_status,
72 struct lws_ss_filepath *fp;
73 char *p = v->d.buf + LWS_PRE, contents[256], pure[256];
74 int n, l, first = 1, fd;
76 l = sizeof(v->d.buf) - LWS_PRE - 1;
78 n = lws_snprintf(p, l, "{\"i\":");
82 n = lws_json_dump_context(v->context, p, l, v->hide_vhosts);
86 n = lws_snprintf(p, l, ", \"files\": [");
93 n = lws_snprintf(p, l, ",");
97 fd = open(fp->filepath, LWS_O_RDONLY);
99 n = read(fd, contents, sizeof(contents) - 1);
102 lws_json_purify(pure, contents, sizeof(pure));
104 n = lws_snprintf(p, l, "{\"path\":\"%s\",\"val\":\"%s\"}",
114 n = lws_snprintf(p, l, "]}");
118 v->d.length = p - (v->d.buf + LWS_PRE);
120 lws_callback_on_writable_all_protocol(v->context, &protocols[0]);
124 callback_lws_server_status(struct lws *wsi, enum lws_callback_reasons reason,
125 void *user, void *in, size_t len)
127 const struct lws_protocol_vhost_options *pvo =
128 (const struct lws_protocol_vhost_options *)in;
129 struct per_vhost_data__lws_server_status *v =
130 (struct per_vhost_data__lws_server_status *)
131 lws_protocol_vh_priv_get(lws_get_vhost(wsi),
132 lws_get_protocol(wsi));
133 struct lws_ss_filepath *fp, *fp1, **fp_old;
134 int m, period = 1000;
138 case LWS_CALLBACK_ESTABLISHED:
139 lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
140 lws_callback_on_writable(wsi);
143 case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
147 lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
148 lws_get_protocol(wsi),
149 sizeof(struct per_vhost_data__lws_server_status));
150 v = (struct per_vhost_data__lws_server_status *)
151 lws_protocol_vh_priv_get(lws_get_vhost(wsi),
152 lws_get_protocol(wsi));
157 if (!strcmp(pvo->name, "hide-vhosts"))
158 v->hide_vhosts = atoi(pvo->value);
159 if (!strcmp(pvo->name, "update-ms"))
160 period = atoi(pvo->value);
161 if (!strcmp(pvo->name, "filepath")) {
162 fp = malloc(sizeof(*fp));
164 lws_snprintf(&fp->filepath[0], sizeof(fp->filepath), "%s", pvo->value);
170 v->context = lws_get_context(wsi);
171 uv_timer_init(lws_uv_getloop(v->context, 0), &v->timeout_watcher);
172 uv_timer_start(&v->timeout_watcher,
173 uv_timeout_cb_server_status, 2000, period);
176 case LWS_CALLBACK_PROTOCOL_DESTROY: /* per vhost */
177 // lwsl_notice("ss: LWS_CALLBACK_PROTOCOL_DESTROY: v=%p, ctx=%p\n", v, v->context);
180 uv_timer_stop(&v->timeout_watcher);
181 uv_close((uv_handle_t *)&v->timeout_watcher, NULL);
190 case LWS_CALLBACK_SERVER_WRITEABLE:
191 m = lws_write(wsi, (unsigned char *)v->d.buf + LWS_PRE,
192 v->d.length, LWS_WRITE_TEXT);
204 static const struct lws_protocols protocols[] = {
207 callback_lws_server_status,
208 sizeof(struct per_session_data__server_status),
213 LWS_EXTERN LWS_VISIBLE int
214 init_protocol_lws_server_status(struct lws_context *context,
215 struct lws_plugin_capability *c)
217 if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
218 lwsl_err("Plugin API %d, library API %d",
219 LWS_PLUGIN_API_MAGIC, c->api_magic);
223 c->protocols = protocols;
224 c->count_protocols = ARRAY_SIZE(protocols);
225 c->extensions = NULL;
226 c->count_extensions = 0;
231 LWS_EXTERN LWS_VISIBLE int
232 destroy_protocol_lws_server_status(struct lws_context *context)