BUG/MEDIUM: cli: some err/warn msg dumps add LR into CSV output on stat's CLI

The initial purpose of CSV stats through CLI was to make it easely
parsable by scripts. But in some specific cases some error or warning
messages strings containing LF were dumped into cells of this CSV.

This made some parsing failure on several tools. In addition, if a
warning or message contains to successive LF, they will be dumped
directly but double LFs tag the end of the response on CLI and the
client may consider a truncated response.

This patch extends the 'csv_enc_append' and 'csv_enc' functions used
to format quoted string content according to RFC  with an additionnal
parameter to convert multi-lines strings to one line: CRs are skipped,
and LFs are replaced with spaces. In addition and optionally, it is
also possible to remove resulting trailing spaces.

The call of this function to fill strings into stat's CSV output is
updated to force this conversion.

This patch should be backported on all supported branches (issue was
already present in v2.0)

(cherry picked from commit ef02dba7bcf4cf41aaf08fa5cabc8dd2d7537f63)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit 5ed535046cdc83ecd4dedec4bda95f2fe268ebd9)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit 36e5427803cee410b476fc8280a41ecb6fb9cd0d)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit e56044c0abdedfe6eb4c1fda342e4ffa36d69a66)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 3c23daf0ca5040cc45e32078ef5b9dc767cdaede)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h
index 30b6bfa..cd580d3 100644
--- a/include/haproxy/tools.h
+++ b/include/haproxy/tools.h
@@ -439,11 +439,19 @@
  * It is useful if the escaped string is used between double quotes in the
  * format.
  *
- *    printf("..., \"%s\", ...\r\n", csv_enc(str, 0, &trash));
+ *    printf("..., \"%s\", ...\r\n", csv_enc(str, 0, 0, &trash));
  *
  * If <quote> is 1, the converter puts the quotes only if any character is
  * escaped. If <quote> is 2, the converter always puts the quotes.
  *
+ * If <oneline> is not 0, CRs are skipped and LFs are replaced by spaces.
+ * This re-format multi-lines strings to only one line. The purpose is to
+ * allow a line by line parsing but also to keep the output compliant with
+ * the CLI witch uses LF to defines the end of the response.
+ *
+ * If <oneline> is 2, In addition to previous action, the trailing spaces are
+ * removed.
+ *
  * <output> is a struct chunk used for storing the output string.
  *
  * The function returns the converted string on its output. If an error
@@ -456,14 +464,15 @@
  * This function appends the encoding to the existing output chunk. Please
  * use csv_enc() instead if you want to replace the output chunk.
  */
-const char *csv_enc_append(const char *str, int quote, struct buffer *output);
+const char *csv_enc_append(const char *str, int quote, int online,
+			   struct buffer *output);
 
 /* same as above but the output chunk is reset first */
-static inline const char *csv_enc(const char *str, int quote,
+static inline const char *csv_enc(const char *str, int quote, int oneline,
 				  struct buffer *output)
 {
 	chunk_reset(output);
-	return csv_enc_append(str, quote, output);
+	return csv_enc_append(str, quote, oneline, output);
 }
 
 /* Decode an URL-encoded string in-place. The resulting string might
diff --git a/src/stats.c b/src/stats.c
index d1f3daa..8b4ae34 100644
--- a/src/stats.c
+++ b/src/stats.c
@@ -382,7 +382,7 @@
 		out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
 		return out->data;
 	}
-	case FF_STR:   return csv_enc_append(field_str(f, 0), 1, out) != NULL;
+	case FF_STR:   return csv_enc_append(field_str(f, 0), 1, 2, out) != NULL;
 	default:       return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
 	}
 }
diff --git a/src/tools.c b/src/tools.c
index 9c307be..89e75ef 100644
--- a/src/tools.c
+++ b/src/tools.c
@@ -2006,11 +2006,19 @@
  * It is useful if the escaped string is used between double quotes in the
  * format.
  *
- *    printf("..., \"%s\", ...\r\n", csv_enc(str, 0, &trash));
+ *    printf("..., \"%s\", ...\r\n", csv_enc(str, 0, 0, &trash));
  *
  * If <quote> is 1, the converter puts the quotes only if any reserved character
  * is present. If <quote> is 2, the converter always puts the quotes.
  *
+ * If <oneline> is not 0, CRs are skipped and LFs are replaced by spaces.
+ * This re-format multi-lines strings to only one line. The purpose is to
+ * allow a line by line parsing but also to keep the output compliant with
+ * the CLI witch uses LF to defines the end of the response.
+ *
+ * If <oneline> is 2, In addition to previous action, the trailing spaces are
+ * removed.
+ *
  * <output> is a struct buffer used for storing the output string.
  *
  * The function returns the converted string on its output. If an error
@@ -2025,7 +2033,7 @@
  * the chunk. Please use csv_enc() instead if you want to replace the output
  * chunk.
  */
-const char *csv_enc_append(const char *str, int quote, struct buffer *output)
+const char *csv_enc_append(const char *str, int quote, int oneline, struct buffer *output)
 {
 	char *end = output->area + output->size;
 	char *out = output->area + output->data;
@@ -2041,6 +2049,19 @@
 		*ptr++ = '"';
 
 	while (*str && ptr < end - 2) { /* -2 for reserving space for <"> and \0. */
+		if (oneline) {
+			if (*str == '\n' ) {
+				/* replace LF by a space */
+				*ptr++ = ' ';
+				str++;
+				continue;
+			}
+			else if (*str == '\r' ) {
+				/* skip CR */
+				str++;
+				continue;
+			}
+		}
 		*ptr = *str;
 		if (*str == '"') {
 			ptr++;
@@ -2054,6 +2075,12 @@
 		str++;
 	}
 
+	if (oneline == 2) {
+		/* remove trailing spaces */
+		while (ptr > out && *(ptr - 1) == ' ')
+			ptr--;
+	}
+
 	if (quote)
 		*ptr++ = '"';