BUG/MEDIUM: conn-stream: Don't erase endpoint flags on reset

Only CS_EP_ERROR flag is now removed from the endpoint when a reset is
performed. When a new the endpoint is allocated, flags are preserved. It is
the caller responsibility to remove other flags, depending on its need.

Concretly, during a connection retry or a L7 retry, we must preserve
flags. In tcpcheck and the CLI, we reset flags.

This patch is 2.6-specific. No backport needed.
diff --git a/src/backend.c b/src/backend.c
index a887eb2..5a8bc0e 100644
--- a/src/backend.c
+++ b/src/backend.c
@@ -1577,6 +1577,7 @@
 					srv_conn = NULL;
 					if (cs_reset_endp(s->csb) < 0)
 						return SF_ERR_INTERNAL;
+					s->csb->endp->flags &= CS_EP_DETACHED;
 				}
 			}
 			else
diff --git a/src/check.c b/src/check.c
index 2db82c4..d65850d 100644
--- a/src/check.c
+++ b/src/check.c
@@ -1164,7 +1164,12 @@
 				TRACE_DEVEL("closing current connection", CHK_EV_TASK_WAKE|CHK_EV_HCHK_RUN, check);
 				check->state &= ~CHK_ST_CLOSE_CONN;
 				conn = NULL;
-				cs_reset_endp(check->cs); /* error will be handled by tcpcheck_main() */
+				if (!cs_reset_endp(check->cs)) {
+					/* error will be handled by tcpcheck_main().
+					 * On success, remove all flags except CS_EP_DETACHED
+					 */
+					check->cs->endp->flags &= CS_EP_DETACHED;
+				}
 				tcpcheck_main(check);
 			}
 			if (check->result == CHK_RES_UNKNOWN) {
diff --git a/src/cli.c b/src/cli.c
index 081d28c..b32b8b1 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -2761,6 +2761,7 @@
 					s->srv_error(s, s->csb);
 				return 1;
 			}
+			s->csb->endp->flags &= CS_EP_DETACHED;
 		}
 
 		sockaddr_free(&s->csb->dst);
diff --git a/src/conn_stream.c b/src/conn_stream.c
index fda8ab4..5f1508d 100644
--- a/src/conn_stream.c
+++ b/src/conn_stream.c
@@ -392,7 +392,8 @@
 
 	if (cs->endp) {
 		/* the cs is the only one one the endpoint */
-		cs_endpoint_init(cs->endp);
+		cs->endp->target = NULL;
+		cs->endp->ctx = NULL;
 		cs->endp->flags |= CS_EP_DETACHED;
 	}
 
@@ -442,12 +443,17 @@
 /* Resets the conn-stream endpoint. It happens when the app layer want to renew
  * its endpoint. For a connection retry for instance. If a mux or an applet is
  * attached, a new endpoint is created. Returns -1 on error and 0 on sucess.
+ *
+ * Only CS_EP_ERROR flag is removed on the endpoint. Orther flags are preserved.
+ * It is the caller responsibility to remove other flags if needed.
  */
 int cs_reset_endp(struct conn_stream *cs)
 {
 	struct cs_endpoint *new_endp;
 
 	BUG_ON(!cs->app);
+
+	cs->endp->flags &= ~CS_EP_ERROR;
 	if (!__cs_endp_target(cs)) {
 		/* endpoint not attached or attached to a mux with no
 		 * target. Thus the endpoint will not be release but just
@@ -465,6 +471,7 @@
 		cs->endp->flags |= CS_EP_ERROR;
 		return -1;
 	}
+	new_endp->flags = cs->endp->flags;
 
 	/* The app is still attached, the cs will not be released */
 	cs_detach_endp(&cs);
diff --git a/src/http_ana.c b/src/http_ana.c
index 7a987a5..a81c53d 100644
--- a/src/http_ana.c
+++ b/src/http_ana.c
@@ -1246,7 +1246,6 @@
 	req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
 	res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
 	res->analysers &= AN_RES_FLT_END;
-	cs->endp->flags &= ~CS_EP_RXBLK_SHUT;
 	s->conn_err_type = STRM_ET_NONE;
 	s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
 	s->conn_exp = TICK_ETERNITY;
@@ -1261,6 +1260,7 @@
 			s->flags |= SF_ERR_INTERNAL;
 		return -1;
 	}
+	cs->endp->flags &= ~CS_EP_RXBLK_SHUT;
 
 	b_free(&req->buf);
 	/* Swap the L7 buffer with the channel buffer */