Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1 | #include <ctype.h> |
| 2 | |
| 3 | #include <common/cfgparse.h> |
| 4 | #include <common/mini-clist.h> |
| 5 | |
| 6 | #include <types/vars.h> |
| 7 | |
| 8 | #include <proto/arg.h> |
| 9 | #include <proto/proto_http.h> |
| 10 | #include <proto/proto_tcp.h> |
| 11 | #include <proto/sample.h> |
| 12 | #include <proto/stream.h> |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 13 | #include <proto/vars.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 14 | |
| 15 | /* This contains a pool of struct vars */ |
| 16 | static struct pool_head *var_pool = NULL; |
| 17 | |
| 18 | /* This array contain all the names of all the HAProxy vars. |
| 19 | * This permits to identify two variables name with |
| 20 | * only one pointer. It permits to not using strdup() for |
| 21 | * each variable name used during the runtime. |
| 22 | */ |
| 23 | static char **var_names = NULL; |
| 24 | static int var_names_nb = 0; |
| 25 | |
| 26 | /* This array of int contains the system limits per context. */ |
| 27 | static unsigned int var_global_limit = 0; |
| 28 | static unsigned int var_global_size = 0; |
| 29 | static unsigned int var_sess_limit = 0; |
| 30 | static unsigned int var_txn_limit = 0; |
| 31 | static unsigned int var_reqres_limit = 0; |
| 32 | |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 33 | /* This function adds or remove memory size from the accounting. The inner |
| 34 | * pointers may be null when setting the outer ones only. |
| 35 | */ |
| 36 | static void var_accounting_diff(struct vars *vars, struct vars *per_sess, struct vars *per_strm, struct vars *per_chn, int size) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 37 | { |
| 38 | switch (vars->scope) { |
| 39 | case SCOPE_REQ: |
| 40 | case SCOPE_RES: |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 41 | per_chn->size += size; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 42 | case SCOPE_TXN: |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 43 | per_strm->size += size; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 44 | case SCOPE_SESS: |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 45 | per_sess->size += size; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 46 | var_global_size += size; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /* This function returns 1 if the <size> is available in the var |
| 51 | * pool <vars>, otherwise returns 0. If the space is avalaible, |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 52 | * the size is reserved. The inner pointers may be null when setting |
| 53 | * the outer ones only. |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 54 | */ |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 55 | static int var_accounting_add(struct vars *vars, struct vars *per_sess, struct vars *per_strm, struct vars *per_chn, int size) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 56 | { |
| 57 | switch (vars->scope) { |
| 58 | case SCOPE_REQ: |
| 59 | case SCOPE_RES: |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 60 | if (var_reqres_limit && per_chn->size + size > var_reqres_limit) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 61 | return 0; |
| 62 | case SCOPE_TXN: |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 63 | if (var_txn_limit && per_strm->size + size > var_txn_limit) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 64 | return 0; |
| 65 | case SCOPE_SESS: |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 66 | if (var_sess_limit && per_sess->size + size > var_sess_limit) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 67 | return 0; |
| 68 | if (var_global_limit && var_global_size + size > var_global_limit) |
| 69 | return 0; |
| 70 | } |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 71 | var_accounting_diff(vars, per_sess, per_strm, per_chn, size); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | /* This function free all the memory used by all the varaibles |
| 76 | * in the list. |
| 77 | */ |
| 78 | void vars_prune(struct vars *vars, struct stream *strm) |
| 79 | { |
| 80 | struct var *var, *tmp; |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 81 | unsigned int size = 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 82 | |
| 83 | list_for_each_entry_safe(var, tmp, &vars->head, l) { |
| 84 | if (var->data.type == SMP_T_STR || |
| 85 | var->data.type == SMP_T_BIN) { |
| 86 | free(var->data.data.str.str); |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 87 | size += var->data.data.str.len; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 88 | } |
| 89 | else if (var->data.type == SMP_T_METH) { |
| 90 | free(var->data.data.meth.str.str); |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 91 | size += var->data.data.meth.str.len; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 92 | } |
| 93 | LIST_DEL(&var->l); |
| 94 | pool_free2(var_pool, var); |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 95 | size += sizeof(struct var); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 96 | } |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 97 | var_accounting_diff(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, -size); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 100 | /* This function frees all the memory used by all the session variables in the |
| 101 | * list starting at <vars>. |
| 102 | */ |
| 103 | void vars_prune_per_sess(struct vars *vars) |
| 104 | { |
| 105 | struct var *var, *tmp; |
| 106 | unsigned int size = 0; |
| 107 | |
| 108 | list_for_each_entry_safe(var, tmp, &vars->head, l) { |
| 109 | if (var->data.type == SMP_T_STR || |
| 110 | var->data.type == SMP_T_BIN) { |
| 111 | free(var->data.data.str.str); |
| 112 | size += var->data.data.str.len; |
| 113 | } |
| 114 | else if (var->data.type == SMP_T_METH) { |
| 115 | free(var->data.data.meth.str.str); |
| 116 | size += var->data.data.meth.str.len; |
| 117 | } |
| 118 | LIST_DEL(&var->l); |
| 119 | pool_free2(var_pool, var); |
| 120 | size += sizeof(struct var); |
| 121 | } |
| 122 | vars->size -= size; |
| 123 | var_global_size -= size; |
| 124 | } |
| 125 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 126 | /* This function init a list of variabes. */ |
| 127 | void vars_init(struct vars *vars, enum vars_scope scope) |
| 128 | { |
| 129 | LIST_INIT(&vars->head); |
| 130 | vars->scope = scope; |
| 131 | vars->size = 0; |
| 132 | } |
| 133 | |
| 134 | /* This function declares a new variable name. It returns a pointer |
| 135 | * on the string identifying the name. This function assures that |
| 136 | * the same name exists only once. |
| 137 | * |
| 138 | * This function check if the variable name is acceptable. |
| 139 | * |
| 140 | * The function returns NULL if an error occurs, and <err> is filled. |
| 141 | * In this case, the HAProxy must be stopped because the structs are |
| 142 | * left inconsistent. Otherwise, it returns the pointer on the global |
| 143 | * name. |
| 144 | */ |
| 145 | static char *register_name(const char *name, int len, enum vars_scope *scope, char **err) |
| 146 | { |
| 147 | int i; |
| 148 | const char *tmp; |
| 149 | |
| 150 | /* Check length. */ |
| 151 | if (len == 0) { |
| 152 | memprintf(err, "Empty variable name cannot be accepted"); |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | /* Check scope. */ |
| 157 | if (len > 5 && strncmp(name, "sess.", 5) == 0) { |
| 158 | name += 5; |
| 159 | len -= 5; |
| 160 | *scope = SCOPE_SESS; |
| 161 | } |
| 162 | else if (len > 4 && strncmp(name, "txn.", 4) == 0) { |
| 163 | name += 4; |
| 164 | len -= 4; |
| 165 | *scope = SCOPE_TXN; |
| 166 | } |
| 167 | else if (len > 4 && strncmp(name, "req.", 4) == 0) { |
| 168 | name += 4; |
| 169 | len -= 4; |
| 170 | *scope = SCOPE_REQ; |
| 171 | } |
| 172 | else if (len > 4 && strncmp(name, "res.", 4) == 0) { |
| 173 | name += 4; |
| 174 | len -= 4; |
| 175 | *scope = SCOPE_RES; |
| 176 | } |
| 177 | else { |
| 178 | memprintf(err, "invalid variable name '%s'. A variable name must be start by its scope. " |
| 179 | "The scope can be 'sess', 'txn', 'req' or 'res'", name); |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | /* Look for existing variable name. */ |
| 184 | for (i = 0; i < var_names_nb; i++) |
| 185 | if (strncmp(var_names[i], name, len) == 0) |
| 186 | return var_names[i]; |
| 187 | |
| 188 | /* Store variable name. */ |
| 189 | var_names_nb++; |
| 190 | var_names = realloc(var_names, var_names_nb * sizeof(*var_names)); |
| 191 | if (!var_names) { |
| 192 | memprintf(err, "out of memory error"); |
| 193 | return NULL; |
| 194 | } |
| 195 | var_names[var_names_nb - 1] = malloc(len + 1); |
| 196 | if (!var_names[var_names_nb - 1]) { |
| 197 | memprintf(err, "out of memory error"); |
| 198 | return NULL; |
| 199 | } |
| 200 | memcpy(var_names[var_names_nb - 1], name, len); |
| 201 | var_names[var_names_nb - 1][len] = '\0'; |
| 202 | |
| 203 | /* Check variable name syntax. */ |
| 204 | tmp = var_names[var_names_nb - 1]; |
| 205 | while (*tmp) { |
| 206 | if (!isalnum((int)(unsigned char)*tmp) && *tmp != '_') { |
| 207 | memprintf(err, "invalid syntax at char '%s'", tmp); |
| 208 | return NULL; |
| 209 | } |
| 210 | tmp++; |
| 211 | } |
| 212 | |
| 213 | /* Return the result. */ |
| 214 | return var_names[var_names_nb - 1]; |
| 215 | } |
| 216 | |
| 217 | /* This function returns an existing variable or returns NULL. */ |
| 218 | static inline struct var *var_get(struct vars *vars, const char *name) |
| 219 | { |
| 220 | struct var *var; |
| 221 | |
| 222 | list_for_each_entry(var, &vars->head, l) |
| 223 | if (var->name == name) |
| 224 | return var; |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | /* Returns 0 if fails, else returns 1. */ |
| 229 | static int smp_fetch_var(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 230 | { |
| 231 | const struct var_desc *var_desc = &args[0].data.var; |
| 232 | struct var *var; |
| 233 | struct vars *vars; |
| 234 | |
| 235 | /* Check the availibity of the variable. */ |
| 236 | switch (var_desc->scope) { |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 237 | case SCOPE_SESS: vars = &smp->strm->sess->vars; break; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 238 | case SCOPE_TXN: vars = &smp->strm->vars_txn; break; |
| 239 | case SCOPE_REQ: |
Thierry FOURNIER | 0b243fd | 2015-06-16 23:52:47 +0200 | [diff] [blame] | 240 | case SCOPE_RES: |
| 241 | default: vars = &smp->strm->vars_reqres; break; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 242 | } |
| 243 | if (vars->scope != var_desc->scope) |
| 244 | return 0; |
| 245 | var = var_get(vars, var_desc->name); |
| 246 | |
| 247 | /* check for the variable avalaibility */ |
| 248 | if (!var) |
| 249 | return 0; |
| 250 | |
| 251 | /* Copy sample. */ |
| 252 | smp->type = var->data.type; |
| 253 | smp->flags |= SMP_F_CONST; |
| 254 | memcpy(&smp->data, &var->data.data, sizeof(smp->data)); |
| 255 | return 1; |
| 256 | } |
| 257 | |
| 258 | /* This function search in the <head> a variable with the same |
| 259 | * pointer value that the <name>. If the variable doesn't exists, |
| 260 | * create it. The function stores a copy of smp> if the variable. |
| 261 | * It returns 0 if fails, else returns 1. |
| 262 | */ |
| 263 | static int sample_store(struct vars *vars, const char *name, struct stream *strm, struct sample *smp) |
| 264 | { |
| 265 | struct var *var; |
| 266 | |
| 267 | /* Look for existing variable name. */ |
| 268 | var = var_get(vars, name); |
| 269 | |
| 270 | if (var) { |
| 271 | /* free its used memory. */ |
| 272 | if (var->data.type == SMP_T_STR || |
| 273 | var->data.type == SMP_T_BIN) { |
| 274 | free(var->data.data.str.str); |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 275 | var_accounting_diff(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, -var->data.data.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 276 | } |
| 277 | else if (var->data.type == SMP_T_METH) { |
| 278 | free(var->data.data.meth.str.str); |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 279 | var_accounting_diff(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, -var->data.data.meth.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 280 | } |
| 281 | } else { |
| 282 | |
| 283 | /* Check memory avalaible. */ |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 284 | if (!var_accounting_add(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, sizeof(struct var))) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 285 | return 0; |
| 286 | |
| 287 | /* Create new entry. */ |
| 288 | var = pool_alloc2(var_pool); |
| 289 | if (!var) |
| 290 | return 0; |
| 291 | LIST_ADDQ(&vars->head, &var->l); |
| 292 | var->name = name; |
| 293 | } |
| 294 | |
| 295 | /* Set type. */ |
| 296 | var->data.type = smp->type; |
| 297 | |
| 298 | /* Copy data. If the data needs memory, the function can fail. */ |
| 299 | switch (var->data.type) { |
| 300 | case SMP_T_BOOL: |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 301 | case SMP_T_SINT: |
| 302 | var->data.data.sint = smp->data.sint; |
| 303 | break; |
| 304 | case SMP_T_IPV4: |
| 305 | var->data.data.ipv4 = smp->data.ipv4; |
| 306 | break; |
| 307 | case SMP_T_IPV6: |
| 308 | var->data.data.ipv6 = smp->data.ipv6; |
| 309 | break; |
| 310 | case SMP_T_STR: |
| 311 | case SMP_T_BIN: |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 312 | if (!var_accounting_add(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, smp->data.str.len)) { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 313 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 314 | return 0; |
| 315 | } |
| 316 | var->data.data.str.str = malloc(smp->data.str.len); |
| 317 | if (!var->data.data.str.str) { |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 318 | var_accounting_diff(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, -smp->data.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 319 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 320 | return 0; |
| 321 | } |
| 322 | var->data.data.str.len = smp->data.str.len; |
| 323 | memcpy(var->data.data.str.str, smp->data.str.str, var->data.data.str.len); |
| 324 | break; |
| 325 | case SMP_T_METH: |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 326 | if (!var_accounting_add(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, smp->data.meth.str.len)) { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 327 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 328 | return 0; |
| 329 | } |
| 330 | var->data.data.meth.str.str = malloc(smp->data.meth.str.len); |
| 331 | if (!var->data.data.meth.str.str) { |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 332 | var_accounting_diff(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, -smp->data.meth.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 333 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 334 | return 0; |
| 335 | } |
| 336 | var->data.data.meth.meth = smp->data.meth.meth; |
| 337 | var->data.data.meth.str.len = smp->data.meth.str.len; |
| 338 | var->data.data.meth.str.size = smp->data.meth.str.len; |
| 339 | memcpy(var->data.data.meth.str.str, smp->data.meth.str.str, var->data.data.meth.str.len); |
| 340 | break; |
| 341 | } |
| 342 | return 1; |
| 343 | } |
| 344 | |
| 345 | /* Returns 0 if fails, else returns 1. */ |
| 346 | static inline int sample_store_stream(const char *name, enum vars_scope scope, |
| 347 | struct stream *strm, struct sample *smp) |
| 348 | { |
| 349 | struct vars *vars; |
| 350 | |
| 351 | switch (scope) { |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 352 | case SCOPE_SESS: vars = &strm->sess->vars; break; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 353 | case SCOPE_TXN: vars = &strm->vars_txn; break; |
| 354 | case SCOPE_REQ: |
Thierry FOURNIER | 0b243fd | 2015-06-16 23:52:47 +0200 | [diff] [blame] | 355 | case SCOPE_RES: |
| 356 | default: vars = &strm->vars_reqres; break; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 357 | } |
| 358 | if (vars->scope != scope) |
| 359 | return 0; |
| 360 | return sample_store(vars, name, strm, smp); |
| 361 | } |
| 362 | |
| 363 | /* Returns 0 if fails, else returns 1. */ |
| 364 | static int smp_conv_store(const struct arg *args, struct sample *smp, void *private) |
| 365 | { |
| 366 | return sample_store_stream(args[0].data.var.name, args[1].data.var.scope, smp->strm, smp); |
| 367 | } |
| 368 | |
| 369 | /* This fucntions check an argument entry and fill it with a variable |
| 370 | * type. The argumen must be a string. If the variable lookup fails, |
| 371 | * the function retuns 0 and fill <err>, otherwise it returns 1. |
| 372 | */ |
| 373 | int vars_check_arg(struct arg *arg, char **err) |
| 374 | { |
| 375 | char *name; |
| 376 | enum vars_scope scope; |
| 377 | |
| 378 | /* Check arg type. */ |
| 379 | if (arg->type != ARGT_STR) { |
| 380 | memprintf(err, "unexpected argument type"); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | /* Register new variable name. */ |
| 385 | name = register_name(arg->data.str.str, arg->data.str.len, &scope, err); |
| 386 | if (!name) |
| 387 | return 0; |
| 388 | |
| 389 | /* Use the global variable name pointer. */ |
| 390 | arg->type = ARGT_VAR; |
| 391 | arg->data.var.name = name; |
| 392 | arg->data.var.scope = scope; |
| 393 | return 1; |
| 394 | } |
| 395 | |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 396 | /* This function store a sample in a variable. |
| 397 | * In error case, it fails silently. |
| 398 | */ |
| 399 | void vars_set_by_name(const char *name, size_t len, struct stream *strm, struct sample *smp) |
| 400 | { |
| 401 | enum vars_scope scope; |
| 402 | |
| 403 | /* Resolve name and scope. */ |
| 404 | name = register_name(name, len, &scope, NULL); |
| 405 | if (!name) |
| 406 | return; |
| 407 | |
| 408 | sample_store_stream(name, scope, strm, smp); |
| 409 | } |
| 410 | |
| 411 | /* this function fills a sample with the |
| 412 | * variable content. Returns 1 if the sample |
| 413 | * is filled, otherwise it returns 0. |
| 414 | */ |
| 415 | int vars_get_by_name(const char *name, size_t len, struct stream *strm, struct sample *smp) |
| 416 | { |
| 417 | struct vars *vars; |
| 418 | struct var *var; |
| 419 | enum vars_scope scope; |
| 420 | |
| 421 | /* Resolve name and scope. */ |
| 422 | name = register_name(name, len, &scope, NULL); |
| 423 | if (!name) |
| 424 | return 0; |
| 425 | |
| 426 | /* Select "vars" pool according with the scope. */ |
| 427 | switch (scope) { |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 428 | case SCOPE_SESS: vars = &strm->sess->vars; break; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 429 | case SCOPE_TXN: vars = &strm->vars_txn; break; |
| 430 | case SCOPE_REQ: |
Thierry FOURNIER | 0b243fd | 2015-06-16 23:52:47 +0200 | [diff] [blame] | 431 | case SCOPE_RES: |
| 432 | default: vars = &strm->vars_reqres; break; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* Check if the scope is avalaible a this point of processing. */ |
| 436 | if (vars->scope != scope) |
| 437 | return 0; |
| 438 | |
| 439 | /* Get the variable entry. */ |
| 440 | var = var_get(vars, name); |
| 441 | if (!var) |
| 442 | return 0; |
| 443 | |
| 444 | /* Copy sample. */ |
| 445 | smp->type = var->data.type; |
| 446 | smp->flags = SMP_F_CONST; |
| 447 | memcpy(&smp->data, &var->data.data, sizeof(smp->data)); |
| 448 | return 1; |
| 449 | } |
| 450 | |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 451 | /* this function fills a sample with the |
| 452 | * content of the varaible described by <var_desc>. Returns 1 |
| 453 | * if the sample is filled, otherwise it returns 0. |
| 454 | */ |
| 455 | int vars_get_by_desc(const struct var_desc *var_desc, struct stream *strm, struct sample *smp) |
| 456 | { |
| 457 | struct vars *vars; |
| 458 | struct var *var; |
| 459 | |
| 460 | /* Select "vars" pool according with the scope. */ |
| 461 | switch (var_desc->scope) { |
| 462 | case SCOPE_SESS: vars = &strm->sess->vars; break; |
| 463 | case SCOPE_TXN: vars = &strm->vars_txn; break; |
| 464 | case SCOPE_REQ: |
| 465 | case SCOPE_RES: |
| 466 | default: vars = &strm->vars_reqres; break; |
| 467 | } |
| 468 | |
| 469 | /* Check if the scope is avalaible a this point of processing. */ |
| 470 | if (vars->scope != var_desc->scope) |
| 471 | return 0; |
| 472 | |
| 473 | /* Get the variable entry. */ |
| 474 | var = var_get(vars, var_desc->name); |
| 475 | if (!var) |
| 476 | return 0; |
| 477 | |
| 478 | /* Copy sample. */ |
| 479 | smp->type = var->data.type; |
| 480 | smp->flags = SMP_F_CONST; |
| 481 | memcpy(&smp->data, &var->data.data, sizeof(smp->data)); |
| 482 | return 1; |
| 483 | } |
| 484 | |
Willy Tarreau | e44136f | 2015-06-23 15:17:33 +0200 | [diff] [blame] | 485 | /* Returns 0 if we need to come back later to complete the sample's retrieval, |
| 486 | * otherwise 1. For now all processing is considered final so we only return 1. |
| 487 | */ |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 488 | static inline int action_store(struct sample_expr *expr, const char *name, |
| 489 | enum vars_scope scope, struct proxy *px, |
| 490 | struct stream *s, int sens) |
| 491 | { |
| 492 | struct sample smp; |
| 493 | |
| 494 | /* Process the expression. */ |
| 495 | memset(&smp, 0, sizeof(smp)); |
| 496 | if (!sample_process(px, s->sess, s, sens|SMP_OPT_FINAL, expr, &smp)) |
Willy Tarreau | e44136f | 2015-06-23 15:17:33 +0200 | [diff] [blame] | 497 | return 1; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 498 | |
| 499 | /* Store the sample, and ignore errors. */ |
| 500 | sample_store_stream(name, scope, s, &smp); |
| 501 | return 1; |
| 502 | } |
| 503 | |
| 504 | /* Returns 0 if miss data, else returns 1. */ |
| 505 | static int action_tcp_req_store(struct tcp_rule *rule, struct proxy *px, struct stream *s) |
| 506 | { |
| 507 | struct sample_expr *expr = rule->act_prm.data[0]; |
| 508 | const char *name = rule->act_prm.data[1]; |
| 509 | int scope = (long)rule->act_prm.data[2]; |
| 510 | |
| 511 | return action_store(expr, name, scope, px, s, SMP_OPT_DIR_REQ); |
| 512 | } |
| 513 | |
| 514 | /* Returns 0 if miss data, else returns 1. */ |
| 515 | static int action_tcp_res_store(struct tcp_rule *rule, struct proxy *px, struct stream *s) |
| 516 | { |
| 517 | struct sample_expr *expr = rule->act_prm.data[0]; |
| 518 | const char *name = rule->act_prm.data[1]; |
| 519 | int scope = (long)rule->act_prm.data[2]; |
| 520 | |
| 521 | return action_store(expr, name, scope, px, s, SMP_OPT_DIR_RES); |
| 522 | } |
| 523 | |
| 524 | /* Returns 0 if miss data, else returns 1. */ |
| 525 | static int action_http_req_store(struct http_req_rule *rule, struct proxy *px, struct stream *s) |
| 526 | { |
| 527 | struct sample_expr *expr = rule->arg.act.p[0]; |
| 528 | const char *name = rule->arg.act.p[1]; |
| 529 | int scope = (long)rule->arg.act.p[2]; |
| 530 | |
| 531 | return action_store(expr, name, scope, px, s, SMP_OPT_DIR_REQ); |
| 532 | } |
| 533 | |
| 534 | /* Returns 0 if miss data, else returns 1. */ |
| 535 | static int action_http_res_store(struct http_res_rule *rule, struct proxy *px, struct stream *s) |
| 536 | { |
| 537 | struct sample_expr *expr = rule->arg.act.p[0]; |
| 538 | const char *name = rule->arg.act.p[1]; |
| 539 | int scope = (long)rule->arg.act.p[2]; |
| 540 | |
| 541 | return action_store(expr, name, scope, px, s, SMP_OPT_DIR_RES); |
| 542 | } |
| 543 | |
| 544 | /* This two function checks the variable name and replace the |
| 545 | * configuration string name by the global string name. its |
| 546 | * the same string, but the global pointer can be easy to |
| 547 | * compare. |
| 548 | * |
| 549 | * The first function checks a sample-fetch and the second |
| 550 | * checks a converter. |
| 551 | */ |
| 552 | static int smp_check_var(struct arg *args, char **err) |
| 553 | { |
| 554 | return vars_check_arg(&args[0], err); |
| 555 | } |
| 556 | |
| 557 | static int conv_check_var(struct arg *args, struct sample_conv *conv, |
| 558 | const char *file, int line, char **err_msg) |
| 559 | { |
| 560 | return vars_check_arg(&args[0], err_msg); |
| 561 | } |
| 562 | |
| 563 | /* This function is a common parser for using variables. It understands |
| 564 | * the format: |
| 565 | * |
| 566 | * set-var(<variable-name>) <expression> |
| 567 | * |
| 568 | * It returns 0 if fails and <err> is filled with an error message. Otherwise, |
| 569 | * it returns 1 and the variable <expr> is filled with the pointer to the |
| 570 | * expression to execute. |
| 571 | */ |
| 572 | static int parse_vars(const char **args, int *arg, struct proxy *px, |
| 573 | struct sample_expr **expr, char **name, |
| 574 | enum vars_scope *scope, char **err) |
| 575 | { |
| 576 | const char *var_name = args[*arg-1]; |
| 577 | int var_len; |
| 578 | |
| 579 | var_name += strlen("set-var"); |
| 580 | if (*var_name != '(') { |
| 581 | memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)'", args[*arg-1]); |
| 582 | return 0; |
| 583 | } |
| 584 | var_name++; /* jump the '(' */ |
| 585 | var_len = strlen(var_name); |
| 586 | var_len--; /* remove the ')' */ |
| 587 | if (var_name[var_len] != ')') { |
| 588 | memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)'", args[*arg-1]); |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | *name = register_name(var_name, var_len, scope, err); |
| 593 | if (!*name) |
| 594 | return 0; |
| 595 | |
| 596 | *expr = sample_parse_expr((char **)args, arg, px->conf.args.file, px->conf.args.line, |
| 597 | err, &px->conf.args); |
| 598 | if (!*expr) |
| 599 | return 0; |
| 600 | |
| 601 | return 1; |
| 602 | } |
| 603 | |
| 604 | static int parse_tcp_req_store(const char **args, int *arg, struct proxy *px, |
| 605 | struct tcp_rule *rule, char **err) |
| 606 | { |
| 607 | struct sample_expr *expr; |
| 608 | int cur_arg = *arg; |
| 609 | char *name; |
| 610 | enum vars_scope scope; |
| 611 | |
| 612 | if (!parse_vars(args, arg, px, &expr, &name, &scope, err)) |
| 613 | return 0; |
| 614 | |
| 615 | if (!(expr->fetch->val & SMP_VAL_FE_REQ_CNT)) { |
| 616 | memprintf(err, |
| 617 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 618 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 619 | free(expr); |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | rule->action = TCP_ACT_CUSTOM_CONT; |
| 624 | rule->action_ptr = action_tcp_req_store; |
| 625 | rule->act_prm.data[0] = expr; |
| 626 | rule->act_prm.data[1] = name; |
| 627 | rule->act_prm.data[2] = (void *)(long)scope; |
| 628 | |
| 629 | return 1; |
| 630 | } |
| 631 | |
| 632 | static int parse_tcp_res_store(const char **args, int *arg, struct proxy *px, |
| 633 | struct tcp_rule *rule, char **err) |
| 634 | { |
| 635 | struct sample_expr *expr; |
| 636 | int cur_arg = *arg; |
| 637 | char *name; |
| 638 | enum vars_scope scope; |
| 639 | |
| 640 | if (!parse_vars(args, arg, px, &expr, &name, &scope, err)) |
| 641 | return 0; |
| 642 | |
| 643 | if (!(expr->fetch->val & SMP_VAL_BE_RES_CNT)) { |
| 644 | memprintf(err, |
| 645 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 646 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 647 | free(expr); |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | rule->action = TCP_ACT_CUSTOM_CONT; |
| 652 | rule->action_ptr = action_tcp_res_store; |
| 653 | rule->act_prm.data[0] = expr; |
| 654 | rule->act_prm.data[1] = name; |
| 655 | rule->act_prm.data[2] = (void *)(long)scope; |
| 656 | |
| 657 | return 1; |
| 658 | } |
| 659 | |
| 660 | static int parse_http_req_store(const char **args, int *arg, struct proxy *px, |
| 661 | struct http_req_rule *rule, char **err) |
| 662 | { |
| 663 | struct sample_expr *expr; |
| 664 | int cur_arg = *arg; |
| 665 | char *name; |
| 666 | enum vars_scope scope; |
| 667 | |
| 668 | if (!parse_vars(args, arg, px, &expr, &name, &scope, err)) |
| 669 | return -1; |
| 670 | |
| 671 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 672 | memprintf(err, |
| 673 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 674 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 675 | free(expr); |
| 676 | return -1; |
| 677 | } |
| 678 | |
| 679 | rule->action = HTTP_REQ_ACT_CUSTOM_CONT; |
| 680 | rule->action_ptr = action_http_req_store; |
| 681 | rule->arg.act.p[0] = expr; |
| 682 | rule->arg.act.p[1] = name; |
| 683 | rule->arg.act.p[2] = (void *)(long)scope; |
| 684 | |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | static int parse_http_res_store(const char **args, int *arg, struct proxy *px, |
| 689 | struct http_res_rule *rule, char **err) |
| 690 | { |
| 691 | struct sample_expr *expr; |
| 692 | int cur_arg = *arg; |
| 693 | char *name; |
| 694 | enum vars_scope scope; |
| 695 | |
| 696 | if (!parse_vars(args, arg, px, &expr, &name, &scope, err)) |
| 697 | return -1; |
| 698 | |
| 699 | if (!(expr->fetch->val & SMP_VAL_BE_HRS_HDR)) { |
| 700 | memprintf(err, |
| 701 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 702 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 703 | free(expr); |
| 704 | return -1; |
| 705 | } |
| 706 | |
| 707 | rule->action = HTTP_RES_ACT_CUSTOM_CONT; |
| 708 | rule->action_ptr = action_http_res_store; |
| 709 | rule->arg.act.p[0] = expr; |
| 710 | rule->arg.act.p[1] = name; |
| 711 | rule->arg.act.p[2] = (void *)(long)scope; |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | static int vars_max_size(char **args, int section_type, struct proxy *curpx, |
| 717 | struct proxy *defpx, const char *file, int line, |
| 718 | char **err, unsigned int *limit) |
| 719 | { |
| 720 | char *error; |
| 721 | |
| 722 | *limit = strtol(args[1], &error, 10); |
| 723 | if (*error != 0) { |
| 724 | memprintf(err, "%s: '%s' is an invalid size", args[0], args[1]); |
| 725 | return -1; |
| 726 | } |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | static int vars_max_size_global(char **args, int section_type, struct proxy *curpx, |
| 731 | struct proxy *defpx, const char *file, int line, |
| 732 | char **err) |
| 733 | { |
| 734 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_global_limit); |
| 735 | } |
| 736 | |
| 737 | static int vars_max_size_sess(char **args, int section_type, struct proxy *curpx, |
| 738 | struct proxy *defpx, const char *file, int line, |
| 739 | char **err) |
| 740 | { |
| 741 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_sess_limit); |
| 742 | } |
| 743 | |
| 744 | static int vars_max_size_txn(char **args, int section_type, struct proxy *curpx, |
| 745 | struct proxy *defpx, const char *file, int line, |
| 746 | char **err) |
| 747 | { |
| 748 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_txn_limit); |
| 749 | } |
| 750 | |
| 751 | static int vars_max_size_reqres(char **args, int section_type, struct proxy *curpx, |
| 752 | struct proxy *defpx, const char *file, int line, |
| 753 | char **err) |
| 754 | { |
| 755 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_reqres_limit); |
| 756 | } |
| 757 | |
| 758 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 759 | |
| 760 | { "var", smp_fetch_var, ARG1(1,STR), smp_check_var, SMP_T_STR, SMP_USE_HTTP_ANY }, |
| 761 | { /* END */ }, |
| 762 | }}; |
| 763 | |
| 764 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
| 765 | { "set-var", smp_conv_store, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY }, |
| 766 | { /* END */ }, |
| 767 | }}; |
| 768 | |
| 769 | static struct tcp_action_kw_list tcp_req_kws = {"vars", { }, { |
| 770 | { "set-var", parse_tcp_req_store, 1 }, |
| 771 | { /* END */ } |
| 772 | }}; |
| 773 | |
| 774 | static struct tcp_action_kw_list tcp_res_kws = {"vars", { }, { |
| 775 | { "set-var", parse_tcp_res_store, 1 }, |
| 776 | { /* END */ } |
| 777 | }}; |
| 778 | |
| 779 | static struct http_req_action_kw_list http_req_kws = {"vars", { }, { |
| 780 | { "set-var", parse_http_req_store, 1 }, |
| 781 | { /* END */ } |
| 782 | }}; |
| 783 | |
| 784 | static struct http_res_action_kw_list http_res_kws = {"vars", { }, { |
| 785 | { "set-var", parse_http_res_store, 1 }, |
| 786 | { /* END */ } |
| 787 | }}; |
| 788 | |
| 789 | static struct cfg_kw_list cfg_kws = {{ },{ |
| 790 | { CFG_GLOBAL, "tune.vars.global-max-size", vars_max_size_global }, |
| 791 | { CFG_GLOBAL, "tune.vars.sess-max-size", vars_max_size_sess }, |
| 792 | { CFG_GLOBAL, "tune.vars.txn-max-size", vars_max_size_txn }, |
| 793 | { CFG_GLOBAL, "tune.vars.reqres-max-size", vars_max_size_reqres }, |
| 794 | { /* END */ } |
| 795 | }}; |
| 796 | |
| 797 | __attribute__((constructor)) |
| 798 | static void __http_protocol_init(void) |
| 799 | { |
| 800 | var_pool = create_pool("vars", sizeof(struct var), MEM_F_SHARED); |
| 801 | |
| 802 | sample_register_fetches(&sample_fetch_keywords); |
| 803 | sample_register_convs(&sample_conv_kws); |
| 804 | tcp_req_cont_keywords_register(&tcp_req_kws); |
| 805 | tcp_res_cont_keywords_register(&tcp_res_kws); |
| 806 | http_req_keywords_register(&http_req_kws); |
| 807 | http_res_keywords_register(&http_res_kws); |
| 808 | cfg_register_keywords(&cfg_kws); |
| 809 | } |