xref: /lighttpd1.4/src/mod_status.c (revision 3a8fc4bc)
1 #include "first.h"
2 
3 #include "base.h"
4 #include "fdevent.h"
5 #include "h2.h"
6 #include "http_chunk.h"
7 #include "http_header.h"
8 #include "log.h"
9 
10 #include "plugin.h"
11 
12 #include <sys/types.h>
13 #include "sys-time.h"
14 
15 #include <fcntl.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdio.h>
19 
20 typedef struct {
21     const buffer *config_url;
22     const buffer *status_url;
23     const buffer *statistics_url;
24 
25     int sort;
26 } plugin_config;
27 
28 typedef struct {
29 	PLUGIN_DATA;
30 	plugin_config defaults;
31 	plugin_config conf;
32 
33 	off_t bytes_written_1s;
34 	off_t requests_1s;
35 	off_t abs_traffic_out;
36 	off_t abs_requests;
37 
38 	off_t traffic_out_5s[5];
39 	off_t requests_5s[5];
40 	int ndx_5s;
41 } plugin_data;
42 
INIT_FUNC(mod_status_init)43 INIT_FUNC(mod_status_init) {
44     return ck_calloc(1, sizeof(plugin_data));
45 }
46 
mod_status_merge_config_cpv(plugin_config * const pconf,const config_plugin_value_t * const cpv)47 static void mod_status_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
48     switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
49       case 0: /* status.status-url */
50         pconf->status_url = cpv->v.b;
51         break;
52       case 1: /* status.config-url */
53         pconf->config_url = cpv->v.b;
54         break;
55       case 2: /* status.statistics-url */
56         pconf->statistics_url = cpv->v.b;
57         break;
58       case 3: /* status.enable-sort */
59         pconf->sort = (int)cpv->v.u;
60         break;
61       default:/* should not happen */
62         return;
63     }
64 }
65 
mod_status_merge_config(plugin_config * const pconf,const config_plugin_value_t * cpv)66 static void mod_status_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
67     do {
68         mod_status_merge_config_cpv(pconf, cpv);
69     } while ((++cpv)->k_id != -1);
70 }
71 
mod_status_patch_config(request_st * const r,plugin_data * const p)72 static void mod_status_patch_config(request_st * const r, plugin_data * const p) {
73     p->conf = p->defaults; /* copy small struct instead of memcpy() */
74     /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
75     for (int i = 1, used = p->nconfig; i < used; ++i) {
76         if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
77             mod_status_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
78     }
79 }
80 
SETDEFAULTS_FUNC(mod_status_set_defaults)81 SETDEFAULTS_FUNC(mod_status_set_defaults) {
82     static const config_plugin_keys_t cpk[] = {
83       { CONST_STR_LEN("status.status-url"),
84         T_CONFIG_STRING,
85         T_CONFIG_SCOPE_CONNECTION }
86      ,{ CONST_STR_LEN("status.config-url"),
87         T_CONFIG_STRING,
88         T_CONFIG_SCOPE_CONNECTION }
89      ,{ CONST_STR_LEN("status.statistics-url"),
90         T_CONFIG_STRING,
91         T_CONFIG_SCOPE_CONNECTION }
92      ,{ CONST_STR_LEN("status.enable-sort"),
93         T_CONFIG_BOOL,
94         T_CONFIG_SCOPE_CONNECTION }
95      ,{ NULL, 0,
96         T_CONFIG_UNSET,
97         T_CONFIG_SCOPE_UNSET }
98     };
99 
100     plugin_data * const p = p_d;
101     if (!config_plugin_values_init(srv, p, cpk, "mod_status"))
102         return HANDLER_ERROR;
103 
104     /* process and validate config directives
105      * (init i to 0 if global context; to 1 to skip empty global context) */
106     for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
107         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
108         for (; -1 != cpv->k_id; ++cpv) {
109             switch (cpv->k_id) {
110               case 0: /* status.status-url */
111               case 1: /* status.config-url */
112               case 2: /* status.statistics-url */
113                 if (buffer_is_blank(cpv->v.b))
114                     cpv->v.b = NULL;
115                 break;
116               case 3: /* status.enable-sort */
117                 break;
118               default:/* should not happen */
119                 break;
120             }
121         }
122     }
123 
124     p->defaults.sort = 1;
125 
126     /* initialize p->defaults from global config context */
127     if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
128         const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
129         if (-1 != cpv->k_id)
130             mod_status_merge_config(&p->defaults, cpv);
131     }
132 
133     return HANDLER_GO_ON;
134 }
135 
136 
mod_status_header_append_sort(buffer * b,plugin_data * p,const char * k,size_t klen)137 static void mod_status_header_append_sort(buffer *b, plugin_data *p, const char* k, size_t klen)
138 {
139     p->conf.sort
140       ? buffer_append_str3(b,
141           CONST_STR_LEN("<th class=\"status\"><a href=\"#\" class=\"sortheader\" onclick=\"resort(this);return false;\">"),
142           k, klen,
143           CONST_STR_LEN("<span class=\"sortarrow\">:</span></a></th>\n"))
144       : buffer_append_str3(b,
145           CONST_STR_LEN("<th class=\"status\">"),
146           k, klen,
147           CONST_STR_LEN("</th>\n"));
148 }
149 
mod_status_get_multiplier(buffer * b,double avg,int size)150 static void mod_status_get_multiplier(buffer *b, double avg, int size) {
151     char unit[] = "  ";
152 
153     if (avg > size) { avg /= size; unit[1] = 'k'; }
154     if (avg > size) { avg /= size; unit[1] = 'M'; }
155     if (avg > size) { avg /= size; unit[1] = 'G'; }
156     if (avg > size) { avg /= size; unit[1] = 'T'; }
157     if (avg > size) { avg /= size; unit[1] = 'P'; }
158     if (avg > size) { avg /= size; unit[1] = 'E'; }
159     if (avg > size) { avg /= size; unit[1] = 'Z'; }
160     if (avg > size) { avg /= size; unit[1] = 'Y'; }
161 
162     if (size == 1000) {
163         buffer_append_int(b, (intmax_t)avg);
164     }
165     else { /* (size == 1024) */
166         char buf[32+1];
167         buffer_append_string_len(b, buf, (size_t)
168                                  snprintf(buf, sizeof(buf), "%.2f", avg));
169     }
170     buffer_append_string_len(b, unit, 2);
171 }
172 
mod_status_html_rtable_r(buffer * const b,const request_st * const r,const unix_time64_t cur_ts)173 static void mod_status_html_rtable_r (buffer * const b, const request_st * const r, const unix_time64_t cur_ts) {
174     buffer_append_str3(b, CONST_STR_LEN("<tr><td class=\"string\">"),
175                           BUF_PTR_LEN(r->dst_addr_buf),
176                           CONST_STR_LEN("</td><td class=\"int\">"));
177 
178     if (r->reqbody_length) {
179         buffer_append_int(b, (r->http_version <= HTTP_VERSION_1_1 || r->h2id)
180                              ? r->reqbody_queue.bytes_in
181                              : http_request_stats_bytes_in(r));
182         buffer_append_char(b, '/');
183         buffer_append_int(b, r->reqbody_length);
184     }
185     else
186         buffer_append_string_len(b, CONST_STR_LEN("0/0"));
187 
188     buffer_append_string_len(b, CONST_STR_LEN("</td><td class=\"int\">"));
189 
190     buffer_append_int(b, r->write_queue.bytes_out);
191     buffer_append_char(b, '/');
192     buffer_append_int(b, r->write_queue.bytes_in);
193 
194     buffer_append_string_len(b, CONST_STR_LEN("</td><td class=\"string\">"));
195 
196     if (http_request_state_is_keep_alive(r))
197         buffer_append_string_len(b, CONST_STR_LEN("keep-alive"));
198     else
199         http_request_state_append(b, r->state);
200 
201     buffer_append_string_len(b, CONST_STR_LEN("</td><td class=\"int\">"));
202 
203     buffer_append_int(b, cur_ts - r->start_hp.tv_sec);
204 
205     buffer_append_string_len(b, CONST_STR_LEN("</td><td class=\"string\">"));
206 
207     if (buffer_is_blank(r->server_name))
208         buffer_append_string_encoded(b, BUF_PTR_LEN(&r->uri.authority), ENCODING_HTML);
209     else
210         buffer_append_string_encoded(b, BUF_PTR_LEN(r->server_name), ENCODING_HTML);
211 
212     buffer_append_string_len(b, CONST_STR_LEN("</td><td class=\"string\">"));
213 
214     if (!buffer_is_blank(&r->uri.path))
215         buffer_append_string_encoded(b, BUF_PTR_LEN(&r->uri.path), ENCODING_HTML);
216 
217     if (!buffer_is_blank(&r->uri.query)) {
218         buffer_append_char(b, '?');
219         buffer_append_string_encoded(b, BUF_PTR_LEN(&r->uri.query), ENCODING_HTML);
220     }
221 
222     if (!buffer_is_blank(&r->target_orig)) {
223         buffer_append_string_len(b, CONST_STR_LEN(" ("));
224         buffer_append_string_encoded(b, BUF_PTR_LEN(&r->target_orig), ENCODING_HTML);
225         buffer_append_char(b, ')');
226     }
227     buffer_append_string_len(b, CONST_STR_LEN("</td><td class=\"string\">"));
228 
229     buffer_append_string_encoded(b, BUF_PTR_LEN(&r->physical.path), ENCODING_HTML);
230 
231     buffer_append_string_len(b, CONST_STR_LEN("</td></tr>\n"));
232 }
233 
mod_status_html_rtable(request_st * const rq,const server * const srv,const unix_time64_t cur_ts)234 static void mod_status_html_rtable (request_st * const rq, const server * const srv, const unix_time64_t cur_ts) {
235     /* connection table and URLs might be large, so double-buffer to aggregate
236      * before sending to chunkqueue, which might be temporary file
237      * (avoid write() per connection) */
238     buffer * const b = rq->tmp_buf;
239     buffer_clear(b);
240     for (const connection *con = srv->conns; con; con = con->next) {
241         const request_st * const r = &con->request;
242         h2con * const h2c = con->h2;
243         { /*(r->http_version <= HTTP_VERSION_1_1 or HTTP/2 stream id 0)*/
244             if (buffer_string_space(b) < 4096) {
245                 http_chunk_append_mem(rq, BUF_PTR_LEN(b));
246                 buffer_clear(b);
247             }
248             mod_status_html_rtable_r(b, r, cur_ts);
249         }
250         if (NULL != h2c) {
251             for (uint32_t j = 0, rused = h2c->rused; j < rused; ++j) {
252                 if (buffer_string_space(b) < 4096) {
253                     http_chunk_append_mem(rq, BUF_PTR_LEN(b));
254                     buffer_clear(b);
255                 }
256                 mod_status_html_rtable_r(b, h2c->r[j], cur_ts);
257             }
258         }
259     }
260     http_chunk_append_mem(rq, BUF_PTR_LEN(b));
261 }
262 
mod_status_handle_server_status_html(server * srv,request_st * const r,plugin_data * p)263 static handler_t mod_status_handle_server_status_html(server *srv, request_st * const r, plugin_data *p) {
264 	buffer * const b = chunkqueue_append_buffer_open(&r->write_queue);
265 	buffer_string_prepare_append(b, 8192-1);/*(status page base HTML is ~5.2k)*/
266 	double avg;
267 	unix_time64_t ts;
268 	const unix_time64_t cur_ts = log_epoch_secs;
269 
270 	int days, hours, mins, seconds;
271 
272 	/*(CON_STATE_CLOSE must be last state in enum connection_state_t)*/
273 	int cstates[CON_STATE_CLOSE+3];
274 	memset(cstates, 0, sizeof(cstates));
275 
276 	buffer_copy_string_len(b, CONST_STR_LEN(
277 				 "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
278 				 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
279 				 "         \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
280 				 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
281 				 " <head>\n"
282 				 "  <title>Status</title>\n"
283 
284 				   "  <style type=\"text/css\">\n"
285 				   "    table.status { border: black solid thin; }\n"
286 				   "    td { white-space: nowrap; }\n"
287 				   "    td.int { background-color: #f0f0f0; text-align: right }\n"
288 				   "    td.string { background-color: #f0f0f0; text-align: left }\n"
289 				   "    th.status { background-color: black; color: white; font-weight: bold; }\n"
290 				   "    a.sortheader { background-color: black; color: white; font-weight: bold; text-decoration: none; display: block; }\n"
291 				   "    span.sortarrow { color: white; text-decoration: none; }\n"
292 				   "  </style>\n"));
293 
294 	if (!buffer_is_blank(&r->uri.query) && 0 == memcmp(r->uri.query.ptr, CONST_STR_LEN("refresh="))) {
295 		/* Note: Refresh is an historical, but non-standard HTTP header
296 		 * References (meta http-equiv="refresh" use is deprecated):
297 		 *   https://www.w3.org/TR/WCAG10-HTML-TECHS/#meta-element
298 		 *   https://www.w3.org/TR/WCAG10-CORE-TECHS/#auto-page-refresh
299 		 *   https://www.w3.org/QA/Tips/reback
300 		 */
301 		const long refresh = strtol(r->uri.query.ptr+sizeof("refresh=")-1, NULL, 10);
302 		if (refresh > 0) {
303 			buffer_append_string_len(b, CONST_STR_LEN("<meta http-equiv=\"refresh\" content=\""));
304 			buffer_append_int(b, refresh < 604800 ? refresh : 604800);
305 			buffer_append_string_len(b, CONST_STR_LEN("\">\n"));
306 		}
307 	}
308 
309 	if (p->conf.sort) {
310 		buffer_append_string_len(b, CONST_STR_LEN(
311 					   "<script type=\"text/javascript\">\n"
312 					   "// <!--\n"
313 					   "var sort_column;\n"
314 					   "var prev_span = null;\n"
315 
316 					   "function get_inner_text(el) {\n"
317 					   " if((typeof el == 'string')||(typeof el == 'undefined'))\n"
318 					   "  return el;\n"
319 					   " if(el.innerText)\n"
320 					   "  return el.innerText;\n"
321 					   " else {\n"
322 					   "  var str = \"\";\n"
323 					   "  var cs = el.childNodes;\n"
324 					   "  var l = cs.length;\n"
325 					   "  for (i=0;i<l;i++) {\n"
326 					   "   if (cs[i].nodeType==1) str += get_inner_text(cs[i]);\n"
327 					   "   else if (cs[i].nodeType==3) str += cs[i].nodeValue;\n"
328 					   "  }\n"
329 					   " }\n"
330 					   " return str;\n"
331 					   "}\n"
332 
333 					   "function sortfn(a,b) {\n"
334 					   " var at = get_inner_text(a.cells[sort_column]);\n"
335 					   " var bt = get_inner_text(b.cells[sort_column]);\n"
336 					   " if (a.cells[sort_column].className == 'int') {\n"
337 					   "  return parseInt(at)-parseInt(bt);\n"
338 					   " } else {\n"
339 					   "  aa = at.toLowerCase();\n"
340 					   "  bb = bt.toLowerCase();\n"
341 					   "  if (aa==bb) return 0;\n"
342 					   "  else if (aa<bb) return -1;\n"
343 					   "  else return 1;\n"
344 					   " }\n"
345 					   "}\n"
346 
347 					   "function resort(lnk) {\n"
348 					   " var span = lnk.childNodes[1];\n"
349 					   " var table = lnk.parentNode.parentNode.parentNode.parentNode;\n"
350 					   " var rows = new Array();\n"
351 					   " for (j=1;j<table.rows.length;j++)\n"
352 					   "  rows[j-1] = table.rows[j];\n"
353 					   " sort_column = lnk.parentNode.cellIndex;\n"
354 					   " rows.sort(sortfn);\n"
355 
356 					   " if (prev_span != null) prev_span.innerHTML = '';\n"
357 					   " if (span.getAttribute('sortdir')=='down') {\n"
358 					   "  span.innerHTML = '&uarr;';\n"
359 					   "  span.setAttribute('sortdir','up');\n"
360 					   "  rows.reverse();\n"
361 					   " } else {\n"
362 					   "  span.innerHTML = '&darr;';\n"
363 					   "  span.setAttribute('sortdir','down');\n"
364 					   " }\n"
365 					   " for (i=0;i<rows.length;i++)\n"
366 					   "  table.tBodies[0].appendChild(rows[i]);\n"
367 					   " prev_span = span;\n"
368 					   "}\n"
369 					   "// -->\n"
370 					   "</script>\n"));
371 	}
372 
373 	buffer_append_string_len(b, CONST_STR_LEN(
374 				 " </head>\n"
375 				 "<body>\n"));
376 
377 
378 
379 	/* connection listing */
380 	buffer_append_string_len(b,
381 	                      CONST_STR_LEN("<h1>Server-Status"));
382 	if (r->conf.server_tag)
383 		buffer_append_str3(b, CONST_STR_LEN(
384 			      " ("),
385 	                      BUF_PTR_LEN(r->conf.server_tag),
386 	                      CONST_STR_LEN(
387 			      ")"));
388 	buffer_append_string_len(b,
389 	                      CONST_STR_LEN("</h1>"
390 	                                    "<table summary=\"status\" class=\"status\">"
391 	                                    "<tr><td>Hostname</td><td class=\"string\">"));
392 	buffer_append_string_encoded(b, BUF_PTR_LEN(&r->uri.authority), ENCODING_HTML);
393 	if (!buffer_is_blank(r->server_name) && r->server_name != &r->uri.authority) {
394 		buffer_append_string_len(b, CONST_STR_LEN(" ("));
395 		buffer_append_string_encoded(b, BUF_PTR_LEN(r->server_name), ENCODING_HTML);
396 		buffer_append_char(b, ')');
397 	}
398 	buffer_append_string_len(b, CONST_STR_LEN("</td></tr>\n"
399 	                                          "<tr><td>Uptime</td><td class=\"string\">"));
400 
401 	ts = cur_ts - srv->startup_ts;
402 
403 	days = ts / (60 * 60 * 24);
404 	ts %= (60 * 60 * 24);
405 
406 	hours = ts / (60 * 60);
407 	ts %= (60 * 60);
408 
409 	mins = ts / (60);
410 	seconds = ts % (60);
411 
412 	if (days) {
413 		buffer_append_int(b, days);
414 		buffer_append_string_len(b, CONST_STR_LEN(" days "));
415 	}
416 
417 	if (hours) {
418 		buffer_append_int(b, hours);
419 		buffer_append_string_len(b, CONST_STR_LEN(" hours "));
420 	}
421 
422 	if (mins) {
423 		buffer_append_int(b, mins);
424 		buffer_append_string_len(b, CONST_STR_LEN(" min "));
425 	}
426 
427 	buffer_append_int(b, seconds);
428 	buffer_append_string_len(b, CONST_STR_LEN(" s"
429 	                                          "</td></tr>\n"
430 	                                          "<tr><td>Started at</td><td class=\"string\">"));
431 
432 	ts = srv->startup_ts;
433 
434 	struct tm tm;
435 	buffer_append_strftime(b, "%F %T", localtime64_r(&ts, &tm));
436 	buffer_append_string_len(b, CONST_STR_LEN("</td></tr>\n"
437 	                                          "<tr><th colspan=\"2\">absolute (since start)</th></tr>\n"
438 	                                          "<tr><td>Requests</td><td class=\"string\">"));
439 	avg = (double)p->abs_requests;
440 	mod_status_get_multiplier(b, avg, 1000);
441 	buffer_append_string_len(b, CONST_STR_LEN("req</td></tr>\n"
442 	                                          "<tr><td>Traffic</td><td class=\"string\">"));
443 	avg = (double)p->abs_traffic_out;
444 	mod_status_get_multiplier(b, avg, 1024);
445 	buffer_append_string_len(b, CONST_STR_LEN("byte</td></tr>\n"
446 	                                          "<tr><th colspan=\"2\">average (since start)</th></tr>\n"
447 	                                          "<tr><td>Requests</td><td class=\"string\">"));
448 	avg = (double)p->abs_requests / (cur_ts - srv->startup_ts);
449 	mod_status_get_multiplier(b, avg, 1000);
450 	buffer_append_string_len(b, CONST_STR_LEN("req/s</td></tr>\n"
451 	                                          "<tr><td>Traffic</td><td class=\"string\">"));
452 	avg = (double)p->abs_traffic_out / (cur_ts - srv->startup_ts);
453 	mod_status_get_multiplier(b, avg, 1024);
454 	buffer_append_string_len(b, CONST_STR_LEN("byte/s</td></tr>\n"
455 	                                          "<tr><th colspan=\"2\">average (5s sliding average)</th></tr>\n"));
456 
457 	avg = (double)(p->requests_5s[0]
458 	             + p->requests_5s[1]
459 	             + p->requests_5s[2]
460 	             + p->requests_5s[3]
461 	             + p->requests_5s[4]);
462 	avg /= 5;
463 	buffer_append_string_len(b, CONST_STR_LEN("<tr><td>Requests</td><td class=\"string\">"));
464 	mod_status_get_multiplier(b, avg, 1000);
465 	buffer_append_string_len(b, CONST_STR_LEN("req/s</td></tr>\n"));
466 
467 	avg = (double)(p->traffic_out_5s[0]
468 	             + p->traffic_out_5s[1]
469 	             + p->traffic_out_5s[2]
470 	             + p->traffic_out_5s[3]
471 	             + p->traffic_out_5s[4]);
472 	avg /= 5;
473 	buffer_append_string_len(b, CONST_STR_LEN("<tr><td>Traffic</td><td class=\"string\">"));
474 	mod_status_get_multiplier(b, avg, 1024);
475 	buffer_append_string_len(b, CONST_STR_LEN("byte/s</td></tr>\n"
476 	                                          "</table>\n"
477 	                                          "<hr />\n<pre>\n"
478 	                                          "<b>"));
479 	buffer_append_int(b, srv->srvconf.max_conns - srv->lim_conns);
480 	buffer_append_string_len(b, CONST_STR_LEN(" connections</b>\n"));
481 
482 	int per_line = 50;
483 	char *s = buffer_extend(b, srv->srvconf.max_conns - srv->lim_conns
484 	                         +(srv->srvconf.max_conns - srv->lim_conns)/50);
485 	for (const connection *c = srv->conns; c; c = c->next) {
486 		const request_st * const cr = &c->request;
487 		if (http_con_state_is_keep_alive(c)) {
488 			*s++ = 'k';
489 			++cstates[CON_STATE_CLOSE+2];
490 		} else {
491 			*s++ = *(http_request_state_short(cr->state));
492 			++cstates[(cr->state <= CON_STATE_CLOSE ? cr->state : CON_STATE_CLOSE+1)];
493 		}
494 
495 		if (0 == --per_line) {
496 			per_line = 50;
497 			*s++ = '\n';
498 		}
499 	}
500 	buffer_append_string_len(b, CONST_STR_LEN("\n\n<table>\n"
501 	                                          "<tr><td style=\"text-align:right\">"));
502 	buffer_append_int(b, cstates[CON_STATE_CLOSE+2]);
503 	buffer_append_string_len(b, CONST_STR_LEN("<td>&nbsp;&nbsp;k = keep-alive</td></tr>\n"));
504 	for (int j = 0; j < CON_STATE_CLOSE+2; ++j) {
505 		/*(skip "unknown" state if there are none; there should not be any unknown)*/
506 		if (0 == cstates[j] && j == CON_STATE_CLOSE+1) continue;
507 		buffer_append_string_len(b, CONST_STR_LEN("<tr><td style=\"text-align:right\">"));
508 		buffer_append_int(b, cstates[j]);
509 		buffer_append_str3(b, CONST_STR_LEN("</td><td>&nbsp;&nbsp;"),
510 		                      http_request_state_short(j), 1,
511 		                      CONST_STR_LEN(" = "));
512 		http_request_state_append(b, j);
513 		buffer_append_string_len(b, CONST_STR_LEN("</td></tr>\n"));
514 	}
515 	buffer_append_string_len(b, CONST_STR_LEN(
516 	  "</table>\n"
517 	  "</pre><hr />\n<h2>Connections</h2>\n"
518 	  "<table summary=\"status\" class=\"status\">\n"
519 	  "<tr>"));
520 	mod_status_header_append_sort(b, p, CONST_STR_LEN("Client IP"));
521 	mod_status_header_append_sort(b, p, CONST_STR_LEN("Read"));
522 	mod_status_header_append_sort(b, p, CONST_STR_LEN("Written"));
523 	mod_status_header_append_sort(b, p, CONST_STR_LEN("State"));
524 	mod_status_header_append_sort(b, p, CONST_STR_LEN("Time"));
525 	mod_status_header_append_sort(b, p, CONST_STR_LEN("Host"));
526 	mod_status_header_append_sort(b, p, CONST_STR_LEN("URI"));
527 	mod_status_header_append_sort(b, p, CONST_STR_LEN("File"));
528 	buffer_append_string_len(b, CONST_STR_LEN("</tr>\n"));
529 
530 	chunkqueue_append_buffer_commit(&r->write_queue);
531 	/* connection table might be large, so buffer separately */
532 
533 	mod_status_html_rtable(r, srv, cur_ts);
534 
535 	http_chunk_append_mem(r, CONST_STR_LEN(
536 		      "</table>\n"
537 		      "</body>\n"
538 		      "</html>\n"
539 		      ));
540 
541 	http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
542 
543 	return 0;
544 }
545 
546 
mod_status_handle_server_status_text(server * srv,request_st * const r,plugin_data * p)547 static handler_t mod_status_handle_server_status_text(server *srv, request_st * const r, plugin_data *p) {
548 	buffer *b = chunkqueue_append_buffer_open(&r->write_queue);
549 
550 	/* output total number of requests */
551 	buffer_append_string_len(b, CONST_STR_LEN("Total Accesses: "));
552 	buffer_append_int(b, (intmax_t)p->abs_requests);
553 
554 	buffer_append_string_len(b, CONST_STR_LEN("\nTotal kBytes: "));
555 	buffer_append_int(b, (intmax_t)(p->abs_traffic_out / 1024));
556 
557 	buffer_append_string_len(b, CONST_STR_LEN("\nUptime: "));
558 	buffer_append_int(b, log_epoch_secs - srv->startup_ts);
559 
560 	buffer_append_string_len(b, CONST_STR_LEN("\nBusyServers: "));
561 	buffer_append_int(b, srv->srvconf.max_conns - srv->lim_conns);
562 
563 	buffer_append_string_len(b, CONST_STR_LEN("\nIdleServers: "));
564 	buffer_append_int(b, srv->lim_conns); /*(could omit)*/
565 
566 	buffer_append_string_len(b, CONST_STR_LEN("\nScoreboard: "));
567 	char *s = buffer_extend(b, srv->srvconf.max_conns+1);
568 	for (const connection *c = srv->conns; c; c = c->next)
569 		*s++ = *(http_con_state_short(c));
570 	memset(s, '_', srv->lim_conns); /*(could omit)*/
571 	s += srv->lim_conns;
572 	*s = '\n';
573 
574 	chunkqueue_append_buffer_commit(&r->write_queue);
575 
576 	/* set text/plain output */
577 	http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/plain"));
578 
579 	return 0;
580 }
581 
582 
mod_status_handle_server_status_json(server * srv,request_st * const r,plugin_data * p)583 static handler_t mod_status_handle_server_status_json(server *srv, request_st * const r, plugin_data *p) {
584 	buffer *b = chunkqueue_append_buffer_open(&r->write_queue);
585 	off_t avg;
586 	unsigned int jsonp = 0;
587 
588 	if (buffer_clen(&r->uri.query) >= sizeof("jsonp=")-1
589 	    && 0 == memcmp(r->uri.query.ptr, CONST_STR_LEN("jsonp="))) {
590 		/* not a full parse of query string for multiple parameters,
591 		* not URL-decoding param and not XML-encoding (XSS protection),
592 		* so simply ensure that json function name isalnum() or '_' */
593 		const char *f = r->uri.query.ptr + sizeof("jsonp=")-1;
594 		int len = 0;
595 		while (light_isalnum(f[len]) || f[len] == '_') ++len;
596 		if (0 != len && light_isalpha(f[0]) && f[len] == '\0') {
597 			buffer_append_str2(b, f, len, CONST_STR_LEN("("));
598 			jsonp = 1;
599 		}
600 	}
601 
602 	buffer_append_string_len(b, CONST_STR_LEN("{\n\t\"RequestsTotal\": "));
603 	buffer_append_int(b, (intmax_t)p->abs_requests);
604 
605 	buffer_append_string_len(b, CONST_STR_LEN(",\n\t\"TrafficTotal\": "));
606 	buffer_append_int(b, (intmax_t)(p->abs_traffic_out / 1024));
607 
608 	buffer_append_string_len(b, CONST_STR_LEN(",\n\t\"Uptime\": "));
609 	buffer_append_int(b, log_epoch_secs - srv->startup_ts);
610 
611 	buffer_append_string_len(b, CONST_STR_LEN(",\n\t\"BusyServers\": "));
612 	buffer_append_int(b, srv->srvconf.max_conns - srv->lim_conns);
613 
614 	buffer_append_string_len(b, CONST_STR_LEN(",\n\t\"IdleServers\": "));
615 	buffer_append_int(b, srv->lim_conns); /*(could omit)*/
616 	buffer_append_string_len(b, CONST_STR_LEN(",\n"));
617 
618 	avg = p->requests_5s[0]
619 	    + p->requests_5s[1]
620 	    + p->requests_5s[2]
621 	    + p->requests_5s[3]
622 	    + p->requests_5s[4];
623 	avg /= 5;
624 
625 	buffer_append_string_len(b, CONST_STR_LEN("\t\"RequestAverage5s\":"));
626 	buffer_append_int(b, avg);
627 	buffer_append_string_len(b, CONST_STR_LEN(",\n"));
628 
629 	avg = p->traffic_out_5s[0]
630 	    + p->traffic_out_5s[1]
631 	    + p->traffic_out_5s[2]
632 	    + p->traffic_out_5s[3]
633 	    + p->traffic_out_5s[4];
634 	avg /= 5;
635 
636 	buffer_append_string_len(b, CONST_STR_LEN("\t\"TrafficAverage5s\":"));
637 	buffer_append_int(b, avg / 1024); /* kbps */
638 	buffer_append_string_len(b, CONST_STR_LEN("\n}"));
639 
640 	if (jsonp) buffer_append_string_len(b, CONST_STR_LEN(");"));
641 
642 	chunkqueue_append_buffer_commit(&r->write_queue);
643 	http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE,
644 	                         CONST_STR_LEN("Content-Type"),
645 	                         CONST_STR_LEN("application/javascript"));
646 	return 0;
647 }
648 
649 
mod_status_handle_server_statistics(request_st * const r)650 static handler_t mod_status_handle_server_statistics(request_st * const r) {
651 	http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE,
652 	                         CONST_STR_LEN("Content-Type"),
653 	                         CONST_STR_LEN("text/plain"));
654 
655 	const array * const st = &plugin_stats;
656 	if (0 == st->used) {
657 		/* we have nothing to send */
658 		r->http_status = 204;
659 		r->resp_body_finished = 1;
660 		return HANDLER_FINISHED;
661 	}
662 
663 	buffer * const b = chunkqueue_append_buffer_open(&r->write_queue);
664 	for (uint32_t i = 0; i < st->used; ++i) {
665 		buffer_append_str2(b, BUF_PTR_LEN(&st->sorted[i]->key),
666 		                      CONST_STR_LEN(": "));
667 		buffer_append_int(b, ((data_integer *)st->sorted[i])->value);
668 		buffer_append_char(b, '\n');
669 	}
670 	chunkqueue_append_buffer_commit(&r->write_queue);
671 
672 	r->http_status = 200;
673 	r->resp_body_finished = 1;
674 
675 	return HANDLER_FINISHED;
676 }
677 
678 
mod_status_handle_server_status(request_st * const r,plugin_data * const p)679 static handler_t mod_status_handle_server_status(request_st * const r, plugin_data * const p) {
680 	server * const srv = r->con->srv;
681 	if (buffer_is_equal_string(&r->uri.query, CONST_STR_LEN("auto"))) {
682 		mod_status_handle_server_status_text(srv, r, p);
683 	} else if (buffer_clen(&r->uri.query) >= sizeof("json")-1
684 		   && 0 == memcmp(r->uri.query.ptr, CONST_STR_LEN("json"))) {
685 		mod_status_handle_server_status_json(srv, r, p);
686 	} else {
687 		mod_status_handle_server_status_html(srv, r, p);
688 	}
689 
690 	r->http_status = 200;
691 	r->resp_body_finished = 1;
692 
693 	return HANDLER_FINISHED;
694 }
695 
696 
mod_status_row_append(buffer * b,const char * k,size_t klen,const char * v,size_t vlen)697 static void mod_status_row_append(buffer *b, const char *k, size_t klen, const char *v, size_t vlen)
698 {
699     struct const_iovec iov[] = {
700       { CONST_STR_LEN("   <tr>\n"
701                       "    <td><b>") }
702      ,{ k, klen }
703      ,{ CONST_STR_LEN("</b></td>\n"
704                       "    <td>") }
705      ,{ v, vlen }
706      ,{ CONST_STR_LEN("</td>\n"
707                       "   </tr>\n") }
708     };
709     buffer_append_iovec(b, iov, sizeof(iov)/sizeof(*iov));
710 }
711 
mod_status_header_append(buffer * b,const char * k,size_t klen)712 static void mod_status_header_append(buffer *b, const char *k, size_t klen)
713 {
714     buffer_append_str3(b,
715       CONST_STR_LEN("   <tr>\n"
716 	            "    <th colspan=\"2\">"),
717       k, klen,
718       CONST_STR_LEN("</th>\n"
719 	            "   </tr>\n"));
720 }
721 
mod_status_handle_server_config(request_st * const r)722 static handler_t mod_status_handle_server_config(request_st * const r) {
723 	server * const srv = r->con->srv;
724 	buffer * const tb = r->tmp_buf;
725 	buffer_clear(tb);
726 	for (uint32_t i = 0; i < srv->plugins.used; ++i) {
727 		const char *name = ((plugin **)srv->plugins.ptr)[i]->name;
728 		if (i != 0) {
729 			buffer_append_string_len(tb, CONST_STR_LEN("<br />"));
730 		}
731 		buffer_append_string_len(tb, name, strlen(name));
732 	}
733 
734 	buffer *b = chunkqueue_append_buffer_open(&r->write_queue);
735 
736 	/*(could expand the following into a single buffer_append_iovec(),
737 	 * but this routine is not expected to be under high load)*/
738 
739 	buffer_append_string_len(b, CONST_STR_LEN(
740 			   "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
741 			   "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
742 			   "         \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
743 			   "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
744 			   " <head>\n"
745 			   "  <title>Status</title>\n"
746 			   " </head>\n"
747 			   " <body>\n"));
748 
749 	if (r->conf.server_tag)
750 		buffer_append_str3(b, CONST_STR_LEN(
751 			   "  <h1>"),
752 	                   BUF_PTR_LEN(r->conf.server_tag),
753 	                   CONST_STR_LEN(
754 			   "  </h1>\n"));
755 
756 	buffer_append_string_len(b, CONST_STR_LEN(
757 			   "  <table summary=\"status\" border=\"1\">\n"));
758 
759 	mod_status_header_append(b, CONST_STR_LEN("Server-Features"));
760 #ifdef HAVE_PCRE
761 	mod_status_row_append(b, CONST_STR_LEN("RegEx Conditionals"), CONST_STR_LEN("enabled"));
762 #else
763 	mod_status_row_append(b, CONST_STR_LEN("RegEx Conditionals"), CONST_STR_LEN("disabled - pcre missing"));
764 #endif
765 	mod_status_header_append(b, CONST_STR_LEN("Network Engine"));
766 
767 	mod_status_row_append(b, CONST_STR_LEN("fd-Event-Handler"),
768                                  srv->srvconf.event_handler,
769                                  strlen(srv->srvconf.event_handler));
770 
771 	mod_status_header_append(b, CONST_STR_LEN("Config-File-Settings"));
772 
773 	mod_status_row_append(b, CONST_STR_LEN("Loaded Modules"), BUF_PTR_LEN(tb));
774 
775 	buffer_append_string_len(b, CONST_STR_LEN(
776 		      "  </table>\n"
777 		      " </body>\n"
778 		      "</html>\n"
779 		      ));
780 
781 	chunkqueue_append_buffer_commit(&r->write_queue);
782 
783 	http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
784 
785 	r->http_status = 200;
786 	r->resp_body_finished = 1;
787 
788 	return HANDLER_FINISHED;
789 }
790 
mod_status_handler(request_st * const r,void * p_d)791 static handler_t mod_status_handler(request_st * const r, void *p_d) {
792 	plugin_data *p = p_d;
793 
794 	if (NULL != r->handler_module) return HANDLER_GO_ON;
795 
796 	mod_status_patch_config(r, p);
797 
798 	if (p->conf.status_url &&
799 	    buffer_is_equal(p->conf.status_url, &r->uri.path)) {
800 		return mod_status_handle_server_status(r, p);
801 	} else if (p->conf.config_url &&
802 	    buffer_is_equal(p->conf.config_url, &r->uri.path)) {
803 		return mod_status_handle_server_config(r);
804 	} else if (p->conf.statistics_url &&
805 	    buffer_is_equal(p->conf.statistics_url, &r->uri.path)) {
806 		return mod_status_handle_server_statistics(r);
807 	}
808 
809 	return HANDLER_GO_ON;
810 }
811 
TRIGGER_FUNC(mod_status_trigger)812 TRIGGER_FUNC(mod_status_trigger) {
813     plugin_data * const p = p_d;
814 
815     /* check all connections */
816     for (const connection *c = srv->conns; c; c = c->next)
817         p->bytes_written_1s += c->bytes_written_cur_second;
818 
819     /* used in calculating sliding average */
820     p->traffic_out_5s[p->ndx_5s] = p->bytes_written_1s;
821     p->requests_5s   [p->ndx_5s] = p->requests_1s;
822     if (++p->ndx_5s == 5) p->ndx_5s = 0;
823 
824     p->abs_traffic_out += p->bytes_written_1s;
825     p->abs_requests += p->requests_1s;
826 
827     p->bytes_written_1s = 0;
828     p->requests_1s = 0;
829 
830     return HANDLER_GO_ON;
831 }
832 
REQUESTDONE_FUNC(mod_status_account)833 REQUESTDONE_FUNC(mod_status_account) {
834     plugin_data * const p = p_d;
835     const connection * const con = r->con;
836 
837     ++p->requests_1s;
838     if (r == &con->request) /*(HTTP/1.x or only HTTP/2 stream 0)*/
839         p->bytes_written_1s += con->bytes_written_cur_second;
840 
841     return HANDLER_GO_ON;
842 }
843 
844 
845 __attribute_cold__
846 int mod_status_plugin_init(plugin *p);
mod_status_plugin_init(plugin * p)847 int mod_status_plugin_init(plugin *p) {
848 	p->version     = LIGHTTPD_VERSION_ID;
849 	p->name        = "status";
850 
851 	p->init        = mod_status_init;
852 	p->set_defaults= mod_status_set_defaults;
853 
854 	p->handle_uri_clean    = mod_status_handler;
855 	p->handle_trigger      = mod_status_trigger;
856 	p->handle_request_done = mod_status_account;
857 
858 	return 0;
859 }
860