[MAJOR] struct chunk rework

Add size to struct chunk and simplify the code as there is
no longer required to pass sizeof in chunk_printf().
This commit is contained in:
Krzysztof Piotr Oledzki 2009-09-27 13:23:20 +02:00 committed by Willy Tarreau
parent ca7d4b98d4
commit 78abe618a8
9 changed files with 200 additions and 154 deletions

View File

@ -409,8 +409,6 @@ int buffer_si_peekline(struct buffer *buf, char *str, int len);
int buffer_replace(struct buffer *b, char *pos, char *end, const char *str); int buffer_replace(struct buffer *b, char *pos, char *end, const char *str);
int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len); int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len); int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
int chunk_printf(struct chunk *chk, int size, const char *fmt, ...)
__attribute__ ((format(printf, 3, 4)));
void buffer_dump(FILE *o, struct buffer *b, int from, int to); void buffer_dump(FILE *o, struct buffer *b, int from, int to);
@ -448,6 +446,65 @@ static inline int buffer_feed_chunk(struct buffer *buf, struct chunk *chunk)
return ret; return ret;
} }
static inline void chunk_init(struct chunk *chk, char *str, size_t size) {
chk->str = str;
chk->len = 0;
chk->size = size;
}
/* report 0 in case of error, 1 if OK. */
static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, size_t len) {
if (len > size)
return 0;
chk->str = str;
chk->len = len;
chk->size = size;
return 1;
}
static inline void chunk_initstr(struct chunk *chk, char *str) {
chk->str = str;
chk->len = strlen(str);
chk->size = 0; /* mark it read-only */
}
static inline int chunk_strcpy(struct chunk *chk, const char *str) {
size_t len;
len = strlen(str);
if (unlikely(len > chk->size))
return 0;
chk->len = len;
memcpy(chk->str, str, len);
return 1;
}
int chunk_printf(struct chunk *chk, const char *fmt, ...)
__attribute__ ((format(printf, 2, 3)));
static inline void chunk_reset(struct chunk *chk) {
chk->str = NULL;
chk->len = -1;
chk->size = 0;
}
static inline void chunk_destroy(struct chunk *chk) {
if (!chk->size)
return;
if (chk->str)
free(chk->str);
chunk_reset(chk);
}
/* /*
* frees the destination chunk if already allocated, allocates a new string, * frees the destination chunk if already allocated, allocates a new string,
* and copies the source into it. The pointer to the destination string is * and copies the source into it. The pointer to the destination string is

View File

@ -148,7 +148,8 @@
/* describes a chunk of string */ /* describes a chunk of string */
struct chunk { struct chunk {
char *str; /* beginning of the string itself. Might not be 0-terminated */ char *str; /* beginning of the string itself. Might not be 0-terminated */
int len; /* size of the string from first to last char. <0 = uninit. */ size_t size; /* total size of the buffer, 0 if the *str is read-only */
size_t len; /* current size of the string from first to last char. <0 = uninit. */
}; };
/* needed for a declaration below */ /* needed for a declaration below */

View File

@ -302,17 +302,20 @@ int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
/* /*
* Does an snprintf() at the end of chunk <chk>, respecting the limit of * Does an snprintf() at the end of chunk <chk>, respecting the limit of
* at most <size> chars. If the size is over, nothing is added. Returns * at most chk->size chars. If the chk->len is over, nothing is added. Returns
* the new chunk size. * the new chunk size.
*/ */
int chunk_printf(struct chunk *chk, int size, const char *fmt, ...) int chunk_printf(struct chunk *chk, const char *fmt, ...)
{ {
va_list argp; va_list argp;
int ret; int ret;
if (!chk->str || !chk->size)
return 0;
va_start(argp, fmt); va_start(argp, fmt);
ret = vsnprintf(chk->str + chk->len, size - chk->len, fmt, argp); ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
if (ret >= size - chk->len) if (ret >= chk->size - chk->len)
/* do not copy anything in case of truncation */ /* do not copy anything in case of truncation */
chk->str[chk->len] = 0; chk->str[chk->len] = 0;
else else

View File

@ -873,10 +873,8 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
curproxy->fe_sps_lim = defproxy.fe_sps_lim; curproxy->fe_sps_lim = defproxy.fe_sps_lim;
/* initialize error relocations */ /* initialize error relocations */
for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
if (defproxy.errmsg[rc].str)
chunk_dup(&curproxy->errmsg[rc], &defproxy.errmsg[rc]); chunk_dup(&curproxy->errmsg[rc], &defproxy.errmsg[rc]);
}
curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR; curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR;
} }
@ -975,7 +973,7 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
defproxy.fwdfor_hdr_len = 0; defproxy.fwdfor_hdr_len = 0;
for (rc = 0; rc < HTTP_ERR_SIZE; rc++) for (rc = 0; rc < HTTP_ERR_SIZE; rc++)
free(defproxy.errmsg[rc].str); chunk_destroy(&defproxy.errmsg[rc]);
/* we cannot free uri_auth because it might already be used */ /* we cannot free uri_auth because it might already be used */
init_default_instance(); init_default_instance();
@ -3467,9 +3465,8 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
if (http_err_codes[rc] == errnum) { if (http_err_codes[rc] == errnum) {
free(curproxy->errmsg[rc].str); chunk_destroy(&curproxy->errmsg[rc]);
curproxy->errmsg[rc].str = err; chunk_initlen(&curproxy->errmsg[rc], err, errlen, errlen);
curproxy->errmsg[rc].len = errlen;
break; break;
} }
} }
@ -3528,9 +3525,8 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
errnum = atol(args[1]); errnum = atol(args[1]);
for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
if (http_err_codes[rc] == errnum) { if (http_err_codes[rc] == errnum) {
free(curproxy->errmsg[rc].str); chunk_destroy(&curproxy->errmsg[rc]);
curproxy->errmsg[rc].str = err; chunk_initlen(&curproxy->errmsg[rc], err, errlen, errlen);
curproxy->errmsg[rc].len = errlen;
break; break;
} }
} }

View File

@ -232,24 +232,23 @@ static void set_server_down(struct server *s)
*/ */
xferred = redistribute_pending(s); xferred = redistribute_pending(s);
msg.len = 0; chunk_init(&msg, trash, sizeof(trash));
msg.str = trash;
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "", "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
s->proxy->id, s->id); s->proxy->id, s->id);
if (s->tracked) if (s->tracked)
chunk_printf(&msg, sizeof(trash), " via %s/%s", chunk_printf(&msg, " via %s/%s",
s->tracked->proxy->id, s->tracked->id); s->tracked->proxy->id, s->tracked->id);
chunk_printf(&msg, sizeof(trash), ", reason: %s", get_check_status_description(s->check_status)); chunk_printf(&msg, ", reason: %s", get_check_status_description(s->check_status));
if (s->check_status >= HCHK_STATUS_L57DATA) if (s->check_status >= HCHK_STATUS_L57DATA)
chunk_printf(&msg, sizeof(trash), ", code: %d", s->check_code); chunk_printf(&msg, ", code: %d", s->check_code);
chunk_printf(&msg, sizeof(trash), ", check duration: %lums", s->check_duration); chunk_printf(&msg, ", check duration: %lums", s->check_duration);
chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers left.%s" chunk_printf(&msg, ". %d active and %d backup servers left.%s"
" %d sessions active, %d requeued, %d remaining in queue.\n", " %d sessions active, %d requeued, %d remaining in queue.\n",
s->proxy->srv_act, s->proxy->srv_bck, s->proxy->srv_act, s->proxy->srv_bck,
(s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
@ -313,22 +312,21 @@ static void set_server_up(struct server *s) {
*/ */
xferred = check_for_pending(s); xferred = check_for_pending(s);
msg.len = 0; chunk_init(&msg, trash, sizeof(trash));
msg.str = trash;
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "", "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
s->proxy->id, s->id); s->proxy->id, s->id);
if (s->tracked) if (s->tracked)
chunk_printf(&msg, sizeof(trash), " via %s/%s", chunk_printf(&msg, " via %s/%s",
s->tracked->proxy->id, s->tracked->id); s->tracked->proxy->id, s->tracked->id);
chunk_printf(&msg, sizeof(trash), ", reason: %s", get_check_status_description(s->check_status)); chunk_printf(&msg, ", reason: %s", get_check_status_description(s->check_status));
if (s->check_status >= HCHK_STATUS_L57DATA) if (s->check_status >= HCHK_STATUS_L57DATA)
chunk_printf(&msg, sizeof(trash), ", code: %d", s->check_code); chunk_printf(&msg, ", code: %d", s->check_code);
chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s" chunk_printf(&msg, ". %d active and %d backup servers online.%s"
" %d sessions requeued, %d total in queue.\n", " %d sessions requeued, %d total in queue.\n",
s->proxy->srv_act, s->proxy->srv_bck, s->proxy->srv_act, s->proxy->srv_bck,
(s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
@ -362,20 +360,19 @@ static void set_server_disabled(struct server *s) {
*/ */
xferred = redistribute_pending(s); xferred = redistribute_pending(s);
msg.len = 0; chunk_init(&msg, trash, sizeof(trash));
msg.str = trash;
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"Load-balancing on %sServer %s/%s is disabled", "Load-balancing on %sServer %s/%s is disabled",
s->state & SRV_BACKUP ? "Backup " : "", s->state & SRV_BACKUP ? "Backup " : "",
s->proxy->id, s->id); s->proxy->id, s->id);
if (s->tracked) if (s->tracked)
chunk_printf(&msg, sizeof(trash), " via %s/%s", chunk_printf(&msg, " via %s/%s",
s->tracked->proxy->id, s->tracked->id); s->tracked->proxy->id, s->tracked->id);
chunk_printf(&msg, sizeof(trash),". %d active and %d backup servers online.%s" chunk_printf(&msg,". %d active and %d backup servers online.%s"
" %d sessions requeued, %d total in queue.\n", " %d sessions requeued, %d total in queue.\n",
s->proxy->srv_act, s->proxy->srv_bck, s->proxy->srv_act, s->proxy->srv_bck,
(s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
@ -407,19 +404,18 @@ static void set_server_enabled(struct server *s) {
*/ */
xferred = check_for_pending(s); xferred = check_for_pending(s);
msg.len = 0; chunk_init(&msg, trash, sizeof(trash));
msg.str = trash;
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"Load-balancing on %sServer %s/%s is enabled again", "Load-balancing on %sServer %s/%s is enabled again",
s->state & SRV_BACKUP ? "Backup " : "", s->state & SRV_BACKUP ? "Backup " : "",
s->proxy->id, s->id); s->proxy->id, s->id);
if (s->tracked) if (s->tracked)
chunk_printf(&msg, sizeof(trash), " via %s/%s", chunk_printf(&msg, " via %s/%s",
s->tracked->proxy->id, s->tracked->id); s->tracked->proxy->id, s->tracked->id);
chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s" chunk_printf(&msg, ". %d active and %d backup servers online.%s"
" %d sessions requeued, %d total in queue.\n", " %d sessions requeued, %d total in queue.\n",
s->proxy->srv_act, s->proxy->srv_bck, s->proxy->srv_act, s->proxy->srv_bck,
(s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",

View File

@ -279,7 +279,7 @@ int event_accept(int fd) {
txn->rsp.sol = txn->rsp.eol = NULL; txn->rsp.sol = txn->rsp.eol = NULL;
txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
txn->auth_hdr.len = -1; chunk_reset(&txn->auth_hdr);
if (p->options2 & PR_O2_REQBUG_OK) if (p->options2 & PR_O2_REQBUG_OK)
txn->req.err_pos = -1; /* let buggy requests pass */ txn->req.err_pos = -1; /* let buggy requests pass */
@ -455,13 +455,15 @@ int event_accept(int fd) {
* or we're in health check mode with the 'httpchk' option enabled. In * or we're in health check mode with the 'httpchk' option enabled. In
* both cases, we return a fake "HTTP/1.0 200 OK" response and we exit. * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
*/ */
struct chunk msg = { .str = "HTTP/1.0 200 OK\r\n\r\n", .len = 19 }; struct chunk msg;
chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n");
stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */ stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
s->req->analysers = 0; s->req->analysers = 0;
t->expire = s->rep->wex; t->expire = s->rep->wex;
} }
else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */ else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */
struct chunk msg = { .str = "OK\n", .len = 3 }; struct chunk msg;
chunk_initstr(&msg, "OK\n");
stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */ stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
s->req->analysers = 0; s->req->analysers = 0;
t->expire = s->rep->wex; t->expire = s->rep->wex;

View File

@ -213,9 +213,9 @@ static int stats_parse_global(char **args, int section_type, struct proxy *curpx
return 0; return 0;
} }
int print_csv_header(struct chunk *msg, int size) int print_csv_header(struct chunk *msg)
{ {
return chunk_printf(msg, size, return chunk_printf(msg,
"# pxname,svname," "# pxname,svname,"
"qcur,qmax," "qcur,qmax,"
"scur,smax,slim,stot," "scur,smax,slim,stot,"
@ -532,8 +532,7 @@ int stats_dump_raw(struct session *s, struct buffer *rep, struct uri_auth *uri)
struct chunk msg; struct chunk msg;
unsigned int up; unsigned int up;
msg.len = 0; chunk_init(&msg, trash, sizeof(trash));
msg.str = trash;
switch (s->data_state) { switch (s->data_state) {
case DATA_ST_INIT: case DATA_ST_INIT:
@ -545,7 +544,7 @@ int stats_dump_raw(struct session *s, struct buffer *rep, struct uri_auth *uri)
case DATA_ST_HEAD: case DATA_ST_HEAD:
if (s->data_ctx.stats.flags & STAT_SHOW_STAT) { if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
print_csv_header(&msg, sizeof(trash)); print_csv_header(&msg);
if (buffer_write_chunk(rep, &msg) >= 0) if (buffer_write_chunk(rep, &msg) >= 0)
return 0; return 0;
} }
@ -556,7 +555,7 @@ int stats_dump_raw(struct session *s, struct buffer *rep, struct uri_auth *uri)
case DATA_ST_INFO: case DATA_ST_INFO:
up = (now.tv_sec - start_date.tv_sec); up = (now.tv_sec - start_date.tv_sec);
if (s->data_ctx.stats.flags & STAT_SHOW_INFO) { if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"Name: " PRODUCT_NAME "\n" "Name: " PRODUCT_NAME "\n"
"Version: " HAPROXY_VERSION "\n" "Version: " HAPROXY_VERSION "\n"
"Release_date: " HAPROXY_DATE "\n" "Release_date: " HAPROXY_DATE "\n"
@ -667,12 +666,11 @@ int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
struct chunk msg; struct chunk msg;
unsigned int up; unsigned int up;
msg.len = 0; chunk_init(&msg, trash, sizeof(trash));
msg.str = trash;
switch (s->data_state) { switch (s->data_state) {
case DATA_ST_INIT: case DATA_ST_INIT:
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"HTTP/1.0 200 OK\r\n" "HTTP/1.0 200 OK\r\n"
"Cache-Control: no-cache\r\n" "Cache-Control: no-cache\r\n"
"Connection: close\r\n" "Connection: close\r\n"
@ -680,10 +678,10 @@ int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
(s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html"); (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH)) if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n", chunk_printf(&msg, "Refresh: %d\r\n",
uri->refresh); uri->refresh);
chunk_printf(&msg, sizeof(trash), "\r\n"); chunk_printf(&msg, "\r\n");
s->txn.status = 200; s->txn.status = 200;
if (buffer_write_chunk(rep, &msg) >= 0) if (buffer_write_chunk(rep, &msg) >= 0)
@ -709,7 +707,7 @@ int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
case DATA_ST_HEAD: case DATA_ST_HEAD:
if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
/* WARNING! This must fit in the first buffer !!! */ /* WARNING! This must fit in the first buffer !!! */
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n" "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
"<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n" "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
"<style type=\"text/css\"><!--\n" "<style type=\"text/css\"><!--\n"
@ -787,7 +785,7 @@ int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
uri->node_name ? uri->node_name : "" uri->node_name ? uri->node_name : ""
); );
} else { } else {
print_csv_header(&msg, sizeof(trash)); print_csv_header(&msg);
} }
if (buffer_write_chunk(rep, &msg) >= 0) if (buffer_write_chunk(rep, &msg) >= 0)
return 0; return 0;
@ -803,7 +801,7 @@ int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
* become tricky if we want to support 4kB buffers ! * become tricky if we want to support 4kB buffers !
*/ */
if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">" "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
PRODUCT_NAME "%s</a></h1>\n" PRODUCT_NAME "%s</a></h1>\n"
"<h2>Statistics Report for pid %d%s%s</h2>\n" "<h2>Statistics Report for pid %d%s%s</h2>\n"
@ -850,13 +848,13 @@ int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
); );
if (s->data_ctx.stats.flags & STAT_HIDE_DOWN) if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<li><a href=\"%s%s%s\">Show all servers</a><br>\n", "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
uri->uri_prefix, uri->uri_prefix,
"", "",
(s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : ""); (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
else else
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n", "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
uri->uri_prefix, uri->uri_prefix,
";up", ";up",
@ -864,31 +862,31 @@ int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
if (uri->refresh > 0) { if (uri->refresh > 0) {
if (s->data_ctx.stats.flags & STAT_NO_REFRESH) if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<li><a href=\"%s%s%s\">Enable refresh</a><br>\n", "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
uri->uri_prefix, uri->uri_prefix,
(s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
""); "");
else else
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<li><a href=\"%s%s%s\">Disable refresh</a><br>\n", "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
uri->uri_prefix, uri->uri_prefix,
(s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
";norefresh"); ";norefresh");
} }
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<li><a href=\"%s%s%s\">Refresh now</a><br>\n", "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
uri->uri_prefix, uri->uri_prefix,
(s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
(s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : ""); (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<li><a href=\"%s;csv%s\">CSV export</a><br>\n", "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
uri->uri_prefix, uri->uri_prefix,
(uri->refresh > 0) ? ";norefresh" : ""); (uri->refresh > 0) ? ";norefresh" : "");
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"</td>" "</td>"
"<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">" "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
"<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n" "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
@ -929,7 +927,7 @@ int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
case DATA_ST_END: case DATA_ST_END:
if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
chunk_printf(&msg, sizeof(trash), "</body></html>\n"); chunk_printf(&msg, "</body></html>\n");
if (buffer_write_chunk(rep, &msg) >= 0) if (buffer_write_chunk(rep, &msg) >= 0)
return 0; return 0;
} }
@ -960,8 +958,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */ struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
struct chunk msg; struct chunk msg;
msg.len = 0; chunk_init(&msg, trash, sizeof(trash));
msg.str = trash;
switch (s->data_ctx.stats.px_st) { switch (s->data_ctx.stats.px_st) {
case DATA_ST_PX_INIT: case DATA_ST_PX_INIT:
@ -1001,7 +998,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
case DATA_ST_PX_TH: case DATA_ST_PX_TH:
if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
/* print a new table */ /* print a new table */
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<table cols=\"29\" class=\"tbl\" width=\"100%%\">\n" "<table cols=\"29\" class=\"tbl\" width=\"100%%\">\n"
"<tr align=\"center\" class=\"titre\">" "<tr align=\"center\" class=\"titre\">"
"<th colspan=2 class=\"pxname\">%s</th>" "<th colspan=2 class=\"pxname\">%s</th>"
@ -1039,7 +1036,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
if ((px->cap & PR_CAP_FE) && if ((px->cap & PR_CAP_FE) &&
(!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) { (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
/* name, queue */ /* name, queue */
"<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>" "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
/* sessions rate : current, max, limit */ /* sessions rate : current, max, limit */
@ -1055,7 +1052,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
U2H3(px->feconn), U2H4(px->feconn_max), U2H5(px->maxconn), U2H3(px->feconn), U2H4(px->feconn_max), U2H5(px->maxconn),
U2H6(px->cum_feconn), U2H7(px->bytes_in), U2H8(px->bytes_out)); U2H6(px->cum_feconn), U2H7(px->bytes_in), U2H8(px->bytes_out));
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
/* denied: req, resp */ /* denied: req, resp */
"<td align=right>%s</td><td align=right>%s</td>" "<td align=right>%s</td><td align=right>%s</td>"
/* errors : request, connect, response */ /* errors : request, connect, response */
@ -1072,7 +1069,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
px->state == PR_STRUN ? "OPEN" : px->state == PR_STRUN ? "OPEN" :
px->state == PR_STIDLE ? "FULL" : "STOP"); px->state == PR_STIDLE ? "FULL" : "STOP");
} else { } else {
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
/* pxid, name, queue cur, queue max, */ /* pxid, name, queue cur, queue max, */
"%s,FRONTEND,,," "%s,FRONTEND,,,"
/* sessions : current, max, limit, total */ /* sessions : current, max, limit, total */
@ -1166,7 +1163,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
"UP %d/%d &darr;", "UP", "UP %d/%d &darr;", "UP",
"NOLB %d/%d &darr;", "NOLB", "NOLB %d/%d &darr;", "NOLB",
"<i>no check</i>" }; "<i>no check</i>" };
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
/* name */ /* name */
"<tr align=\"center\" class=\"%s%d\"><td>%s</td>" "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
/* queue : current, max, limit */ /* queue : current, max, limit */
@ -1184,7 +1181,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
U2H5(sv->cur_sess), U2H6(sv->cur_sess_max), LIM2A7(sv->maxconn, "-"), U2H5(sv->cur_sess), U2H6(sv->cur_sess_max), LIM2A7(sv->maxconn, "-"),
U2H8(sv->cum_sess), U2H9(sv->cum_lbconn)); U2H8(sv->cum_sess), U2H9(sv->cum_lbconn));
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
/* bytes : in, out */ /* bytes : in, out */
"<td align=right>%s</td><td align=right>%s</td>" "<td align=right>%s</td><td align=right>%s</td>"
/* denied: req, resp */ /* denied: req, resp */
@ -1200,32 +1197,32 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
sv->retries, sv->redispatches); sv->retries, sv->redispatches);
/* status, lest check */ /* status, lest check */
chunk_printf(&msg, sizeof(trash), "<td nowrap>"); chunk_printf(&msg, "<td nowrap>");
if (sv->state & SRV_CHECKED) { if (sv->state & SRV_CHECKED) {
chunk_printf(&msg, sizeof(trash), "%s ", chunk_printf(&msg, "%s ",
human_time(now.tv_sec - sv->last_change, 1)); human_time(now.tv_sec - sv->last_change, 1));
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
srv_hlt_st[sv_state], srv_hlt_st[sv_state],
(svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health), (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
(svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise)); (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
chunk_printf(&msg, sizeof(trash), "</td><td title=\"%s\" nowrap> %s%s", chunk_printf(&msg, "</td><td title=\"%s\" nowrap> %s%s",
get_check_status_description(sv->check_status), get_check_status_description(sv->check_status),
tv_iszero(&sv->check_start)?"":"* ", tv_iszero(&sv->check_start)?"":"* ",
get_check_status_info(sv->check_status)); get_check_status_info(sv->check_status));
if (sv->check_status >= HCHK_STATUS_L57DATA) if (sv->check_status >= HCHK_STATUS_L57DATA)
chunk_printf(&msg, sizeof(trash), "/%d", sv->check_code); chunk_printf(&msg, "/%d", sv->check_code);
if (sv->check_status >= HCHK_STATUS_CHECKED) if (sv->check_status >= HCHK_STATUS_CHECKED)
chunk_printf(&msg, sizeof(trash), " in %lums", sv->check_duration); chunk_printf(&msg, " in %lums", sv->check_duration);
} else { } else {
chunk_printf(&msg, sizeof(trash), "</td><td>"); chunk_printf(&msg, "</td><td>");
} }
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
/* weight */ /* weight */
"</td><td>%d</td>" "</td><td>%d</td>"
/* act, bck */ /* act, bck */
@ -1237,17 +1234,17 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
/* check failures: unique, fatal, down time */ /* check failures: unique, fatal, down time */
if (sv->state & SRV_CHECKED) if (sv->state & SRV_CHECKED)
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<td align=right>%lld</td><td align=right>%lld</td>" "<td align=right>%lld</td><td align=right>%lld</td>"
"<td nowrap align=right>%s</td>" "<td nowrap align=right>%s</td>"
"", "",
svs->failed_checks, svs->down_trans, svs->failed_checks, svs->down_trans,
human_time(srv_downtime(sv), 1)); human_time(srv_downtime(sv), 1));
else if (sv != svs) else if (sv != svs)
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id); "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id);
else else
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<td colspan=3></td>"); "<td colspan=3></td>");
/* throttle */ /* throttle */
@ -1256,10 +1253,10 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
now.tv_sec >= sv->last_change) { now.tv_sec >= sv->last_change) {
unsigned int ratio; unsigned int ratio;
ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart); ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<td>%d %%</td></tr>\n", ratio); "<td>%d %%</td></tr>\n", ratio);
} else { } else {
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"<td>-</td></tr>\n"); "<td>-</td></tr>\n");
} }
} else { } else {
@ -1267,7 +1264,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
"UP %d/%d,", "UP,", "UP %d/%d,", "UP,",
"NOLB %d/%d,", "NOLB,", "NOLB %d/%d,", "NOLB,",
"no check," }; "no check," };
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
/* pxid, name */ /* pxid, name */
"%s,%s," "%s,%s,"
/* queue : current, max */ /* queue : current, max */
@ -1292,12 +1289,12 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
sv->retries, sv->redispatches); sv->retries, sv->redispatches);
/* status */ /* status */
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
srv_hlt_st[sv_state], srv_hlt_st[sv_state],
(sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health), (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
(sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise)); (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
/* weight, active, backup */ /* weight, active, backup */
"%d,%d,%d," "%d,%d,%d,"
"", "",
@ -1307,16 +1304,16 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
/* check failures: unique, fatal; last change, total downtime */ /* check failures: unique, fatal; last change, total downtime */
if (sv->state & SRV_CHECKED) if (sv->state & SRV_CHECKED)
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"%lld,%lld,%d,%d,", "%lld,%lld,%d,%d,",
sv->failed_checks, sv->down_trans, sv->failed_checks, sv->down_trans,
(int)(now.tv_sec - sv->last_change), srv_downtime(sv)); (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
else else
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
",,,,"); ",,,,");
/* queue limit, pid, iid, sid, */ /* queue limit, pid, iid, sid, */
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"%s," "%s,"
"%d,%d,%d,", "%d,%d,%d,",
LIM2A0(sv->maxqueue, ""), LIM2A0(sv->maxqueue, ""),
@ -1328,49 +1325,49 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
now.tv_sec >= sv->last_change) { now.tv_sec >= sv->last_change) {
unsigned int ratio; unsigned int ratio;
ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart); ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
chunk_printf(&msg, sizeof(trash), "%d", ratio); chunk_printf(&msg, "%d", ratio);
} }
/* sessions: lbtot */ /* sessions: lbtot */
chunk_printf(&msg, sizeof(trash), ",%lld,", sv->cum_lbconn); chunk_printf(&msg, ",%lld,", sv->cum_lbconn);
/* tracked */ /* tracked */
if (sv->tracked) if (sv->tracked)
chunk_printf(&msg, sizeof(trash), "%s/%s,", chunk_printf(&msg, "%s/%s,",
sv->tracked->proxy->id, sv->tracked->id); sv->tracked->proxy->id, sv->tracked->id);
else else
chunk_printf(&msg, sizeof(trash), ","); chunk_printf(&msg, ",");
/* type */ /* type */
chunk_printf(&msg, sizeof(trash), "%d,", STATS_TYPE_SV); chunk_printf(&msg, "%d,", STATS_TYPE_SV);
/* rate */ /* rate */
chunk_printf(&msg, sizeof(trash), "%u,,%u,", chunk_printf(&msg, "%u,,%u,",
read_freq_ctr(&sv->sess_per_sec), read_freq_ctr(&sv->sess_per_sec),
sv->sps_max); sv->sps_max);
if (sv->state & SRV_CHECKED) { if (sv->state & SRV_CHECKED) {
/* check_status */ /* check_status */
chunk_printf(&msg, sizeof(trash), "%s,", get_check_status_info(sv->check_status)); chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
/* check_code */ /* check_code */
if (sv->check_status >= HCHK_STATUS_L57DATA) if (sv->check_status >= HCHK_STATUS_L57DATA)
chunk_printf(&msg, sizeof(trash), "%u,", sv->check_code); chunk_printf(&msg, "%u,", sv->check_code);
else else
chunk_printf(&msg, sizeof(trash), ","); chunk_printf(&msg, ",");
/* check_duration */ /* check_duration */
if (sv->check_status >= HCHK_STATUS_CHECKED) if (sv->check_status >= HCHK_STATUS_CHECKED)
chunk_printf(&msg, sizeof(trash), "%lu,", sv->check_duration); chunk_printf(&msg, "%lu,", sv->check_duration);
else else
chunk_printf(&msg, sizeof(trash), ","); chunk_printf(&msg, ",");
} else { } else {
chunk_printf(&msg, sizeof(trash), ",,,"); chunk_printf(&msg, ",,,");
} }
/* finish with EOL */ /* finish with EOL */
chunk_printf(&msg, sizeof(trash), "\n"); chunk_printf(&msg, "\n");
} }
if (buffer_write_chunk(rep, &msg) >= 0) if (buffer_write_chunk(rep, &msg) >= 0)
return 0; return 0;
@ -1384,7 +1381,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
if ((px->cap & PR_CAP_BE) && if ((px->cap & PR_CAP_BE) &&
(!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) { (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
/* name */ /* name */
"<tr align=center class=\"backend\"><td>Backend</td>" "<tr align=center class=\"backend\"><td>Backend</td>"
/* queue : current, max */ /* queue : current, max */
@ -1395,7 +1392,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->nbpend_max), U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->nbpend_max),
U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_sps_max)); U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_sps_max));
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
/* sessions : current, max, limit, total, lbtot */ /* sessions : current, max, limit, total, lbtot */
"<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>" "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
"<td align=right>%s</td><td align=right>%s</td>" "<td align=right>%s</td><td align=right>%s</td>"
@ -1406,7 +1403,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
U2H6(px->cum_beconn), U2H7(px->cum_lbconn), U2H6(px->cum_beconn), U2H7(px->cum_lbconn),
U2H8(px->bytes_in), U2H9(px->bytes_out)); U2H8(px->bytes_in), U2H9(px->bytes_out));
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
/* denied: req, resp */ /* denied: req, resp */
"<td align=right>%s</td><td align=right>%s</td>" "<td align=right>%s</td><td align=right>%s</td>"
/* errors : request, connect, response */ /* errors : request, connect, response */
@ -1429,7 +1426,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
(px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
px->srv_act, px->srv_bck); px->srv_act, px->srv_bck);
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
/* rest of backend: nothing, down transitions, total downtime, throttle */ /* rest of backend: nothing, down transitions, total downtime, throttle */
"<td align=center>&nbsp;</td><td align=\"right\">%d</td>" "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
"<td align=\"right\" nowrap>%s</td>" "<td align=\"right\" nowrap>%s</td>"
@ -1438,7 +1435,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
px->down_trans, px->down_trans,
px->srv?human_time(be_downtime(px), 1):"&nbsp;"); px->srv?human_time(be_downtime(px), 1):"&nbsp;");
} else { } else {
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
/* pxid, name */ /* pxid, name */
"%s,BACKEND," "%s,BACKEND,"
/* queue : current, max */ /* queue : current, max */
@ -1494,7 +1491,7 @@ int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
case DATA_ST_PX_END: case DATA_ST_PX_END:
if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) { if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
chunk_printf(&msg, sizeof(trash), "</table><p>\n"); chunk_printf(&msg, "</table><p>\n");
if (buffer_write_chunk(rep, &msg) >= 0) if (buffer_write_chunk(rep, &msg) >= 0)
return 0; return 0;
@ -1542,8 +1539,7 @@ void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
if (s->ana_state != STATS_ST_REP) if (s->ana_state != STATS_ST_REP)
return; return;
msg.len = 0; chunk_init(&msg, trash, sizeof(trash));
msg.str = trash;
switch (s->data_state) { switch (s->data_state) {
case DATA_ST_INIT: case DATA_ST_INIT:
@ -1573,7 +1569,7 @@ void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list); curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
"%p: proto=%s", "%p: proto=%s",
curr_sess, curr_sess,
curr_sess->listener->proto->name); curr_sess->listener->proto->name);
@ -1584,7 +1580,7 @@ void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
(const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr, (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
pn, sizeof(pn)); pn, sizeof(pn));
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
" src=%s:%d fe=%s be=%s srv=%s", " src=%s:%d fe=%s be=%s srv=%s",
pn, pn,
ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port), ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
@ -1598,7 +1594,7 @@ void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
(const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr, (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
pn, sizeof(pn)); pn, sizeof(pn));
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
" src=%s:%d fe=%s be=%s srv=%s", " src=%s:%d fe=%s be=%s srv=%s",
pn, pn,
ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port), ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
@ -1613,13 +1609,13 @@ void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
break; break;
} }
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
" as=%d ts=%02x age=%s calls=%d", " as=%d ts=%02x age=%s calls=%d",
curr_sess->ana_state, curr_sess->task->state, curr_sess->ana_state, curr_sess->task->state,
human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1), human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
curr_sess->task->calls); curr_sess->task->calls);
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
" rq[f=%06xh,l=%d,an=%02xh,rx=%s", " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
curr_sess->req->flags, curr_sess->req->flags,
curr_sess->req->l, curr_sess->req->l,
@ -1628,19 +1624,19 @@ void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms), human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
TICKS_TO_MS(1000)) : ""); TICKS_TO_MS(1000)) : "");
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
",wx=%s", ",wx=%s",
curr_sess->req->wex ? curr_sess->req->wex ?
human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms), human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
TICKS_TO_MS(1000)) : ""); TICKS_TO_MS(1000)) : "");
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
",ax=%s]", ",ax=%s]",
curr_sess->req->analyse_exp ? curr_sess->req->analyse_exp ?
human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms), human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
TICKS_TO_MS(1000)) : ""); TICKS_TO_MS(1000)) : "");
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
" rp[f=%06xh,l=%d,an=%02xh,rx=%s", " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
curr_sess->rep->flags, curr_sess->rep->flags,
curr_sess->rep->l, curr_sess->rep->l,
@ -1649,19 +1645,19 @@ void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms), human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
TICKS_TO_MS(1000)) : ""); TICKS_TO_MS(1000)) : "");
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
",wx=%s", ",wx=%s",
curr_sess->rep->wex ? curr_sess->rep->wex ?
human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms), human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
TICKS_TO_MS(1000)) : ""); TICKS_TO_MS(1000)) : "");
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
",ax=%s]", ",ax=%s]",
curr_sess->rep->analyse_exp ? curr_sess->rep->analyse_exp ?
human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms), human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
TICKS_TO_MS(1000)) : ""); TICKS_TO_MS(1000)) : "");
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
" s0=[%d,%1xh,fd=%d,ex=%s]", " s0=[%d,%1xh,fd=%d,ex=%s]",
curr_sess->si[0].state, curr_sess->si[0].state,
curr_sess->si[0].flags, curr_sess->si[0].flags,
@ -1670,7 +1666,7 @@ void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms), human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
TICKS_TO_MS(1000)) : ""); TICKS_TO_MS(1000)) : "");
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
" s1=[%d,%1xh,fd=%d,ex=%s]", " s1=[%d,%1xh,fd=%d,ex=%s]",
curr_sess->si[1].state, curr_sess->si[1].state,
curr_sess->si[1].flags, curr_sess->si[1].flags,
@ -1679,15 +1675,15 @@ void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms), human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
TICKS_TO_MS(1000)) : ""); TICKS_TO_MS(1000)) : "");
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
" exp=%s", " exp=%s",
curr_sess->task->expire ? curr_sess->task->expire ?
human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms), human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
TICKS_TO_MS(1000)) : ""); TICKS_TO_MS(1000)) : "");
if (task_in_rq(curr_sess->task)) if (task_in_rq(curr_sess->task))
chunk_printf(&msg, sizeof(trash), " run(nice=%d)", curr_sess->task->nice); chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
chunk_printf(&msg, sizeof(trash), "\n"); chunk_printf(&msg, "\n");
if (buffer_write_chunk(rep, &msg) >= 0) { if (buffer_write_chunk(rep, &msg) >= 0) {
/* let's try again later from this session. We add ourselves into /* let's try again later from this session. We add ourselves into
@ -1718,17 +1714,17 @@ void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
* continuation of a previous truncated line begin with "+" instead of " " * continuation of a previous truncated line begin with "+" instead of " "
* after the offset. The new pointer is returned. * after the offset. The new pointer is returned.
*/ */
static int dump_error_line(struct chunk *out, int size, static int dump_error_line(struct chunk *out, struct error_snapshot *err,
struct error_snapshot *err, int *line, int ptr) int *line, int ptr)
{ {
int end; int end;
unsigned char c; unsigned char c;
end = out->len + 80; end = out->len + 80;
if (end > size) if (end > out->size)
return ptr; return ptr;
chunk_printf(out, size, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+'); chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
while (ptr < err->len) { while (ptr < err->len) {
c = err->buf[ptr]; c = err->buf[ptr];
@ -1789,8 +1785,7 @@ void stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
if (s->ana_state != STATS_ST_REP) if (s->ana_state != STATS_ST_REP)
return; return;
msg.len = 0; chunk_init(&msg, trash, sizeof(trash));
msg.str = trash;
if (!s->data_ctx.errors.px) { if (!s->data_ctx.errors.px) {
/* the function had not been called yet, let's prepare the /* the function had not been called yet, let's prepare the
@ -1828,7 +1823,7 @@ void stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
struct tm tm; struct tm tm;
get_localtime(es->when.tv_sec, &tm); get_localtime(es->when.tv_sec, &tm);
chunk_printf(&msg, sizeof(trash), "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]", chunk_printf(&msg, "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000)); tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
@ -1844,7 +1839,7 @@ void stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
switch (s->data_ctx.errors.buf) { switch (s->data_ctx.errors.buf) {
case 0: case 0:
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
" frontend %s (#%d): invalid request\n" " frontend %s (#%d): invalid request\n"
" src %s, session #%d, backend %s (#%d), server %s (#%d)\n" " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
" request length %d bytes, error at position %d:\n\n", " request length %d bytes, error at position %d:\n\n",
@ -1855,7 +1850,7 @@ void stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
es->len, es->pos); es->len, es->pos);
break; break;
case 1: case 1:
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
" backend %s (#%d) : invalid response\n" " backend %s (#%d) : invalid response\n"
" src %s, session #%d, frontend %s (#%d), server %s (#%d)\n" " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
" response length %d bytes, error at position %d:\n\n", " response length %d bytes, error at position %d:\n\n",
@ -1877,7 +1872,7 @@ void stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
if (s->data_ctx.errors.sid != es->sid) { if (s->data_ctx.errors.sid != es->sid) {
/* the snapshot changed while we were dumping it */ /* the snapshot changed while we were dumping it */
chunk_printf(&msg, sizeof(trash), chunk_printf(&msg,
" WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n"); " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
if (buffer_write_chunk(rep, &msg) >= 0) if (buffer_write_chunk(rep, &msg) >= 0)
return; return;
@ -1890,7 +1885,7 @@ void stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
int newline; int newline;
newline = s->data_ctx.errors.bol; newline = s->data_ctx.errors.bol;
newptr = dump_error_line(&msg, sizeof(trash), es, &newline, s->data_ctx.errors.ptr); newptr = dump_error_line(&msg, es, &newline, s->data_ctx.errors.ptr);
if (newptr == s->data_ctx.errors.ptr) if (newptr == s->data_ctx.errors.ptr)
return; return;

View File

@ -704,7 +704,7 @@ void deinit(void)
free(p->monitor_uri); free(p->monitor_uri);
for (i = 0; i < HTTP_ERR_SIZE; i++) for (i = 0; i < HTTP_ERR_SIZE; i++)
free(p->errmsg[i].str); chunk_destroy(&p->errmsg[i]);
for (i = 0; i < p->nb_reqadd; i++) for (i = 0; i < p->nb_reqadd; i++)
free(p->req_add[i]); free(p->req_add[i]);

View File

@ -640,7 +640,7 @@ void perform_http_redirect(struct session *s, struct stream_interface *si)
memcpy(rdr.str, HTTP_302, rdr.len); memcpy(rdr.str, HTTP_302, rdr.len);
/* 2: add the server's prefix */ /* 2: add the server's prefix */
if (rdr.len + s->srv->rdr_len > sizeof(trash)) if (rdr.len + s->srv->rdr_len > rdr.size)
return; return;
memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
@ -653,7 +653,7 @@ void perform_http_redirect(struct session *s, struct stream_interface *si)
return; return;
len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
return; return;
memcpy(rdr.str + rdr.len, path, len); memcpy(rdr.str + rdr.len, path, len);
@ -2190,23 +2190,19 @@ int http_process_req_common(struct session *s, struct buffer *req, int an_bit, s
/* build redirect message */ /* build redirect message */
switch(rule->code) { switch(rule->code) {
case 303: case 303:
rdr.len = strlen(HTTP_303);
msg_fmt = HTTP_303; msg_fmt = HTTP_303;
break; break;
case 301: case 301:
rdr.len = strlen(HTTP_301);
msg_fmt = HTTP_301; msg_fmt = HTTP_301;
break; break;
case 302: case 302:
default: default:
rdr.len = strlen(HTTP_302);
msg_fmt = HTTP_302; msg_fmt = HTTP_302;
break; break;
} }
if (unlikely(rdr.len > sizeof(trash))) if (unlikely(chunk_strcpy(&rdr, msg_fmt)))
goto return_bad_req; goto return_bad_req;
memcpy(rdr.str, msg_fmt, rdr.len);
switch(rule->type) { switch(rule->type) {
case REDIRECT_TYPE_PREFIX: { case REDIRECT_TYPE_PREFIX: {
@ -2232,7 +2228,7 @@ int http_process_req_common(struct session *s, struct buffer *req, int an_bit, s
pathlen = 1; pathlen = 1;
} }
if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
goto return_bad_req; goto return_bad_req;
/* add prefix. Note that if prefix == "/", we don't want to /* add prefix. Note that if prefix == "/", we don't want to
@ -2251,7 +2247,7 @@ int http_process_req_common(struct session *s, struct buffer *req, int an_bit, s
} }
case REDIRECT_TYPE_LOCATION: case REDIRECT_TYPE_LOCATION:
default: default:
if (rdr.len + rule->rdr_len > sizeof(trash) - 4) if (rdr.len + rule->rdr_len > rdr.size - 4)
goto return_bad_req; goto return_bad_req;
/* add location */ /* add location */
@ -4614,8 +4610,8 @@ int stats_check_uri_auth(struct session *t, struct proxy *backend)
struct chunk msg; struct chunk msg;
/* no need to go further */ /* no need to go further */
msg.str = trash; sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
txn->status = 401; txn->status = 401;
stream_int_retnclose(t->req->prod, &msg); stream_int_retnclose(t->req->prod, &msg);
t->req->analysers = 0; t->req->analysers = 0;