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 | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 31 | #include <haproxy/sample.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 32 | #include <haproxy/tools.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 33 | #include <haproxy/version.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 34 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 35 | |
| 36 | /* List head of all known action keywords for "http-request" */ |
| 37 | struct 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" */ |
| 42 | struct action_kw_list http_res_keywords = { |
| 43 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 44 | }; |
| 45 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 46 | /* List head of all known action keywords for "http-after-response" */ |
| 47 | struct action_kw_list http_after_res_keywords = { |
| 48 | .list = LIST_HEAD_INIT(http_after_res_keywords.list) |
| 49 | }; |
| 50 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 51 | /* |
| 52 | * Return the struct http_req_action_kw associated to a keyword. |
| 53 | */ |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 54 | struct action_kw *action_http_req_custom(const char *kw) |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 55 | { |
| 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 Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 62 | struct action_kw *action_http_res_custom(const char *kw) |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 63 | { |
| 64 | return action_lookup(&http_res_keywords.list, kw); |
| 65 | } |
| 66 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 67 | /* |
| 68 | * Return the struct http_after_res_action_kw associated to a keyword. |
| 69 | */ |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 70 | struct action_kw *action_http_after_res_custom(const char *kw) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 71 | { |
| 72 | return action_lookup(&http_after_res_keywords.list, kw); |
| 73 | } |
| 74 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 75 | /* parse an "http-request" rule */ |
| 76 | struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 77 | { |
| 78 | struct act_rule *rule; |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 79 | const struct action_kw *custom = NULL; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 80 | int cur_arg; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 81 | |
| 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 Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 87 | rule->from = ACT_F_HTTP_REQ; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 88 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 89 | if (((custom = action_http_req_custom(args[0])) != NULL)) { |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 90 | char *errmsg = NULL; |
| 91 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 92 | cur_arg = 1; |
| 93 | /* try in the module list */ |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 94 | 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 Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 101 | else if (errmsg) { |
| 102 | ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg); |
| 103 | free(errmsg); |
| 104 | } |
| 105 | } |
| 106 | else { |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 107 | const char *best = action_suggest(args[0], &http_req_keywords.list, NULL); |
| 108 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 109 | action_build_list(&http_req_keywords.list, &trash); |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 110 | 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 Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 116 | 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 Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 132 | ha_alert("parsing [%s:%d]: 'http-request %s' expects" |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 133 | " 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 */ |
| 145 | struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 146 | { |
| 147 | struct act_rule *rule; |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 148 | const struct action_kw *custom = NULL; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 149 | int cur_arg; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 150 | |
| 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 Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 156 | rule->from = ACT_F_HTTP_RES; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 157 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 158 | if (((custom = action_http_res_custom(args[0])) != NULL)) { |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 159 | char *errmsg = NULL; |
| 160 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 161 | cur_arg = 1; |
| 162 | /* try in the module list */ |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 163 | 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 Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 170 | else if (errmsg) { |
| 171 | ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg); |
| 172 | free(errmsg); |
| 173 | } |
| 174 | } |
| 175 | else { |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 176 | const char *best = action_suggest(args[0], &http_res_keywords.list, NULL); |
| 177 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 178 | action_build_list(&http_res_keywords.list, &trash); |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 179 | 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 Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 185 | 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 Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 213 | |
| 214 | /* parse an "http-after-response" rule */ |
| 215 | struct 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 Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 218 | const struct action_kw *custom = NULL; |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 219 | 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 Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 246 | const char *best = action_suggest(args[0], &http_after_res_keywords.list, NULL); |
| 247 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 248 | action_build_list(&http_after_res_keywords.list, &trash); |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 249 | 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 Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 255 | 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 Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 283 | /* 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 | */ |
| 288 | struct 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 Faulet | c87e468 | 2020-01-28 09:13:41 +0100 | [diff] [blame] | 298 | unsigned int flags = (!dir ? REDIRECT_FLAG_FROM_REQ : REDIRECT_FLAG_NONE); |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 299 | 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 Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 355 | else if (strcmp(args[cur_arg], "drop-query") == 0) { |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 356 | flags |= REDIRECT_FLAG_DROP_QS; |
| 357 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 358 | else if (strcmp(args[cur_arg], "append-slash") == 0) { |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 359 | 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 Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 447 | __attribute__((constructor)) |
| 448 | static 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 | */ |