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