]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: provide a generic var-to-sample conversion function
authorWilly Tarreau <w@1wt.eu>
Wed, 6 Oct 2021 13:29:00 +0000 (15:29 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 6 Oct 2021 23:36:51 +0000 (01:36 +0200)
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.

include/haproxy/sample.h
src/sample.c

index 5f4a6cb08630c526d5b2fa7d80e085855ce04d49..670205f03cf01b16d63f5679f8c48857b45161ca 100644 (file)
@@ -40,6 +40,7 @@ struct sample *sample_process(struct proxy *px, struct session *sess,
 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);
index 67853d29606c60329ece1792516947b619d9667a..7cee16c34bcc65760c4665a3aedff94f26731196 100644 (file)
@@ -1630,6 +1630,24 @@ static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp
        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, NULL))
+               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;