blob: 74a6ec8ffb629f3239b6f77ed0bed84dd8da7b03 [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:
301 case SMP_T_UINT:
302 case SMP_T_SINT:
303 var->data.data.sint = smp->data.sint;
304 break;
305 case SMP_T_IPV4:
306 var->data.data.ipv4 = smp->data.ipv4;
307 break;
308 case SMP_T_IPV6:
309 var->data.data.ipv6 = smp->data.ipv6;
310 break;
311 case SMP_T_STR:
312 case SMP_T_BIN:
Willy Tarreauebcd4842015-06-19 11:59:02 +0200313 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 +0200314 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
315 return 0;
316 }
317 var->data.data.str.str = malloc(smp->data.str.len);
318 if (!var->data.data.str.str) {
Willy Tarreauebcd4842015-06-19 11:59:02 +0200319 var_accounting_diff(vars, &strm->sess->vars, &strm->vars_txn, &strm->vars_reqres, -smp->data.str.len);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200320 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
321 return 0;
322 }
323 var->data.data.str.len = smp->data.str.len;
324 memcpy(var->data.data.str.str, smp->data.str.str, var->data.data.str.len);
325 break;
326 case SMP_T_METH:
Willy Tarreauebcd4842015-06-19 11:59:02 +0200327 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 +0200328 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
329 return 0;
330 }
331 var->data.data.meth.str.str = malloc(smp->data.meth.str.len);
332 if (!var->data.data.meth.str.str) {
Willy Tarreauebcd4842015-06-19 11:59:02 +0200333 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 +0200334 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
335 return 0;
336 }
337 var->data.data.meth.meth = smp->data.meth.meth;
338 var->data.data.meth.str.len = smp->data.meth.str.len;
339 var->data.data.meth.str.size = smp->data.meth.str.len;
340 memcpy(var->data.data.meth.str.str, smp->data.meth.str.str, var->data.data.meth.str.len);
341 break;
342 }
343 return 1;
344}
345
346/* Returns 0 if fails, else returns 1. */
347static inline int sample_store_stream(const char *name, enum vars_scope scope,
348 struct stream *strm, struct sample *smp)
349{
350 struct vars *vars;
351
352 switch (scope) {
Willy Tarreauebcd4842015-06-19 11:59:02 +0200353 case SCOPE_SESS: vars = &strm->sess->vars; break;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200354 case SCOPE_TXN: vars = &strm->vars_txn; break;
355 case SCOPE_REQ:
Thierry FOURNIER0b243fd2015-06-16 23:52:47 +0200356 case SCOPE_RES:
357 default: vars = &strm->vars_reqres; break;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200358 }
359 if (vars->scope != scope)
360 return 0;
361 return sample_store(vars, name, strm, smp);
362}
363
364/* Returns 0 if fails, else returns 1. */
365static int smp_conv_store(const struct arg *args, struct sample *smp, void *private)
366{
367 return sample_store_stream(args[0].data.var.name, args[1].data.var.scope, smp->strm, smp);
368}
369
370/* This fucntions check an argument entry and fill it with a variable
371 * type. The argumen must be a string. If the variable lookup fails,
372 * the function retuns 0 and fill <err>, otherwise it returns 1.
373 */
374int vars_check_arg(struct arg *arg, char **err)
375{
376 char *name;
377 enum vars_scope scope;
378
379 /* Check arg type. */
380 if (arg->type != ARGT_STR) {
381 memprintf(err, "unexpected argument type");
382 return 0;
383 }
384
385 /* Register new variable name. */
386 name = register_name(arg->data.str.str, arg->data.str.len, &scope, err);
387 if (!name)
388 return 0;
389
390 /* Use the global variable name pointer. */
391 arg->type = ARGT_VAR;
392 arg->data.var.name = name;
393 arg->data.var.scope = scope;
394 return 1;
395}
396
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200397/* This function store a sample in a variable.
398 * In error case, it fails silently.
399 */
400void vars_set_by_name(const char *name, size_t len, struct stream *strm, struct sample *smp)
401{
402 enum vars_scope scope;
403
404 /* Resolve name and scope. */
405 name = register_name(name, len, &scope, NULL);
406 if (!name)
407 return;
408
409 sample_store_stream(name, scope, strm, smp);
410}
411
412/* this function fills a sample with the
413 * variable content. Returns 1 if the sample
414 * is filled, otherwise it returns 0.
415 */
416int vars_get_by_name(const char *name, size_t len, struct stream *strm, struct sample *smp)
417{
418 struct vars *vars;
419 struct var *var;
420 enum vars_scope scope;
421
422 /* Resolve name and scope. */
423 name = register_name(name, len, &scope, NULL);
424 if (!name)
425 return 0;
426
427 /* Select "vars" pool according with the scope. */
428 switch (scope) {
Willy Tarreauebcd4842015-06-19 11:59:02 +0200429 case SCOPE_SESS: vars = &strm->sess->vars; break;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200430 case SCOPE_TXN: vars = &strm->vars_txn; break;
431 case SCOPE_REQ:
Thierry FOURNIER0b243fd2015-06-16 23:52:47 +0200432 case SCOPE_RES:
433 default: vars = &strm->vars_reqres; break;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200434 }
435
436 /* Check if the scope is avalaible a this point of processing. */
437 if (vars->scope != scope)
438 return 0;
439
440 /* Get the variable entry. */
441 var = var_get(vars, name);
442 if (!var)
443 return 0;
444
445 /* Copy sample. */
446 smp->type = var->data.type;
447 smp->flags = SMP_F_CONST;
448 memcpy(&smp->data, &var->data.data, sizeof(smp->data));
449 return 1;
450}
451
Willy Tarreaue44136f2015-06-23 15:17:33 +0200452/* Returns 0 if we need to come back later to complete the sample's retrieval,
453 * otherwise 1. For now all processing is considered final so we only return 1.
454 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200455static inline int action_store(struct sample_expr *expr, const char *name,
456 enum vars_scope scope, struct proxy *px,
457 struct stream *s, int sens)
458{
459 struct sample smp;
460
461 /* Process the expression. */
462 memset(&smp, 0, sizeof(smp));
463 if (!sample_process(px, s->sess, s, sens|SMP_OPT_FINAL, expr, &smp))
Willy Tarreaue44136f2015-06-23 15:17:33 +0200464 return 1;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200465
466 /* Store the sample, and ignore errors. */
467 sample_store_stream(name, scope, s, &smp);
468 return 1;
469}
470
471/* Returns 0 if miss data, else returns 1. */
472static int action_tcp_req_store(struct tcp_rule *rule, struct proxy *px, struct stream *s)
473{
474 struct sample_expr *expr = rule->act_prm.data[0];
475 const char *name = rule->act_prm.data[1];
476 int scope = (long)rule->act_prm.data[2];
477
478 return action_store(expr, name, scope, px, s, SMP_OPT_DIR_REQ);
479}
480
481/* Returns 0 if miss data, else returns 1. */
482static int action_tcp_res_store(struct tcp_rule *rule, struct proxy *px, struct stream *s)
483{
484 struct sample_expr *expr = rule->act_prm.data[0];
485 const char *name = rule->act_prm.data[1];
486 int scope = (long)rule->act_prm.data[2];
487
488 return action_store(expr, name, scope, px, s, SMP_OPT_DIR_RES);
489}
490
491/* Returns 0 if miss data, else returns 1. */
492static int action_http_req_store(struct http_req_rule *rule, struct proxy *px, struct stream *s)
493{
494 struct sample_expr *expr = rule->arg.act.p[0];
495 const char *name = rule->arg.act.p[1];
496 int scope = (long)rule->arg.act.p[2];
497
498 return action_store(expr, name, scope, px, s, SMP_OPT_DIR_REQ);
499}
500
501/* Returns 0 if miss data, else returns 1. */
502static int action_http_res_store(struct http_res_rule *rule, struct proxy *px, struct stream *s)
503{
504 struct sample_expr *expr = rule->arg.act.p[0];
505 const char *name = rule->arg.act.p[1];
506 int scope = (long)rule->arg.act.p[2];
507
508 return action_store(expr, name, scope, px, s, SMP_OPT_DIR_RES);
509}
510
511/* This two function checks the variable name and replace the
512 * configuration string name by the global string name. its
513 * the same string, but the global pointer can be easy to
514 * compare.
515 *
516 * The first function checks a sample-fetch and the second
517 * checks a converter.
518 */
519static int smp_check_var(struct arg *args, char **err)
520{
521 return vars_check_arg(&args[0], err);
522}
523
524static int conv_check_var(struct arg *args, struct sample_conv *conv,
525 const char *file, int line, char **err_msg)
526{
527 return vars_check_arg(&args[0], err_msg);
528}
529
530/* This function is a common parser for using variables. It understands
531 * the format:
532 *
533 * set-var(<variable-name>) <expression>
534 *
535 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
536 * it returns 1 and the variable <expr> is filled with the pointer to the
537 * expression to execute.
538 */
539static int parse_vars(const char **args, int *arg, struct proxy *px,
540 struct sample_expr **expr, char **name,
541 enum vars_scope *scope, char **err)
542{
543 const char *var_name = args[*arg-1];
544 int var_len;
545
546 var_name += strlen("set-var");
547 if (*var_name != '(') {
548 memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)'", args[*arg-1]);
549 return 0;
550 }
551 var_name++; /* jump the '(' */
552 var_len = strlen(var_name);
553 var_len--; /* remove the ')' */
554 if (var_name[var_len] != ')') {
555 memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)'", args[*arg-1]);
556 return 0;
557 }
558
559 *name = register_name(var_name, var_len, scope, err);
560 if (!*name)
561 return 0;
562
563 *expr = sample_parse_expr((char **)args, arg, px->conf.args.file, px->conf.args.line,
564 err, &px->conf.args);
565 if (!*expr)
566 return 0;
567
568 return 1;
569}
570
571static int parse_tcp_req_store(const char **args, int *arg, struct proxy *px,
572 struct tcp_rule *rule, char **err)
573{
574 struct sample_expr *expr;
575 int cur_arg = *arg;
576 char *name;
577 enum vars_scope scope;
578
579 if (!parse_vars(args, arg, px, &expr, &name, &scope, err))
580 return 0;
581
582 if (!(expr->fetch->val & SMP_VAL_FE_REQ_CNT)) {
583 memprintf(err,
584 "fetch method '%s' extracts information from '%s', none of which is available here",
585 args[cur_arg-1], sample_src_names(expr->fetch->use));
586 free(expr);
587 return 0;
588 }
589
590 rule->action = TCP_ACT_CUSTOM_CONT;
591 rule->action_ptr = action_tcp_req_store;
592 rule->act_prm.data[0] = expr;
593 rule->act_prm.data[1] = name;
594 rule->act_prm.data[2] = (void *)(long)scope;
595
596 return 1;
597}
598
599static int parse_tcp_res_store(const char **args, int *arg, struct proxy *px,
600 struct tcp_rule *rule, char **err)
601{
602 struct sample_expr *expr;
603 int cur_arg = *arg;
604 char *name;
605 enum vars_scope scope;
606
607 if (!parse_vars(args, arg, px, &expr, &name, &scope, err))
608 return 0;
609
610 if (!(expr->fetch->val & SMP_VAL_BE_RES_CNT)) {
611 memprintf(err,
612 "fetch method '%s' extracts information from '%s', none of which is available here",
613 args[cur_arg-1], sample_src_names(expr->fetch->use));
614 free(expr);
615 return 0;
616 }
617
618 rule->action = TCP_ACT_CUSTOM_CONT;
619 rule->action_ptr = action_tcp_res_store;
620 rule->act_prm.data[0] = expr;
621 rule->act_prm.data[1] = name;
622 rule->act_prm.data[2] = (void *)(long)scope;
623
624 return 1;
625}
626
627static int parse_http_req_store(const char **args, int *arg, struct proxy *px,
628 struct http_req_rule *rule, char **err)
629{
630 struct sample_expr *expr;
631 int cur_arg = *arg;
632 char *name;
633 enum vars_scope scope;
634
635 if (!parse_vars(args, arg, px, &expr, &name, &scope, err))
636 return -1;
637
638 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
639 memprintf(err,
640 "fetch method '%s' extracts information from '%s', none of which is available here",
641 args[cur_arg-1], sample_src_names(expr->fetch->use));
642 free(expr);
643 return -1;
644 }
645
646 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
647 rule->action_ptr = action_http_req_store;
648 rule->arg.act.p[0] = expr;
649 rule->arg.act.p[1] = name;
650 rule->arg.act.p[2] = (void *)(long)scope;
651
652 return 0;
653}
654
655static int parse_http_res_store(const char **args, int *arg, struct proxy *px,
656 struct http_res_rule *rule, char **err)
657{
658 struct sample_expr *expr;
659 int cur_arg = *arg;
660 char *name;
661 enum vars_scope scope;
662
663 if (!parse_vars(args, arg, px, &expr, &name, &scope, err))
664 return -1;
665
666 if (!(expr->fetch->val & SMP_VAL_BE_HRS_HDR)) {
667 memprintf(err,
668 "fetch method '%s' extracts information from '%s', none of which is available here",
669 args[cur_arg-1], sample_src_names(expr->fetch->use));
670 free(expr);
671 return -1;
672 }
673
674 rule->action = HTTP_RES_ACT_CUSTOM_CONT;
675 rule->action_ptr = action_http_res_store;
676 rule->arg.act.p[0] = expr;
677 rule->arg.act.p[1] = name;
678 rule->arg.act.p[2] = (void *)(long)scope;
679
680 return 0;
681}
682
683static int vars_max_size(char **args, int section_type, struct proxy *curpx,
684 struct proxy *defpx, const char *file, int line,
685 char **err, unsigned int *limit)
686{
687 char *error;
688
689 *limit = strtol(args[1], &error, 10);
690 if (*error != 0) {
691 memprintf(err, "%s: '%s' is an invalid size", args[0], args[1]);
692 return -1;
693 }
694 return 0;
695}
696
697static int vars_max_size_global(char **args, int section_type, struct proxy *curpx,
698 struct proxy *defpx, const char *file, int line,
699 char **err)
700{
701 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_global_limit);
702}
703
704static int vars_max_size_sess(char **args, int section_type, struct proxy *curpx,
705 struct proxy *defpx, const char *file, int line,
706 char **err)
707{
708 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_sess_limit);
709}
710
711static int vars_max_size_txn(char **args, int section_type, struct proxy *curpx,
712 struct proxy *defpx, const char *file, int line,
713 char **err)
714{
715 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_txn_limit);
716}
717
718static int vars_max_size_reqres(char **args, int section_type, struct proxy *curpx,
719 struct proxy *defpx, const char *file, int line,
720 char **err)
721{
722 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_reqres_limit);
723}
724
725static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
726
727 { "var", smp_fetch_var, ARG1(1,STR), smp_check_var, SMP_T_STR, SMP_USE_HTTP_ANY },
728 { /* END */ },
729}};
730
731static struct sample_conv_kw_list sample_conv_kws = {ILH, {
732 { "set-var", smp_conv_store, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY },
733 { /* END */ },
734}};
735
736static struct tcp_action_kw_list tcp_req_kws = {"vars", { }, {
737 { "set-var", parse_tcp_req_store, 1 },
738 { /* END */ }
739}};
740
741static struct tcp_action_kw_list tcp_res_kws = {"vars", { }, {
742 { "set-var", parse_tcp_res_store, 1 },
743 { /* END */ }
744}};
745
746static struct http_req_action_kw_list http_req_kws = {"vars", { }, {
747 { "set-var", parse_http_req_store, 1 },
748 { /* END */ }
749}};
750
751static struct http_res_action_kw_list http_res_kws = {"vars", { }, {
752 { "set-var", parse_http_res_store, 1 },
753 { /* END */ }
754}};
755
756static struct cfg_kw_list cfg_kws = {{ },{
757 { CFG_GLOBAL, "tune.vars.global-max-size", vars_max_size_global },
758 { CFG_GLOBAL, "tune.vars.sess-max-size", vars_max_size_sess },
759 { CFG_GLOBAL, "tune.vars.txn-max-size", vars_max_size_txn },
760 { CFG_GLOBAL, "tune.vars.reqres-max-size", vars_max_size_reqres },
761 { /* END */ }
762}};
763
764__attribute__((constructor))
765static void __http_protocol_init(void)
766{
767 var_pool = create_pool("vars", sizeof(struct var), MEM_F_SHARED);
768
769 sample_register_fetches(&sample_fetch_keywords);
770 sample_register_convs(&sample_conv_kws);
771 tcp_req_cont_keywords_register(&tcp_req_kws);
772 tcp_res_cont_keywords_register(&tcp_res_kws);
773 http_req_keywords_register(&http_req_kws);
774 http_res_keywords_register(&http_res_kws);
775 cfg_register_keywords(&cfg_kws);
776}