MEDIUM: map: uses HAProxy facilities to store default value

The default value is stored in a special struct that describe the
map. This default value is parsed with special parser. This is
useless because HAProxy provides a space to store the default
value (the args) and HAProxy provides also standard parser for
the input types (args again).

This patch remove this special storage and replace it by an argument.
In other way the args of maps are declared as the expected type in
place of strings.
This commit is contained in:
Thierry FOURNIER 2015-03-13 00:10:16 +01:00 committed by Willy Tarreau
parent 9e7ec08976
commit 933e5deb2b
2 changed files with 43 additions and 45 deletions

View File

@ -37,9 +37,6 @@ struct map_descriptor {
struct sample_conv *conv; /* original converter descriptor */ struct sample_conv *conv; /* original converter descriptor */
struct pattern_head pat; /* the pattern matching associated to the map */ struct pattern_head pat; /* the pattern matching associated to the map */
int do_free; /* set if <pat> is the orignal pat and must be freed */ int do_free; /* set if <pat> is the orignal pat and must be freed */
char *default_value; /* a copy of default value. This copy is
useful if the type is str */
struct sample_storage *def; /* contain the default value */
}; };
#endif /* _TYPES_MAP_H */ #endif /* _TYPES_MAP_H */

View File

@ -157,26 +157,6 @@ static int sample_load_map(struct arg *arg, struct sample_conv *conv,
1, err, file, line)) 1, err, file, line))
return 0; return 0;
/* The second argument is the default value */
if (arg[1].type == ARGT_STR) {
desc->default_value = strdup(arg[1].data.str.str);
if (!desc->default_value) {
memprintf(err, "out of memory");
return 0;
}
desc->def = calloc(1, sizeof(*desc->def));
if (!desc->def) {
memprintf(err, "out of memory");
return 0;
}
if (!desc->pat.parse_smp(desc->default_value, desc->def)) {
memprintf(err, "Cannot parse default value");
return 0;
}
}
else
desc->def = NULL;
/* replace the first argument by this definition */ /* replace the first argument by this definition */
arg[0].type = ARGT_MAP; arg[0].type = ARGT_MAP;
arg[0].data.map = desc; arg[0].data.map = desc;
@ -213,13 +193,34 @@ static int sample_conv_map(struct session *session, const struct arg *arg_p,
} }
/* If no default value avalaible, the converter fails. */ /* If no default value avalaible, the converter fails. */
if (!desc->def) if (arg_p[1].type == ARGT_STOP)
return 0; return 0;
/* Return the default value. */ /* Return the default value. */
smp->type = desc->def->type; switch (desc->conv->out_type) {
smp->flags |= SMP_F_CONST;
memcpy(&smp->data, &desc->def->data, sizeof(smp->data)); case SMP_T_STR:
smp->type = SMP_T_STR;
smp->flags |= SMP_F_CONST;
smp->data.str = arg_p[1].data.str;
break;
case SMP_T_UINT:
smp->type = SMP_T_UINT;
smp->data.uint = arg_p[1].data.uint;
break;
case SMP_T_IPV4:
smp->type = SMP_T_IPV4;
smp->data.ipv4 = arg_p[1].data.ipv4;
break;
case SMP_T_IPV6:
smp->type = SMP_T_IPV6;
smp->data.ipv6 = arg_p[1].data.ipv6;
break;
}
return 1; return 1;
} }
@ -251,25 +252,25 @@ static struct sample_conv_kw_list sample_conv_kws = {ILH, {
{ "map_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_STR, (void *)PAT_MATCH_INT }, { "map_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_STR, (void *)PAT_MATCH_INT },
{ "map_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_STR, (void *)PAT_MATCH_IP }, { "map_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_STR, (void *)PAT_MATCH_IP },
{ "map_str_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_STR }, { "map_str_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_STR },
{ "map_beg_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_BEG }, { "map_beg_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_BEG },
{ "map_sub_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_SUB }, { "map_sub_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_SUB },
{ "map_dir_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_DIR }, { "map_dir_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_DIR },
{ "map_dom_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_DOM }, { "map_dom_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_DOM },
{ "map_end_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_END }, { "map_end_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_END },
{ "map_reg_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_REG }, { "map_reg_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_REG },
{ "map_int_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_UINT, (void *)PAT_MATCH_INT }, { "map_int_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_UINT, SMP_T_UINT, (void *)PAT_MATCH_INT },
{ "map_ip_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_UINT, (void *)PAT_MATCH_IP }, { "map_ip_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_ADDR, SMP_T_UINT, (void *)PAT_MATCH_IP },
{ "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_STR }, { "map_str_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_STR },
{ "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_BEG }, { "map_beg_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_BEG },
{ "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_SUB }, { "map_sub_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_SUB },
{ "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_DIR }, { "map_dir_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_DIR },
{ "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_DOM }, { "map_dom_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_DOM },
{ "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_END }, { "map_end_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_END },
{ "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_REG }, { "map_reg_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_REG },
{ "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_IPV4, (void *)PAT_MATCH_INT }, { "map_int_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_UINT, SMP_T_IPV4, (void *)PAT_MATCH_INT },
{ "map_ip_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_IPV4, (void *)PAT_MATCH_IP }, { "map_ip_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_ADDR, SMP_T_IPV4, (void *)PAT_MATCH_IP },
{ /* END */ }, { /* END */ },
}}; }};