MINOR: sample: provide a generic var-to-sample conversion function
We're using variable-to-sample conversion at least 4 times in the code,
two of which are bogus. Let's introduce a generic conversion function
that performs the required checks.
(cherry picked from commit 168e8de1d06adc7aa3e7e2cc2a36935a77c79b9c)
[cf: call to vars_get_by_desc() was updated to remove the third param]
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/include/haproxy/sample.h b/include/haproxy/sample.h
index 5f4a6cb..670205f 100644
--- a/include/haproxy/sample.h
+++ b/include/haproxy/sample.h
@@ -40,6 +40,7 @@
struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
struct stream *strm, unsigned int opt,
struct sample_expr *expr, int smp_type);
+int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type);
int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp);
int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp);
void release_sample_expr(struct sample_expr *expr);
diff --git a/src/sample.c b/src/sample.c
index 10e534e..3c31218 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -1635,6 +1635,24 @@
return 1;
}
+/* This function returns a sample struct filled with the conversion of variable
+ * <var> to sample type <type> (SMP_T_*), via a cast to the target type. If the
+ * variable cannot be retrieved or casted, 0 is returned, otherwise 1.
+ *
+ * Keep in mind that the sample content may be written to a pre-allocated
+ * trash chunk as returned by get_trash_chunk().
+ */
+int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type)
+{
+ if (!vars_get_by_desc(var, smp))
+ return 0;
+ if (!sample_casts[smp->data.type][type])
+ return 0;
+ if (!sample_casts[smp->data.type][type](smp))
+ return 0;
+ return 1;
+}
+
static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
{
blk_SHA_CTX ctx;