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