[MEDIUM] stats: prepare the connection for closing before dumping

We will need to modify the stats dump functions so that they can
be used in interactive mode. For this, we want their caller to
prepare the connection for a close, not themselves to do it.
Let's simply move the stream_int_retnclose() out.
diff --git a/src/dumpstats.c b/src/dumpstats.c
index 681ae89..97de0e9 100644
--- a/src/dumpstats.c
+++ b/src/dumpstats.c
@@ -272,16 +272,19 @@
 			s->data_ctx.stats.flags |= STAT_SHOW_STAT;
 			s->data_ctx.stats.flags |= STAT_FMT_CSV;
 			s->ana_state = STATS_ST_REP;
+			stream_int_retnclose(s->rep->cons, NULL);
 			buffer_install_hijacker(s, s->rep, stats_dump_raw_to_buffer);
 		}
 		else if (strcmp(args[1], "info") == 0) {
 			s->data_ctx.stats.flags |= STAT_SHOW_INFO;
 			s->data_ctx.stats.flags |= STAT_FMT_CSV;
 			s->ana_state = STATS_ST_REP;
+			stream_int_retnclose(s->rep->cons, NULL);
 			buffer_install_hijacker(s, s->rep, stats_dump_raw_to_buffer);
 		}
 		else if (strcmp(args[1], "sess") == 0) {
 			s->ana_state = STATS_ST_REP;
+			stream_int_retnclose(s->rep->cons, NULL);
 			buffer_install_hijacker(s, s->rep, stats_dump_sess_to_buffer);
 		}
 		else if (strcmp(args[1], "errors") == 0) {
@@ -291,6 +294,7 @@
 				s->data_ctx.errors.iid	= -1;
 			s->data_ctx.errors.px = NULL;
 			s->ana_state = STATS_ST_REP;
+			stream_int_retnclose(s->rep->cons, NULL);
 			buffer_install_hijacker(s, s->rep, stats_dump_errors_to_buffer);
 		}
 		else { /* neither "stat" nor "info" nor "sess" */
@@ -391,7 +395,6 @@
 		/* the function had not been called yet, let's prepare the
 		 * buffer for a response.
 		 */
-		stream_int_retnclose(rep->cons, &msg);
 		s->data_state = DATA_ST_HEAD;
 		/* fall through */
 
@@ -538,7 +541,9 @@
 		chunk_printf(&msg, sizeof(trash), "\r\n");
 
 		s->txn.status = 200;
-		stream_int_retnclose(rep->cons, &msg); // send the start of the response.
+		if (buffer_write_chunk(rep, &msg) >= 0)
+			return 0;
+
 		msg.len = 0;
 
 		if (!(s->flags & SN_ERR_MASK))  // this is not really an error but it is
@@ -1404,7 +1409,6 @@
 		 * this pointer. We know we have reached the end when this
 		 * pointer points back to the head of the sessions list.
 		 */
-		stream_int_retnclose(rep->cons, &msg);
 		LIST_INIT(&s->data_ctx.sess.bref.users);
 		s->data_ctx.sess.bref.ref = sessions.n;
 		s->data_state = DATA_ST_LIST;
@@ -1647,7 +1651,6 @@
 		/* the function had not been called yet, let's prepare the
 		 * buffer for a response.
 		 */
-		stream_int_retnclose(rep->cons, &msg);
 		s->data_ctx.errors.px = proxy;
 		s->data_ctx.errors.buf = 0;
 		s->data_ctx.errors.bol = 0;
diff --git a/src/proto_http.c b/src/proto_http.c
index a159769..fd76887 100644
--- a/src/proto_http.c
+++ b/src/proto_http.c
@@ -3257,7 +3257,9 @@
 	}
 	else if (s->data_source == DATA_SRC_STATS) {
 		/* dump server statistics */
-		int ret = stats_dump_http(s, rep, s->be->uri_auth);
+		int ret;
+		stream_int_retnclose(rep->cons, NULL);
+		ret = stats_dump_http(s, rep, s->be->uri_auth);
 		if (ret >= 0)
 			return;
 		/* -1 indicates an error */