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