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"
28 struct lws_ss_load_sample {
33 struct lws_ss_filepath {
34 struct lws_ss_filepath *next;
42 struct lws_ss_load_sample load[64];
47 struct per_session_data__server_status {
52 struct per_vhost_data__lws_server_status {
53 uv_timer_t timeout_watcher;
54 struct lws_context *context;
57 struct lws_ss_dumps d;
58 struct lws_ss_filepath *fp;
61 static const struct lws_protocols protocols[1];
64 uv_timeout_cb_server_status(uv_timer_t *w
65 #if UV_VERSION_MAJOR == 0
70 struct per_vhost_data__lws_server_status *v = lws_container_of(w,
71 struct per_vhost_data__lws_server_status,
73 struct lws_ss_filepath *fp;
74 char *p = v->d.buf + LWS_PRE, contents[256], pure[256];
75 int n, l, first = 1, fd;
77 l = sizeof(v->d.buf) - LWS_PRE - 1;
79 n = lws_snprintf(p, l, "{\"i\":");
83 n = lws_json_dump_context(v->context, p, l, v->hide_vhosts);
87 n = lws_snprintf(p, l, ", \"files\": [");
94 n = lws_snprintf(p, l, ",");
98 fd = open(fp->filepath, LWS_O_RDONLY);
100 n = read(fd, contents, sizeof(contents) - 1);
103 lws_json_purify(pure, contents, sizeof(pure));
105 n = lws_snprintf(p, l, "{\"path\":\"%s\",\"val\":\"%s\"}",
115 n = lws_snprintf(p, l, "]}");
119 v->d.length = p - (v->d.buf + LWS_PRE);
121 lws_callback_on_writable_all_protocol(v->context, &protocols[0]);
125 callback_lws_server_status(struct lws *wsi, enum lws_callback_reasons reason,
126 void *user, void *in, size_t len)
128 const struct lws_protocol_vhost_options *pvo =
129 (const struct lws_protocol_vhost_options *)in;
130 struct per_vhost_data__lws_server_status *v =
131 (struct per_vhost_data__lws_server_status *)
132 lws_protocol_vh_priv_get(lws_get_vhost(wsi),
133 lws_get_protocol(wsi));
134 struct lws_ss_filepath *fp, *fp1, **fp_old;
135 int m, period = 1000;
139 case LWS_CALLBACK_ESTABLISHED:
140 lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
141 lws_callback_on_writable(wsi);
144 case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
148 lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
149 lws_get_protocol(wsi),
150 sizeof(struct per_vhost_data__lws_server_status));
151 v = (struct per_vhost_data__lws_server_status *)
152 lws_protocol_vh_priv_get(lws_get_vhost(wsi),
153 lws_get_protocol(wsi));
158 if (!strcmp(pvo->name, "hide-vhosts"))
159 v->hide_vhosts = atoi(pvo->value);
160 if (!strcmp(pvo->name, "update-ms"))
161 period = atoi(pvo->value);
162 if (!strcmp(pvo->name, "filepath")) {
163 fp = malloc(sizeof(*fp));
165 lws_snprintf(&fp->filepath[0], sizeof(fp->filepath), "%s", pvo->value);
171 v->context = lws_get_context(wsi);
172 uv_timer_init(lws_uv_getloop(v->context, 0), &v->timeout_watcher);
173 uv_timer_start(&v->timeout_watcher,
174 uv_timeout_cb_server_status, 2000, period);
177 case LWS_CALLBACK_PROTOCOL_DESTROY: /* per vhost */
178 // lwsl_notice("ss: LWS_CALLBACK_PROTOCOL_DESTROY: v=%p, ctx=%p\n", v, v->context);
181 uv_timer_stop(&v->timeout_watcher);
182 uv_close((uv_handle_t *)&v->timeout_watcher, NULL);
191 case LWS_CALLBACK_SERVER_WRITEABLE:
192 m = lws_write(wsi, (unsigned char *)v->d.buf + LWS_PRE,
193 v->d.length, LWS_WRITE_TEXT);
205 static const struct lws_protocols protocols[] = {
208 callback_lws_server_status,
209 sizeof(struct per_session_data__server_status),
214 LWS_EXTERN LWS_VISIBLE int
215 init_protocol_lws_server_status(struct lws_context *context,
216 struct lws_plugin_capability *c)
218 if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
219 lwsl_err("Plugin API %d, library API %d",
220 LWS_PLUGIN_API_MAGIC, c->api_magic);
224 c->protocols = protocols;
225 c->count_protocols = ARRAY_SIZE(protocols);
226 c->extensions = NULL;
227 c->count_extensions = 0;
232 LWS_EXTERN LWS_VISIBLE int
233 destroy_protocol_lws_server_status(struct lws_context *context)