From f3e9c7347e1e2d630d446d3fd902dbabaddfe6f3 Mon Sep 17 00:00:00 2001
From: Andy Green <andy@warmcat.com>
Date: Thu, 14 Apr 2016 15:07:44 +0800
Subject: [PATCH] json dump vhost

Signed-off-by: Andy Green <andy@warmcat.com>
---
 lib/libwebsockets.c           | 84 +++++++++++++++++++++++++++++++++++
 lib/libwebsockets.h           |  3 ++
 lib/output.c                  |  2 +
 plugins/protocol_lws_status.c |  5 ---
 4 files changed, 89 insertions(+), 5 deletions(-)

diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index 77096da0..1c7c85f8 100644
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -2160,3 +2160,87 @@ lws_set_extension_option(struct lws *wsi, const char *ext_name,
 	return -1;
 }
 #endif
+
+
+LWS_EXTERN int
+lws_json_dump_vhost(const struct lws_vhost *vh, char *buf, int len)
+{
+	static const char * const prots[] = {
+		"http://",
+		"https://",
+		"file://",
+		"cgi://",
+		">http://",
+		">https://",
+	};
+	char *orig = buf, *end = buf + len - 1, first = 1;
+	int n = 0;
+
+	if (len < 100)
+		return 0;
+
+	buf += snprintf(buf, end - buf,
+			"{\n \"name\":\"%s\",\n"
+			" \"port\":\"%d\",\n"
+			" \"use-ssl\":\"%d\",\n"
+			" \"sts\":\"%d\",\n"
+			" \"rx\":\"%lu\",\n"
+			" \"tx\":\"%lu\",\n",
+			vh->name, vh->listen_port,
+#ifdef LWS_OPENSSL_SUPPORT
+			vh->use_ssl,
+#else
+			0,
+#endif
+			!!(vh->options & LWS_SERVER_OPTION_STS),
+			vh->rx, vh->tx
+	);
+
+	if (vh->mount_list) {
+		struct lws_http_mount *m = vh->mount_list;
+
+		buf += snprintf(buf, end - buf, ",\n \"mounts\":[");
+		while (m) {
+			if (!first)
+				buf += snprintf(buf, end - buf, ",");
+			buf += snprintf(buf, end - buf,
+					"\n  {\n   \"mountpoint\":\"%s\",\n"
+					"  \"origin\":\"%s%s\""
+					,
+					m->mountpoint,
+					prots[m->origin_protocol],
+					m->origin);
+			if (m->def)
+				buf += snprintf(buf, end - buf,
+						",\n  \"default\":\"%s\"",
+						m->def);
+			buf += snprintf(buf, end - buf, "\n  }");
+			first = 0;
+			m = m->mount_next;
+		}
+		buf += snprintf(buf, end - buf, "\n ]");
+	}
+
+	if (vh->protocols) {
+		n = 0;
+		first = 1;
+
+		buf += snprintf(buf, end - buf, ",\n \"ws-protocols\":[");
+		while (n < vh->count_protocols) {
+			if (!first)
+				buf += snprintf(buf, end - buf, ",");
+			buf += snprintf(buf, end - buf,
+					"\n  {\n   \"%s\":\{\n"
+					"    \"status\":\"ok\"\n   }\n  }"
+					,
+					vh->protocols[n].name);
+			first = 0;
+			n++;
+		}
+		buf += snprintf(buf, end - buf, "\n ]");
+	}
+
+	buf += snprintf(buf, end - buf, "\n}");
+
+	return buf - orig;
+}
diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h
index 4f3e3032..340c0fca 100644
--- a/lib/libwebsockets.h
+++ b/lib/libwebsockets.h
@@ -1561,6 +1561,9 @@ lws_write_http_mount(struct lws_http_mount *next, struct lws_http_mount **res,
 		     struct lws_protocol_vhost_options *cgienv,
 		     int cgi_timeout);
 
+LWS_EXTERN int
+lws_json_dump_vhost(const struct lws_vhost *vh, char *buf, int len);
+
 LWS_VISIBLE LWS_EXTERN void
 lws_set_log_level(int level,
 		  void (*log_emit_function)(int level, const char *line));
diff --git a/lib/output.c b/lib/output.c
index d23c3281..9edc28ff 100644
--- a/lib/output.c
+++ b/lib/output.c
@@ -111,6 +111,8 @@ int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
 		   wsi->trunc_offset))) {
 		lwsl_err("****** %x Sending new, pending truncated ...\n", wsi);
 		assert(0);
+
+		return -1;
 	}
 
 	m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_TX_DO_SEND, &buf, len);
diff --git a/plugins/protocol_lws_status.c b/plugins/protocol_lws_status.c
index 359fd940..76d2563e 100644
--- a/plugins/protocol_lws_status.c
+++ b/plugins/protocol_lws_status.c
@@ -149,11 +149,6 @@ callback_lws_status(struct lws *wsi, enum lws_callback_reasons reason,
 		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) {
-- 
GitLab