]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: sample: generalize sample_fetch_string() as sample_fetch_as_type()
authorAdis Nezirovic <anezirovic@haproxy.com>
Mon, 6 Jul 2015 13:41:02 +0000 (15:41 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 6 Jul 2015 14:17:25 +0000 (16:17 +0200)
This modification makes possible to use sample_fetch_string() in more places,
where we might need to fetch sample values which are not plain strings. This
way we don't need to fetch string, and convert it into another type afterwards.

When using aliased types, the caller should explicitly check which exact type
was returned (e.g. SMP_T_IPV4 or SMP_T_IPV6 for SMP_T_ADDR).

All usages of sample_fetch_string() are converted to use new function.

include/proto/sample.h
src/log.c
src/proto_http.c
src/proto_tcp.c
src/sample.c

index 02ccc520616fdc3570f11c3e2184fc13d24fb49c..6a01ed3307f20d1059fdbb771f89d1d8b71d6179 100644 (file)
@@ -33,9 +33,9 @@ struct sample_conv *find_sample_conv(const char *kw, int len);
 struct sample *sample_process(struct proxy *px, struct session *sess,
                               struct stream *strm, unsigned int opt,
                               struct sample_expr *expr, struct sample *p);
-struct sample *sample_fetch_string(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);
+                                   struct sample_expr *expr, int smp_type);
 void sample_register_fetches(struct sample_fetch_kw_list *psl);
 void sample_register_convs(struct sample_conv_kw_list *psl);
 const char *sample_src_names(unsigned int use);
index f0db0a82913e39a6571f22e49f59ae52a291bca5..577618cffce71d1c56a0e4ed32c4decfb736ad3b 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -980,9 +980,9 @@ int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list
                        case LOG_FMT_EXPR: // sample expression, may be request or response
                                key = NULL;
                                if (tmp->options & LOG_OPT_REQ_CAP)
-                                       key = sample_fetch_string(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr);
+                                       key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
                                if (!key && (tmp->options & LOG_OPT_RES_CAP))
-                                       key = sample_fetch_string(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr);
+                                       key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
                                if (tmp->options & LOG_OPT_HTTP)
                                        ret = encode_chunk(tmplog, dst + maxsize,
                                                           '%', http_encode_map, key ? &key->data.str : &empty);
index 1c30b3bf454e979176d70dfd91e5fbcf09b881ad..c24479240634b5963e0ccc9eeb902a7b308ee5a4 100644 (file)
@@ -12498,7 +12498,7 @@ int http_action_req_capture(struct http_req_rule *rule, struct proxy *px, struct
        char **cap = s->req_cap;
        int len;
 
-       key = sample_fetch_string(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr);
+       key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr, SMP_T_STR);
        if (!key)
                return 1;
 
@@ -12541,7 +12541,7 @@ int http_action_req_capture_by_id(struct http_req_rule *rule, struct proxy *px,
        if (!h)
                return 1;
 
-       key = sample_fetch_string(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr);
+       key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr, SMP_T_STR);
        if (!key)
                return 1;
 
@@ -12710,7 +12710,7 @@ int http_action_res_capture_by_id(struct http_res_rule *rule, struct proxy *px,
        if (!h)
                return 1;
 
-       key = sample_fetch_string(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, expr);
+       key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, expr, SMP_T_STR);
        if (!key)
                return 1;
 
index 6d1ec03a992a047e6247e15d66eaa366be4dfb47..9f9de9bae6efcccd302e1d0377426b07d929850f 100644 (file)
@@ -1206,7 +1206,7 @@ resume_execution:
                                char **cap = s->req_cap;
                                int len;
 
-                               key = sample_fetch_string(s->be, sess, s, SMP_OPT_DIR_REQ | partial, rule->act_prm.cap.expr);
+                               key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ | partial, rule->act_prm.cap.expr, SMP_T_STR);
                                if (!key)
                                        continue;
 
index 309ea11a2cdaf76690bf1deb79c6a14ac2210cb9..330f08a53ce832faff708dd1f690b73bb6b3e023 100644 (file)
@@ -1338,8 +1338,9 @@ int smp_resolve_args(struct proxy *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. If a stable sample can be fetched, or an unstable one
- * when <opt> contains SMP_OPT_FINAL, the sample is converted to a string and
+ * not explicitly set to <smp_type>, but shall be compatible with it as
+ * specified by 'sample_casts' table. If a stable sample can be fetched, or an
+ * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
  * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
  * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
  * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
@@ -1355,9 +1356,9 @@ int smp_resolve_args(struct proxy *p)
  *   smp      1        0     Not present yet, may appear later (eg: header)
  *   smp      1        1     never happens (either flag is cleared on output)
  */
-struct sample *sample_fetch_string(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)
+                                   struct sample_expr *expr, int smp_type)
 {
        struct sample *smp = &temp_smp;
 
@@ -1369,13 +1370,12 @@ struct sample *sample_fetch_string(struct proxy *px, struct session *sess,
                return NULL;
        }
 
-       if (!sample_casts[smp->type][SMP_T_STR])
+       if (!sample_casts[smp->type][smp_type])
                return NULL;
 
-       if (!sample_casts[smp->type][SMP_T_STR](smp))
+       if (!sample_casts[smp->type][smp_type](smp))
                return NULL;
 
-       smp->type = SMP_T_STR;
        smp->flags &= ~SMP_F_MAY_CHANGE;
        return smp;
 }