MINOR: applet: Add function to release appctx on error during init stage

appctx_free_on_early_error() must be used to release a freshly created
frontend appctx if an error occurred during the init stage. It takes care to
release the stream instead of the appctx if it exists. For a backend appctx,
it just calls appctx_free().
diff --git a/include/haproxy/applet.h b/include/haproxy/applet.h
index b9c581d..f8d180c 100644
--- a/include/haproxy/applet.h
+++ b/include/haproxy/applet.h
@@ -42,6 +42,7 @@
 
 struct appctx *appctx_new(struct applet *applet, struct cs_endpoint *endp);
 int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buffer *input);
+void appctx_free_on_early_error(struct appctx *appctx);
 
 /* Helper function to call .init applet callback function, if it exists. Returns 0
  * on success and -1 on error.
diff --git a/src/applet.c b/src/applet.c
index 4bb345b..e8b56c1 100644
--- a/src/applet.c
+++ b/src/applet.c
@@ -81,7 +81,8 @@
  * It returns 0 on success and -1 on error. In this case, it is the caller
  * responsibility to release the appctx. However, the session is released if it
  * was created. On success, if an error is encountered in the caller function,
- * the stream must be released instead of the appctx.
+ * the stream must be released instead of the appctx. To be sure,
+ * appctx_free_on_early_error() must be called in this case.
  */
 int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buffer *input)
 {
@@ -100,6 +101,21 @@
 	return 0;
 }
 
+/* Release function to call when an error occurred during init stage of a
+ * frontend appctx. For a backend appctx, it just calls appctx_free()
+ */
+void appctx_free_on_early_error(struct appctx *appctx)
+{
+	/* If a frontend apctx is attached to a conn-stream, release the stream
+	 * instead of the appctx.
+	 */
+	if (!(appctx->endp->flags & CS_EP_ORPHAN) && !(appctx_cs(appctx)->flags & CS_FL_ISBACK)) {
+		stream_free(appctx_strm(appctx));
+		return;
+	}
+	appctx_free(appctx);
+}
+
 /* reserves a command context of at least <size> bytes in the <appctx>, for
  * use by a CLI command or any regular applet. The pointer to this context is
  * stored in ctx.svcctx and is returned. The caller doesn't need to release