blob: cf3d12c1332b1eca67e05bc5c2a410383f00fbfc [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 Faulet4629d082019-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 Faulet4629d082019-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
Baptiste Assmann63b220d2020-01-16 14:34:22 +0100423 /* capture slots can only be declared in frontends, so we can't check their
424 * existence in backends at configuration parsing step
425 */
426 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200427 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
428 rule->arg.capid.idx);
429 return 0;
430 }
431
432 return 1;
433}
434
435/* parse an "http-request capture" action. It takes a single argument which is
436 * a sample fetch expression. It stores the expression into arg->act.p[0] and
437 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
438 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
439 */
440static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
441 struct act_rule *rule, char **err)
442{
443 struct sample_expr *expr;
444 struct cap_hdr *hdr;
445 int cur_arg;
446 int len = 0;
447
448 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
449 if (strcmp(args[cur_arg], "if") == 0 ||
450 strcmp(args[cur_arg], "unless") == 0)
451 break;
452
453 if (cur_arg < *orig_arg + 3) {
454 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
455 return ACT_RET_PRS_ERR;
456 }
457
458 cur_arg = *orig_arg;
459 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
460 if (!expr)
461 return ACT_RET_PRS_ERR;
462
463 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
464 memprintf(err,
465 "fetch method '%s' extracts information from '%s', none of which is available here",
466 args[cur_arg-1], sample_src_names(expr->fetch->use));
467 free(expr);
468 return ACT_RET_PRS_ERR;
469 }
470
471 if (!args[cur_arg] || !*args[cur_arg]) {
472 memprintf(err, "expects 'len or 'id'");
473 free(expr);
474 return ACT_RET_PRS_ERR;
475 }
476
477 if (strcmp(args[cur_arg], "len") == 0) {
478 cur_arg++;
479
480 if (!(px->cap & PR_CAP_FE)) {
481 memprintf(err, "proxy '%s' has no frontend capability", px->id);
482 return ACT_RET_PRS_ERR;
483 }
484
485 px->conf.args.ctx = ARGC_CAP;
486
487 if (!args[cur_arg]) {
488 memprintf(err, "missing length value");
489 free(expr);
490 return ACT_RET_PRS_ERR;
491 }
492 /* we copy the table name for now, it will be resolved later */
493 len = atoi(args[cur_arg]);
494 if (len <= 0) {
495 memprintf(err, "length must be > 0");
496 free(expr);
497 return ACT_RET_PRS_ERR;
498 }
499 cur_arg++;
500
Willy Tarreau79e57332018-10-02 16:01:16 +0200501 hdr = calloc(1, sizeof(*hdr));
502 hdr->next = px->req_cap;
503 hdr->name = NULL; /* not a header capture */
504 hdr->namelen = 0;
505 hdr->len = len;
506 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
507 hdr->index = px->nb_req_cap++;
508
509 px->req_cap = hdr;
510 px->to_log |= LW_REQHDR;
511
512 rule->action = ACT_CUSTOM;
513 rule->action_ptr = http_action_req_capture;
514 rule->arg.cap.expr = expr;
515 rule->arg.cap.hdr = hdr;
516 }
517
518 else if (strcmp(args[cur_arg], "id") == 0) {
519 int id;
520 char *error;
521
522 cur_arg++;
523
524 if (!args[cur_arg]) {
525 memprintf(err, "missing id value");
526 free(expr);
527 return ACT_RET_PRS_ERR;
528 }
529
530 id = strtol(args[cur_arg], &error, 10);
531 if (*error != '\0') {
532 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
533 free(expr);
534 return ACT_RET_PRS_ERR;
535 }
536 cur_arg++;
537
538 px->conf.args.ctx = ARGC_CAP;
539
540 rule->action = ACT_CUSTOM;
541 rule->action_ptr = http_action_req_capture_by_id;
542 rule->check_ptr = check_http_req_capture;
543 rule->arg.capid.expr = expr;
544 rule->arg.capid.idx = id;
545 }
546
547 else {
548 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
549 free(expr);
550 return ACT_RET_PRS_ERR;
551 }
552
553 *orig_arg = cur_arg;
554 return ACT_RET_PRS_OK;
555}
556
557/* This function executes the "capture" action and store the result in a
558 * capture slot if exists. It executes a fetch expression, turns the result
559 * into a string and puts it in a capture slot. It always returns 1. If an
560 * error occurs the action is cancelled, but the rule processing continues.
561 */
562static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
563 struct session *sess, struct stream *s, int flags)
564{
565 struct sample *key;
566 struct cap_hdr *h;
567 char **cap = s->res_cap;
568 struct proxy *fe = strm_fe(s);
569 int len;
570 int i;
571
572 /* Look for the original configuration. */
573 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
574 h != NULL && i != rule->arg.capid.idx ;
575 i--, h = h->next);
576 if (!h)
577 return ACT_RET_CONT;
578
579 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
580 if (!key)
581 return ACT_RET_CONT;
582
583 if (cap[h->index] == NULL)
584 cap[h->index] = pool_alloc(h->pool);
585
586 if (cap[h->index] == NULL) /* no more capture memory */
587 return ACT_RET_CONT;
588
589 len = key->data.u.str.data;
590 if (len > h->len)
591 len = h->len;
592
593 memcpy(cap[h->index], key->data.u.str.area, len);
594 cap[h->index][len] = 0;
595 return ACT_RET_CONT;
596}
597
598/* Check an "http-response capture" action.
599 *
600 * The function returns 1 in success case, otherwise, it returns 0 and err is
601 * filled.
602 */
603static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
604{
605 if (rule->action_ptr != http_action_res_capture_by_id)
606 return 1;
607
608 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
609 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
610 rule->arg.capid.idx);
611 return 0;
612 }
613
614 return 1;
615}
616
617/* parse an "http-response capture" action. It takes a single argument which is
618 * a sample fetch expression. It stores the expression into arg->act.p[0] and
619 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
620 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
621 */
622static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
623 struct act_rule *rule, char **err)
624{
625 struct sample_expr *expr;
626 int cur_arg;
627 int id;
628 char *error;
629
630 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
631 if (strcmp(args[cur_arg], "if") == 0 ||
632 strcmp(args[cur_arg], "unless") == 0)
633 break;
634
635 if (cur_arg < *orig_arg + 3) {
636 memprintf(err, "expects <expression> id <idx>");
637 return ACT_RET_PRS_ERR;
638 }
639
640 cur_arg = *orig_arg;
641 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
642 if (!expr)
643 return ACT_RET_PRS_ERR;
644
645 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
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 ACT_RET_PRS_ERR;
651 }
652
653 if (!args[cur_arg] || !*args[cur_arg]) {
654 memprintf(err, "expects 'id'");
655 free(expr);
656 return ACT_RET_PRS_ERR;
657 }
658
659 if (strcmp(args[cur_arg], "id") != 0) {
660 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
661 free(expr);
662 return ACT_RET_PRS_ERR;
663 }
664
665 cur_arg++;
666
667 if (!args[cur_arg]) {
668 memprintf(err, "missing id value");
669 free(expr);
670 return ACT_RET_PRS_ERR;
671 }
672
673 id = strtol(args[cur_arg], &error, 10);
674 if (*error != '\0') {
675 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
676 free(expr);
677 return ACT_RET_PRS_ERR;
678 }
679 cur_arg++;
680
681 px->conf.args.ctx = ARGC_CAP;
682
683 rule->action = ACT_CUSTOM;
684 rule->action_ptr = http_action_res_capture_by_id;
685 rule->check_ptr = check_http_res_capture;
686 rule->arg.capid.expr = expr;
687 rule->arg.capid.idx = id;
688
689 *orig_arg = cur_arg;
690 return ACT_RET_PRS_OK;
691}
692
693/************************************************************************/
694/* All supported http-request action keywords must be declared here. */
695/************************************************************************/
696
697static struct action_kw_list http_req_actions = {
698 .kw = {
699 { "capture", parse_http_req_capture },
700 { "reject", parse_http_action_reject },
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200701 { "disable-l7-retry", parse_http_req_disable_l7_retry },
Willy Tarreau33810222019-06-12 17:44:02 +0200702 { "replace-uri", parse_replace_uri },
Willy Tarreau79e57332018-10-02 16:01:16 +0200703 { "set-method", parse_set_req_line },
704 { "set-path", parse_set_req_line },
705 { "set-query", parse_set_req_line },
706 { "set-uri", parse_set_req_line },
707 { NULL, NULL }
708 }
709};
710
Willy Tarreau0108d902018-11-25 19:14:37 +0100711INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
712
Willy Tarreau79e57332018-10-02 16:01:16 +0200713static struct action_kw_list http_res_actions = {
714 .kw = {
715 { "capture", parse_http_res_capture },
716 { "set-status", parse_http_set_status },
717 { NULL, NULL }
718 }
719};
720
Willy Tarreau0108d902018-11-25 19:14:37 +0100721INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +0200722
723/*
724 * Local variables:
725 * c-indent-level: 8
726 * c-basic-offset: 8
727 * End:
728 */