blob: fa521f35e1879a6ab17c568467be0e0207522bb2 [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
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200452/* Returns 0 if miss data, else returns 1. */
453static inline int action_store(struct sample_expr *expr, const char *name,
454 enum vars_scope scope, struct proxy *px,
455 struct stream *s, int sens)
456{
457 struct sample smp;
458
459 /* Process the expression. */
460 memset(&smp, 0, sizeof(smp));
461 if (!sample_process(px, s->sess, s, sens|SMP_OPT_FINAL, expr, &smp))
462 return 0;
463
464 /* Store the sample, and ignore errors. */
465 sample_store_stream(name, scope, s, &smp);
466 return 1;
467}
468
469/* Returns 0 if miss data, else returns 1. */
470static int action_tcp_req_store(struct tcp_rule *rule, struct proxy *px, struct stream *s)
471{
472 struct sample_expr *expr = rule->act_prm.data[0];
473 const char *name = rule->act_prm.data[1];
474 int scope = (long)rule->act_prm.data[2];
475
476 return action_store(expr, name, scope, px, s, SMP_OPT_DIR_REQ);
477}
478
479/* Returns 0 if miss data, else returns 1. */
480static int action_tcp_res_store(struct tcp_rule *rule, struct proxy *px, struct stream *s)
481{
482 struct sample_expr *expr = rule->act_prm.data[0];
483 const char *name = rule->act_prm.data[1];
484 int scope = (long)rule->act_prm.data[2];
485
486 return action_store(expr, name, scope, px, s, SMP_OPT_DIR_RES);
487}
488
489/* Returns 0 if miss data, else returns 1. */
490static int action_http_req_store(struct http_req_rule *rule, struct proxy *px, struct stream *s)
491{
492 struct sample_expr *expr = rule->arg.act.p[0];
493 const char *name = rule->arg.act.p[1];
494 int scope = (long)rule->arg.act.p[2];
495
496 return action_store(expr, name, scope, px, s, SMP_OPT_DIR_REQ);
497}
498
499/* Returns 0 if miss data, else returns 1. */
500static int action_http_res_store(struct http_res_rule *rule, struct proxy *px, struct stream *s)
501{
502 struct sample_expr *expr = rule->arg.act.p[0];
503 const char *name = rule->arg.act.p[1];
504 int scope = (long)rule->arg.act.p[2];
505
506 return action_store(expr, name, scope, px, s, SMP_OPT_DIR_RES);
507}
508
509/* This two function checks the variable name and replace the
510 * configuration string name by the global string name. its
511 * the same string, but the global pointer can be easy to
512 * compare.
513 *
514 * The first function checks a sample-fetch and the second
515 * checks a converter.
516 */
517static int smp_check_var(struct arg *args, char **err)
518{
519 return vars_check_arg(&args[0], err);
520}
521
522static int conv_check_var(struct arg *args, struct sample_conv *conv,
523 const char *file, int line, char **err_msg)
524{
525 return vars_check_arg(&args[0], err_msg);
526}
527
528/* This function is a common parser for using variables. It understands
529 * the format:
530 *
531 * set-var(<variable-name>) <expression>
532 *
533 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
534 * it returns 1 and the variable <expr> is filled with the pointer to the
535 * expression to execute.
536 */
537static int parse_vars(const char **args, int *arg, struct proxy *px,
538 struct sample_expr **expr, char **name,
539 enum vars_scope *scope, char **err)
540{
541 const char *var_name = args[*arg-1];
542 int var_len;
543
544 var_name += strlen("set-var");
545 if (*var_name != '(') {
546 memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)'", args[*arg-1]);
547 return 0;
548 }
549 var_name++; /* jump the '(' */
550 var_len = strlen(var_name);
551 var_len--; /* remove the ')' */
552 if (var_name[var_len] != ')') {
553 memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)'", args[*arg-1]);
554 return 0;
555 }
556
557 *name = register_name(var_name, var_len, scope, err);
558 if (!*name)
559 return 0;
560
561 *expr = sample_parse_expr((char **)args, arg, px->conf.args.file, px->conf.args.line,
562 err, &px->conf.args);
563 if (!*expr)
564 return 0;
565
566 return 1;
567}
568
569static int parse_tcp_req_store(const char **args, int *arg, struct proxy *px,
570 struct tcp_rule *rule, char **err)
571{
572 struct sample_expr *expr;
573 int cur_arg = *arg;
574 char *name;
575 enum vars_scope scope;
576
577 if (!parse_vars(args, arg, px, &expr, &name, &scope, err))
578 return 0;
579
580 if (!(expr->fetch->val & SMP_VAL_FE_REQ_CNT)) {
581 memprintf(err,
582 "fetch method '%s' extracts information from '%s', none of which is available here",
583 args[cur_arg-1], sample_src_names(expr->fetch->use));
584 free(expr);
585 return 0;
586 }
587
588 rule->action = TCP_ACT_CUSTOM_CONT;
589 rule->action_ptr = action_tcp_req_store;
590 rule->act_prm.data[0] = expr;
591 rule->act_prm.data[1] = name;
592 rule->act_prm.data[2] = (void *)(long)scope;
593
594 return 1;
595}
596
597static int parse_tcp_res_store(const char **args, int *arg, struct proxy *px,
598 struct tcp_rule *rule, char **err)
599{
600 struct sample_expr *expr;
601 int cur_arg = *arg;
602 char *name;
603 enum vars_scope scope;
604
605 if (!parse_vars(args, arg, px, &expr, &name, &scope, err))
606 return 0;
607
608 if (!(expr->fetch->val & SMP_VAL_BE_RES_CNT)) {
609 memprintf(err,
610 "fetch method '%s' extracts information from '%s', none of which is available here",
611 args[cur_arg-1], sample_src_names(expr->fetch->use));
612 free(expr);
613 return 0;
614 }
615
616 rule->action = TCP_ACT_CUSTOM_CONT;
617 rule->action_ptr = action_tcp_res_store;
618 rule->act_prm.data[0] = expr;
619 rule->act_prm.data[1] = name;
620 rule->act_prm.data[2] = (void *)(long)scope;
621
622 return 1;
623}
624
625static int parse_http_req_store(const char **args, int *arg, struct proxy *px,
626 struct http_req_rule *rule, char **err)
627{
628 struct sample_expr *expr;
629 int cur_arg = *arg;
630 char *name;
631 enum vars_scope scope;
632
633 if (!parse_vars(args, arg, px, &expr, &name, &scope, err))
634 return -1;
635
636 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
637 memprintf(err,
638 "fetch method '%s' extracts information from '%s', none of which is available here",
639 args[cur_arg-1], sample_src_names(expr->fetch->use));
640 free(expr);
641 return -1;
642 }
643
644 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
645 rule->action_ptr = action_http_req_store;
646 rule->arg.act.p[0] = expr;
647 rule->arg.act.p[1] = name;
648 rule->arg.act.p[2] = (void *)(long)scope;
649
650 return 0;
651}
652
653static int parse_http_res_store(const char **args, int *arg, struct proxy *px,
654 struct http_res_rule *rule, char **err)
655{
656 struct sample_expr *expr;
657 int cur_arg = *arg;
658 char *name;
659 enum vars_scope scope;
660
661 if (!parse_vars(args, arg, px, &expr, &name, &scope, err))
662 return -1;
663
664 if (!(expr->fetch->val & SMP_VAL_BE_HRS_HDR)) {
665 memprintf(err,
666 "fetch method '%s' extracts information from '%s', none of which is available here",
667 args[cur_arg-1], sample_src_names(expr->fetch->use));
668 free(expr);
669 return -1;
670 }
671
672 rule->action = HTTP_RES_ACT_CUSTOM_CONT;
673 rule->action_ptr = action_http_res_store;
674 rule->arg.act.p[0] = expr;
675 rule->arg.act.p[1] = name;
676 rule->arg.act.p[2] = (void *)(long)scope;
677
678 return 0;
679}
680
681static int vars_max_size(char **args, int section_type, struct proxy *curpx,
682 struct proxy *defpx, const char *file, int line,
683 char **err, unsigned int *limit)
684{
685 char *error;
686
687 *limit = strtol(args[1], &error, 10);
688 if (*error != 0) {
689 memprintf(err, "%s: '%s' is an invalid size", args[0], args[1]);
690 return -1;
691 }
692 return 0;
693}
694
695static int vars_max_size_global(char **args, int section_type, struct proxy *curpx,
696 struct proxy *defpx, const char *file, int line,
697 char **err)
698{
699 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_global_limit);
700}
701
702static int vars_max_size_sess(char **args, int section_type, struct proxy *curpx,
703 struct proxy *defpx, const char *file, int line,
704 char **err)
705{
706 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_sess_limit);
707}
708
709static int vars_max_size_txn(char **args, int section_type, struct proxy *curpx,
710 struct proxy *defpx, const char *file, int line,
711 char **err)
712{
713 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_txn_limit);
714}
715
716static int vars_max_size_reqres(char **args, int section_type, struct proxy *curpx,
717 struct proxy *defpx, const char *file, int line,
718 char **err)
719{
720 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_reqres_limit);
721}
722
723static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
724
725 { "var", smp_fetch_var, ARG1(1,STR), smp_check_var, SMP_T_STR, SMP_USE_HTTP_ANY },
726 { /* END */ },
727}};
728
729static struct sample_conv_kw_list sample_conv_kws = {ILH, {
730 { "set-var", smp_conv_store, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY },
731 { /* END */ },
732}};
733
734static struct tcp_action_kw_list tcp_req_kws = {"vars", { }, {
735 { "set-var", parse_tcp_req_store, 1 },
736 { /* END */ }
737}};
738
739static struct tcp_action_kw_list tcp_res_kws = {"vars", { }, {
740 { "set-var", parse_tcp_res_store, 1 },
741 { /* END */ }
742}};
743
744static struct http_req_action_kw_list http_req_kws = {"vars", { }, {
745 { "set-var", parse_http_req_store, 1 },
746 { /* END */ }
747}};
748
749static struct http_res_action_kw_list http_res_kws = {"vars", { }, {
750 { "set-var", parse_http_res_store, 1 },
751 { /* END */ }
752}};
753
754static struct cfg_kw_list cfg_kws = {{ },{
755 { CFG_GLOBAL, "tune.vars.global-max-size", vars_max_size_global },
756 { CFG_GLOBAL, "tune.vars.sess-max-size", vars_max_size_sess },
757 { CFG_GLOBAL, "tune.vars.txn-max-size", vars_max_size_txn },
758 { CFG_GLOBAL, "tune.vars.reqres-max-size", vars_max_size_reqres },
759 { /* END */ }
760}};
761
762__attribute__((constructor))
763static void __http_protocol_init(void)
764{
765 var_pool = create_pool("vars", sizeof(struct var), MEM_F_SHARED);
766
767 sample_register_fetches(&sample_fetch_keywords);
768 sample_register_convs(&sample_conv_kws);
769 tcp_req_cont_keywords_register(&tcp_req_kws);
770 tcp_res_cont_keywords_register(&tcp_res_kws);
771 http_req_keywords_register(&http_req_kws);
772 http_res_keywords_register(&http_res_kws);
773 cfg_register_keywords(&cfg_kws);
774}