REORG/MEDIUM: stream_interface: move applet->state and private to connection

The state and the private pointer are not specific to the applets, since SSL
will require exactly both of them. Move them to the connection layer now and
rename them. We also now ensure that both are NULL on first call.
diff --git a/src/dumpstats.c b/src/dumpstats.c
index 8a81678..6b4c84e 100644
--- a/src/dumpstats.c
+++ b/src/dumpstats.c
@@ -125,7 +125,7 @@
 	/* we have a dedicated I/O handler for the stats */
 	stream_int_register_handler(&s->si[1], &cli_applet);
 	copy_target(&s->target, &s->si[1].target); // for logging only
-	s->si[1].applet.private = s;
+	s->si[1].conn.data_ctx = s;
 	s->si[1].applet.st1 = 0;
 	s->si[1].applet.st0 = STAT_CLI_INIT;
 
@@ -417,7 +417,7 @@
 static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
 					   struct proxy *proxy, struct proxy *target)
 {
-	struct session *s = si->applet.private;
+	struct session *s = si->conn.data_ctx;
 
 	chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
 		     proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
@@ -506,7 +506,7 @@
 
 static void stats_sock_table_key_request(struct stream_interface *si, char **args, bool show)
 {
-	struct session *s = si->applet.private;
+	struct session *s = si->conn.data_ctx;
 	struct proxy *px = si->applet.ctx.table.target;
 	struct stksess *ts;
 	uint32_t uint32_key;
@@ -623,7 +623,7 @@
 static void stats_sock_table_request(struct stream_interface *si, char **args, bool show)
 {
 	si->applet.ctx.table.data_type = -1;
-	si->applet.state = STAT_ST_INIT;
+	si->conn.data_st = STAT_ST_INIT;
 	si->applet.ctx.table.target = NULL;
 	si->applet.ctx.table.proxy = NULL;
 	si->applet.ctx.table.entry = NULL;
@@ -745,7 +745,7 @@
  */
 static int stats_sock_parse_request(struct stream_interface *si, char *line)
 {
-	struct session *s = si->applet.private;
+	struct session *s = si->conn.data_ctx;
 	char *args[MAX_STATS_ARGS + 1];
 	int arg;
 
@@ -784,17 +784,17 @@
 
 			si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
 			si->applet.ctx.stats.flags |= STAT_FMT_CSV;
-			si->applet.state = STAT_ST_INIT;
+			si->conn.data_st = STAT_ST_INIT;
 			si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
 		}
 		else if (strcmp(args[1], "info") == 0) {
 			si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
 			si->applet.ctx.stats.flags |= STAT_FMT_CSV;
-			si->applet.state = STAT_ST_INIT;
+			si->conn.data_st = STAT_ST_INIT;
 			si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
 		}
 		else if (strcmp(args[1], "sess") == 0) {
-			si->applet.state = STAT_ST_INIT;
+			si->conn.data_st = STAT_ST_INIT;
 			if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
 				si->applet.ctx.cli.msg = stats_permission_denied_msg;
 				si->applet.st0 = STAT_CLI_PRINT;
@@ -819,7 +819,7 @@
 			else
 				si->applet.ctx.errors.iid	= -1;
 			si->applet.ctx.errors.px = NULL;
-			si->applet.state = STAT_ST_INIT;
+			si->conn.data_st = STAT_ST_INIT;
 			si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
 		}
 		else if (strcmp(args[1], "table") == 0) {
@@ -1567,10 +1567,10 @@
 
 	chunk_init(&msg, trash, trashlen);
 
-	switch (si->applet.state) {
+	switch (si->conn.data_st) {
 	case STAT_ST_INIT:
 		/* the function had not been called yet */
-		si->applet.state = STAT_ST_HEAD;
+		si->conn.data_st = STAT_ST_HEAD;
 		/* fall through */
 
 	case STAT_ST_HEAD:
@@ -1580,7 +1580,7 @@
 				return 0;
 		}
 
-		si->applet.state = STAT_ST_INFO;
+		si->conn.data_st = STAT_ST_INFO;
 		/* fall through */
 
 	case STAT_ST_INFO:
@@ -1633,7 +1633,7 @@
 		si->applet.ctx.stats.px = proxy;
 		si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
 		si->applet.ctx.stats.sv = NULL;
-		si->applet.state = STAT_ST_LIST;
+		si->conn.data_st = STAT_ST_LIST;
 		/* fall through */
 
 	case STAT_ST_LIST:
@@ -1654,11 +1654,11 @@
 			/* here, we just have reached the last proxy */
 		}
 
-		si->applet.state = STAT_ST_END;
+		si->conn.data_st = STAT_ST_END;
 		/* fall through */
 
 	case STAT_ST_END:
-		si->applet.state = STAT_ST_FIN;
+		si->conn.data_st = STAT_ST_FIN;
 		/* fall through */
 
 	case STAT_ST_FIN:
@@ -1666,7 +1666,7 @@
 
 	default:
 		/* unknown state ! */
-		si->applet.state = STAT_ST_FIN;
+		si->conn.data_st = STAT_ST_FIN;
 		return 1;
 	}
 }
@@ -1678,12 +1678,12 @@
  */
 static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
 {
-	struct session *s = si->applet.private;
+	struct session *s = si->conn.data_ctx;
 	struct chunk msg;
 
 	chunk_init(&msg, trash, trashlen);
 
-	switch (si->applet.state) {
+	switch (si->conn.data_st) {
 	case STAT_ST_INIT:
 		chunk_printf(&msg,
 			"HTTP/1.0 303 See Other\r\n"
@@ -1709,7 +1709,7 @@
 		if (!(s->flags & SN_FINST_MASK))
 			s->flags |= SN_FINST_R;
 
-		si->applet.state = STAT_ST_FIN;
+		si->conn.data_st = STAT_ST_FIN;
 		return 1;
 	}
 	return 1;
@@ -1723,7 +1723,7 @@
  */
 static void http_stats_io_handler(struct stream_interface *si)
 {
-	struct session *s = si->applet.private;
+	struct session *s = si->conn.data_ctx;
 	struct buffer *req = si->ob;
 	struct buffer *res = si->ib;
 
@@ -1779,7 +1779,7 @@
  */
 static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
 {
-	struct session *s = si->applet.private;
+	struct session *s = si->conn.data_ctx;
 	struct buffer *rep = si->ib;
 	struct proxy *px;
 	struct chunk msg;
@@ -1787,7 +1787,7 @@
 
 	chunk_init(&msg, trash, trashlen);
 
-	switch (si->applet.state) {
+	switch (si->conn.data_st) {
 	case STAT_ST_INIT:
 		chunk_printf(&msg,
 			     "HTTP/1.0 200 OK\r\n"
@@ -1813,11 +1813,11 @@
 
 		if (s->txn.meth == HTTP_METH_HEAD) {
 			/* that's all we return in case of HEAD request */
-			si->applet.state = STAT_ST_FIN;
+			si->conn.data_st = STAT_ST_FIN;
 			return 1;
 		}
 
-		si->applet.state = STAT_ST_HEAD; /* let's start producing data */
+		si->conn.data_st = STAT_ST_HEAD; /* let's start producing data */
 		/* fall through */
 
 	case STAT_ST_HEAD:
@@ -1921,7 +1921,7 @@
 		if (bi_putchk(rep, &msg) == -1)
 			return 0;
 
-		si->applet.state = STAT_ST_INFO;
+		si->conn.data_st = STAT_ST_INFO;
 		/* fall through */
 
 	case STAT_ST_INFO:
@@ -2099,7 +2099,7 @@
 
 		si->applet.ctx.stats.px = proxy;
 		si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
-		si->applet.state = STAT_ST_LIST;
+		si->conn.data_st = STAT_ST_LIST;
 		/* fall through */
 
 	case STAT_ST_LIST:
@@ -2118,7 +2118,7 @@
 		}
 		/* here, we just have reached the last proxy */
 
-		si->applet.state = STAT_ST_END;
+		si->conn.data_st = STAT_ST_END;
 		/* fall through */
 
 	case STAT_ST_END:
@@ -2128,7 +2128,7 @@
 				return 0;
 		}
 
-		si->applet.state = STAT_ST_FIN;
+		si->conn.data_st = STAT_ST_FIN;
 		/* fall through */
 
 	case STAT_ST_FIN:
@@ -2136,7 +2136,7 @@
 
 	default:
 		/* unknown state ! */
-		si->applet.state = STAT_ST_FIN;
+		si->conn.data_st = STAT_ST_FIN;
 		return -1;
 	}
 }
@@ -2149,7 +2149,7 @@
  */
 static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
 {
-	struct session *s = si->applet.private;
+	struct session *s = si->conn.data_ctx;
 	struct buffer *rep = si->ib;
 	struct server *sv, *svs;	/* server and server-state, server-state=server or server->track */
 	struct listener *l;
@@ -3454,7 +3454,7 @@
 		/* If we're forced to shut down, we might have to remove our
 		 * reference to the last session being dumped.
 		 */
-		if (si->applet.state == STAT_ST_LIST) {
+		if (si->conn.data_st == STAT_ST_LIST) {
 			if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
 				LIST_DEL(&si->applet.ctx.sess.bref.users);
 				LIST_INIT(&si->applet.ctx.sess.bref.users);
@@ -3465,7 +3465,7 @@
 
 	chunk_init(&msg, trash, trashlen);
 
-	switch (si->applet.state) {
+	switch (si->conn.data_st) {
 	case STAT_ST_INIT:
 		/* the function had not been called yet, let's prepare the
 		 * buffer for a response. We initialize the current session
@@ -3476,7 +3476,7 @@
 		 */
 		LIST_INIT(&si->applet.ctx.sess.bref.users);
 		si->applet.ctx.sess.bref.ref = sessions.n;
-		si->applet.state = STAT_ST_LIST;
+		si->conn.data_st = STAT_ST_LIST;
 		/* fall through */
 
 	case STAT_ST_LIST:
@@ -3641,11 +3641,11 @@
 			return 1;
 		}
 
-		si->applet.state = STAT_ST_FIN;
+		si->conn.data_st = STAT_ST_FIN;
 		/* fall through */
 
 	default:
-		si->applet.state = STAT_ST_FIN;
+		si->conn.data_st = STAT_ST_FIN;
 		return 1;
 	}
 }
@@ -3657,14 +3657,14 @@
  */
 static int stats_table_request(struct stream_interface *si, bool show)
 {
-	struct session *s = si->applet.private;
+	struct session *s = si->conn.data_ctx;
 	struct chunk msg;
 	struct ebmb_node *eb;
 	int dt;
 	bool skip_entry;
 
 	/*
-	 * We have 3 possible states in si->applet.state :
+	 * We have 3 possible states in si->conn.data_st :
 	 *   - STAT_ST_INIT : the first call
 	 *   - STAT_ST_INFO : the proxy pointer points to the next table to
 	 *     dump, the entry pointer is NULL ;
@@ -3677,7 +3677,7 @@
 
 	if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
 		/* in case of abort, remove any refcount we might have set on an entry */
-		if (si->applet.state == STAT_ST_LIST) {
+		if (si->conn.data_st == STAT_ST_LIST) {
 			si->applet.ctx.table.entry->ref_cnt--;
 			stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
 		}
@@ -3686,22 +3686,22 @@
 
 	chunk_init(&msg, trash, trashlen);
 
-	while (si->applet.state != STAT_ST_FIN) {
-		switch (si->applet.state) {
+	while (si->conn.data_st != STAT_ST_FIN) {
+		switch (si->conn.data_st) {
 		case STAT_ST_INIT:
 			si->applet.ctx.table.proxy = si->applet.ctx.table.target;
 			if (!si->applet.ctx.table.proxy)
 				si->applet.ctx.table.proxy = proxy;
 
 			si->applet.ctx.table.entry = NULL;
-			si->applet.state = STAT_ST_INFO;
+			si->conn.data_st = STAT_ST_INFO;
 			break;
 
 		case STAT_ST_INFO:
 			if (!si->applet.ctx.table.proxy ||
 			    (si->applet.ctx.table.target &&
 			     si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
-				si->applet.state = STAT_ST_END;
+				si->conn.data_st = STAT_ST_END;
 				break;
 			}
 
@@ -3717,7 +3717,7 @@
 					if (eb) {
 						si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
 						si->applet.ctx.table.entry->ref_cnt++;
-						si->applet.state = STAT_ST_LIST;
+						si->conn.data_st = STAT_ST_LIST;
 						break;
 					}
 				}
@@ -3797,11 +3797,11 @@
 				stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
 
 			si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
-			si->applet.state = STAT_ST_INFO;
+			si->conn.data_st = STAT_ST_INFO;
 			break;
 
 		case STAT_ST_END:
-			si->applet.state = STAT_ST_FIN;
+			si->conn.data_st = STAT_ST_FIN;
 			break;
 		}
 	}
diff --git a/src/peers.c b/src/peers.c
index 6fcf2f3..721eecb 100644
--- a/src/peers.c
+++ b/src/peers.c
@@ -185,9 +185,9 @@
 {
 	struct task *t = (struct task *)si->owner;
 	struct session *s = (struct session *)t->context;
-	struct peer_session *ps = (struct peer_session *)si->applet.private;
+	struct peer_session *ps = (struct peer_session *)si->conn.data_ctx;
 
-	/* si->applet.private is not a peer session */
+	/* si->conn.data_ctx is not a peer session */
 	if (si->applet.st0 < PEER_SESSION_SENDSUCCESS)
 		return;
 
@@ -227,7 +227,7 @@
 switchstate:
 		switch(si->applet.st0) {
 			case PEER_SESSION_ACCEPT:
-				si->applet.private = NULL;
+				si->conn.data_ctx = NULL;
 				si->applet.st0 = PEER_SESSION_GETVERSION;
 				/* fall through */
 			case PEER_SESSION_GETVERSION:
@@ -333,12 +333,12 @@
 					goto switchstate;
 				}
 
-				si->applet.private = curpeer;
+				si->conn.data_ctx = curpeer;
 				si->applet.st0 = PEER_SESSION_GETTABLE;
 				/* fall through */
 			}
 			case PEER_SESSION_GETTABLE: {
-				struct peer *curpeer = (struct peer *)si->applet.private;
+				struct peer *curpeer = (struct peer *)si->conn.data_ctx;
 				struct shared_table *st;
 				struct peer_session *ps = NULL;
 				unsigned long key_type;
@@ -349,12 +349,12 @@
 				if (reql <= 0) { /* closed or EOL not found */
 					if (reql == 0)
 						goto out;
-					si->applet.private = NULL;
+					si->conn.data_ctx = NULL;
 					si->applet.st0 = PEER_SESSION_END;
 					goto switchstate;
 				}
-				/* Re init si->applet.private to null, to handle correctly a release case */
-				si->applet.private = NULL;
+				/* Re init si->conn.data_ctx to null, to handle correctly a release case */
+				si->conn.data_ctx = NULL;
 
 				if (trash[reql-1] != '\n') {
 					/* Incomplete line, we quit */
@@ -380,7 +380,7 @@
 
 				p = strchr(p+1, ' ');
 				if (!p) {
-					si->applet.private = NULL;
+					si->conn.data_ctx = NULL;
 					si->applet.st0 = PEER_SESSION_EXIT;
 					si->applet.st1 = PEER_SESSION_ERRPROTO;
 					goto switchstate;
@@ -439,12 +439,12 @@
 					goto switchstate;
 				}
 
-				si->applet.private = ps;
+				si->conn.data_ctx = ps;
 				si->applet.st0 = PEER_SESSION_SENDSUCCESS;
 				/* fall through */
 			}
 			case PEER_SESSION_SENDSUCCESS:{
-				struct peer_session *ps = (struct peer_session *)si->applet.private;
+				struct peer_session *ps = (struct peer_session *)si->conn.data_ctx;
 
 				repl = snprintf(trash, trashlen, "%d\n", PEER_SESSION_SUCCESSCODE);
 				repl = bi_putblk(si->ib, trash, repl);
@@ -494,7 +494,7 @@
 				goto switchstate;
 			}
 			case PEER_SESSION_CONNECT: {
-				struct peer_session *ps = (struct peer_session *)si->applet.private;
+				struct peer_session *ps = (struct peer_session *)si->conn.data_ctx;
 
 				/* Send headers */
 				repl = snprintf(trash, trashlen,
@@ -524,7 +524,7 @@
 				/* fall through */
 			}
 			case PEER_SESSION_GETSTATUS: {
-				struct peer_session *ps = (struct peer_session *)si->applet.private;
+				struct peer_session *ps = (struct peer_session *)si->conn.data_ctx;
 
 				if (si->ib->flags & BF_WRITE_PARTIAL)
 					ps->statuscode = PEER_SESSION_CONNECTEDCODE;
@@ -595,7 +595,7 @@
 				/* fall through */
 			}
 			case PEER_SESSION_WAITMSG: {
-				struct peer_session *ps = (struct peer_session *)si->applet.private;
+				struct peer_session *ps = (struct peer_session *)si->conn.data_ctx;
 				char c;
 				int totl = 0;
 
@@ -1064,7 +1064,7 @@
 	/* call release to reinit resync states if needed */
 	peer_session_release(oldsi);
 	oldsi->applet.st0 = PEER_SESSION_END;
-	oldsi->applet.private = NULL;
+	oldsi->conn.data_ctx = NULL;
 	task_wakeup(session->task, TASK_WOKEN_MSG);
 }
 
@@ -1079,7 +1079,7 @@
 	 /* we have a dedicated I/O handler for the stats */
 	stream_int_register_handler(&s->si[1], &peer_applet);
 	copy_target(&s->target, &s->si[1].target); // for logging only
-	s->si[1].applet.private = s;
+	s->si[1].conn.data_ctx = s;
 	s->si[1].applet.st0 = PEER_SESSION_ACCEPT;
 
 	tv_zero(&s->logs.tv_request);
@@ -1162,7 +1162,7 @@
 	s->si[0].flags = SI_FL_NONE;
 	if (s->fe->options2 & PR_O2_INDEPSTR)
 		s->si[0].flags |= SI_FL_INDEP_STR;
-	s->si[0].applet.private = (void *)ps;
+	s->si[0].conn.data_ctx = (void *)ps;
 	s->si[0].applet.st0 = PEER_SESSION_CONNECT;
 
 	stream_int_register_handler(&s->si[0], &peer_applet);
diff --git a/src/proto_http.c b/src/proto_http.c
index 189fcf3..7cf413d 100644
--- a/src/proto_http.c
+++ b/src/proto_http.c
@@ -2967,7 +2967,7 @@
 		s->task->nice = -32; /* small boost for HTTP statistics */
 		stream_int_register_handler(s->rep->prod, &http_stats_applet);
 		copy_target(&s->target, &s->rep->prod->target); // for logging only
-		s->rep->prod->applet.private = s;
+		s->rep->prod->conn.data_ctx = s;
 		s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
 		req->analysers = 0;
 		if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
diff --git a/src/stream_interface.c b/src/stream_interface.c
index c70ee35..1005eaf 100644
--- a/src/stream_interface.c
+++ b/src/stream_interface.c
@@ -341,7 +341,6 @@
 	stream_interface_prepare(si, &stream_int_embedded);
 	si->conn.ctrl = NULL;
 	set_target_applet(&si->target, app);
-	si->applet.state = 0;
 	si->release   = app->release;
 	si->flags |= SI_FL_WAIT_DATA;
 	return si->owner;