clean: fixes for appveyor warnings
[platform/upstream/libwebsockets.git] / plugins / protocol_lws_server_status.c
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
21 #define LWS_DLL
22 #define LWS_INTERNAL
23 #include "../lib/libwebsockets.h"
24 #include <string.h>
25 #include <stdlib.h>
26 #include <fcntl.h>
27
28 struct lws_ss_load_sample {
29         time_t t;
30         int load_x100;
31 };
32
33 struct lws_ss_filepath {
34         struct lws_ss_filepath *next;
35         char filepath[128];
36 };
37
38 struct lws_ss_dumps {
39         char buf[32768];
40         int length;
41
42         struct lws_ss_load_sample load[64];
43         int load_head;
44         int load_tail;
45 };
46
47 struct per_session_data__server_status {
48         int ver;
49         int pos;
50 };
51
52 struct per_vhost_data__lws_server_status {
53         uv_timer_t timeout_watcher;
54         struct lws_context *context;
55         int hide_vhosts;
56         int tow_flag;
57         struct lws_ss_dumps d;
58         struct lws_ss_filepath *fp;
59 };
60
61 static const struct lws_protocols protocols[1];
62
63 static void
64 uv_timeout_cb_server_status(uv_timer_t *w
65 #if UV_VERSION_MAJOR == 0
66                 , int status
67 #endif
68 )
69 {
70         struct per_vhost_data__lws_server_status *v = lws_container_of(w,
71                         struct per_vhost_data__lws_server_status,
72                         timeout_watcher);
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;
76
77         l = sizeof(v->d.buf) - LWS_PRE - 1;
78
79         n = lws_snprintf(p, l, "{\"i\":");
80         p += n;
81         l -= n;
82
83         n = lws_json_dump_context(v->context, p, l, v->hide_vhosts);
84         p += n;
85         l -= n;
86
87         n = lws_snprintf(p, l, ", \"files\": [");
88         p += n;
89         l -= n;
90
91         fp = v->fp;
92         while (fp) {
93                 if (!first) {
94                         n = lws_snprintf(p, l, ",");
95                         p += n;
96                         l -= n;
97                 }
98                 fd = open(fp->filepath, LWS_O_RDONLY);
99                 if (fd >= 0) {
100                         n = read(fd, contents, sizeof(contents) - 1);
101                         if (n >= 0) {
102                                 contents[n] = '\0';
103                                 lws_json_purify(pure, contents, sizeof(pure));
104
105                                 n = lws_snprintf(p, l, "{\"path\":\"%s\",\"val\":\"%s\"}",
106                                                  fp->filepath, pure);
107                                 p += n;
108                                 l -= n;
109                                 first = 0;
110                         }
111                         close(fd);
112                 }
113                 fp = fp->next;
114         }
115         n = lws_snprintf(p, l, "]}");
116         p += n;
117         l -= n;
118
119         v->d.length = p - (v->d.buf + LWS_PRE);
120
121         lws_callback_on_writable_all_protocol(v->context, &protocols[0]);
122 }
123
124 static int
125 callback_lws_server_status(struct lws *wsi, enum lws_callback_reasons reason,
126                            void *user, void *in, size_t len)
127 {
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;
136
137         switch (reason) {
138
139         case LWS_CALLBACK_ESTABLISHED:
140                 lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
141                 lws_callback_on_writable(wsi);
142                 break;
143
144         case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
145                 if (v)
146                         break;
147
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));
154
155                 fp_old = &v->fp;
156
157                 while (pvo) {
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));
164                                 fp->next = NULL;
165                                 lws_snprintf(&fp->filepath[0], sizeof(fp->filepath), "%s", pvo->value);
166                                 *fp_old = fp;
167                                 fp_old = &fp->next;
168                         }
169                         pvo = pvo->next;
170                 }
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);
175                 break;
176
177         case LWS_CALLBACK_PROTOCOL_DESTROY: /* per vhost */
178         //      lwsl_notice("ss: LWS_CALLBACK_PROTOCOL_DESTROY: v=%p, ctx=%p\n", v, v->context);
179                 if (!v)
180                         break;
181                 uv_timer_stop(&v->timeout_watcher);
182                 uv_close((uv_handle_t *)&v->timeout_watcher, NULL);
183                 fp = v->fp;
184                 while (fp) {
185                         fp1= fp->next;
186                         free(fp);
187                         fp = fp1;
188                 }
189                 break;
190
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);
194                 if (m < 0)
195                         return -1;
196                 break;
197
198         default:
199                 break;
200         }
201
202         return 0;
203 }
204
205 static const struct lws_protocols protocols[] = {
206         {
207                 "lws-server-status",
208                 callback_lws_server_status,
209                 sizeof(struct per_session_data__server_status),
210                 1024,
211         },
212 };
213
214 LWS_EXTERN LWS_VISIBLE int
215 init_protocol_lws_server_status(struct lws_context *context,
216                              struct lws_plugin_capability *c)
217 {
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);
221                 return 1;
222         }
223
224         c->protocols = protocols;
225         c->count_protocols = ARRAY_SIZE(protocols);
226         c->extensions = NULL;
227         c->count_extensions = 0;
228
229         return 0;
230 }
231
232 LWS_EXTERN LWS_VISIBLE int
233 destroy_protocol_lws_server_status(struct lws_context *context)
234 {
235         return 0;
236 }
237