MAJOR: connections: Defer mux creation for outgoing connection if alpn is set.

If an ALPN (or a NPN) was chosen for a server, defer choosing the mux until
after the SSL handshake is done, and the ALPN/NPN has been negociated, so
that we know which mux to pick.
diff --git a/include/proto/backend.h b/include/proto/backend.h
index 69ee31c..54ae057 100644
--- a/include/proto/backend.h
+++ b/include/proto/backend.h
@@ -31,7 +31,7 @@
 #include <types/stream.h>
 
 int assign_server(struct stream *s);
-int assign_server_address(struct stream *s);
+int assign_server_address(struct stream *s, struct connection *srv_conn);
 int assign_server_and_queue(struct stream *s);
 int connect_server(struct stream *s);
 int srv_redispatch_connect(struct stream *t);
diff --git a/include/proto/connection.h b/include/proto/connection.h
index 611e6ad..11d4aa4 100644
--- a/include/proto/connection.h
+++ b/include/proto/connection.h
@@ -672,6 +672,16 @@
 		LIST_DEL(&sess->conn_list);
 		LIST_INIT(&sess->conn_list);
 	}
+	/* If we temporarily stored the connection as the stream_interface's
+	 * end point, remove it.
+	 */
+	if (conn->mux_ctx != NULL && conn->mux == NULL) {
+		struct stream *s = conn->mux_ctx;
+
+		if (objt_conn(s->si[1].end) == conn)
+			s->si[1].end = NULL;
+	}
+
 	conn_force_unsubscribe(conn);
 	LIST_DEL(&conn->list);
 	LIST_INIT(&conn->list);
@@ -1034,6 +1044,9 @@
 	if (srv && srv->mux_proto)
 		mux_ops = srv->mux_proto->mux;
 	else {
+		struct ist mux_proto;
+		const char *alpn_str = NULL;
+		int alpn_len = 0;
 		int mode;
 
 		if (prx->mode == PR_MODE_TCP)
@@ -1043,7 +1056,10 @@
 		else
 			mode = PROTO_MODE_HTTP;
 
+		conn_get_alpn(conn, &alpn_str, &alpn_len);
+		mux_proto = ist2(alpn_str, alpn_len);
+
-		mux_ops = conn_get_best_mux(conn, ist(NULL), PROTO_SIDE_BE, mode);
+		mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
 		if (!mux_ops)
 			return -1;
 	}
diff --git a/include/proto/stream_interface.h b/include/proto/stream_interface.h
index 3d2a6db..5568fdb 100644
--- a/include/proto/stream_interface.h
+++ b/include/proto/stream_interface.h
@@ -460,10 +460,8 @@
 }
 
 /* Calls chk_snd on the connection using the ctrl layer */
-static inline int si_connect(struct stream_interface *si)
+static inline int si_connect(struct stream_interface *si, struct connection *conn)
 {
-	struct conn_stream *cs = objt_cs(si->end);
-	struct connection *conn = cs_conn(cs);
 	int ret = SF_ERR_NONE;
 
 	if (unlikely(!conn || !conn->ctrl || !conn->ctrl->connect))