From b06f26cba16884de23f33230f37e32231d029488 Mon Sep 17 00:00:00 2001 From: Robert James Kaes Date: Thu, 13 Mar 2003 21:31:03 +0000 Subject: # Changed showstats() to use the HTML variable functions when possible. It still retains the hard-coded page for when an HTML file is not available. [Steven Young] --- src/stats.c | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/stats.c b/src/stats.c index e55f339..ea0b629 100644 --- a/src/stats.c +++ b/src/stats.c @@ -1,4 +1,4 @@ -/* $Id: stats.c,v 1.12 2002-05-29 20:49:55 rjkaes Exp $ +/* $Id: stats.c,v 1.13 2003-03-13 21:31:03 rjkaes Exp $ * * This module handles the statistics for tinyproxy. There are only two * public API functions. The reason for the functions, rather than just a @@ -25,6 +25,7 @@ #include "log.h" #include "heap.h" +#include "htmlerror.h" #include "stats.h" #include "utils.h" @@ -70,23 +71,45 @@ showstats(struct conn_s *connptr) "\r\n\r\n"; char *message_buffer; + char opens[16], reqs[16], badconns[16], denied[16], refused[16]; + FILE *statfile; - message_buffer = safemalloc(MAXBUFFSIZE); - if (!message_buffer) - return -1; + snprintf(opens, sizeof(opens), "%lu", stats->num_open); + snprintf(reqs, sizeof(reqs), "%lu", stats->num_reqs); + snprintf(badconns, sizeof(badconns), "%lu", stats->num_badcons); + snprintf(denied, sizeof(denied), "%lu", stats->num_denied); + snprintf(refused, sizeof(refused), "%lu", stats->num_refused); + + if (!config.statpage || (!(statfile = fopen(config.statpage, "r")))) { + message_buffer = safemalloc(MAXBUFFSIZE); + if (!message_buffer) + return -1; + + snprintf(message_buffer, MAXBUFFSIZE, msg, + PACKAGE, VERSION, PACKAGE, VERSION, + stats->num_open, + stats->num_reqs, + stats->num_badcons, stats->num_denied, stats->num_refused); - snprintf(message_buffer, MAXBUFFSIZE, msg, - PACKAGE, VERSION, PACKAGE, VERSION, - stats->num_open, - stats->num_reqs, - stats->num_badcons, stats->num_denied, stats->num_refused); + if (send_http_message(connptr, 200, "OK", message_buffer) < 0) { + safefree(message_buffer); + return -1; + } - if (send_http_message(connptr, 200, "OK", message_buffer) < 0) { safefree(message_buffer); - return -1; + return 0; } - safefree(message_buffer); + add_error_variable(connptr, "opens", opens); + add_error_variable(connptr, "reqs", reqs); + add_error_variable(connptr, "badconns", badconns); + add_error_variable(connptr, "denied", denied); + add_error_variable(connptr, "refused", refused); + add_standard_vars(connptr); + send_http_headers(connptr, 200, "Statistic requested"); + send_html_file(statfile, connptr); + fclose(statfile); + return 0; } -- cgit v1.2.3