blob: a34c560950b981e4d9844b2a307f3773408ed207 [file] [log] [blame]
Willy Tarreau61c112a2018-10-02 16:43:32 +02001/*
2 * HTTP rules parsing and registration
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
Willy Tarreaudcc048a2020-06-04 19:11:43 +020019#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020020#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020021#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/arg.h>
23#include <haproxy/capture-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020025#include <haproxy/chunk.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020026#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020027#include <haproxy/http.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020028#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020029#include <haproxy/log.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020030#include <haproxy/pool.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/sample.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020032#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020033#include <haproxy/version.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020034
Willy Tarreau61c112a2018-10-02 16:43:32 +020035
36/* List head of all known action keywords for "http-request" */
37struct action_kw_list http_req_keywords = {
38 .list = LIST_HEAD_INIT(http_req_keywords.list)
39};
40
41/* List head of all known action keywords for "http-response" */
42struct action_kw_list http_res_keywords = {
43 .list = LIST_HEAD_INIT(http_res_keywords.list)
44};
45
Christopher Faulet6d0c3df2020-01-22 09:26:35 +010046/* List head of all known action keywords for "http-after-response" */
47struct action_kw_list http_after_res_keywords = {
48 .list = LIST_HEAD_INIT(http_after_res_keywords.list)
49};
50
Willy Tarreau61c112a2018-10-02 16:43:32 +020051/*
52 * Return the struct http_req_action_kw associated to a keyword.
53 */
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010054struct action_kw *action_http_req_custom(const char *kw)
Willy Tarreau61c112a2018-10-02 16:43:32 +020055{
56 return action_lookup(&http_req_keywords.list, kw);
57}
58
59/*
60 * Return the struct http_res_action_kw associated to a keyword.
61 */
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010062struct action_kw *action_http_res_custom(const char *kw)
Willy Tarreau61c112a2018-10-02 16:43:32 +020063{
64 return action_lookup(&http_res_keywords.list, kw);
65}
66
Christopher Faulet6d0c3df2020-01-22 09:26:35 +010067/*
68 * Return the struct http_after_res_action_kw associated to a keyword.
69 */
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010070struct action_kw *action_http_after_res_custom(const char *kw)
Christopher Faulet6d0c3df2020-01-22 09:26:35 +010071{
72 return action_lookup(&http_after_res_keywords.list, kw);
73}
74
Willy Tarreau61c112a2018-10-02 16:43:32 +020075/* parse an "http-request" rule */
76struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
77{
78 struct act_rule *rule;
Willy Tarreau49bf7be2021-03-12 12:01:34 +010079 const struct action_kw *custom = NULL;
Willy Tarreau61c112a2018-10-02 16:43:32 +020080 int cur_arg;
Willy Tarreau61c112a2018-10-02 16:43:32 +020081
82 rule = calloc(1, sizeof(*rule));
83 if (!rule) {
84 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
85 goto out_err;
86 }
Christopher Faulet81e20172019-12-12 16:40:30 +010087 rule->from = ACT_F_HTTP_REQ;
Willy Tarreau61c112a2018-10-02 16:43:32 +020088
Christopher Faulet81e20172019-12-12 16:40:30 +010089 if (((custom = action_http_req_custom(args[0])) != NULL)) {
Willy Tarreau61c112a2018-10-02 16:43:32 +020090 char *errmsg = NULL;
91
Willy Tarreau61c112a2018-10-02 16:43:32 +020092 cur_arg = 1;
93 /* try in the module list */
Willy Tarreau61c112a2018-10-02 16:43:32 +020094 rule->kw = custom;
95 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
96 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
97 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
98 free(errmsg);
99 goto out_err;
100 }
Christopher Faulet81e20172019-12-12 16:40:30 +0100101 else if (errmsg) {
102 ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg);
103 free(errmsg);
104 }
105 }
106 else {
Willy Tarreau49bf7be2021-03-12 12:01:34 +0100107 const char *best = action_suggest(args[0], &http_req_keywords.list, NULL);
108
Willy Tarreau61c112a2018-10-02 16:43:32 +0200109 action_build_list(&http_req_keywords.list, &trash);
Willy Tarreau49bf7be2021-03-12 12:01:34 +0100110 ha_alert("parsing [%s:%d]: 'http-request' expects %s, but got '%s'%s.%s%s%s\n",
111 file, linenum, trash.area,
112 args[0], *args[0] ? "" : " (missing argument)",
113 best ? " Did you mean '" : "",
114 best ? best : "",
115 best ? "' maybe ?" : "");
Willy Tarreau61c112a2018-10-02 16:43:32 +0200116 goto out_err;
117 }
118
119 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
120 struct acl_cond *cond;
121 char *errmsg = NULL;
122
123 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
124 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
125 file, linenum, args[0], errmsg);
126 free(errmsg);
127 goto out_err;
128 }
129 rule->cond = cond;
130 }
131 else if (*args[cur_arg]) {
Christopher Faulet81e20172019-12-12 16:40:30 +0100132 ha_alert("parsing [%s:%d]: 'http-request %s' expects"
Willy Tarreau61c112a2018-10-02 16:43:32 +0200133 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
134 file, linenum, args[0], args[cur_arg]);
135 goto out_err;
136 }
137
138 return rule;
139 out_err:
140 free(rule);
141 return NULL;
142}
143
144/* parse an "http-respose" rule */
145struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
146{
147 struct act_rule *rule;
Willy Tarreau49bf7be2021-03-12 12:01:34 +0100148 const struct action_kw *custom = NULL;
Willy Tarreau61c112a2018-10-02 16:43:32 +0200149 int cur_arg;
Willy Tarreau61c112a2018-10-02 16:43:32 +0200150
151 rule = calloc(1, sizeof(*rule));
152 if (!rule) {
153 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
154 goto out_err;
155 }
Christopher Faulet81e20172019-12-12 16:40:30 +0100156 rule->from = ACT_F_HTTP_RES;
Willy Tarreau61c112a2018-10-02 16:43:32 +0200157
Christopher Faulet81e20172019-12-12 16:40:30 +0100158 if (((custom = action_http_res_custom(args[0])) != NULL)) {
Willy Tarreau61c112a2018-10-02 16:43:32 +0200159 char *errmsg = NULL;
160
Willy Tarreau61c112a2018-10-02 16:43:32 +0200161 cur_arg = 1;
162 /* try in the module list */
Willy Tarreau61c112a2018-10-02 16:43:32 +0200163 rule->kw = custom;
164 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
165 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
166 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
167 free(errmsg);
168 goto out_err;
169 }
Christopher Faulet81e20172019-12-12 16:40:30 +0100170 else if (errmsg) {
171 ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg);
172 free(errmsg);
173 }
174 }
175 else {
Willy Tarreau49bf7be2021-03-12 12:01:34 +0100176 const char *best = action_suggest(args[0], &http_res_keywords.list, NULL);
177
Willy Tarreau61c112a2018-10-02 16:43:32 +0200178 action_build_list(&http_res_keywords.list, &trash);
Willy Tarreau49bf7be2021-03-12 12:01:34 +0100179 ha_alert("parsing [%s:%d]: 'http-response' expects %s, but got '%s'%s.%s%s%s\n",
180 file, linenum, trash.area,
181 args[0], *args[0] ? "" : " (missing argument)",
182 best ? " Did you mean '" : "",
183 best ? best : "",
184 best ? "' maybe ?" : "");
Willy Tarreau61c112a2018-10-02 16:43:32 +0200185 goto out_err;
186 }
187
188 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
189 struct acl_cond *cond;
190 char *errmsg = NULL;
191
192 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
193 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
194 file, linenum, args[0], errmsg);
195 free(errmsg);
196 goto out_err;
197 }
198 rule->cond = cond;
199 }
200 else if (*args[cur_arg]) {
201 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
202 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
203 file, linenum, args[0], args[cur_arg]);
204 goto out_err;
205 }
206
207 return rule;
208 out_err:
209 free(rule);
210 return NULL;
211}
212
Christopher Faulet6d0c3df2020-01-22 09:26:35 +0100213
214/* parse an "http-after-response" rule */
215struct act_rule *parse_http_after_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
216{
217 struct act_rule *rule;
Willy Tarreau49bf7be2021-03-12 12:01:34 +0100218 const struct action_kw *custom = NULL;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +0100219 int cur_arg;
220
221 rule = calloc(1, sizeof(*rule));
222 if (!rule) {
223 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
224 goto out_err;
225 }
226 rule->from = ACT_F_HTTP_RES;
227
228 if (((custom = action_http_after_res_custom(args[0])) != NULL)) {
229 char *errmsg = NULL;
230
231 cur_arg = 1;
232 /* try in the module list */
233 rule->kw = custom;
234 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
235 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-after-response %s' rule : %s.\n",
236 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
237 free(errmsg);
238 goto out_err;
239 }
240 else if (errmsg) {
241 ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg);
242 free(errmsg);
243 }
244 }
245 else {
Willy Tarreau49bf7be2021-03-12 12:01:34 +0100246 const char *best = action_suggest(args[0], &http_after_res_keywords.list, NULL);
247
Christopher Faulet6d0c3df2020-01-22 09:26:35 +0100248 action_build_list(&http_after_res_keywords.list, &trash);
Willy Tarreau49bf7be2021-03-12 12:01:34 +0100249 ha_alert("parsing [%s:%d]: 'http-after-response' expects %s, but got '%s'%s.%s%s%s\n",
250 file, linenum, trash.area,
251 args[0], *args[0] ? "" : " (missing argument)",
252 best ? " Did you mean '" : "",
253 best ? best : "",
254 best ? "' maybe ?" : "");
Christopher Faulet6d0c3df2020-01-22 09:26:35 +0100255 goto out_err;
256 }
257
258 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
259 struct acl_cond *cond;
260 char *errmsg = NULL;
261
262 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
263 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-after-response %s' condition : %s.\n",
264 file, linenum, args[0], errmsg);
265 free(errmsg);
266 goto out_err;
267 }
268 rule->cond = cond;
269 }
270 else if (*args[cur_arg]) {
271 ha_alert("parsing [%s:%d]: 'http-after-response %s' expects"
272 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
273 file, linenum, args[0], args[cur_arg]);
274 goto out_err;
275 }
276
277 return rule;
278 out_err:
279 free(rule);
280 return NULL;
281}
282
Willy Tarreau61c112a2018-10-02 16:43:32 +0200283/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
284 * with <err> filled with the error message. If <use_fmt> is not null, builds a
285 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
286 * the direction of the rule, and equals 0 for request, non-zero for responses.
287 */
288struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
289 const char **args, char **errmsg, int use_fmt, int dir)
290{
291 struct redirect_rule *rule;
292 int cur_arg;
293 int type = REDIRECT_TYPE_NONE;
294 int code = 302;
295 const char *destination = NULL;
296 const char *cookie = NULL;
297 int cookie_set = 0;
Christopher Fauletc87e4682020-01-28 09:13:41 +0100298 unsigned int flags = (!dir ? REDIRECT_FLAG_FROM_REQ : REDIRECT_FLAG_NONE);
Willy Tarreau61c112a2018-10-02 16:43:32 +0200299 struct acl_cond *cond = NULL;
300
301 cur_arg = 0;
302 while (*(args[cur_arg])) {
303 if (strcmp(args[cur_arg], "location") == 0) {
304 if (!*args[cur_arg + 1])
305 goto missing_arg;
306
307 type = REDIRECT_TYPE_LOCATION;
308 cur_arg++;
309 destination = args[cur_arg];
310 }
311 else if (strcmp(args[cur_arg], "prefix") == 0) {
312 if (!*args[cur_arg + 1])
313 goto missing_arg;
314 type = REDIRECT_TYPE_PREFIX;
315 cur_arg++;
316 destination = args[cur_arg];
317 }
318 else if (strcmp(args[cur_arg], "scheme") == 0) {
319 if (!*args[cur_arg + 1])
320 goto missing_arg;
321
322 type = REDIRECT_TYPE_SCHEME;
323 cur_arg++;
324 destination = args[cur_arg];
325 }
326 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
327 if (!*args[cur_arg + 1])
328 goto missing_arg;
329
330 cur_arg++;
331 cookie = args[cur_arg];
332 cookie_set = 1;
333 }
334 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
335 if (!*args[cur_arg + 1])
336 goto missing_arg;
337
338 cur_arg++;
339 cookie = args[cur_arg];
340 cookie_set = 0;
341 }
342 else if (strcmp(args[cur_arg], "code") == 0) {
343 if (!*args[cur_arg + 1])
344 goto missing_arg;
345
346 cur_arg++;
347 code = atol(args[cur_arg]);
348 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
349 memprintf(errmsg,
350 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
351 args[cur_arg - 1], args[cur_arg]);
352 return NULL;
353 }
354 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100355 else if (strcmp(args[cur_arg], "drop-query") == 0) {
Willy Tarreau61c112a2018-10-02 16:43:32 +0200356 flags |= REDIRECT_FLAG_DROP_QS;
357 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100358 else if (strcmp(args[cur_arg], "append-slash") == 0) {
Willy Tarreau61c112a2018-10-02 16:43:32 +0200359 flags |= REDIRECT_FLAG_APPEND_SLASH;
360 }
361 else if (strcmp(args[cur_arg], "if") == 0 ||
362 strcmp(args[cur_arg], "unless") == 0) {
363 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
364 if (!cond) {
365 memprintf(errmsg, "error in condition: %s", *errmsg);
366 return NULL;
367 }
368 break;
369 }
370 else {
371 memprintf(errmsg,
372 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
373 args[cur_arg]);
374 return NULL;
375 }
376 cur_arg++;
377 }
378
379 if (type == REDIRECT_TYPE_NONE) {
380 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
381 return NULL;
382 }
383
384 if (dir && type != REDIRECT_TYPE_LOCATION) {
385 memprintf(errmsg, "response only supports redirect type 'location'");
386 return NULL;
387 }
388
389 rule = calloc(1, sizeof(*rule));
390 rule->cond = cond;
391 LIST_INIT(&rule->rdr_fmt);
392
393 if (!use_fmt) {
394 /* old-style static redirect rule */
395 rule->rdr_str = strdup(destination);
396 rule->rdr_len = strlen(destination);
397 }
398 else {
399 /* log-format based redirect rule */
400
401 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
402 * if prefix == "/", we don't want to add anything, otherwise it
403 * makes it hard for the user to configure a self-redirection.
404 */
405 curproxy->conf.args.ctx = ARGC_RDR;
406 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
407 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
408 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
409 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
410 errmsg)) {
411 return NULL;
412 }
413 free(curproxy->conf.lfs_file);
414 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
415 curproxy->conf.lfs_line = curproxy->conf.args.line;
416 }
417 }
418
419 if (cookie) {
420 /* depending on cookie_set, either we want to set the cookie, or to clear it.
421 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
422 */
423 rule->cookie_len = strlen(cookie);
424 if (cookie_set) {
425 rule->cookie_str = malloc(rule->cookie_len + 10);
426 memcpy(rule->cookie_str, cookie, rule->cookie_len);
427 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
428 rule->cookie_len += 9;
429 } else {
430 rule->cookie_str = malloc(rule->cookie_len + 21);
431 memcpy(rule->cookie_str, cookie, rule->cookie_len);
432 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
433 rule->cookie_len += 20;
434 }
435 }
436 rule->type = type;
437 rule->code = code;
438 rule->flags = flags;
439 LIST_INIT(&rule->list);
440 return rule;
441
442 missing_arg:
443 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
444 return NULL;
445}
446
Willy Tarreau61c112a2018-10-02 16:43:32 +0200447__attribute__((constructor))
448static void __http_rules_init(void)
449{
450}
451
452/*
453 * Local variables:
454 * c-indent-level: 8
455 * c-basic-offset: 8
456 * End:
457 */