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) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 86 | free(var->data.u.str.str); |
| 87 | size += var->data.u.str.len; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 88 | } |
| 89 | else if (var->data.type == SMP_T_METH) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 90 | free(var->data.u.meth.str.str); |
| 91 | size += var->data.u.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) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 111 | free(var->data.u.str.str); |
| 112 | size += var->data.u.str.len; |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 113 | } |
| 114 | else if (var->data.type == SMP_T_METH) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 115 | free(var->data.u.meth.str.str); |
| 116 | size += var->data.u.meth.str.len; |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 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. */ |
Thierry FOURNIER | 5cc18d4 | 2015-08-19 09:02:36 +0200 | [diff] [blame] | 252 | smp->data = var->data; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 253 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 254 | return 1; |
| 255 | } |
| 256 | |
| 257 | /* This function search in the <head> a variable with the same |
| 258 | * pointer value that the <name>. If the variable doesn't exists, |
| 259 | * create it. The function stores a copy of smp> if the variable. |
| 260 | * It returns 0 if fails, else returns 1. |
| 261 | */ |
| 262 | static int sample_store(struct vars *vars, const char *name, struct stream *strm, struct sample *smp) |
| 263 | { |
| 264 | struct var *var; |
| 265 | |
| 266 | /* Look for existing variable name. */ |
| 267 | var = var_get(vars, name); |
| 268 | |
| 269 | if (var) { |
| 270 | /* free its used memory. */ |
| 271 | if (var->data.type == SMP_T_STR || |
| 272 | var->data.type == SMP_T_BIN) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 273 | free(var->data.u.str.str); |
| 274 | var_accounting_diff(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, -var->data.u.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 275 | } |
| 276 | else if (var->data.type == SMP_T_METH) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 277 | free(var->data.u.meth.str.str); |
| 278 | var_accounting_diff(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, -var->data.u.meth.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 279 | } |
| 280 | } else { |
| 281 | |
| 282 | /* Check memory avalaible. */ |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 283 | 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] | 284 | return 0; |
| 285 | |
| 286 | /* Create new entry. */ |
| 287 | var = pool_alloc2(var_pool); |
| 288 | if (!var) |
| 289 | return 0; |
| 290 | LIST_ADDQ(&vars->head, &var->l); |
| 291 | var->name = name; |
| 292 | } |
| 293 | |
| 294 | /* Set type. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 295 | var->data.type = smp->data.type; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 296 | |
| 297 | /* Copy data. If the data needs memory, the function can fail. */ |
| 298 | switch (var->data.type) { |
| 299 | case SMP_T_BOOL: |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 300 | case SMP_T_SINT: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 301 | var->data.u.sint = smp->data.u.sint; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 302 | break; |
| 303 | case SMP_T_IPV4: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 304 | var->data.u.ipv4 = smp->data.u.ipv4; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 305 | break; |
| 306 | case SMP_T_IPV6: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 307 | var->data.u.ipv6 = smp->data.u.ipv6; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 308 | break; |
| 309 | case SMP_T_STR: |
| 310 | case SMP_T_BIN: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 311 | if (!var_accounting_add(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, smp->data.u.str.len)) { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 312 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 313 | return 0; |
| 314 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 315 | var->data.u.str.str = malloc(smp->data.u.str.len); |
| 316 | if (!var->data.u.str.str) { |
| 317 | var_accounting_diff(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, -smp->data.u.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 318 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 319 | return 0; |
| 320 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 321 | var->data.u.str.len = smp->data.u.str.len; |
| 322 | memcpy(var->data.u.str.str, smp->data.u.str.str, var->data.u.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 323 | break; |
| 324 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 325 | if (!var_accounting_add(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, smp->data.u.meth.str.len)) { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 326 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 327 | return 0; |
| 328 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 329 | var->data.u.meth.str.str = malloc(smp->data.u.meth.str.len); |
| 330 | if (!var->data.u.meth.str.str) { |
| 331 | var_accounting_diff(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, -smp->data.u.meth.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 332 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 333 | return 0; |
| 334 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 335 | var->data.u.meth.meth = smp->data.u.meth.meth; |
| 336 | var->data.u.meth.str.len = smp->data.u.meth.str.len; |
| 337 | var->data.u.meth.str.size = smp->data.u.meth.str.len; |
| 338 | memcpy(var->data.u.meth.str.str, smp->data.u.meth.str.str, var->data.u.meth.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | return 1; |
| 342 | } |
| 343 | |
| 344 | /* Returns 0 if fails, else returns 1. */ |
| 345 | static inline int sample_store_stream(const char *name, enum vars_scope scope, |
| 346 | struct stream *strm, struct sample *smp) |
| 347 | { |
| 348 | struct vars *vars; |
| 349 | |
| 350 | switch (scope) { |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 351 | case SCOPE_SESS: vars = &strm->sess->vars; break; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 352 | case SCOPE_TXN: vars = &strm->vars_txn; break; |
| 353 | case SCOPE_REQ: |
Thierry FOURNIER | 0b243fd | 2015-06-16 23:52:47 +0200 | [diff] [blame] | 354 | case SCOPE_RES: |
| 355 | default: vars = &strm->vars_reqres; break; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 356 | } |
| 357 | if (vars->scope != scope) |
| 358 | return 0; |
| 359 | return sample_store(vars, name, strm, smp); |
| 360 | } |
| 361 | |
| 362 | /* Returns 0 if fails, else returns 1. */ |
| 363 | static int smp_conv_store(const struct arg *args, struct sample *smp, void *private) |
| 364 | { |
| 365 | return sample_store_stream(args[0].data.var.name, args[1].data.var.scope, smp->strm, smp); |
| 366 | } |
| 367 | |
| 368 | /* This fucntions check an argument entry and fill it with a variable |
| 369 | * type. The argumen must be a string. If the variable lookup fails, |
| 370 | * the function retuns 0 and fill <err>, otherwise it returns 1. |
| 371 | */ |
| 372 | int vars_check_arg(struct arg *arg, char **err) |
| 373 | { |
| 374 | char *name; |
| 375 | enum vars_scope scope; |
| 376 | |
| 377 | /* Check arg type. */ |
| 378 | if (arg->type != ARGT_STR) { |
| 379 | memprintf(err, "unexpected argument type"); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | /* Register new variable name. */ |
| 384 | name = register_name(arg->data.str.str, arg->data.str.len, &scope, err); |
| 385 | if (!name) |
| 386 | return 0; |
| 387 | |
| 388 | /* Use the global variable name pointer. */ |
| 389 | arg->type = ARGT_VAR; |
| 390 | arg->data.var.name = name; |
| 391 | arg->data.var.scope = scope; |
| 392 | return 1; |
| 393 | } |
| 394 | |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 395 | /* This function store a sample in a variable. |
| 396 | * In error case, it fails silently. |
| 397 | */ |
| 398 | void vars_set_by_name(const char *name, size_t len, struct stream *strm, struct sample *smp) |
| 399 | { |
| 400 | enum vars_scope scope; |
| 401 | |
| 402 | /* Resolve name and scope. */ |
| 403 | name = register_name(name, len, &scope, NULL); |
| 404 | if (!name) |
| 405 | return; |
| 406 | |
| 407 | sample_store_stream(name, scope, strm, smp); |
| 408 | } |
| 409 | |
| 410 | /* this function fills a sample with the |
| 411 | * variable content. Returns 1 if the sample |
| 412 | * is filled, otherwise it returns 0. |
| 413 | */ |
| 414 | int vars_get_by_name(const char *name, size_t len, struct stream *strm, struct sample *smp) |
| 415 | { |
| 416 | struct vars *vars; |
| 417 | struct var *var; |
| 418 | enum vars_scope scope; |
| 419 | |
| 420 | /* Resolve name and scope. */ |
| 421 | name = register_name(name, len, &scope, NULL); |
| 422 | if (!name) |
| 423 | return 0; |
| 424 | |
| 425 | /* Select "vars" pool according with the scope. */ |
| 426 | switch (scope) { |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 427 | case SCOPE_SESS: vars = &strm->sess->vars; break; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 428 | case SCOPE_TXN: vars = &strm->vars_txn; break; |
| 429 | case SCOPE_REQ: |
Thierry FOURNIER | 0b243fd | 2015-06-16 23:52:47 +0200 | [diff] [blame] | 430 | case SCOPE_RES: |
| 431 | default: vars = &strm->vars_reqres; break; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* Check if the scope is avalaible a this point of processing. */ |
| 435 | if (vars->scope != scope) |
| 436 | return 0; |
| 437 | |
| 438 | /* Get the variable entry. */ |
| 439 | var = var_get(vars, name); |
| 440 | if (!var) |
| 441 | return 0; |
| 442 | |
| 443 | /* Copy sample. */ |
Thierry FOURNIER | 5cc18d4 | 2015-08-19 09:02:36 +0200 | [diff] [blame] | 444 | smp->data = var->data; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 445 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 446 | return 1; |
| 447 | } |
| 448 | |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 449 | /* this function fills a sample with the |
| 450 | * content of the varaible described by <var_desc>. Returns 1 |
| 451 | * if the sample is filled, otherwise it returns 0. |
| 452 | */ |
| 453 | int vars_get_by_desc(const struct var_desc *var_desc, struct stream *strm, struct sample *smp) |
| 454 | { |
| 455 | struct vars *vars; |
| 456 | struct var *var; |
| 457 | |
| 458 | /* Select "vars" pool according with the scope. */ |
| 459 | switch (var_desc->scope) { |
| 460 | case SCOPE_SESS: vars = &strm->sess->vars; break; |
| 461 | case SCOPE_TXN: vars = &strm->vars_txn; break; |
| 462 | case SCOPE_REQ: |
| 463 | case SCOPE_RES: |
| 464 | default: vars = &strm->vars_reqres; break; |
| 465 | } |
| 466 | |
| 467 | /* Check if the scope is avalaible a this point of processing. */ |
| 468 | if (vars->scope != var_desc->scope) |
| 469 | return 0; |
| 470 | |
| 471 | /* Get the variable entry. */ |
| 472 | var = var_get(vars, var_desc->name); |
| 473 | if (!var) |
| 474 | return 0; |
| 475 | |
| 476 | /* Copy sample. */ |
Thierry FOURNIER | 5cc18d4 | 2015-08-19 09:02:36 +0200 | [diff] [blame] | 477 | smp->data = var->data; |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 478 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 479 | return 1; |
| 480 | } |
| 481 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 482 | /* Always returns ACT_RET_CONT even if an error occurs. */ |
| 483 | static enum act_return action_store(struct act_rule *rule, struct proxy *px, |
| 484 | struct session *sess, struct stream *s) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 485 | { |
| 486 | struct sample smp; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 487 | int dir; |
| 488 | |
| 489 | switch (rule->from) { |
| 490 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 491 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 492 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 493 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
| 494 | default: |
| 495 | send_log(px, LOG_ERR, "Vars: internal error while execute action store."); |
| 496 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 497 | Alert("Vars: internal error while execute action store.\n"); |
| 498 | return ACT_RET_CONT; |
| 499 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 500 | |
| 501 | /* Process the expression. */ |
| 502 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 503 | if (!sample_process(px, s->sess, s, dir|SMP_OPT_FINAL, |
| 504 | rule->arg.vars.expr, &smp)) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 505 | return ACT_RET_CONT; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 506 | |
| 507 | /* Store the sample, and ignore errors. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 508 | sample_store_stream(rule->arg.vars.name, rule->arg.vars.scope, s, &smp); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 509 | return ACT_RET_CONT; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 510 | } |
| 511 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 512 | /* This two function checks the variable name and replace the |
| 513 | * configuration string name by the global string name. its |
| 514 | * the same string, but the global pointer can be easy to |
| 515 | * compare. |
| 516 | * |
| 517 | * The first function checks a sample-fetch and the second |
| 518 | * checks a converter. |
| 519 | */ |
| 520 | static int smp_check_var(struct arg *args, char **err) |
| 521 | { |
| 522 | return vars_check_arg(&args[0], err); |
| 523 | } |
| 524 | |
| 525 | static int conv_check_var(struct arg *args, struct sample_conv *conv, |
| 526 | const char *file, int line, char **err_msg) |
| 527 | { |
| 528 | return vars_check_arg(&args[0], err_msg); |
| 529 | } |
| 530 | |
| 531 | /* This function is a common parser for using variables. It understands |
| 532 | * the format: |
| 533 | * |
| 534 | * set-var(<variable-name>) <expression> |
| 535 | * |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 536 | * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error |
| 537 | * message. Otherwise, it returns ACT_RET_PRS_OK and the variable <expr> |
| 538 | * is filled with the pointer to the expression to execute. |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 539 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 540 | static enum act_parse_ret parse_store(const char **args, int *arg, struct proxy *px, |
| 541 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 542 | { |
| 543 | const char *var_name = args[*arg-1]; |
| 544 | int var_len; |
Thierry FOURNIER | 48a9cd1 | 2015-07-28 19:00:28 +0200 | [diff] [blame] | 545 | const char *kw_name; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 546 | int flags; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 547 | |
| 548 | var_name += strlen("set-var"); |
| 549 | if (*var_name != '(') { |
| 550 | memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)'", args[*arg-1]); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 551 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 552 | } |
| 553 | var_name++; /* jump the '(' */ |
| 554 | var_len = strlen(var_name); |
| 555 | var_len--; /* remove the ')' */ |
| 556 | if (var_name[var_len] != ')') { |
| 557 | memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)'", args[*arg-1]); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 558 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 561 | rule->arg.vars.name = register_name(var_name, var_len, &rule->arg.vars.scope, err); |
| 562 | if (!rule->arg.vars.name) |
| 563 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 564 | |
Thierry FOURNIER | 48a9cd1 | 2015-07-28 19:00:28 +0200 | [diff] [blame] | 565 | kw_name = args[*arg-1]; |
| 566 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 567 | rule->arg.vars.expr = sample_parse_expr((char **)args, arg, px->conf.args.file, |
| 568 | px->conf.args.line, err, &px->conf.args); |
| 569 | if (!rule->arg.vars.expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 570 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 571 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 572 | switch (rule->from) { |
| 573 | case ACT_F_TCP_REQ_CNT: flags = SMP_VAL_FE_REQ_CNT; break; |
| 574 | case ACT_F_TCP_RES_CNT: flags = SMP_VAL_BE_RES_CNT; break; |
| 575 | case ACT_F_HTTP_REQ: flags = SMP_VAL_FE_HRQ_HDR; break; |
| 576 | case ACT_F_HTTP_RES: flags = SMP_VAL_BE_HRS_HDR; break; |
| 577 | default: |
| 578 | memprintf(err, |
| 579 | "internal error, unexpected rule->from=%d, please report this bug!", |
| 580 | rule->from); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 581 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 582 | } |
| 583 | if (!(rule->arg.vars.expr->fetch->val & flags)) { |
| 584 | memprintf(err, |
| 585 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 586 | kw_name, sample_src_names(rule->arg.vars.expr->fetch->use)); |
| 587 | free(rule->arg.vars.expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 588 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 589 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 590 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 591 | rule->action = ACT_ACTION_CONT; |
| 592 | rule->action_ptr = action_store; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 593 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | static int vars_max_size(char **args, int section_type, struct proxy *curpx, |
| 597 | struct proxy *defpx, const char *file, int line, |
| 598 | char **err, unsigned int *limit) |
| 599 | { |
| 600 | char *error; |
| 601 | |
| 602 | *limit = strtol(args[1], &error, 10); |
| 603 | if (*error != 0) { |
| 604 | memprintf(err, "%s: '%s' is an invalid size", args[0], args[1]); |
| 605 | return -1; |
| 606 | } |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static int vars_max_size_global(char **args, int section_type, struct proxy *curpx, |
| 611 | struct proxy *defpx, const char *file, int line, |
| 612 | char **err) |
| 613 | { |
| 614 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_global_limit); |
| 615 | } |
| 616 | |
| 617 | static int vars_max_size_sess(char **args, int section_type, struct proxy *curpx, |
| 618 | struct proxy *defpx, const char *file, int line, |
| 619 | char **err) |
| 620 | { |
| 621 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_sess_limit); |
| 622 | } |
| 623 | |
| 624 | static int vars_max_size_txn(char **args, int section_type, struct proxy *curpx, |
| 625 | struct proxy *defpx, const char *file, int line, |
| 626 | char **err) |
| 627 | { |
| 628 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_txn_limit); |
| 629 | } |
| 630 | |
| 631 | static int vars_max_size_reqres(char **args, int section_type, struct proxy *curpx, |
| 632 | struct proxy *defpx, const char *file, int line, |
| 633 | char **err) |
| 634 | { |
| 635 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_reqres_limit); |
| 636 | } |
| 637 | |
| 638 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 639 | |
| 640 | { "var", smp_fetch_var, ARG1(1,STR), smp_check_var, SMP_T_STR, SMP_USE_HTTP_ANY }, |
| 641 | { /* END */ }, |
| 642 | }}; |
| 643 | |
| 644 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
| 645 | { "set-var", smp_conv_store, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY }, |
| 646 | { /* END */ }, |
| 647 | }}; |
| 648 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 649 | static struct action_kw_list tcp_req_kws = { { }, { |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 650 | { "set-var", parse_store, 1 }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 651 | { /* END */ } |
| 652 | }}; |
| 653 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 654 | static struct action_kw_list tcp_res_kws = { { }, { |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 655 | { "set-var", parse_store, 1 }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 656 | { /* END */ } |
| 657 | }}; |
| 658 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 659 | static struct action_kw_list http_req_kws = { { }, { |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 660 | { "set-var", parse_store, 1 }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 661 | { /* END */ } |
| 662 | }}; |
| 663 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 664 | static struct action_kw_list http_res_kws = { { }, { |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 665 | { "set-var", parse_store, 1 }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 666 | { /* END */ } |
| 667 | }}; |
| 668 | |
| 669 | static struct cfg_kw_list cfg_kws = {{ },{ |
| 670 | { CFG_GLOBAL, "tune.vars.global-max-size", vars_max_size_global }, |
| 671 | { CFG_GLOBAL, "tune.vars.sess-max-size", vars_max_size_sess }, |
| 672 | { CFG_GLOBAL, "tune.vars.txn-max-size", vars_max_size_txn }, |
| 673 | { CFG_GLOBAL, "tune.vars.reqres-max-size", vars_max_size_reqres }, |
| 674 | { /* END */ } |
| 675 | }}; |
| 676 | |
| 677 | __attribute__((constructor)) |
| 678 | static void __http_protocol_init(void) |
| 679 | { |
| 680 | var_pool = create_pool("vars", sizeof(struct var), MEM_F_SHARED); |
| 681 | |
| 682 | sample_register_fetches(&sample_fetch_keywords); |
| 683 | sample_register_convs(&sample_conv_kws); |
| 684 | tcp_req_cont_keywords_register(&tcp_req_kws); |
| 685 | tcp_res_cont_keywords_register(&tcp_res_kws); |
| 686 | http_req_keywords_register(&http_req_kws); |
| 687 | http_res_keywords_register(&http_res_kws); |
| 688 | cfg_register_keywords(&cfg_kws); |
| 689 | } |