#include <haproxy/api.h>
#include <haproxy/acl.h>
#include <haproxy/activity.h>
+#include <haproxy/applet.h>
#include <haproxy/arg.h>
#include <haproxy/backend.h>
#include <haproxy/channel.h>
DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_CS_ST, s);
if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
- struct appctx *appctx;
-
- /* The target is an applet but the SC is in SC_ST_REQ. Thus it
- * means no appctx are attached to the SC. Otherwise, it will be
- * in SC_ST_RDY state. So, try to create the appctx now.
- */
- BUG_ON(sc_appctx(sc));
- appctx = sc_applet_create(sc, objt_applet(s->target));
- if (!appctx) {
- /* No more memory, let's immediately abort. Force the
- * error code to ignore the ERR_LOCAL which is not a
- * real error.
- */
- s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
-
- sc_abort(sc);
- sc_shutdown(sc);
- sc->flags |= SC_FL_ERROR;
- s->conn_err_type = STRM_ET_CONN_RES;
- sc->state = SC_ST_CLO;
- if (s->srv_error)
- s->srv_error(s, sc);
- DBG_TRACE_STATE("failed to register applet", STRM_EV_STRM_PROC|STRM_EV_CS_ST|STRM_EV_STRM_ERR, s);
- goto end;
+ struct appctx *appctx = sc_appctx(sc);
+
+ if (appctx == NULL) {
+ appctx = sc_applet_create(sc, objt_applet(s->target));
+ if (!appctx) {
+ /* No more memory, let's immediately abort. Force the
+ * error code to ignore the ERR_LOCAL which is not a
+ * real error.
+ */
+ s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
+
+ sc_abort(sc);
+ sc_shutdown(sc);
+ sc->flags |= SC_FL_ERROR;
+ s->conn_err_type = STRM_ET_CONN_RES;
+ sc->state = SC_ST_CLO;
+ if (s->srv_error)
+ s->srv_error(s, sc);
+ DBG_TRACE_STATE("failed to register applet", STRM_EV_STRM_PROC|STRM_EV_CS_ST|STRM_EV_STRM_ERR, s);
+ goto end;
+ }
+ DBG_TRACE_STATE("applet registered", STRM_EV_STRM_PROC|STRM_EV_CS_ST, s);
}
- DBG_TRACE_STATE("applet registered", STRM_EV_STRM_PROC|STRM_EV_CS_ST, s);
+ BUG_ON(appctx->applet != __objt_applet(s->target));
+
+ sc->state = SC_ST_RDY;
+ sc_will_read(sc);
+ applet_need_more_data(appctx);
+ appctx_wakeup(appctx);
+ DBG_TRACE_STATE("applet ready", STRM_EV_STRM_PROC|STRM_EV_CS_ST, s);
goto end;
}
}
}
-/* This function parses the use-service action ruleset. It executes
+/* This function executes the use-service action ruleset. It executes
* the associated ACL and set an applet as a stream or txn final node.
- * it returns ACT_RET_ERR if an error occurs, the proxy left in
- * consistent state. It returns ACT_RET_STOP in success case because
- * use-service must be a terminal action. Returns ACT_RET_YIELD
- * if the initialisation function require more data.
+ * It returns ACT_RET_ERR if an error occurs, the proxy left in
+ * consistent state, or ACT_RET_STOP in success case because use-service
+ * must be a terminal action.
+ *
+ * The applet is not woken up here. sc_applet_create() attaches it to the
+ * back stream connector in a dormant state: it remains in SC_ST_INI
+ * state and it is not scheduled. This way, the request analysis is not
+ * disturbed by the applet: it will not try to consume the request
+ * before the analysis ends and it cannot produce a premature response.
+ * The applet is woken up by back_handle_st_req(), when the request
+ * analysis is over and the stream connector switches to the ready
+ * state.
*/
enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
struct session *sess, struct stream *s, int flags)
{
struct appctx *appctx;
- /* Initialises the applet if it is required. */
- if (flags & ACT_OPT_FIRST) {
- /* Register applet. this function schedules the applet. */
- stream_set_target(s, &rule->applet.obj_type);
- appctx = sc_applet_create(s->scb, objt_applet(s->target));
- if (unlikely(!appctx))
- return ACT_RET_ERR;
+ /* Register the applet. It is attached to the back stream connector,
+ * in a dormant state, and it will be woken up when the request
+ * analysis is over.
+ */
+ stream_set_target(s, &rule->applet.obj_type);
+ appctx = sc_applet_create(s->scb, objt_applet(s->target));
+ if (unlikely(!appctx))
+ return ACT_RET_ERR;
- /* Finish initialisation of the context. */
- if (appctx_init(appctx) == -1)
- return ACT_RET_ERR;
- }
- else
- appctx = __sc_appctx(s->scb);
+ /* Finish initialisation of the context. */
+ if (appctx_init(appctx) == -1)
+ return ACT_RET_ERR;
if (rule->from != ACT_F_HTTP_REQ) {
/* report it if the request was intercepted by the frontend */
s->flags |= SF_ASSIGNED;
}
- /* Now we can schedule the applet. */
- applet_need_more_data(appctx);
- appctx_wakeup(appctx);
return ACT_RET_STOP;
}