blob: 87d7a29f8da17170639b88c3cc9c7f365550ad00 [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].
Willy Tarreaudfc85772019-12-17 06:52:51 +0100136 * The component to act on (path/uri) is taken from act.p[0] which contains 1
137 * for the path or 3 for the URI (values used by http_req_replace_stline()).
Willy Tarreau33810222019-06-12 17:44:02 +0200138 * It always returns ACT_RET_CONT. If an error occurs, the action is canceled,
139 * but the rule processing continues.
140 */
141static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
142 struct session *sess, struct stream *s, int flags)
143{
144 enum act_return ret = ACT_RET_ERR;
145 struct buffer *replace, *output;
146 struct ist uri;
147 int len;
148
149 replace = alloc_trash_chunk();
150 output = alloc_trash_chunk();
151 if (!replace || !output)
152 goto leave;
153
154 if (IS_HTX_STRM(s))
155 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
156 else
157 uri = ist2(ci_head(&s->req) + s->txn->req.sl.rq.u, s->txn->req.sl.rq.u_l);
158
Willy Tarreaudfc85772019-12-17 06:52:51 +0100159 if (rule->arg.act.p[0] == (void *)1)
Jerome Magninf931d042020-02-21 10:37:48 +0100160 uri = iststop(http_get_path(uri), '?');
Willy Tarreaudfc85772019-12-17 06:52:51 +0100161
Willy Tarreau33810222019-06-12 17:44:02 +0200162 if (!regex_exec_match2(rule->arg.act.p[1], uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
163 goto leave;
164
165 replace->data = build_logline(s, replace->area, replace->size, (struct list *)&rule->arg.act.p[2]);
166
167 /* note: uri.ptr doesn't need to be zero-terminated because it will
168 * only be used to pick pmatch references.
169 */
170 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
171 if (len == -1)
172 goto leave;
173
Willy Tarreaudfc85772019-12-17 06:52:51 +0100174 http_replace_req_line((long)rule->arg.act.p[0], output->area, len, px, s);
Willy Tarreau33810222019-06-12 17:44:02 +0200175
176 ret = ACT_RET_CONT;
177
178leave:
179 free_trash_chunk(output);
180 free_trash_chunk(replace);
181 return ret;
182}
183
Willy Tarreaudfc85772019-12-17 06:52:51 +0100184/* parse a "replace-uri" or "replace-path" http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200185 * This action takes 2 arguments (a regex and a replacement format string).
Willy Tarreaudfc85772019-12-17 06:52:51 +0100186 * The resulting rule makes use of arg->act.p[0] to store the action (1/3 for now),
Willy Tarreau33810222019-06-12 17:44:02 +0200187 * p[1] to store the compiled regex, and arg->act.p[2..3] to store the log-format
188 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
189 */
190static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
191 struct act_rule *rule, char **err)
192{
193 int cur_arg = *orig_arg;
194 char *error = NULL;
195
196 rule->action = ACT_CUSTOM;
Willy Tarreaudfc85772019-12-17 06:52:51 +0100197 if (strcmp(args[cur_arg-1], "replace-path") == 0)
198 rule->arg.act.p[0] = (void *)1; // replace-path
199 else
200 rule->arg.act.p[0] = (void *)3; // replace-uri
201
Willy Tarreau33810222019-06-12 17:44:02 +0200202 rule->action_ptr = http_action_replace_uri;
203
204 if (!*args[cur_arg] || !*args[cur_arg+1] ||
205 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
206 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
207 return ACT_RET_PRS_ERR;
208 }
209
210 if (!(rule->arg.act.p[1] = regex_comp(args[cur_arg], 1, 1, &error))) {
211 memprintf(err, "failed to parse the regex : %s", error);
212 free(error);
213 return ACT_RET_PRS_ERR;
214 }
215
216 LIST_INIT((struct list *)&rule->arg.act.p[2]);
217 px->conf.args.ctx = ARGC_HRQ;
218 if (!parse_logformat_string(args[cur_arg + 1], px, (struct list *)&rule->arg.act.p[2], LOG_OPT_HTTP,
219 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
220 return ACT_RET_PRS_ERR;
221 }
222
223 (*orig_arg) += 2;
224 return ACT_RET_PRS_OK;
225}
226
Willy Tarreau79e57332018-10-02 16:01:16 +0200227/* This function is just a compliant action wrapper for "set-status". */
228static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
229 struct session *sess, struct stream *s, int flags)
230{
231 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
232 return ACT_RET_CONT;
233}
234
235/* parse set-status action:
236 * This action accepts a single argument of type int representing
237 * an http status code. It returns ACT_RET_PRS_OK on success,
238 * ACT_RET_PRS_ERR on error.
239 */
240static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
241 struct act_rule *rule, char **err)
242{
243 char *error;
244
245 rule->action = ACT_CUSTOM;
246 rule->action_ptr = action_http_set_status;
247
248 /* Check if an argument is available */
249 if (!*args[*orig_arg]) {
250 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
251 return ACT_RET_PRS_ERR;
252 }
253
254 /* convert status code as integer */
255 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
256 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
257 memprintf(err, "expects an integer status code between 100 and 999");
258 return ACT_RET_PRS_ERR;
259 }
260
261 (*orig_arg)++;
262
263 /* set custom reason string */
264 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
265 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
266 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
267 (*orig_arg)++;
268 rule->arg.status.reason = strdup(args[*orig_arg]);
269 (*orig_arg)++;
270 }
271
272 return ACT_RET_PRS_OK;
273}
274
275/* This function executes the "reject" HTTP action. It clears the request and
276 * response buffer without sending any response. It can be useful as an HTTP
277 * alternative to the silent-drop action to defend against DoS attacks, and may
278 * also be used with HTTP/2 to close a connection instead of just a stream.
279 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet4629d082019-07-04 11:27:15 +0200280 * flags will indicate "PR". It always returns ACT_RET_DONE.
Willy Tarreau79e57332018-10-02 16:01:16 +0200281 */
282static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
283 struct session *sess, struct stream *s, int flags)
284{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100285 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200286 channel_abort(&s->req);
287 channel_abort(&s->res);
Christopher Faulet424ce542020-03-06 15:07:09 +0100288 s->req.analysers &= AN_REQ_FLT_END;
289 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200290
Olivier Houcharda798bf52019-03-08 18:52:00 +0100291 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
292 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200293 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100294 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200295
296 if (!(s->flags & SF_ERR_MASK))
297 s->flags |= SF_ERR_PRXCOND;
298 if (!(s->flags & SF_FINST_MASK))
299 s->flags |= SF_FINST_R;
300
Christopher Faulet4629d082019-07-04 11:27:15 +0200301 return ACT_RET_DONE;
Willy Tarreau79e57332018-10-02 16:01:16 +0200302}
303
304/* parse the "reject" action:
305 * This action takes no argument and returns ACT_RET_PRS_OK on success,
306 * ACT_RET_PRS_ERR on error.
307 */
308static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
309 struct act_rule *rule, char **err)
310{
311 rule->action = ACT_CUSTOM;
312 rule->action_ptr = http_action_reject;
313 return ACT_RET_PRS_OK;
314}
315
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200316/* This function executes the "disable-l7-retry" HTTP action.
317 * It disables L7 retries (all retry except for a connection failure). This
318 * can be useful for example to avoid retrying on POST requests.
319 * It just removes the L7 retry flag on the stream_interface, and always
320 * return ACT_RET_CONT;
321 */
322static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
323 struct session *sess, struct stream *s, int flags)
324{
325 struct stream_interface *si = &s->si[1];
326
327 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
328 * let's be future-proof and remove it anyway.
329 */
330 si->flags &= ~SI_FL_L7_RETRY;
331 si->flags |= SI_FL_D_L7_RETRY;
332 return ACT_RET_CONT;
333}
334
335/* parse the "disable-l7-retry" action:
336 * This action takes no argument and returns ACT_RET_PRS_OK on success,
337 * ACT_RET_PRS_ERR on error.
338 */
339static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
340 int *orig_args, struct proxy *px,
341 struct act_rule *rule, char **err)
342{
343 rule->action = ACT_CUSTOM;
344 rule->action_ptr = http_req_disable_l7_retry;
345 return ACT_RET_PRS_OK;
346}
347
Willy Tarreau79e57332018-10-02 16:01:16 +0200348/* This function executes the "capture" action. It executes a fetch expression,
349 * turns the result into a string and puts it in a capture slot. It always
350 * returns 1. If an error occurs the action is cancelled, but the rule
351 * processing continues.
352 */
353static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
354 struct session *sess, struct stream *s, int flags)
355{
356 struct sample *key;
357 struct cap_hdr *h = rule->arg.cap.hdr;
358 char **cap = s->req_cap;
359 int len;
360
361 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
362 if (!key)
363 return ACT_RET_CONT;
364
365 if (cap[h->index] == NULL)
366 cap[h->index] = pool_alloc(h->pool);
367
368 if (cap[h->index] == NULL) /* no more capture memory */
369 return ACT_RET_CONT;
370
371 len = key->data.u.str.data;
372 if (len > h->len)
373 len = h->len;
374
375 memcpy(cap[h->index], key->data.u.str.area, len);
376 cap[h->index][len] = 0;
377 return ACT_RET_CONT;
378}
379
380/* This function executes the "capture" action and store the result in a
381 * capture slot if exists. It executes a fetch expression, turns the result
382 * into a string and puts it in a capture slot. It always returns 1. If an
383 * error occurs the action is cancelled, but the rule processing continues.
384 */
385static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
386 struct session *sess, struct stream *s, int flags)
387{
388 struct sample *key;
389 struct cap_hdr *h;
390 char **cap = s->req_cap;
391 struct proxy *fe = strm_fe(s);
392 int len;
393 int i;
394
395 /* Look for the original configuration. */
396 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
397 h != NULL && i != rule->arg.capid.idx ;
398 i--, h = h->next);
399 if (!h)
400 return ACT_RET_CONT;
401
402 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
403 if (!key)
404 return ACT_RET_CONT;
405
406 if (cap[h->index] == NULL)
407 cap[h->index] = pool_alloc(h->pool);
408
409 if (cap[h->index] == NULL) /* no more capture memory */
410 return ACT_RET_CONT;
411
412 len = key->data.u.str.data;
413 if (len > h->len)
414 len = h->len;
415
416 memcpy(cap[h->index], key->data.u.str.area, len);
417 cap[h->index][len] = 0;
418 return ACT_RET_CONT;
419}
420
421/* Check an "http-request capture" action.
422 *
423 * The function returns 1 in success case, otherwise, it returns 0 and err is
424 * filled.
425 */
426static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
427{
428 if (rule->action_ptr != http_action_req_capture_by_id)
429 return 1;
430
Baptiste Assmann63b220d2020-01-16 14:34:22 +0100431 /* capture slots can only be declared in frontends, so we can't check their
432 * existence in backends at configuration parsing step
433 */
434 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200435 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
436 rule->arg.capid.idx);
437 return 0;
438 }
439
440 return 1;
441}
442
443/* parse an "http-request capture" action. It takes a single argument which is
444 * a sample fetch expression. It stores the expression into arg->act.p[0] and
445 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
446 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
447 */
448static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
449 struct act_rule *rule, char **err)
450{
451 struct sample_expr *expr;
452 struct cap_hdr *hdr;
453 int cur_arg;
454 int len = 0;
455
456 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
457 if (strcmp(args[cur_arg], "if") == 0 ||
458 strcmp(args[cur_arg], "unless") == 0)
459 break;
460
461 if (cur_arg < *orig_arg + 3) {
462 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
463 return ACT_RET_PRS_ERR;
464 }
465
466 cur_arg = *orig_arg;
467 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
468 if (!expr)
469 return ACT_RET_PRS_ERR;
470
471 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
472 memprintf(err,
473 "fetch method '%s' extracts information from '%s', none of which is available here",
474 args[cur_arg-1], sample_src_names(expr->fetch->use));
475 free(expr);
476 return ACT_RET_PRS_ERR;
477 }
478
479 if (!args[cur_arg] || !*args[cur_arg]) {
480 memprintf(err, "expects 'len or 'id'");
481 free(expr);
482 return ACT_RET_PRS_ERR;
483 }
484
485 if (strcmp(args[cur_arg], "len") == 0) {
486 cur_arg++;
487
488 if (!(px->cap & PR_CAP_FE)) {
489 memprintf(err, "proxy '%s' has no frontend capability", px->id);
490 return ACT_RET_PRS_ERR;
491 }
492
493 px->conf.args.ctx = ARGC_CAP;
494
495 if (!args[cur_arg]) {
496 memprintf(err, "missing length value");
497 free(expr);
498 return ACT_RET_PRS_ERR;
499 }
500 /* we copy the table name for now, it will be resolved later */
501 len = atoi(args[cur_arg]);
502 if (len <= 0) {
503 memprintf(err, "length must be > 0");
504 free(expr);
505 return ACT_RET_PRS_ERR;
506 }
507 cur_arg++;
508
Willy Tarreau79e57332018-10-02 16:01:16 +0200509 hdr = calloc(1, sizeof(*hdr));
510 hdr->next = px->req_cap;
511 hdr->name = NULL; /* not a header capture */
512 hdr->namelen = 0;
513 hdr->len = len;
514 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
515 hdr->index = px->nb_req_cap++;
516
517 px->req_cap = hdr;
518 px->to_log |= LW_REQHDR;
519
520 rule->action = ACT_CUSTOM;
521 rule->action_ptr = http_action_req_capture;
522 rule->arg.cap.expr = expr;
523 rule->arg.cap.hdr = hdr;
524 }
525
526 else if (strcmp(args[cur_arg], "id") == 0) {
527 int id;
528 char *error;
529
530 cur_arg++;
531
532 if (!args[cur_arg]) {
533 memprintf(err, "missing id value");
534 free(expr);
535 return ACT_RET_PRS_ERR;
536 }
537
538 id = strtol(args[cur_arg], &error, 10);
539 if (*error != '\0') {
540 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
541 free(expr);
542 return ACT_RET_PRS_ERR;
543 }
544 cur_arg++;
545
546 px->conf.args.ctx = ARGC_CAP;
547
548 rule->action = ACT_CUSTOM;
549 rule->action_ptr = http_action_req_capture_by_id;
550 rule->check_ptr = check_http_req_capture;
551 rule->arg.capid.expr = expr;
552 rule->arg.capid.idx = id;
553 }
554
555 else {
556 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
557 free(expr);
558 return ACT_RET_PRS_ERR;
559 }
560
561 *orig_arg = cur_arg;
562 return ACT_RET_PRS_OK;
563}
564
565/* This function executes the "capture" action and store the result in a
566 * capture slot if exists. It executes a fetch expression, turns the result
567 * into a string and puts it in a capture slot. It always returns 1. If an
568 * error occurs the action is cancelled, but the rule processing continues.
569 */
570static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
571 struct session *sess, struct stream *s, int flags)
572{
573 struct sample *key;
574 struct cap_hdr *h;
575 char **cap = s->res_cap;
576 struct proxy *fe = strm_fe(s);
577 int len;
578 int i;
579
580 /* Look for the original configuration. */
581 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
582 h != NULL && i != rule->arg.capid.idx ;
583 i--, h = h->next);
584 if (!h)
585 return ACT_RET_CONT;
586
587 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
588 if (!key)
589 return ACT_RET_CONT;
590
591 if (cap[h->index] == NULL)
592 cap[h->index] = pool_alloc(h->pool);
593
594 if (cap[h->index] == NULL) /* no more capture memory */
595 return ACT_RET_CONT;
596
597 len = key->data.u.str.data;
598 if (len > h->len)
599 len = h->len;
600
601 memcpy(cap[h->index], key->data.u.str.area, len);
602 cap[h->index][len] = 0;
603 return ACT_RET_CONT;
604}
605
606/* Check an "http-response capture" action.
607 *
608 * The function returns 1 in success case, otherwise, it returns 0 and err is
609 * filled.
610 */
611static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
612{
613 if (rule->action_ptr != http_action_res_capture_by_id)
614 return 1;
615
616 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
617 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
618 rule->arg.capid.idx);
619 return 0;
620 }
621
622 return 1;
623}
624
625/* parse an "http-response capture" action. It takes a single argument which is
626 * a sample fetch expression. It stores the expression into arg->act.p[0] and
627 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
628 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
629 */
630static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
631 struct act_rule *rule, char **err)
632{
633 struct sample_expr *expr;
634 int cur_arg;
635 int id;
636 char *error;
637
638 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
639 if (strcmp(args[cur_arg], "if") == 0 ||
640 strcmp(args[cur_arg], "unless") == 0)
641 break;
642
643 if (cur_arg < *orig_arg + 3) {
644 memprintf(err, "expects <expression> id <idx>");
645 return ACT_RET_PRS_ERR;
646 }
647
648 cur_arg = *orig_arg;
649 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
650 if (!expr)
651 return ACT_RET_PRS_ERR;
652
653 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
654 memprintf(err,
655 "fetch method '%s' extracts information from '%s', none of which is available here",
656 args[cur_arg-1], sample_src_names(expr->fetch->use));
657 free(expr);
658 return ACT_RET_PRS_ERR;
659 }
660
661 if (!args[cur_arg] || !*args[cur_arg]) {
662 memprintf(err, "expects 'id'");
663 free(expr);
664 return ACT_RET_PRS_ERR;
665 }
666
667 if (strcmp(args[cur_arg], "id") != 0) {
668 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
669 free(expr);
670 return ACT_RET_PRS_ERR;
671 }
672
673 cur_arg++;
674
675 if (!args[cur_arg]) {
676 memprintf(err, "missing id value");
677 free(expr);
678 return ACT_RET_PRS_ERR;
679 }
680
681 id = strtol(args[cur_arg], &error, 10);
682 if (*error != '\0') {
683 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
684 free(expr);
685 return ACT_RET_PRS_ERR;
686 }
687 cur_arg++;
688
689 px->conf.args.ctx = ARGC_CAP;
690
691 rule->action = ACT_CUSTOM;
692 rule->action_ptr = http_action_res_capture_by_id;
693 rule->check_ptr = check_http_res_capture;
694 rule->arg.capid.expr = expr;
695 rule->arg.capid.idx = id;
696
697 *orig_arg = cur_arg;
698 return ACT_RET_PRS_OK;
699}
700
701/************************************************************************/
702/* All supported http-request action keywords must be declared here. */
703/************************************************************************/
704
705static struct action_kw_list http_req_actions = {
706 .kw = {
707 { "capture", parse_http_req_capture },
708 { "reject", parse_http_action_reject },
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200709 { "disable-l7-retry", parse_http_req_disable_l7_retry },
Willy Tarreaudfc85772019-12-17 06:52:51 +0100710 { "replace-path", parse_replace_uri },
Willy Tarreau33810222019-06-12 17:44:02 +0200711 { "replace-uri", parse_replace_uri },
Willy Tarreau79e57332018-10-02 16:01:16 +0200712 { "set-method", parse_set_req_line },
713 { "set-path", parse_set_req_line },
714 { "set-query", parse_set_req_line },
715 { "set-uri", parse_set_req_line },
716 { NULL, NULL }
717 }
718};
719
Willy Tarreau0108d902018-11-25 19:14:37 +0100720INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
721
Willy Tarreau79e57332018-10-02 16:01:16 +0200722static struct action_kw_list http_res_actions = {
723 .kw = {
724 { "capture", parse_http_res_capture },
725 { "set-status", parse_http_set_status },
726 { NULL, NULL }
727 }
728};
729
Willy Tarreau0108d902018-11-25 19:14:37 +0100730INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +0200731
732/*
733 * Local variables:
734 * c-indent-level: 8
735 * c-basic-offset: 8
736 * End:
737 */