mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-08-07 07:37:02 +02:00
CLEANUP: sample: generalize sample_fetch_string() as sample_fetch_as_type()
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.
This commit is contained in:
parent
4e5d58e517
commit
79beb248b9
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
||||
|
@ -1206,7 +1206,7 @@ int tcp_inspect_request(struct stream *s, struct channel *req, int an_bit)
|
||||
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;
|
||||
|
||||
|
14
src/sample.c
14
src/sample.c
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user