Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 19 | #include <haproxy/acl.h> |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 20 | #include <haproxy/action.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 21 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 22 | #include <haproxy/arg.h> |
| 23 | #include <haproxy/capture-t.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 24 | #include <haproxy/cfgparse.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 25 | #include <haproxy/chunk.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 26 | #include <haproxy/global.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 27 | #include <haproxy/http.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 28 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 29 | #include <haproxy/log.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 30 | #include <haproxy/pool.h> |
Willy Tarreau | d1dd250 | 2021-05-08 20:30:37 +0200 | [diff] [blame] | 31 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 32 | #include <haproxy/sample.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 33 | #include <haproxy/tools.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 34 | #include <haproxy/version.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 36 | |
| 37 | /* List head of all known action keywords for "http-request" */ |
| 38 | struct action_kw_list http_req_keywords = { |
| 39 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 40 | }; |
| 41 | |
| 42 | /* List head of all known action keywords for "http-response" */ |
| 43 | struct action_kw_list http_res_keywords = { |
| 44 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 45 | }; |
| 46 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 47 | /* List head of all known action keywords for "http-after-response" */ |
| 48 | struct action_kw_list http_after_res_keywords = { |
| 49 | .list = LIST_HEAD_INIT(http_after_res_keywords.list) |
| 50 | }; |
| 51 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 52 | /* |
| 53 | * Return the struct http_req_action_kw associated to a keyword. |
| 54 | */ |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 55 | struct action_kw *action_http_req_custom(const char *kw) |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 56 | { |
| 57 | return action_lookup(&http_req_keywords.list, kw); |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Return the struct http_res_action_kw associated to a keyword. |
| 62 | */ |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 63 | struct action_kw *action_http_res_custom(const char *kw) |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 64 | { |
| 65 | return action_lookup(&http_res_keywords.list, kw); |
| 66 | } |
| 67 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 68 | /* |
| 69 | * Return the struct http_after_res_action_kw associated to a keyword. |
| 70 | */ |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 71 | struct action_kw *action_http_after_res_custom(const char *kw) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 72 | { |
| 73 | return action_lookup(&http_after_res_keywords.list, kw); |
| 74 | } |
| 75 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 76 | /* parse an "http-request" rule */ |
| 77 | struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 78 | { |
| 79 | struct act_rule *rule; |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 80 | const struct action_kw *custom = NULL; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 81 | int cur_arg; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 82 | |
| 83 | rule = calloc(1, sizeof(*rule)); |
| 84 | if (!rule) { |
| 85 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 86 | goto out_err; |
| 87 | } |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 88 | rule->from = ACT_F_HTTP_REQ; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 89 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 90 | if (((custom = action_http_req_custom(args[0])) != NULL)) { |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 91 | char *errmsg = NULL; |
| 92 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 93 | cur_arg = 1; |
| 94 | /* try in the module list */ |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 95 | rule->kw = custom; |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 96 | |
| 97 | if (custom->flags & KWF_EXPERIMENTAL) { |
| 98 | if (!experimental_directives_allowed) { |
| 99 | ha_alert("parsing [%s:%d] : '%s' action is experimental, must be allowed via a global 'expose-experimental-directives'\n", |
| 100 | file, linenum, custom->kw); |
| 101 | goto out_err; |
| 102 | } |
| 103 | mark_tainted(TAINTED_CONFIG_EXP_KW_DECLARED); |
| 104 | } |
| 105 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 106 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
| 107 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 108 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 109 | free(errmsg); |
| 110 | goto out_err; |
| 111 | } |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 112 | else if (errmsg) { |
| 113 | ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg); |
| 114 | free(errmsg); |
| 115 | } |
| 116 | } |
| 117 | else { |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 118 | const char *best = action_suggest(args[0], &http_req_keywords.list, NULL); |
| 119 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 120 | action_build_list(&http_req_keywords.list, &trash); |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 121 | ha_alert("parsing [%s:%d]: 'http-request' expects %s, but got '%s'%s.%s%s%s\n", |
| 122 | file, linenum, trash.area, |
| 123 | args[0], *args[0] ? "" : " (missing argument)", |
| 124 | best ? " Did you mean '" : "", |
| 125 | best ? best : "", |
| 126 | best ? "' maybe ?" : ""); |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 127 | goto out_err; |
| 128 | } |
| 129 | |
| 130 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 131 | struct acl_cond *cond; |
| 132 | char *errmsg = NULL; |
| 133 | |
| 134 | if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 135 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 136 | file, linenum, args[0], errmsg); |
| 137 | free(errmsg); |
| 138 | goto out_err; |
| 139 | } |
| 140 | rule->cond = cond; |
| 141 | } |
| 142 | else if (*args[cur_arg]) { |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 143 | ha_alert("parsing [%s:%d]: 'http-request %s' expects" |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 144 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 145 | file, linenum, args[0], args[cur_arg]); |
| 146 | goto out_err; |
| 147 | } |
| 148 | |
| 149 | return rule; |
| 150 | out_err: |
| 151 | free(rule); |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | /* parse an "http-respose" rule */ |
| 156 | struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 157 | { |
| 158 | struct act_rule *rule; |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 159 | const struct action_kw *custom = NULL; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 160 | int cur_arg; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 161 | |
| 162 | rule = calloc(1, sizeof(*rule)); |
| 163 | if (!rule) { |
| 164 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 165 | goto out_err; |
| 166 | } |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 167 | rule->from = ACT_F_HTTP_RES; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 168 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 169 | if (((custom = action_http_res_custom(args[0])) != NULL)) { |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 170 | char *errmsg = NULL; |
| 171 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 172 | cur_arg = 1; |
| 173 | /* try in the module list */ |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 174 | rule->kw = custom; |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 175 | |
| 176 | if (custom->flags & KWF_EXPERIMENTAL) { |
| 177 | if (!experimental_directives_allowed) { |
| 178 | ha_alert("parsing [%s:%d] : '%s' action is experimental, must be allowed via a global 'expose-experimental-directives'\n", |
| 179 | file, linenum, custom->kw); |
| 180 | goto out_err; |
| 181 | } |
| 182 | mark_tainted(TAINTED_CONFIG_EXP_KW_DECLARED); |
| 183 | } |
| 184 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 185 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
| 186 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 187 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 188 | free(errmsg); |
| 189 | goto out_err; |
| 190 | } |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 191 | else if (errmsg) { |
| 192 | ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg); |
| 193 | free(errmsg); |
| 194 | } |
| 195 | } |
| 196 | else { |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 197 | const char *best = action_suggest(args[0], &http_res_keywords.list, NULL); |
| 198 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 199 | action_build_list(&http_res_keywords.list, &trash); |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 200 | ha_alert("parsing [%s:%d]: 'http-response' expects %s, but got '%s'%s.%s%s%s\n", |
| 201 | file, linenum, trash.area, |
| 202 | args[0], *args[0] ? "" : " (missing argument)", |
| 203 | best ? " Did you mean '" : "", |
| 204 | best ? best : "", |
| 205 | best ? "' maybe ?" : ""); |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 206 | goto out_err; |
| 207 | } |
| 208 | |
| 209 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 210 | struct acl_cond *cond; |
| 211 | char *errmsg = NULL; |
| 212 | |
| 213 | if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 214 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 215 | file, linenum, args[0], errmsg); |
| 216 | free(errmsg); |
| 217 | goto out_err; |
| 218 | } |
| 219 | rule->cond = cond; |
| 220 | } |
| 221 | else if (*args[cur_arg]) { |
| 222 | ha_alert("parsing [%s:%d]: 'http-response %s' expects" |
| 223 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 224 | file, linenum, args[0], args[cur_arg]); |
| 225 | goto out_err; |
| 226 | } |
| 227 | |
| 228 | return rule; |
| 229 | out_err: |
| 230 | free(rule); |
| 231 | return NULL; |
| 232 | } |
| 233 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 234 | |
| 235 | /* parse an "http-after-response" rule */ |
| 236 | struct act_rule *parse_http_after_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 237 | { |
| 238 | struct act_rule *rule; |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 239 | const struct action_kw *custom = NULL; |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 240 | int cur_arg; |
| 241 | |
| 242 | rule = calloc(1, sizeof(*rule)); |
| 243 | if (!rule) { |
| 244 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 245 | goto out_err; |
| 246 | } |
| 247 | rule->from = ACT_F_HTTP_RES; |
| 248 | |
| 249 | if (((custom = action_http_after_res_custom(args[0])) != NULL)) { |
| 250 | char *errmsg = NULL; |
| 251 | |
| 252 | cur_arg = 1; |
| 253 | /* try in the module list */ |
| 254 | rule->kw = custom; |
| 255 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
| 256 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-after-response %s' rule : %s.\n", |
| 257 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 258 | free(errmsg); |
| 259 | goto out_err; |
| 260 | } |
| 261 | else if (errmsg) { |
| 262 | ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg); |
| 263 | free(errmsg); |
| 264 | } |
| 265 | } |
| 266 | else { |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 267 | const char *best = action_suggest(args[0], &http_after_res_keywords.list, NULL); |
| 268 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 269 | action_build_list(&http_after_res_keywords.list, &trash); |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 270 | ha_alert("parsing [%s:%d]: 'http-after-response' expects %s, but got '%s'%s.%s%s%s\n", |
| 271 | file, linenum, trash.area, |
| 272 | args[0], *args[0] ? "" : " (missing argument)", |
| 273 | best ? " Did you mean '" : "", |
| 274 | best ? best : "", |
| 275 | best ? "' maybe ?" : ""); |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 276 | goto out_err; |
| 277 | } |
| 278 | |
| 279 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 280 | struct acl_cond *cond; |
| 281 | char *errmsg = NULL; |
| 282 | |
| 283 | if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 284 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-after-response %s' condition : %s.\n", |
| 285 | file, linenum, args[0], errmsg); |
| 286 | free(errmsg); |
| 287 | goto out_err; |
| 288 | } |
| 289 | rule->cond = cond; |
| 290 | } |
| 291 | else if (*args[cur_arg]) { |
| 292 | ha_alert("parsing [%s:%d]: 'http-after-response %s' expects" |
| 293 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 294 | file, linenum, args[0], args[cur_arg]); |
| 295 | goto out_err; |
| 296 | } |
| 297 | |
| 298 | return rule; |
| 299 | out_err: |
| 300 | free(rule); |
| 301 | return NULL; |
| 302 | } |
| 303 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 304 | /* Parses a redirect rule. Returns the redirect rule on success or NULL on error, |
| 305 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 306 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 307 | * the direction of the rule, and equals 0 for request, non-zero for responses. |
| 308 | */ |
| 309 | struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy, |
| 310 | const char **args, char **errmsg, int use_fmt, int dir) |
| 311 | { |
| 312 | struct redirect_rule *rule; |
| 313 | int cur_arg; |
| 314 | int type = REDIRECT_TYPE_NONE; |
| 315 | int code = 302; |
| 316 | const char *destination = NULL; |
| 317 | const char *cookie = NULL; |
| 318 | int cookie_set = 0; |
Christopher Faulet | c87e468 | 2020-01-28 09:13:41 +0100 | [diff] [blame] | 319 | unsigned int flags = (!dir ? REDIRECT_FLAG_FROM_REQ : REDIRECT_FLAG_NONE); |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 320 | struct acl_cond *cond = NULL; |
| 321 | |
| 322 | cur_arg = 0; |
| 323 | while (*(args[cur_arg])) { |
| 324 | if (strcmp(args[cur_arg], "location") == 0) { |
| 325 | if (!*args[cur_arg + 1]) |
| 326 | goto missing_arg; |
| 327 | |
| 328 | type = REDIRECT_TYPE_LOCATION; |
| 329 | cur_arg++; |
| 330 | destination = args[cur_arg]; |
| 331 | } |
| 332 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 333 | if (!*args[cur_arg + 1]) |
| 334 | goto missing_arg; |
| 335 | type = REDIRECT_TYPE_PREFIX; |
| 336 | cur_arg++; |
| 337 | destination = args[cur_arg]; |
| 338 | } |
| 339 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 340 | if (!*args[cur_arg + 1]) |
| 341 | goto missing_arg; |
| 342 | |
| 343 | type = REDIRECT_TYPE_SCHEME; |
| 344 | cur_arg++; |
| 345 | destination = args[cur_arg]; |
| 346 | } |
| 347 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 348 | if (!*args[cur_arg + 1]) |
| 349 | goto missing_arg; |
| 350 | |
| 351 | cur_arg++; |
| 352 | cookie = args[cur_arg]; |
| 353 | cookie_set = 1; |
| 354 | } |
| 355 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 356 | if (!*args[cur_arg + 1]) |
| 357 | goto missing_arg; |
| 358 | |
| 359 | cur_arg++; |
| 360 | cookie = args[cur_arg]; |
| 361 | cookie_set = 0; |
| 362 | } |
| 363 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 364 | if (!*args[cur_arg + 1]) |
| 365 | goto missing_arg; |
| 366 | |
| 367 | cur_arg++; |
| 368 | code = atol(args[cur_arg]); |
| 369 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
| 370 | memprintf(errmsg, |
| 371 | "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)", |
| 372 | args[cur_arg - 1], args[cur_arg]); |
| 373 | return NULL; |
| 374 | } |
| 375 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 376 | else if (strcmp(args[cur_arg], "drop-query") == 0) { |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 377 | flags |= REDIRECT_FLAG_DROP_QS; |
| 378 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 379 | else if (strcmp(args[cur_arg], "append-slash") == 0) { |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 380 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 381 | } |
| 382 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 383 | strcmp(args[cur_arg], "unless") == 0) { |
| 384 | cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg); |
| 385 | if (!cond) { |
| 386 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 387 | return NULL; |
| 388 | } |
| 389 | break; |
| 390 | } |
| 391 | else { |
| 392 | memprintf(errmsg, |
| 393 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 394 | args[cur_arg]); |
| 395 | return NULL; |
| 396 | } |
| 397 | cur_arg++; |
| 398 | } |
| 399 | |
| 400 | if (type == REDIRECT_TYPE_NONE) { |
| 401 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 402 | return NULL; |
| 403 | } |
| 404 | |
| 405 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 406 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 407 | return NULL; |
| 408 | } |
| 409 | |
| 410 | rule = calloc(1, sizeof(*rule)); |
Remi Tricot-Le Breton | b6864a5 | 2021-05-19 11:32:04 +0200 | [diff] [blame] | 411 | if (!rule) { |
| 412 | memprintf(errmsg, "parsing [%s:%d]: out of memory.", file, linenum); |
| 413 | return NULL; |
| 414 | } |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 415 | rule->cond = cond; |
| 416 | LIST_INIT(&rule->rdr_fmt); |
| 417 | |
| 418 | if (!use_fmt) { |
| 419 | /* old-style static redirect rule */ |
| 420 | rule->rdr_str = strdup(destination); |
| 421 | rule->rdr_len = strlen(destination); |
| 422 | } |
| 423 | else { |
| 424 | /* log-format based redirect rule */ |
| 425 | |
| 426 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 427 | * if prefix == "/", we don't want to add anything, otherwise it |
| 428 | * makes it hard for the user to configure a self-redirection. |
| 429 | */ |
| 430 | curproxy->conf.args.ctx = ARGC_RDR; |
| 431 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
| 432 | if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
| 433 | dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR |
| 434 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 435 | errmsg)) { |
| 436 | return NULL; |
| 437 | } |
| 438 | free(curproxy->conf.lfs_file); |
| 439 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 440 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | if (cookie) { |
| 445 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 446 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 447 | */ |
| 448 | rule->cookie_len = strlen(cookie); |
| 449 | if (cookie_set) { |
| 450 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 451 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 452 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 453 | rule->cookie_len += 9; |
| 454 | } else { |
| 455 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 456 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 457 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 458 | rule->cookie_len += 20; |
| 459 | } |
| 460 | } |
| 461 | rule->type = type; |
| 462 | rule->code = code; |
| 463 | rule->flags = flags; |
| 464 | LIST_INIT(&rule->list); |
| 465 | return rule; |
| 466 | |
| 467 | missing_arg: |
| 468 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 469 | return NULL; |
| 470 | } |
| 471 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 472 | __attribute__((constructor)) |
| 473 | static void __http_rules_init(void) |
| 474 | { |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * Local variables: |
| 479 | * c-indent-level: 8 |
| 480 | * c-basic-offset: 8 |
| 481 | * End: |
| 482 | */ |