]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: samples: add a function to fetch and convert any sample to a string
authorWilly Tarreau <w@1wt.eu>
Thu, 20 Dec 2012 23:02:32 +0000 (00:02 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 21 Dec 2012 16:57:24 +0000 (17:57 +0100)
Any sample type can now easily be converted to a string that can be used
anywhere. This will be used for logging and passing information in headers.

include/proto/sample.h
src/sample.c

index e6808dd4baec5b77c93c8b08d20d132968f0a083..5241a3a7ecc6a8fafcaf17dcb19d64fa55e00fa0 100644 (file)
@@ -34,6 +34,8 @@ struct sample_expr *sample_parse_expr(char **str, int *idx, char *err, int err_s
 struct sample *sample_process(struct proxy *px, struct session *l4,
                                void *l7, unsigned int dir, struct sample_expr *expr,
                                struct sample *p);
+struct sample *sample_fetch_string(struct proxy *px, struct session *l4, void *l7,
+                                   unsigned int opt, struct sample_expr *expr);
 void sample_register_fetches(struct sample_fetch_kw_list *psl);
 void sample_register_convs(struct sample_conv_kw_list *psl);
 struct chunk *sample_get_trash_chunk(void);
index f5828fa90f72a9a77d9ffb1b2a35b325a25ade5f..3c0d01ef4a77123c29594b6199553473a27d30aa 100644 (file)
@@ -539,6 +539,31 @@ struct sample *sample_process(struct proxy *px, struct session *l4, void *l7,
        return p;
 }
 
+/*
+ * Process a fetch + format conversion as defined by the sample expression <expr>
+ * on request or response considering the <opt> parameter. The output is always of
+ * type string. Returns either NULL if no sample could be extracted, or a pointer
+ * to the converted result stored in static temp_smp in format string.
+ */
+struct sample *sample_fetch_string(struct proxy *px, struct session *l4, void *l7,
+                                   unsigned int opt, struct sample_expr *expr)
+{
+       struct sample *smp;
+
+       smp = sample_process(px, l4, l7, opt, expr, NULL);
+       if (!smp)
+               return NULL;
+
+       if (!sample_casts[smp->type][SMP_T_CSTR])
+               return NULL;
+
+       if (!sample_casts[smp->type][SMP_T_CSTR](smp))
+               return NULL;
+
+       smp->type = SMP_T_CSTR;
+       return smp;
+}
+
 /*****************************************************************/
 /*    Sample format convert functions                            */
 /*    These functions set the data type on return.               */