blob: 0653dd59e83d0f308fa2254fcc82bb67080e6c74 [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP actions
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
19#include <common/chunk.h>
20#include <common/compat.h>
21#include <common/config.h>
22#include <common/debug.h>
23#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010024#include <common/initcall.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020025#include <common/memory.h>
26#include <common/standard.h>
27#include <common/version.h>
28
29#include <types/capture.h>
30#include <types/global.h>
31
32#include <proto/acl.h>
33#include <proto/arg.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020034#include <proto/http_rules.h>
Willy Tarreau33810222019-06-12 17:44:02 +020035#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020036#include <proto/log.h>
37#include <proto/proto_http.h>
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +010038#include <proto/stream_interface.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020039
40
41/* This function executes one of the set-{method,path,query,uri} actions. It
42 * builds a string in the trash from the specified format string. It finds
43 * the action to be performed in <http.action>, previously filled by function
44 * parse_set_req_line(). The replacement action is excuted by the function
45 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
46 * occurs the action is canceled, but the rule processing continue.
47 */
48static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
49 struct session *sess, struct stream *s, int flags)
50{
51 struct buffer *replace;
52 enum act_return ret = ACT_RET_ERR;
53
54 replace = alloc_trash_chunk();
55 if (!replace)
56 goto leave;
57
58 /* If we have to create a query string, prepare a '?'. */
59 if (rule->arg.http.action == 2)
60 replace->area[replace->data++] = '?';
61 replace->data += build_logline(s, replace->area + replace->data,
62 replace->size - replace->data,
63 &rule->arg.http.logfmt);
64
65 http_replace_req_line(rule->arg.http.action, replace->area,
66 replace->data, px, s);
67
68 ret = ACT_RET_CONT;
69
70leave:
71 free_trash_chunk(replace);
72 return ret;
73}
74
75/* parse an http-request action among :
76 * set-method
77 * set-path
78 * set-query
79 * set-uri
80 *
81 * All of them accept a single argument of type string representing a log-format.
82 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
83 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
84 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
85 */
86static enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
87 struct act_rule *rule, char **err)
88{
89 int cur_arg = *orig_arg;
90
91 rule->action = ACT_CUSTOM;
92
93 switch (args[0][4]) {
94 case 'm' :
95 rule->arg.http.action = 0;
96 rule->action_ptr = http_action_set_req_line;
97 break;
98 case 'p' :
99 rule->arg.http.action = 1;
100 rule->action_ptr = http_action_set_req_line;
101 break;
102 case 'q' :
103 rule->arg.http.action = 2;
104 rule->action_ptr = http_action_set_req_line;
105 break;
106 case 'u' :
107 rule->arg.http.action = 3;
108 rule->action_ptr = http_action_set_req_line;
109 break;
110 default:
111 memprintf(err, "internal error: unhandled action '%s'", args[0]);
112 return ACT_RET_PRS_ERR;
113 }
114
115 if (!*args[cur_arg] ||
116 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
117 memprintf(err, "expects exactly 1 argument <format>");
118 return ACT_RET_PRS_ERR;
119 }
120
121 LIST_INIT(&rule->arg.http.logfmt);
122 px->conf.args.ctx = ARGC_HRQ;
123 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
124 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
125 return ACT_RET_PRS_ERR;
126 }
127
128 (*orig_arg)++;
129 return ACT_RET_PRS_OK;
130}
131
Willy Tarreau33810222019-06-12 17:44:02 +0200132/* This function executes a replace-uri action. It finds its arguments in
133 * <rule>.arg.act.p[]. It builds a string in the trash from the format string
134 * previously filled by function parse_replace_uri() and will execute the regex
135 * in p[1] to replace the URI. It uses the format string present in act.p[2..3].
136 * It always returns ACT_RET_CONT. If an error occurs, the action is canceled,
137 * but the rule processing continues.
138 */
139static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
140 struct session *sess, struct stream *s, int flags)
141{
142 enum act_return ret = ACT_RET_ERR;
143 struct buffer *replace, *output;
144 struct ist uri;
145 int len;
146
147 replace = alloc_trash_chunk();
148 output = alloc_trash_chunk();
149 if (!replace || !output)
150 goto leave;
151
152 if (IS_HTX_STRM(s))
153 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
154 else
155 uri = ist2(ci_head(&s->req) + s->txn->req.sl.rq.u, s->txn->req.sl.rq.u_l);
156
157 if (!regex_exec_match2(rule->arg.act.p[1], uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
158 goto leave;
159
160 replace->data = build_logline(s, replace->area, replace->size, (struct list *)&rule->arg.act.p[2]);
161
162 /* note: uri.ptr doesn't need to be zero-terminated because it will
163 * only be used to pick pmatch references.
164 */
165 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
166 if (len == -1)
167 goto leave;
168
169 /* 3 is the set-uri action */
170 http_replace_req_line(3, output->area, len, px, s);
171
172 ret = ACT_RET_CONT;
173
174leave:
175 free_trash_chunk(output);
176 free_trash_chunk(replace);
177 return ret;
178}
179
180/* parse a "replace-uri" http-request action.
181 * This action takes 2 arguments (a regex and a replacement format string).
182 * The resulting rule makes use of arg->act.p[0] to store the action (0 for now),
183 * p[1] to store the compiled regex, and arg->act.p[2..3] to store the log-format
184 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
185 */
186static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
187 struct act_rule *rule, char **err)
188{
189 int cur_arg = *orig_arg;
190 char *error = NULL;
191
192 rule->action = ACT_CUSTOM;
193 rule->arg.act.p[0] = (void *)0; // replace-uri
194 rule->action_ptr = http_action_replace_uri;
195
196 if (!*args[cur_arg] || !*args[cur_arg+1] ||
197 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
198 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
199 return ACT_RET_PRS_ERR;
200 }
201
202 if (!(rule->arg.act.p[1] = regex_comp(args[cur_arg], 1, 1, &error))) {
203 memprintf(err, "failed to parse the regex : %s", error);
204 free(error);
205 return ACT_RET_PRS_ERR;
206 }
207
208 LIST_INIT((struct list *)&rule->arg.act.p[2]);
209 px->conf.args.ctx = ARGC_HRQ;
210 if (!parse_logformat_string(args[cur_arg + 1], px, (struct list *)&rule->arg.act.p[2], LOG_OPT_HTTP,
211 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
212 return ACT_RET_PRS_ERR;
213 }
214
215 (*orig_arg) += 2;
216 return ACT_RET_PRS_OK;
217}
218
Willy Tarreau79e57332018-10-02 16:01:16 +0200219/* This function is just a compliant action wrapper for "set-status". */
220static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
221 struct session *sess, struct stream *s, int flags)
222{
223 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
224 return ACT_RET_CONT;
225}
226
227/* parse set-status action:
228 * This action accepts a single argument of type int representing
229 * an http status code. It returns ACT_RET_PRS_OK on success,
230 * ACT_RET_PRS_ERR on error.
231 */
232static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
233 struct act_rule *rule, char **err)
234{
235 char *error;
236
237 rule->action = ACT_CUSTOM;
238 rule->action_ptr = action_http_set_status;
239
240 /* Check if an argument is available */
241 if (!*args[*orig_arg]) {
242 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
243 return ACT_RET_PRS_ERR;
244 }
245
246 /* convert status code as integer */
247 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
248 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
249 memprintf(err, "expects an integer status code between 100 and 999");
250 return ACT_RET_PRS_ERR;
251 }
252
253 (*orig_arg)++;
254
255 /* set custom reason string */
256 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
257 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
258 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
259 (*orig_arg)++;
260 rule->arg.status.reason = strdup(args[*orig_arg]);
261 (*orig_arg)++;
262 }
263
264 return ACT_RET_PRS_OK;
265}
266
267/* This function executes the "reject" HTTP action. It clears the request and
268 * response buffer without sending any response. It can be useful as an HTTP
269 * alternative to the silent-drop action to defend against DoS attacks, and may
270 * also be used with HTTP/2 to close a connection instead of just a stream.
271 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet8f1aa772019-07-04 11:27:15 +0200272 * flags will indicate "PR". It always returns ACT_RET_DONE.
Willy Tarreau79e57332018-10-02 16:01:16 +0200273 */
274static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
275 struct session *sess, struct stream *s, int flags)
276{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100277 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200278 channel_abort(&s->req);
279 channel_abort(&s->res);
280 s->req.analysers = 0;
281 s->res.analysers = 0;
282
Olivier Houcharda798bf52019-03-08 18:52:00 +0100283 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
284 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200285 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100286 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200287
288 if (!(s->flags & SF_ERR_MASK))
289 s->flags |= SF_ERR_PRXCOND;
290 if (!(s->flags & SF_FINST_MASK))
291 s->flags |= SF_FINST_R;
292
Christopher Faulet8f1aa772019-07-04 11:27:15 +0200293 return ACT_RET_DONE;
Willy Tarreau79e57332018-10-02 16:01:16 +0200294}
295
296/* parse the "reject" action:
297 * This action takes no argument and returns ACT_RET_PRS_OK on success,
298 * ACT_RET_PRS_ERR on error.
299 */
300static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
301 struct act_rule *rule, char **err)
302{
303 rule->action = ACT_CUSTOM;
304 rule->action_ptr = http_action_reject;
305 return ACT_RET_PRS_OK;
306}
307
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200308/* This function executes the "disable-l7-retry" HTTP action.
309 * It disables L7 retries (all retry except for a connection failure). This
310 * can be useful for example to avoid retrying on POST requests.
311 * It just removes the L7 retry flag on the stream_interface, and always
312 * return ACT_RET_CONT;
313 */
314static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
315 struct session *sess, struct stream *s, int flags)
316{
317 struct stream_interface *si = &s->si[1];
318
319 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
320 * let's be future-proof and remove it anyway.
321 */
322 si->flags &= ~SI_FL_L7_RETRY;
323 si->flags |= SI_FL_D_L7_RETRY;
324 return ACT_RET_CONT;
325}
326
327/* parse the "disable-l7-retry" action:
328 * This action takes no argument and returns ACT_RET_PRS_OK on success,
329 * ACT_RET_PRS_ERR on error.
330 */
331static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
332 int *orig_args, struct proxy *px,
333 struct act_rule *rule, char **err)
334{
335 rule->action = ACT_CUSTOM;
336 rule->action_ptr = http_req_disable_l7_retry;
337 return ACT_RET_PRS_OK;
338}
339
Willy Tarreau79e57332018-10-02 16:01:16 +0200340/* This function executes the "capture" action. It executes a fetch expression,
341 * turns the result into a string and puts it in a capture slot. It always
342 * returns 1. If an error occurs the action is cancelled, but the rule
343 * processing continues.
344 */
345static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
346 struct session *sess, struct stream *s, int flags)
347{
348 struct sample *key;
349 struct cap_hdr *h = rule->arg.cap.hdr;
350 char **cap = s->req_cap;
351 int len;
352
353 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
354 if (!key)
355 return ACT_RET_CONT;
356
357 if (cap[h->index] == NULL)
358 cap[h->index] = pool_alloc(h->pool);
359
360 if (cap[h->index] == NULL) /* no more capture memory */
361 return ACT_RET_CONT;
362
363 len = key->data.u.str.data;
364 if (len > h->len)
365 len = h->len;
366
367 memcpy(cap[h->index], key->data.u.str.area, len);
368 cap[h->index][len] = 0;
369 return ACT_RET_CONT;
370}
371
372/* This function executes the "capture" action and store the result in a
373 * capture slot if exists. It executes a fetch expression, turns the result
374 * into a string and puts it in a capture slot. It always returns 1. If an
375 * error occurs the action is cancelled, but the rule processing continues.
376 */
377static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
378 struct session *sess, struct stream *s, int flags)
379{
380 struct sample *key;
381 struct cap_hdr *h;
382 char **cap = s->req_cap;
383 struct proxy *fe = strm_fe(s);
384 int len;
385 int i;
386
387 /* Look for the original configuration. */
388 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
389 h != NULL && i != rule->arg.capid.idx ;
390 i--, h = h->next);
391 if (!h)
392 return ACT_RET_CONT;
393
394 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
395 if (!key)
396 return ACT_RET_CONT;
397
398 if (cap[h->index] == NULL)
399 cap[h->index] = pool_alloc(h->pool);
400
401 if (cap[h->index] == NULL) /* no more capture memory */
402 return ACT_RET_CONT;
403
404 len = key->data.u.str.data;
405 if (len > h->len)
406 len = h->len;
407
408 memcpy(cap[h->index], key->data.u.str.area, len);
409 cap[h->index][len] = 0;
410 return ACT_RET_CONT;
411}
412
413/* Check an "http-request capture" action.
414 *
415 * The function returns 1 in success case, otherwise, it returns 0 and err is
416 * filled.
417 */
418static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
419{
420 if (rule->action_ptr != http_action_req_capture_by_id)
421 return 1;
422
423 if (rule->arg.capid.idx >= px->nb_req_cap) {
424 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
425 rule->arg.capid.idx);
426 return 0;
427 }
428
429 return 1;
430}
431
432/* parse an "http-request capture" action. It takes a single argument which is
433 * a sample fetch expression. It stores the expression into arg->act.p[0] and
434 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
435 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
436 */
437static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
438 struct act_rule *rule, char **err)
439{
440 struct sample_expr *expr;
441 struct cap_hdr *hdr;
442 int cur_arg;
443 int len = 0;
444
445 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
446 if (strcmp(args[cur_arg], "if") == 0 ||
447 strcmp(args[cur_arg], "unless") == 0)
448 break;
449
450 if (cur_arg < *orig_arg + 3) {
451 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
452 return ACT_RET_PRS_ERR;
453 }
454
455 cur_arg = *orig_arg;
456 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
457 if (!expr)
458 return ACT_RET_PRS_ERR;
459
460 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
461 memprintf(err,
462 "fetch method '%s' extracts information from '%s', none of which is available here",
463 args[cur_arg-1], sample_src_names(expr->fetch->use));
464 free(expr);
465 return ACT_RET_PRS_ERR;
466 }
467
468 if (!args[cur_arg] || !*args[cur_arg]) {
469 memprintf(err, "expects 'len or 'id'");
470 free(expr);
471 return ACT_RET_PRS_ERR;
472 }
473
474 if (strcmp(args[cur_arg], "len") == 0) {
475 cur_arg++;
476
477 if (!(px->cap & PR_CAP_FE)) {
478 memprintf(err, "proxy '%s' has no frontend capability", px->id);
479 return ACT_RET_PRS_ERR;
480 }
481
482 px->conf.args.ctx = ARGC_CAP;
483
484 if (!args[cur_arg]) {
485 memprintf(err, "missing length value");
486 free(expr);
487 return ACT_RET_PRS_ERR;
488 }
489 /* we copy the table name for now, it will be resolved later */
490 len = atoi(args[cur_arg]);
491 if (len <= 0) {
492 memprintf(err, "length must be > 0");
493 free(expr);
494 return ACT_RET_PRS_ERR;
495 }
496 cur_arg++;
497
Willy Tarreau79e57332018-10-02 16:01:16 +0200498 hdr = calloc(1, sizeof(*hdr));
499 hdr->next = px->req_cap;
500 hdr->name = NULL; /* not a header capture */
501 hdr->namelen = 0;
502 hdr->len = len;
503 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
504 hdr->index = px->nb_req_cap++;
505
506 px->req_cap = hdr;
507 px->to_log |= LW_REQHDR;
508
509 rule->action = ACT_CUSTOM;
510 rule->action_ptr = http_action_req_capture;
511 rule->arg.cap.expr = expr;
512 rule->arg.cap.hdr = hdr;
513 }
514
515 else if (strcmp(args[cur_arg], "id") == 0) {
516 int id;
517 char *error;
518
519 cur_arg++;
520
521 if (!args[cur_arg]) {
522 memprintf(err, "missing id value");
523 free(expr);
524 return ACT_RET_PRS_ERR;
525 }
526
527 id = strtol(args[cur_arg], &error, 10);
528 if (*error != '\0') {
529 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
530 free(expr);
531 return ACT_RET_PRS_ERR;
532 }
533 cur_arg++;
534
535 px->conf.args.ctx = ARGC_CAP;
536
537 rule->action = ACT_CUSTOM;
538 rule->action_ptr = http_action_req_capture_by_id;
539 rule->check_ptr = check_http_req_capture;
540 rule->arg.capid.expr = expr;
541 rule->arg.capid.idx = id;
542 }
543
544 else {
545 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
546 free(expr);
547 return ACT_RET_PRS_ERR;
548 }
549
550 *orig_arg = cur_arg;
551 return ACT_RET_PRS_OK;
552}
553
554/* This function executes the "capture" action and store the result in a
555 * capture slot if exists. It executes a fetch expression, turns the result
556 * into a string and puts it in a capture slot. It always returns 1. If an
557 * error occurs the action is cancelled, but the rule processing continues.
558 */
559static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
560 struct session *sess, struct stream *s, int flags)
561{
562 struct sample *key;
563 struct cap_hdr *h;
564 char **cap = s->res_cap;
565 struct proxy *fe = strm_fe(s);
566 int len;
567 int i;
568
569 /* Look for the original configuration. */
570 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
571 h != NULL && i != rule->arg.capid.idx ;
572 i--, h = h->next);
573 if (!h)
574 return ACT_RET_CONT;
575
576 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
577 if (!key)
578 return ACT_RET_CONT;
579
580 if (cap[h->index] == NULL)
581 cap[h->index] = pool_alloc(h->pool);
582
583 if (cap[h->index] == NULL) /* no more capture memory */
584 return ACT_RET_CONT;
585
586 len = key->data.u.str.data;
587 if (len > h->len)
588 len = h->len;
589
590 memcpy(cap[h->index], key->data.u.str.area, len);
591 cap[h->index][len] = 0;
592 return ACT_RET_CONT;
593}
594
595/* Check an "http-response capture" action.
596 *
597 * The function returns 1 in success case, otherwise, it returns 0 and err is
598 * filled.
599 */
600static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
601{
602 if (rule->action_ptr != http_action_res_capture_by_id)
603 return 1;
604
605 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
606 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
607 rule->arg.capid.idx);
608 return 0;
609 }
610
611 return 1;
612}
613
614/* parse an "http-response capture" action. It takes a single argument which is
615 * a sample fetch expression. It stores the expression into arg->act.p[0] and
616 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
617 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
618 */
619static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
620 struct act_rule *rule, char **err)
621{
622 struct sample_expr *expr;
623 int cur_arg;
624 int id;
625 char *error;
626
627 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
628 if (strcmp(args[cur_arg], "if") == 0 ||
629 strcmp(args[cur_arg], "unless") == 0)
630 break;
631
632 if (cur_arg < *orig_arg + 3) {
633 memprintf(err, "expects <expression> id <idx>");
634 return ACT_RET_PRS_ERR;
635 }
636
637 cur_arg = *orig_arg;
638 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
639 if (!expr)
640 return ACT_RET_PRS_ERR;
641
642 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
643 memprintf(err,
644 "fetch method '%s' extracts information from '%s', none of which is available here",
645 args[cur_arg-1], sample_src_names(expr->fetch->use));
646 free(expr);
647 return ACT_RET_PRS_ERR;
648 }
649
650 if (!args[cur_arg] || !*args[cur_arg]) {
651 memprintf(err, "expects 'id'");
652 free(expr);
653 return ACT_RET_PRS_ERR;
654 }
655
656 if (strcmp(args[cur_arg], "id") != 0) {
657 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
658 free(expr);
659 return ACT_RET_PRS_ERR;
660 }
661
662 cur_arg++;
663
664 if (!args[cur_arg]) {
665 memprintf(err, "missing id value");
666 free(expr);
667 return ACT_RET_PRS_ERR;
668 }
669
670 id = strtol(args[cur_arg], &error, 10);
671 if (*error != '\0') {
672 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
673 free(expr);
674 return ACT_RET_PRS_ERR;
675 }
676 cur_arg++;
677
678 px->conf.args.ctx = ARGC_CAP;
679
680 rule->action = ACT_CUSTOM;
681 rule->action_ptr = http_action_res_capture_by_id;
682 rule->check_ptr = check_http_res_capture;
683 rule->arg.capid.expr = expr;
684 rule->arg.capid.idx = id;
685
686 *orig_arg = cur_arg;
687 return ACT_RET_PRS_OK;
688}
689
690/************************************************************************/
691/* All supported http-request action keywords must be declared here. */
692/************************************************************************/
693
694static struct action_kw_list http_req_actions = {
695 .kw = {
696 { "capture", parse_http_req_capture },
697 { "reject", parse_http_action_reject },
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200698 { "disable-l7-retry", parse_http_req_disable_l7_retry },
Willy Tarreau33810222019-06-12 17:44:02 +0200699 { "replace-uri", parse_replace_uri },
Willy Tarreau79e57332018-10-02 16:01:16 +0200700 { "set-method", parse_set_req_line },
701 { "set-path", parse_set_req_line },
702 { "set-query", parse_set_req_line },
703 { "set-uri", parse_set_req_line },
704 { NULL, NULL }
705 }
706};
707
Willy Tarreau0108d902018-11-25 19:14:37 +0100708INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
709
Willy Tarreau79e57332018-10-02 16:01:16 +0200710static struct action_kw_list http_res_actions = {
711 .kw = {
712 { "capture", parse_http_res_capture },
713 { "set-status", parse_http_set_status },
714 { NULL, NULL }
715 }
716};
717
Willy Tarreau0108d902018-11-25 19:14:37 +0100718INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +0200719
720/*
721 * Local variables:
722 * c-indent-level: 8
723 * c-basic-offset: 8
724 * End:
725 */