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> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 10 | #include <proto/sample.h> |
| 11 | #include <proto/stream.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 12 | #include <proto/tcp_rules.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; |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 29 | static unsigned int var_proc_limit = 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 30 | static unsigned int var_sess_limit = 0; |
| 31 | static unsigned int var_txn_limit = 0; |
| 32 | static unsigned int var_reqres_limit = 0; |
| 33 | |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 34 | |
| 35 | #ifdef USE_THREAD |
| 36 | HA_RWLOCK_T var_names_rwlock; |
| 37 | #endif |
| 38 | |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 39 | /* This function adds or remove memory size from the accounting. The inner |
| 40 | * pointers may be null when setting the outer ones only. |
| 41 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 42 | static void var_accounting_diff(struct vars *vars, struct session *sess, struct stream *strm, int size) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 43 | { |
| 44 | switch (vars->scope) { |
| 45 | case SCOPE_REQ: |
| 46 | case SCOPE_RES: |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 47 | HA_ATOMIC_ADD(&strm->vars_reqres.size, size); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 48 | /* fall through */ |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 49 | case SCOPE_TXN: |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 50 | HA_ATOMIC_ADD(&strm->vars_txn.size, size); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 51 | /* fall through */ |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 52 | case SCOPE_SESS: |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 53 | HA_ATOMIC_ADD(&sess->vars.size, size); |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 54 | /* fall through */ |
| 55 | case SCOPE_PROC: |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 56 | HA_ATOMIC_ADD(&global.vars.size, size); |
| 57 | HA_ATOMIC_ADD(&var_global_size, size); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | /* This function returns 1 if the <size> is available in the var |
| 62 | * pool <vars>, otherwise returns 0. If the space is avalaible, |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 63 | * the size is reserved. The inner pointers may be null when setting |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 64 | * the outer ones only. The accounting uses either <sess> or <strm> |
| 65 | * depending on the scope. <strm> may be NULL when no stream is known |
| 66 | * and only the session exists (eg: tcp-request connection). |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 67 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 68 | static int var_accounting_add(struct vars *vars, struct session *sess, struct stream *strm, int size) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 69 | { |
| 70 | switch (vars->scope) { |
| 71 | case SCOPE_REQ: |
| 72 | case SCOPE_RES: |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 73 | if (var_reqres_limit && strm->vars_reqres.size + size > var_reqres_limit) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 74 | return 0; |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 75 | /* fall through */ |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 76 | case SCOPE_TXN: |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 77 | if (var_txn_limit && strm->vars_txn.size + size > var_txn_limit) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 78 | return 0; |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 79 | /* fall through */ |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 80 | case SCOPE_SESS: |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 81 | if (var_sess_limit && sess->vars.size + size > var_sess_limit) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 82 | return 0; |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 83 | /* fall through */ |
| 84 | case SCOPE_PROC: |
| 85 | if (var_proc_limit && global.vars.size + size > var_proc_limit) |
| 86 | return 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 87 | if (var_global_limit && var_global_size + size > var_global_limit) |
| 88 | return 0; |
| 89 | } |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 90 | var_accounting_diff(vars, sess, strm, size); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 91 | return 1; |
| 92 | } |
| 93 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 94 | /* This fnuction remove a variable from the list and free memory it used */ |
| 95 | unsigned int var_clear(struct var *var) |
| 96 | { |
| 97 | unsigned int size = 0; |
| 98 | |
| 99 | if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) { |
| 100 | free(var->data.u.str.str); |
| 101 | size += var->data.u.str.len; |
| 102 | } |
Christopher Faulet | d02210c | 2017-07-24 16:24:39 +0200 | [diff] [blame] | 103 | else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 104 | free(var->data.u.meth.str.str); |
| 105 | size += var->data.u.meth.str.len; |
| 106 | } |
| 107 | LIST_DEL(&var->l); |
| 108 | pool_free2(var_pool, var); |
| 109 | size += sizeof(struct var); |
| 110 | return size; |
| 111 | } |
| 112 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 113 | /* This function free all the memory used by all the varaibles |
| 114 | * in the list. |
| 115 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 116 | void vars_prune(struct vars *vars, struct session *sess, struct stream *strm) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 117 | { |
| 118 | struct var *var, *tmp; |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 119 | unsigned int size = 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 120 | |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 121 | RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 122 | list_for_each_entry_safe(var, tmp, &vars->head, l) { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 123 | size += var_clear(var); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 124 | } |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 125 | RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 126 | var_accounting_diff(vars, sess, strm, -size); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 129 | /* This function frees all the memory used by all the session variables in the |
| 130 | * list starting at <vars>. |
| 131 | */ |
| 132 | void vars_prune_per_sess(struct vars *vars) |
| 133 | { |
| 134 | struct var *var, *tmp; |
| 135 | unsigned int size = 0; |
| 136 | |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 137 | RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock); |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 138 | list_for_each_entry_safe(var, tmp, &vars->head, l) { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 139 | size += var_clear(var); |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 140 | } |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 141 | RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock); |
| 142 | |
| 143 | HA_ATOMIC_SUB(&vars->size, size); |
| 144 | HA_ATOMIC_SUB(&global.vars.size, size); |
| 145 | HA_ATOMIC_SUB(&var_global_size, size); |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 148 | /* This function init a list of variabes. */ |
| 149 | void vars_init(struct vars *vars, enum vars_scope scope) |
| 150 | { |
| 151 | LIST_INIT(&vars->head); |
| 152 | vars->scope = scope; |
| 153 | vars->size = 0; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 154 | RWLOCK_INIT(&vars->rwlock); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* This function declares a new variable name. It returns a pointer |
| 158 | * on the string identifying the name. This function assures that |
| 159 | * the same name exists only once. |
| 160 | * |
| 161 | * This function check if the variable name is acceptable. |
| 162 | * |
| 163 | * The function returns NULL if an error occurs, and <err> is filled. |
| 164 | * In this case, the HAProxy must be stopped because the structs are |
| 165 | * left inconsistent. Otherwise, it returns the pointer on the global |
| 166 | * name. |
| 167 | */ |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 168 | static char *register_name(const char *name, int len, enum vars_scope *scope, |
| 169 | int alloc, char **err) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 170 | { |
| 171 | int i; |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 172 | char **var_names2; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 173 | const char *tmp; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 174 | char *res = NULL; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 175 | |
| 176 | /* Check length. */ |
| 177 | if (len == 0) { |
| 178 | memprintf(err, "Empty variable name cannot be accepted"); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 179 | res = NULL; |
| 180 | goto end; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* Check scope. */ |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 184 | if (len > 5 && strncmp(name, "proc.", 5) == 0) { |
| 185 | name += 5; |
| 186 | len -= 5; |
| 187 | *scope = SCOPE_PROC; |
| 188 | } |
| 189 | else if (len > 5 && strncmp(name, "sess.", 5) == 0) { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 190 | name += 5; |
| 191 | len -= 5; |
| 192 | *scope = SCOPE_SESS; |
| 193 | } |
| 194 | else if (len > 4 && strncmp(name, "txn.", 4) == 0) { |
| 195 | name += 4; |
| 196 | len -= 4; |
| 197 | *scope = SCOPE_TXN; |
| 198 | } |
| 199 | else if (len > 4 && strncmp(name, "req.", 4) == 0) { |
| 200 | name += 4; |
| 201 | len -= 4; |
| 202 | *scope = SCOPE_REQ; |
| 203 | } |
| 204 | else if (len > 4 && strncmp(name, "res.", 4) == 0) { |
| 205 | name += 4; |
| 206 | len -= 4; |
| 207 | *scope = SCOPE_RES; |
| 208 | } |
| 209 | else { |
| 210 | memprintf(err, "invalid variable name '%s'. A variable name must be start by its scope. " |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 211 | "The scope can be 'proc', 'sess', 'txn', 'req' or 'res'", name); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 212 | res = NULL; |
| 213 | goto end; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 216 | if (alloc) |
| 217 | RWLOCK_WRLOCK(VARS_LOCK, &var_names_rwlock); |
| 218 | else |
| 219 | RWLOCK_RDLOCK(VARS_LOCK, &var_names_rwlock); |
| 220 | |
| 221 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 222 | /* Look for existing variable name. */ |
| 223 | for (i = 0; i < var_names_nb; i++) |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 224 | if (strncmp(var_names[i], name, len) == 0 && var_names[i][len] == '\0') { |
| 225 | res = var_names[i]; |
| 226 | goto end; |
| 227 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 228 | |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 229 | if (!alloc) { |
| 230 | res = NULL; |
| 231 | goto end; |
| 232 | } |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 233 | |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 234 | /* Store variable name. If realloc fails, var_names remains valid */ |
| 235 | var_names2 = realloc(var_names, (var_names_nb + 1) * sizeof(*var_names)); |
| 236 | if (!var_names2) { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 237 | memprintf(err, "out of memory error"); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 238 | res = NULL; |
| 239 | goto end; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 240 | } |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 241 | var_names_nb++; |
| 242 | var_names = var_names2; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 243 | var_names[var_names_nb - 1] = malloc(len + 1); |
| 244 | if (!var_names[var_names_nb - 1]) { |
| 245 | memprintf(err, "out of memory error"); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 246 | res = NULL; |
| 247 | goto end; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 248 | } |
| 249 | memcpy(var_names[var_names_nb - 1], name, len); |
| 250 | var_names[var_names_nb - 1][len] = '\0'; |
| 251 | |
| 252 | /* Check variable name syntax. */ |
| 253 | tmp = var_names[var_names_nb - 1]; |
| 254 | while (*tmp) { |
Christopher Faulet | b71557a | 2016-10-31 10:49:03 +0100 | [diff] [blame] | 255 | if (!isalnum((int)(unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 256 | memprintf(err, "invalid syntax at char '%s'", tmp); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 257 | res = NULL; |
| 258 | goto end; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 259 | } |
| 260 | tmp++; |
| 261 | } |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 262 | res = var_names[var_names_nb - 1]; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 263 | |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 264 | end: |
| 265 | if (alloc) |
| 266 | RWLOCK_WRUNLOCK(VARS_LOCK, &var_names_rwlock); |
| 267 | else |
| 268 | RWLOCK_RDUNLOCK(VARS_LOCK, &var_names_rwlock); |
| 269 | |
| 270 | return res; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* This function returns an existing variable or returns NULL. */ |
| 274 | static inline struct var *var_get(struct vars *vars, const char *name) |
| 275 | { |
| 276 | struct var *var; |
| 277 | |
| 278 | list_for_each_entry(var, &vars->head, l) |
| 279 | if (var->name == name) |
| 280 | return var; |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | /* Returns 0 if fails, else returns 1. */ |
| 285 | static int smp_fetch_var(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 286 | { |
| 287 | const struct var_desc *var_desc = &args[0].data.var; |
| 288 | struct var *var; |
| 289 | struct vars *vars; |
| 290 | |
| 291 | /* Check the availibity of the variable. */ |
| 292 | switch (var_desc->scope) { |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 293 | case SCOPE_PROC: |
| 294 | vars = &global.vars; |
| 295 | break; |
Willy Tarreau | 7513d00 | 2016-10-21 17:14:35 +0200 | [diff] [blame] | 296 | case SCOPE_SESS: |
| 297 | vars = &smp->sess->vars; |
| 298 | break; |
| 299 | case SCOPE_TXN: |
| 300 | if (!smp->strm) |
| 301 | return 0; |
| 302 | vars = &smp->strm->vars_txn; |
| 303 | break; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 304 | case SCOPE_REQ: |
Thierry FOURNIER | 0b243fd | 2015-06-16 23:52:47 +0200 | [diff] [blame] | 305 | case SCOPE_RES: |
Willy Tarreau | 7513d00 | 2016-10-21 17:14:35 +0200 | [diff] [blame] | 306 | default: |
| 307 | if (!smp->strm) |
| 308 | return 0; |
| 309 | vars = &smp->strm->vars_reqres; |
| 310 | break; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 311 | } |
| 312 | if (vars->scope != var_desc->scope) |
| 313 | return 0; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 314 | |
| 315 | RWLOCK_RDLOCK(VARS_LOCK, &vars->rwlock); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 316 | var = var_get(vars, var_desc->name); |
| 317 | |
| 318 | /* check for the variable avalaibility */ |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 319 | if (!var) { |
| 320 | RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 321 | return 0; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 322 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 323 | |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 324 | /* Duplicate the sample data because it could modified by another |
| 325 | * thread */ |
Thierry FOURNIER | 5cc18d4 | 2015-08-19 09:02:36 +0200 | [diff] [blame] | 326 | smp->data = var->data; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 327 | smp_dup(smp); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 328 | smp->flags |= SMP_F_CONST; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 329 | |
| 330 | RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 331 | return 1; |
| 332 | } |
| 333 | |
| 334 | /* This function search in the <head> a variable with the same |
| 335 | * pointer value that the <name>. If the variable doesn't exists, |
| 336 | * create it. The function stores a copy of smp> if the variable. |
| 337 | * It returns 0 if fails, else returns 1. |
| 338 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 339 | static int sample_store(struct vars *vars, const char *name, struct sample *smp) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 340 | { |
| 341 | struct var *var; |
| 342 | |
| 343 | /* Look for existing variable name. */ |
| 344 | var = var_get(vars, name); |
| 345 | |
| 346 | if (var) { |
| 347 | /* free its used memory. */ |
| 348 | if (var->data.type == SMP_T_STR || |
| 349 | var->data.type == SMP_T_BIN) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 350 | free(var->data.u.str.str); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 351 | var_accounting_diff(vars, smp->sess, smp->strm, -var->data.u.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 352 | } |
Christopher Faulet | d02210c | 2017-07-24 16:24:39 +0200 | [diff] [blame] | 353 | else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 354 | free(var->data.u.meth.str.str); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 355 | var_accounting_diff(vars, smp->sess, smp->strm, -var->data.u.meth.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 356 | } |
| 357 | } else { |
| 358 | |
| 359 | /* Check memory avalaible. */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 360 | if (!var_accounting_add(vars, smp->sess, smp->strm, sizeof(struct var))) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 361 | return 0; |
| 362 | |
| 363 | /* Create new entry. */ |
| 364 | var = pool_alloc2(var_pool); |
| 365 | if (!var) |
| 366 | return 0; |
| 367 | LIST_ADDQ(&vars->head, &var->l); |
| 368 | var->name = name; |
| 369 | } |
| 370 | |
| 371 | /* Set type. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 372 | var->data.type = smp->data.type; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 373 | |
| 374 | /* Copy data. If the data needs memory, the function can fail. */ |
| 375 | switch (var->data.type) { |
| 376 | case SMP_T_BOOL: |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 377 | case SMP_T_SINT: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 378 | var->data.u.sint = smp->data.u.sint; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 379 | break; |
| 380 | case SMP_T_IPV4: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 381 | var->data.u.ipv4 = smp->data.u.ipv4; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 382 | break; |
| 383 | case SMP_T_IPV6: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 384 | var->data.u.ipv6 = smp->data.u.ipv6; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 385 | break; |
| 386 | case SMP_T_STR: |
| 387 | case SMP_T_BIN: |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 388 | if (!var_accounting_add(vars, smp->sess, smp->strm, smp->data.u.str.len)) { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 389 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 390 | return 0; |
| 391 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 392 | var->data.u.str.str = malloc(smp->data.u.str.len); |
| 393 | if (!var->data.u.str.str) { |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 394 | var_accounting_diff(vars, smp->sess, smp->strm, -smp->data.u.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 395 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 396 | return 0; |
| 397 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 398 | var->data.u.str.len = smp->data.u.str.len; |
| 399 | 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] | 400 | break; |
| 401 | case SMP_T_METH: |
Christopher Faulet | d02210c | 2017-07-24 16:24:39 +0200 | [diff] [blame] | 402 | var->data.u.meth.meth = smp->data.u.meth.meth; |
| 403 | if (smp->data.u.meth.meth != HTTP_METH_OTHER) |
| 404 | break; |
| 405 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 406 | if (!var_accounting_add(vars, smp->sess, smp->strm, smp->data.u.meth.str.len)) { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 407 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 408 | return 0; |
| 409 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 410 | var->data.u.meth.str.str = malloc(smp->data.u.meth.str.len); |
| 411 | if (!var->data.u.meth.str.str) { |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 412 | var_accounting_diff(vars, smp->sess, smp->strm, -smp->data.u.meth.str.len); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 413 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 414 | return 0; |
| 415 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 416 | var->data.u.meth.str.len = smp->data.u.meth.str.len; |
| 417 | var->data.u.meth.str.size = smp->data.u.meth.str.len; |
| 418 | 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] | 419 | break; |
| 420 | } |
| 421 | return 1; |
| 422 | } |
| 423 | |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 424 | /* Returns 0 if fails, else returns 1. Note that stream may be null for SCOPE_SESS. */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 425 | static inline int sample_store_stream(const char *name, enum vars_scope scope, struct sample *smp) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 426 | { |
| 427 | struct vars *vars; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 428 | int ret; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 429 | |
| 430 | switch (scope) { |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 431 | case SCOPE_PROC: vars = &global.vars; break; |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 432 | case SCOPE_SESS: vars = &smp->sess->vars; break; |
| 433 | case SCOPE_TXN: vars = &smp->strm->vars_txn; break; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 434 | case SCOPE_REQ: |
Thierry FOURNIER | 0b243fd | 2015-06-16 23:52:47 +0200 | [diff] [blame] | 435 | case SCOPE_RES: |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 436 | default: vars = &smp->strm->vars_reqres; break; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 437 | } |
| 438 | if (vars->scope != scope) |
| 439 | return 0; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 440 | |
| 441 | RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock); |
| 442 | ret = sample_store(vars, name, smp); |
| 443 | RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock); |
| 444 | return ret; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 447 | /* Returns 0 if fails, else returns 1. Note that stream may be null for SCOPE_SESS. */ |
| 448 | static inline int sample_clear_stream(const char *name, enum vars_scope scope, struct sample *smp) |
| 449 | { |
| 450 | struct vars *vars; |
| 451 | struct var *var; |
| 452 | unsigned int size = 0; |
| 453 | |
| 454 | switch (scope) { |
| 455 | case SCOPE_PROC: vars = &global.vars; break; |
| 456 | case SCOPE_SESS: vars = &smp->sess->vars; break; |
| 457 | case SCOPE_TXN: vars = &smp->strm->vars_txn; break; |
| 458 | case SCOPE_REQ: |
| 459 | case SCOPE_RES: |
| 460 | default: vars = &smp->strm->vars_reqres; break; |
| 461 | } |
| 462 | if (vars->scope != scope) |
| 463 | return 0; |
| 464 | |
| 465 | /* Look for existing variable name. */ |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 466 | RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 467 | var = var_get(vars, name); |
| 468 | if (var) { |
| 469 | size = var_clear(var); |
| 470 | var_accounting_diff(vars, smp->sess, smp->strm, -size); |
| 471 | } |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 472 | RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 473 | return 1; |
| 474 | } |
| 475 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 476 | /* Returns 0 if fails, else returns 1. */ |
| 477 | static int smp_conv_store(const struct arg *args, struct sample *smp, void *private) |
| 478 | { |
Christopher Faulet | 0099a8c | 2016-11-09 16:15:32 +0100 | [diff] [blame] | 479 | return sample_store_stream(args[0].data.var.name, args[0].data.var.scope, smp); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 482 | /* Returns 0 if fails, else returns 1. */ |
| 483 | static int smp_conv_clear(const struct arg *args, struct sample *smp, void *private) |
| 484 | { |
| 485 | return sample_clear_stream(args[0].data.var.name, args[0].data.var.scope, smp); |
| 486 | } |
| 487 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 488 | /* This fucntions check an argument entry and fill it with a variable |
| 489 | * type. The argumen must be a string. If the variable lookup fails, |
| 490 | * the function retuns 0 and fill <err>, otherwise it returns 1. |
| 491 | */ |
| 492 | int vars_check_arg(struct arg *arg, char **err) |
| 493 | { |
| 494 | char *name; |
| 495 | enum vars_scope scope; |
| 496 | |
| 497 | /* Check arg type. */ |
| 498 | if (arg->type != ARGT_STR) { |
| 499 | memprintf(err, "unexpected argument type"); |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | /* Register new variable name. */ |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 504 | name = register_name(arg->data.str.str, arg->data.str.len, &scope, 1, err); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 505 | if (!name) |
| 506 | return 0; |
| 507 | |
| 508 | /* Use the global variable name pointer. */ |
| 509 | arg->type = ARGT_VAR; |
| 510 | arg->data.var.name = name; |
| 511 | arg->data.var.scope = scope; |
| 512 | return 1; |
| 513 | } |
| 514 | |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 515 | /* This function store a sample in a variable if it was already defined. |
| 516 | * In error case, it fails silently. |
| 517 | */ |
| 518 | void vars_set_by_name_ifexist(const char *name, size_t len, struct sample *smp) |
| 519 | { |
| 520 | enum vars_scope scope; |
| 521 | |
| 522 | /* Resolve name and scope. */ |
| 523 | name = register_name(name, len, &scope, 0, NULL); |
| 524 | if (!name) |
| 525 | return; |
| 526 | |
| 527 | sample_store_stream(name, scope, smp); |
| 528 | } |
| 529 | |
| 530 | |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 531 | /* This function store a sample in a variable. |
| 532 | * In error case, it fails silently. |
| 533 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 534 | void vars_set_by_name(const char *name, size_t len, struct sample *smp) |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 535 | { |
| 536 | enum vars_scope scope; |
| 537 | |
| 538 | /* Resolve name and scope. */ |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 539 | name = register_name(name, len, &scope, 1, NULL); |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 540 | if (!name) |
| 541 | return; |
| 542 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 543 | sample_store_stream(name, scope, smp); |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 546 | /* This function unset a variable if it was already defined. |
| 547 | * In error case, it fails silently. |
| 548 | */ |
| 549 | void vars_unset_by_name_ifexist(const char *name, size_t len, struct sample *smp) |
| 550 | { |
| 551 | enum vars_scope scope; |
| 552 | |
| 553 | /* Resolve name and scope. */ |
| 554 | name = register_name(name, len, &scope, 0, NULL); |
| 555 | if (!name) |
| 556 | return; |
| 557 | |
| 558 | sample_clear_stream(name, scope, smp); |
| 559 | } |
| 560 | |
| 561 | |
| 562 | /* This function unset a variable. |
| 563 | * In error case, it fails silently. |
| 564 | */ |
| 565 | void vars_unset_by_name(const char *name, size_t len, struct sample *smp) |
| 566 | { |
| 567 | enum vars_scope scope; |
| 568 | |
| 569 | /* Resolve name and scope. */ |
| 570 | name = register_name(name, len, &scope, 1, NULL); |
| 571 | if (!name) |
| 572 | return; |
| 573 | |
| 574 | sample_clear_stream(name, scope, smp); |
| 575 | } |
| 576 | |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 577 | /* this function fills a sample with the |
| 578 | * variable content. Returns 1 if the sample |
| 579 | * is filled, otherwise it returns 0. |
| 580 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 581 | int vars_get_by_name(const char *name, size_t len, struct sample *smp) |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 582 | { |
| 583 | struct vars *vars; |
| 584 | struct var *var; |
| 585 | enum vars_scope scope; |
| 586 | |
| 587 | /* Resolve name and scope. */ |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 588 | name = register_name(name, len, &scope, 1, NULL); |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 589 | if (!name) |
| 590 | return 0; |
| 591 | |
| 592 | /* Select "vars" pool according with the scope. */ |
| 593 | switch (scope) { |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 594 | case SCOPE_PROC: vars = &global.vars; break; |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 595 | case SCOPE_SESS: vars = &smp->sess->vars; break; |
| 596 | case SCOPE_TXN: vars = &smp->strm->vars_txn; break; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 597 | case SCOPE_REQ: |
Thierry FOURNIER | 0b243fd | 2015-06-16 23:52:47 +0200 | [diff] [blame] | 598 | case SCOPE_RES: |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 599 | default: vars = &smp->strm->vars_reqres; break; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* Check if the scope is avalaible a this point of processing. */ |
| 603 | if (vars->scope != scope) |
| 604 | return 0; |
| 605 | |
| 606 | /* Get the variable entry. */ |
| 607 | var = var_get(vars, name); |
| 608 | if (!var) |
| 609 | return 0; |
| 610 | |
| 611 | /* Copy sample. */ |
Thierry FOURNIER | 5cc18d4 | 2015-08-19 09:02:36 +0200 | [diff] [blame] | 612 | smp->data = var->data; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 613 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 614 | return 1; |
| 615 | } |
| 616 | |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 617 | /* this function fills a sample with the |
| 618 | * content of the varaible described by <var_desc>. Returns 1 |
| 619 | * if the sample is filled, otherwise it returns 0. |
| 620 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 621 | int vars_get_by_desc(const struct var_desc *var_desc, struct sample *smp) |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 622 | { |
| 623 | struct vars *vars; |
| 624 | struct var *var; |
| 625 | |
| 626 | /* Select "vars" pool according with the scope. */ |
| 627 | switch (var_desc->scope) { |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 628 | case SCOPE_PROC: vars = &global.vars; break; |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 629 | case SCOPE_SESS: vars = &smp->sess->vars; break; |
| 630 | case SCOPE_TXN: vars = &smp->strm->vars_txn; break; |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 631 | case SCOPE_REQ: |
| 632 | case SCOPE_RES: |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 633 | default: vars = &smp->strm->vars_reqres; break; |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /* Check if the scope is avalaible a this point of processing. */ |
| 637 | if (vars->scope != var_desc->scope) |
| 638 | return 0; |
| 639 | |
| 640 | /* Get the variable entry. */ |
| 641 | var = var_get(vars, var_desc->name); |
| 642 | if (!var) |
| 643 | return 0; |
| 644 | |
| 645 | /* Copy sample. */ |
Thierry FOURNIER | 5cc18d4 | 2015-08-19 09:02:36 +0200 | [diff] [blame] | 646 | smp->data = var->data; |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 647 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 648 | return 1; |
| 649 | } |
| 650 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 651 | /* Always returns ACT_RET_CONT even if an error occurs. */ |
| 652 | static enum act_return action_store(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 653 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 654 | { |
| 655 | struct sample smp; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 656 | int dir; |
| 657 | |
| 658 | switch (rule->from) { |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 659 | case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 660 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 661 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 662 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 663 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
| 664 | default: |
| 665 | send_log(px, LOG_ERR, "Vars: internal error while execute action store."); |
| 666 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 667 | Alert("Vars: internal error while execute action store.\n"); |
| 668 | return ACT_RET_CONT; |
| 669 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 670 | |
| 671 | /* Process the expression. */ |
| 672 | memset(&smp, 0, sizeof(smp)); |
Willy Tarreau | 108a8fd | 2016-10-21 17:13:24 +0200 | [diff] [blame] | 673 | if (!sample_process(px, sess, s, dir|SMP_OPT_FINAL, |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 674 | rule->arg.vars.expr, &smp)) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 675 | return ACT_RET_CONT; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 676 | |
| 677 | /* Store the sample, and ignore errors. */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 678 | sample_store_stream(rule->arg.vars.name, rule->arg.vars.scope, &smp); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 679 | return ACT_RET_CONT; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 682 | /* Always returns ACT_RET_CONT even if an error occurs. */ |
| 683 | static enum act_return action_clear(struct act_rule *rule, struct proxy *px, |
| 684 | struct session *sess, struct stream *s, int flags) |
| 685 | { |
| 686 | struct sample smp; |
| 687 | |
| 688 | memset(&smp, 0, sizeof(smp)); |
| 689 | smp_set_owner(&smp, px, sess, s, SMP_OPT_FINAL); |
| 690 | |
| 691 | /* Clear the variable using the sample context, and ignore errors. */ |
| 692 | sample_clear_stream(rule->arg.vars.name, rule->arg.vars.scope, &smp); |
| 693 | return ACT_RET_CONT; |
| 694 | } |
| 695 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 696 | /* This two function checks the variable name and replace the |
| 697 | * configuration string name by the global string name. its |
| 698 | * the same string, but the global pointer can be easy to |
| 699 | * compare. |
| 700 | * |
| 701 | * The first function checks a sample-fetch and the second |
| 702 | * checks a converter. |
| 703 | */ |
| 704 | static int smp_check_var(struct arg *args, char **err) |
| 705 | { |
| 706 | return vars_check_arg(&args[0], err); |
| 707 | } |
| 708 | |
| 709 | static int conv_check_var(struct arg *args, struct sample_conv *conv, |
| 710 | const char *file, int line, char **err_msg) |
| 711 | { |
| 712 | return vars_check_arg(&args[0], err_msg); |
| 713 | } |
| 714 | |
| 715 | /* This function is a common parser for using variables. It understands |
| 716 | * the format: |
| 717 | * |
| 718 | * set-var(<variable-name>) <expression> |
| 719 | * |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 720 | * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error |
| 721 | * message. Otherwise, it returns ACT_RET_PRS_OK and the variable <expr> |
| 722 | * is filled with the pointer to the expression to execute. |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 723 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 724 | static enum act_parse_ret parse_store(const char **args, int *arg, struct proxy *px, |
| 725 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 726 | { |
| 727 | const char *var_name = args[*arg-1]; |
| 728 | int var_len; |
Thierry FOURNIER | 48a9cd1 | 2015-07-28 19:00:28 +0200 | [diff] [blame] | 729 | const char *kw_name; |
Willy Tarreau | e365815 | 2016-11-24 21:23:28 +0100 | [diff] [blame] | 730 | int flags, set_var = 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 731 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 732 | if (!strncmp(var_name, "set-var", 7)) { |
| 733 | var_name += 7; |
| 734 | set_var = 1; |
| 735 | } |
| 736 | if (!strncmp(var_name, "unset-var", 9)) { |
| 737 | var_name += 9; |
| 738 | set_var = 0; |
| 739 | } |
| 740 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 741 | if (*var_name != '(') { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 742 | memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)' or 'unset-var(<var-name>)'", |
| 743 | args[*arg-1]); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 744 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 745 | } |
| 746 | var_name++; /* jump the '(' */ |
| 747 | var_len = strlen(var_name); |
| 748 | var_len--; /* remove the ')' */ |
| 749 | if (var_name[var_len] != ')') { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 750 | memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)' or 'unset-var(<var-name>)'", |
| 751 | args[*arg-1]); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 752 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 753 | } |
| 754 | |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 755 | rule->arg.vars.name = register_name(var_name, var_len, &rule->arg.vars.scope, 1, err); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 756 | if (!rule->arg.vars.name) |
| 757 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 758 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 759 | /* There is no fetch method when variable is unset. Just set the right |
| 760 | * action and return. */ |
| 761 | if (!set_var) { |
| 762 | if (*args[*arg]) { |
| 763 | memprintf(err, "fetch method not supported"); |
| 764 | return ACT_RET_PRS_ERR; |
| 765 | } |
| 766 | rule->action = ACT_CUSTOM; |
| 767 | rule->action_ptr = action_clear; |
| 768 | return ACT_RET_PRS_OK; |
| 769 | } |
| 770 | |
Thierry FOURNIER | 48a9cd1 | 2015-07-28 19:00:28 +0200 | [diff] [blame] | 771 | kw_name = args[*arg-1]; |
| 772 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 773 | rule->arg.vars.expr = sample_parse_expr((char **)args, arg, px->conf.args.file, |
| 774 | px->conf.args.line, err, &px->conf.args); |
| 775 | if (!rule->arg.vars.expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 776 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 777 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 778 | switch (rule->from) { |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 779 | case ACT_F_TCP_REQ_SES: flags = SMP_VAL_FE_SES_ACC; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 780 | case ACT_F_TCP_REQ_CNT: flags = SMP_VAL_FE_REQ_CNT; break; |
| 781 | case ACT_F_TCP_RES_CNT: flags = SMP_VAL_BE_RES_CNT; break; |
| 782 | case ACT_F_HTTP_REQ: flags = SMP_VAL_FE_HRQ_HDR; break; |
| 783 | case ACT_F_HTTP_RES: flags = SMP_VAL_BE_HRS_HDR; break; |
| 784 | default: |
| 785 | memprintf(err, |
| 786 | "internal error, unexpected rule->from=%d, please report this bug!", |
| 787 | rule->from); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 788 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 789 | } |
| 790 | if (!(rule->arg.vars.expr->fetch->val & flags)) { |
| 791 | memprintf(err, |
| 792 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 793 | kw_name, sample_src_names(rule->arg.vars.expr->fetch->use)); |
| 794 | free(rule->arg.vars.expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 795 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 796 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 797 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 798 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 799 | rule->action_ptr = action_store; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 800 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | static int vars_max_size(char **args, int section_type, struct proxy *curpx, |
| 804 | struct proxy *defpx, const char *file, int line, |
| 805 | char **err, unsigned int *limit) |
| 806 | { |
| 807 | char *error; |
| 808 | |
| 809 | *limit = strtol(args[1], &error, 10); |
| 810 | if (*error != 0) { |
| 811 | memprintf(err, "%s: '%s' is an invalid size", args[0], args[1]); |
| 812 | return -1; |
| 813 | } |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | static int vars_max_size_global(char **args, int section_type, struct proxy *curpx, |
| 818 | struct proxy *defpx, const char *file, int line, |
| 819 | char **err) |
| 820 | { |
| 821 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_global_limit); |
| 822 | } |
| 823 | |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 824 | static int vars_max_size_proc(char **args, int section_type, struct proxy *curpx, |
| 825 | struct proxy *defpx, const char *file, int line, |
| 826 | char **err) |
| 827 | { |
| 828 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_proc_limit); |
| 829 | } |
| 830 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 831 | static int vars_max_size_sess(char **args, int section_type, struct proxy *curpx, |
| 832 | struct proxy *defpx, const char *file, int line, |
| 833 | char **err) |
| 834 | { |
| 835 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_sess_limit); |
| 836 | } |
| 837 | |
| 838 | static int vars_max_size_txn(char **args, int section_type, struct proxy *curpx, |
| 839 | struct proxy *defpx, const char *file, int line, |
| 840 | char **err) |
| 841 | { |
| 842 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_txn_limit); |
| 843 | } |
| 844 | |
| 845 | static int vars_max_size_reqres(char **args, int section_type, struct proxy *curpx, |
| 846 | struct proxy *defpx, const char *file, int line, |
| 847 | char **err) |
| 848 | { |
| 849 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_reqres_limit); |
| 850 | } |
| 851 | |
| 852 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 853 | |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 854 | { "var", smp_fetch_var, ARG1(1,STR), smp_check_var, SMP_T_STR, SMP_USE_L4CLI }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 855 | { /* END */ }, |
| 856 | }}; |
| 857 | |
| 858 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 859 | { "set-var", smp_conv_store, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY }, |
| 860 | { "unset-var", smp_conv_clear, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 861 | { /* END */ }, |
| 862 | }}; |
| 863 | |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 864 | static struct action_kw_list tcp_req_sess_kws = { { }, { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 865 | { "set-var", parse_store, 1 }, |
| 866 | { "unset-var", parse_store, 1 }, |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 867 | { /* END */ } |
| 868 | }}; |
| 869 | |
| 870 | static struct action_kw_list tcp_req_cont_kws = { { }, { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 871 | { "set-var", parse_store, 1 }, |
| 872 | { "unset-var", parse_store, 1 }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 873 | { /* END */ } |
| 874 | }}; |
| 875 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 876 | static struct action_kw_list tcp_res_kws = { { }, { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 877 | { "set-var", parse_store, 1 }, |
| 878 | { "unset-var", parse_store, 1 }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 879 | { /* END */ } |
| 880 | }}; |
| 881 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 882 | static struct action_kw_list http_req_kws = { { }, { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 883 | { "set-var", parse_store, 1 }, |
| 884 | { "unset-var", parse_store, 1 }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 885 | { /* END */ } |
| 886 | }}; |
| 887 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 888 | static struct action_kw_list http_res_kws = { { }, { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 889 | { "set-var", parse_store, 1 }, |
| 890 | { "unset-var", parse_store, 1 }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 891 | { /* END */ } |
| 892 | }}; |
| 893 | |
| 894 | static struct cfg_kw_list cfg_kws = {{ },{ |
| 895 | { CFG_GLOBAL, "tune.vars.global-max-size", vars_max_size_global }, |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 896 | { CFG_GLOBAL, "tune.vars.proc-max-size", vars_max_size_proc }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 897 | { CFG_GLOBAL, "tune.vars.sess-max-size", vars_max_size_sess }, |
| 898 | { CFG_GLOBAL, "tune.vars.txn-max-size", vars_max_size_txn }, |
| 899 | { CFG_GLOBAL, "tune.vars.reqres-max-size", vars_max_size_reqres }, |
| 900 | { /* END */ } |
| 901 | }}; |
| 902 | |
| 903 | __attribute__((constructor)) |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 904 | static void __vars_init(void) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 905 | { |
| 906 | var_pool = create_pool("vars", sizeof(struct var), MEM_F_SHARED); |
| 907 | |
| 908 | sample_register_fetches(&sample_fetch_keywords); |
| 909 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 910 | tcp_req_sess_keywords_register(&tcp_req_sess_kws); |
| 911 | tcp_req_cont_keywords_register(&tcp_req_cont_kws); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 912 | tcp_res_cont_keywords_register(&tcp_res_kws); |
| 913 | http_req_keywords_register(&http_req_kws); |
| 914 | http_res_keywords_register(&http_res_kws); |
| 915 | cfg_register_keywords(&cfg_kws); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 916 | |
| 917 | RWLOCK_INIT(&var_names_rwlock); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 918 | } |