Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions to manipulate HTTP messages using the internal representation. |
| 3 | * |
| 4 | * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 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 | */ |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 12 | #include <sys/types.h> |
| 13 | #include <sys/stat.h> |
Amaury Denoyelle | 4c0882b | 2021-07-07 10:49:26 +0200 | [diff] [blame] | 14 | #include <ctype.h> |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 15 | #include <fcntl.h> |
| 16 | #include <unistd.h> |
| 17 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 18 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 19 | #include <haproxy/arg.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 20 | #include <haproxy/cfgparse.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 21 | #include <haproxy/global.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 22 | #include <haproxy/h1.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 23 | #include <haproxy/http.h> |
Amaury Denoyelle | 2c5a7ee | 2022-08-17 16:33:53 +0200 | [diff] [blame] | 24 | #include <haproxy/http-hdr.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 25 | #include <haproxy/http_fetch.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 26 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 27 | #include <haproxy/htx.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/log.h> |
| 29 | #include <haproxy/regex.h> |
| 30 | #include <haproxy/sample.h> |
Willy Tarreau | 4cbf62d | 2021-05-08 13:01:23 +0200 | [diff] [blame] | 31 | #include <haproxy/tools.h> |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 32 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 33 | |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 34 | struct buffer http_err_chunks[HTTP_ERR_SIZE]; |
Christopher Faulet | 1b13eca | 2020-05-14 09:54:26 +0200 | [diff] [blame] | 35 | struct http_reply http_err_replies[HTTP_ERR_SIZE]; |
| 36 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 37 | struct eb_root http_error_messages = EB_ROOT; |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 38 | struct list http_errors_list = LIST_HEAD_INIT(http_errors_list); |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 39 | struct list http_replies_list = LIST_HEAD_INIT(http_replies_list); |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 40 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 41 | /* The declaration of an errorfiles/errorfile directives. Used during config |
| 42 | * parsing only. */ |
| 43 | struct conf_errors { |
| 44 | char type; /* directive type (0: errorfiles, 1: errorfile) */ |
| 45 | union { |
| 46 | struct { |
| 47 | int status; /* the status code associated to this error */ |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 48 | struct http_reply *reply; /* the http reply for the errorfile */ |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 49 | } errorfile; /* describe an "errorfile" directive */ |
| 50 | struct { |
| 51 | char *name; /* the http-errors section name */ |
| 52 | char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */ |
| 53 | } errorfiles; /* describe an "errorfiles" directive */ |
| 54 | } info; |
| 55 | |
| 56 | char *file; /* file where the directive appears */ |
| 57 | int line; /* line where the directive appears */ |
| 58 | |
| 59 | struct list list; /* next conf_errors */ |
| 60 | }; |
| 61 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 62 | /* Returns the next unporocessed start line in the HTX message. It returns NULL |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 63 | * if the start-line is undefined (first == -1). Otherwise, it returns the |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 64 | * pointer on the htx_sl structure. |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 65 | */ |
Tim Duesterhus | b8ee894 | 2021-04-03 20:39:20 +0200 | [diff] [blame] | 66 | struct htx_sl *http_get_stline(const struct htx *htx) |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 67 | { |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 68 | struct htx_blk *blk; |
Christopher Faulet | 573fe73 | 2018-11-28 16:55:12 +0100 | [diff] [blame] | 69 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 70 | blk = htx_get_first_blk(htx); |
Christopher Faulet | a7d6cf2 | 2021-04-15 10:25:35 +0200 | [diff] [blame] | 71 | if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 72 | return NULL; |
| 73 | return htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Christopher Faulet | 727a3f1 | 2020-02-07 16:39:41 +0100 | [diff] [blame] | 76 | /* Returns the headers size in the HTX message */ |
| 77 | size_t http_get_hdrs_size(struct htx *htx) |
| 78 | { |
| 79 | struct htx_blk *blk; |
| 80 | size_t sz = 0; |
| 81 | |
| 82 | blk = htx_get_first_blk(htx); |
| 83 | if (!blk || htx_get_blk_type(blk) > HTX_BLK_EOH) |
| 84 | return sz; |
| 85 | |
| 86 | for (; blk; blk = htx_get_next_blk(htx, blk)) { |
| 87 | sz += htx_get_blksz(blk); |
| 88 | if (htx_get_blk_type(blk) == HTX_BLK_EOH) |
| 89 | break; |
| 90 | } |
| 91 | return sz; |
| 92 | } |
| 93 | |
Christopher Faulet | 8dd33e1 | 2020-05-05 07:42:42 +0200 | [diff] [blame] | 94 | /* Finds the first or next occurrence of header matching <pattern> in the HTX |
| 95 | * message <htx> using the context <ctx>. This structure holds everything |
| 96 | * necessary to use the header and find next occurrence. If its <blk> member is |
| 97 | * NULL, the header is searched from the beginning. Otherwise, the next |
| 98 | * occurrence is returned. The function returns 1 when it finds a value, and 0 |
| 99 | * when there is no more. It is designed to work with headers defined as |
| 100 | * comma-separated lists. If HTTP_FIND_FL_FULL flag is set, it works on |
| 101 | * full-line headers in whose comma is not a delimiter but is part of the |
| 102 | * syntax. A special case, if ctx->value is NULL when searching for a new values |
| 103 | * of a header, the current header is rescanned. This allows rescanning after a |
| 104 | * header deletion. |
| 105 | * |
| 106 | * The matching method is chosen by checking the flags : |
| 107 | * |
| 108 | * * HTTP_FIND_FL_MATCH_REG : <pattern> is a regex. header names matching |
| 109 | * the regex are evaluated. |
| 110 | * * HTTP_FIND_FL_MATCH_STR : <pattern> is a string. The header names equal |
| 111 | * to the string are evaluated. |
| 112 | * * HTTP_FIND_FL_MATCH_PFX : <pattern> is a string. The header names |
| 113 | * starting by the string are evaluated. |
| 114 | * * HTTP_FIND_FL_MATCH_SFX : <pattern> is a string. The header names |
| 115 | * ending by the string are evaluated. |
| 116 | * * HTTP_FIND_FL_MATCH_SUB : <pattern> is a string. The header names |
| 117 | * containing the string are evaluated. |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 118 | */ |
Christopher Faulet | 8dd33e1 | 2020-05-05 07:42:42 +0200 | [diff] [blame] | 119 | |
| 120 | #define HTTP_FIND_FL_MATCH_STR 0x0001 |
| 121 | #define HTTP_FIND_FL_MATCH_PFX 0x0002 |
| 122 | #define HTTP_FIND_FL_MATCH_SFX 0x0003 |
| 123 | #define HTTP_FIND_FL_MATCH_SUB 0x0004 |
| 124 | #define HTTP_FIND_FL_MATCH_REG 0x0005 |
| 125 | /* 0x0006..0x000f: for other matching methods */ |
| 126 | #define HTTP_FIND_FL_MATCH_TYPE 0x000F |
| 127 | #define HTTP_FIND_FL_FULL 0x0010 |
| 128 | |
| 129 | static int __http_find_header(const struct htx *htx, const void *pattern, struct http_hdr_ctx *ctx, int flags) |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 130 | { |
| 131 | struct htx_blk *blk = ctx->blk; |
| 132 | struct ist n, v; |
| 133 | enum htx_blk_type type; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 134 | |
| 135 | if (blk) { |
| 136 | char *p; |
| 137 | |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 138 | if (!isttest(ctx->value)) |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 139 | goto rescan_hdr; |
Christopher Faulet | 8dd33e1 | 2020-05-05 07:42:42 +0200 | [diff] [blame] | 140 | if (flags & HTTP_FIND_FL_FULL) |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 141 | goto next_blk; |
| 142 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 143 | p = istend(ctx->value) + ctx->lws_after; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 144 | v.len -= (p - v.ptr); |
| 145 | v.ptr = p; |
| 146 | if (!v.len) |
| 147 | goto next_blk; |
| 148 | /* Skip comma */ |
| 149 | if (*(v.ptr) == ',') { |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 150 | v = istnext(v); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | goto return_hdr; |
| 154 | } |
| 155 | |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 156 | if (htx_is_empty(htx)) |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 157 | return 0; |
| 158 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 159 | for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 160 | rescan_hdr: |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 161 | type = htx_get_blk_type(blk); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 162 | if (type == HTX_BLK_EOH) |
Christopher Faulet | 573fe73 | 2018-11-28 16:55:12 +0100 | [diff] [blame] | 163 | break; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 164 | if (type != HTX_BLK_HDR) |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 165 | continue; |
Christopher Faulet | 8dd33e1 | 2020-05-05 07:42:42 +0200 | [diff] [blame] | 166 | |
| 167 | if ((flags & HTTP_FIND_FL_MATCH_TYPE) == HTTP_FIND_FL_MATCH_REG) { |
| 168 | const struct my_regex *re = pattern; |
| 169 | |
| 170 | n = htx_get_blk_name(htx, blk); |
| 171 | if (!regex_exec2(re, n.ptr, n.len)) |
| 172 | goto next_blk; |
| 173 | } |
| 174 | else { |
| 175 | const struct ist name = *(const struct ist *)(pattern); |
| 176 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 177 | /* If no name was passed, we want any header. So skip the comparison */ |
Christopher Faulet | 8dd33e1 | 2020-05-05 07:42:42 +0200 | [diff] [blame] | 178 | if (!istlen(name)) |
| 179 | goto match; |
| 180 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 181 | n = htx_get_blk_name(htx, blk); |
Christopher Faulet | 8dd33e1 | 2020-05-05 07:42:42 +0200 | [diff] [blame] | 182 | switch (flags & HTTP_FIND_FL_MATCH_TYPE) { |
| 183 | case HTTP_FIND_FL_MATCH_STR: |
| 184 | if (!isteqi(n, name)) |
| 185 | goto next_blk; |
| 186 | break; |
| 187 | case HTTP_FIND_FL_MATCH_PFX: |
| 188 | if (istlen(n) < istlen(name)) |
| 189 | goto next_blk; |
| 190 | |
| 191 | n = ist2(istptr(n), istlen(name)); |
| 192 | if (!isteqi(n, name)) |
| 193 | goto next_blk; |
| 194 | break; |
| 195 | case HTTP_FIND_FL_MATCH_SFX: |
| 196 | if (istlen(n) < istlen(name)) |
| 197 | goto next_blk; |
| 198 | |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 199 | n = ist2(istend(n) - istlen(name), |
| 200 | istlen(name)); |
Christopher Faulet | 8dd33e1 | 2020-05-05 07:42:42 +0200 | [diff] [blame] | 201 | if (!isteqi(n, name)) |
| 202 | goto next_blk; |
| 203 | break; |
| 204 | case HTTP_FIND_FL_MATCH_SUB: |
Maciej Zdeb | 302b9f8 | 2020-11-20 12:12:24 +0000 | [diff] [blame] | 205 | if (!strnistr(n.ptr, n.len, name.ptr, name.len)) |
Christopher Faulet | 8dd33e1 | 2020-05-05 07:42:42 +0200 | [diff] [blame] | 206 | goto next_blk; |
| 207 | break; |
| 208 | default: |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 209 | goto next_blk; |
Christopher Faulet | 8dd33e1 | 2020-05-05 07:42:42 +0200 | [diff] [blame] | 210 | break; |
| 211 | } |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 212 | } |
Christopher Faulet | 8dd33e1 | 2020-05-05 07:42:42 +0200 | [diff] [blame] | 213 | match: |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 214 | v = htx_get_blk_value(htx, blk); |
| 215 | |
| 216 | return_hdr: |
| 217 | ctx->lws_before = 0; |
| 218 | ctx->lws_after = 0; |
| 219 | while (v.len && HTTP_IS_LWS(*v.ptr)) { |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 220 | v = istnext(v); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 221 | ctx->lws_before++; |
| 222 | } |
Christopher Faulet | 8dd33e1 | 2020-05-05 07:42:42 +0200 | [diff] [blame] | 223 | if (!(flags & HTTP_FIND_FL_FULL)) |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 224 | v.len = http_find_hdr_value_end(v.ptr, istend(v)) - v.ptr; |
| 225 | |
| 226 | while (v.len && HTTP_IS_LWS(*(istend(v) - 1))) { |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 227 | v.len--; |
| 228 | ctx->lws_after++; |
| 229 | } |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 230 | ctx->blk = blk; |
| 231 | ctx->value = v; |
| 232 | return 1; |
| 233 | |
| 234 | next_blk: |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 235 | ; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | ctx->blk = NULL; |
| 239 | ctx->value = ist(""); |
| 240 | ctx->lws_before = ctx->lws_after = 0; |
| 241 | return 0; |
| 242 | } |
| 243 | |
Christopher Faulet | 8dd33e1 | 2020-05-05 07:42:42 +0200 | [diff] [blame] | 244 | |
| 245 | /* Header names must match <name> */ |
| 246 | int http_find_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full) |
| 247 | { |
| 248 | return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0)); |
| 249 | } |
| 250 | |
| 251 | /* Header names must match <name>. Same than http_find_header */ |
| 252 | int http_find_str_header(const struct htx *htx, const struct ist name, struct http_hdr_ctx *ctx, int full) |
| 253 | { |
| 254 | return __http_find_header(htx, &name, ctx, HTTP_FIND_FL_MATCH_STR | (full ? HTTP_FIND_FL_FULL : 0)); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /* Header names must start with <prefix> */ |
| 259 | int http_find_pfx_header(const struct htx *htx, const struct ist prefix, struct http_hdr_ctx *ctx, int full) |
| 260 | { |
| 261 | return __http_find_header(htx, &prefix, ctx, HTTP_FIND_FL_MATCH_PFX | (full ? HTTP_FIND_FL_FULL : 0)); |
| 262 | } |
| 263 | |
| 264 | /* Header names must end with <suffix> */ |
| 265 | int http_find_sfx_header(const struct htx *htx, const struct ist suffix, struct http_hdr_ctx *ctx, int full) |
| 266 | { |
| 267 | return __http_find_header(htx, &suffix, ctx, HTTP_FIND_FL_MATCH_SFX | (full ? HTTP_FIND_FL_FULL : 0)); |
| 268 | } |
| 269 | /* Header names must contain <sub> */ |
| 270 | int http_find_sub_header(const struct htx *htx, const struct ist sub, struct http_hdr_ctx *ctx, int full) |
| 271 | { |
| 272 | return __http_find_header(htx, &sub, ctx, HTTP_FIND_FL_MATCH_SUB | (full ? HTTP_FIND_FL_FULL : 0)); |
| 273 | } |
| 274 | |
| 275 | /* Header names must match <re> regex*/ |
| 276 | int http_match_header(const struct htx *htx, const struct my_regex *re, struct http_hdr_ctx *ctx, int full) |
| 277 | { |
| 278 | return __http_find_header(htx, re, ctx, HTTP_FIND_FL_MATCH_REG | (full ? HTTP_FIND_FL_FULL : 0)); |
| 279 | } |
| 280 | |
| 281 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 282 | /* Adds a header block int the HTX message <htx>, just before the EOH block. It |
| 283 | * returns 1 on success, otherwise it returns 0. |
| 284 | */ |
| 285 | int http_add_header(struct htx *htx, const struct ist n, const struct ist v) |
| 286 | { |
| 287 | struct htx_blk *blk; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 288 | struct htx_sl *sl; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 289 | enum htx_blk_type type = htx_get_tail_type(htx); |
| 290 | int32_t prev; |
| 291 | |
| 292 | blk = htx_add_header(htx, n, v); |
| 293 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 294 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 295 | |
| 296 | if (unlikely(type < HTX_BLK_EOH)) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 297 | goto end; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 298 | |
| 299 | /* <blk> is the head, swap it iteratively with its predecessor to place |
| 300 | * it just before the end-of-header block. So blocks remains ordered. */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 301 | for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) { |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 302 | struct htx_blk *pblk = htx_get_blk(htx, prev); |
| 303 | enum htx_blk_type type = htx_get_blk_type(pblk); |
| 304 | |
| 305 | /* Swap .addr and .info fields */ |
| 306 | blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr; |
| 307 | blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info; |
| 308 | |
| 309 | if (blk->addr == pblk->addr) |
| 310 | blk->addr += htx_get_blksz(pblk); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 311 | |
| 312 | /* Stop when end-of-header is reached */ |
| 313 | if (type == HTX_BLK_EOH) |
| 314 | break; |
| 315 | |
| 316 | blk = pblk; |
| 317 | } |
Christopher Faulet | 05aab64 | 2019-04-11 13:43:57 +0200 | [diff] [blame] | 318 | |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 319 | end: |
| 320 | sl = http_get_stline(htx); |
Christopher Faulet | 3e1f7f4 | 2020-02-28 09:47:07 +0100 | [diff] [blame] | 321 | if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(n, ist("host"))) { |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 322 | if (!http_update_authority(htx, sl, v)) |
| 323 | goto fail; |
| 324 | } |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 325 | return 1; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 326 | |
| 327 | fail: |
| 328 | return 0; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 329 | } |
| 330 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 331 | /* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 332 | * success, otherwise it returns 0. |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 333 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 334 | int http_replace_stline(struct htx *htx, const struct ist p1, const struct ist p2, const struct ist p3) |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 335 | { |
Christopher Faulet | 7b7d507 | 2019-05-13 15:22:59 +0200 | [diff] [blame] | 336 | struct htx_blk *blk; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 337 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 338 | blk = htx_get_first_blk(htx); |
| 339 | if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3)) |
Christopher Faulet | 7b7d507 | 2019-05-13 15:22:59 +0200 | [diff] [blame] | 340 | return 0; |
| 341 | return 1; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 344 | /* Replace the request method in the HTX message <htx> by <meth>. It returns 1 |
| 345 | * on success, otherwise 0. |
| 346 | */ |
| 347 | int http_replace_req_meth(struct htx *htx, const struct ist meth) |
| 348 | { |
| 349 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 350 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 351 | struct ist uri, vsn; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 352 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 353 | if (!sl) |
| 354 | return 0; |
| 355 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 356 | /* Start by copying old uri and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 357 | chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */ |
| 358 | uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 359 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 360 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 361 | vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 362 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 363 | /* create the new start line */ |
| 364 | sl->info.req.meth = find_http_meth(meth.ptr, meth.len); |
| 365 | return http_replace_stline(htx, meth, uri, vsn); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on |
| 369 | * success, otherwise 0. |
| 370 | */ |
| 371 | int http_replace_req_uri(struct htx *htx, const struct ist uri) |
| 372 | { |
| 373 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 374 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 375 | struct ist meth, vsn; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 376 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 377 | if (!sl) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 378 | goto fail; |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 379 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 380 | /* Start by copying old method and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 381 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 382 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 383 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 384 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 385 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 386 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 387 | /* create the new start line */ |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 388 | if (!http_replace_stline(htx, meth, uri, vsn)) |
| 389 | goto fail; |
| 390 | |
| 391 | sl = http_get_stline(htx); |
| 392 | if (!http_update_host(htx, sl, uri)) |
| 393 | goto fail; |
| 394 | |
| 395 | return 1; |
| 396 | fail: |
| 397 | return 0; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 398 | } |
| 399 | |
Christopher Faulet | b8ce505 | 2020-08-31 16:11:57 +0200 | [diff] [blame] | 400 | /* Replace the request path in the HTX message <htx> by <path>. The host part is |
| 401 | * preserverd. if <with_qs> is set, the query string is evaluated as part of the |
| 402 | * path and replaced. Otherwise, it is preserved too. It returns 1 on success, |
| 403 | * otherwise 0. |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 404 | */ |
Christopher Faulet | b8ce505 | 2020-08-31 16:11:57 +0200 | [diff] [blame] | 405 | int http_replace_req_path(struct htx *htx, const struct ist path, int with_qs) |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 406 | { |
| 407 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 408 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 409 | struct ist meth, uri, vsn, p; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 410 | size_t plen = 0; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 411 | struct http_uri_parser parser; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 412 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 413 | if (!sl) |
| 414 | return 0; |
| 415 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 416 | uri = htx_sl_req_uri(sl); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 417 | parser = http_uri_parser_init(uri); |
| 418 | p = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 419 | if (!isttest(p)) |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 420 | p = uri; |
Christopher Faulet | b8ce505 | 2020-08-31 16:11:57 +0200 | [diff] [blame] | 421 | if (with_qs) |
| 422 | plen = p.len; |
| 423 | else { |
| 424 | while (plen < p.len && *(p.ptr + plen) != '?') |
| 425 | plen++; |
| 426 | } |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 427 | |
| 428 | /* Start by copying old method and version and create the new uri */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 429 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 430 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 431 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 432 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 433 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
| 434 | |
| 435 | chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */ |
Tim Duesterhus | 9f7ed8a | 2021-11-08 09:05:04 +0100 | [diff] [blame] | 436 | chunk_istcat(temp, path); /* uri: new path */ |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 437 | chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 438 | uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 439 | |
| 440 | /* create the new start line */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 441 | return http_replace_stline(htx, meth, uri, vsn); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* Replace the request query-string in the HTX message <htx> by <query>. The |
| 445 | * host part and the path are preserved. It returns 1 on success, otherwise |
| 446 | * 0. |
| 447 | */ |
| 448 | int http_replace_req_query(struct htx *htx, const struct ist query) |
| 449 | { |
| 450 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 451 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 452 | struct ist meth, uri, vsn, q; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 453 | int offset = 1; |
| 454 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 455 | if (!sl) |
| 456 | return 0; |
| 457 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 458 | uri = htx_sl_req_uri(sl); |
| 459 | q = uri; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 460 | while (q.len > 0 && *(q.ptr) != '?') { |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 461 | q = istnext(q); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* skip the question mark or indicate that we must insert it |
| 465 | * (but only if the format string is not empty then). |
| 466 | */ |
| 467 | if (q.len) { |
Tim Duesterhus | 284fbe1 | 2021-11-04 22:35:44 +0100 | [diff] [blame] | 468 | q = istnext(q); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 469 | } |
| 470 | else if (query.len > 1) |
| 471 | offset = 0; |
| 472 | |
| 473 | /* Start by copying old method and version and create the new uri */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 474 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 475 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 476 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 477 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 478 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 479 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 480 | chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */ |
| 481 | chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */ |
| 482 | uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 483 | |
| 484 | /* create the new start line */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 485 | return http_replace_stline(htx, meth, uri, vsn); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* Replace the response status in the HTX message <htx> by <status>. It returns |
| 489 | * 1 on success, otherwise 0. |
| 490 | */ |
Christopher Faulet | bde2c4c | 2020-08-31 16:43:34 +0200 | [diff] [blame] | 491 | int http_replace_res_status(struct htx *htx, const struct ist status, const struct ist reason) |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 492 | { |
| 493 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 494 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | bde2c4c | 2020-08-31 16:43:34 +0200 | [diff] [blame] | 495 | struct ist vsn, r; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 496 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 497 | if (!sl) |
| 498 | return 0; |
| 499 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 500 | /* Start by copying old uri and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 501 | chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */ |
| 502 | vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl)); |
Christopher Faulet | bde2c4c | 2020-08-31 16:43:34 +0200 | [diff] [blame] | 503 | r = reason; |
| 504 | if (!isttest(r)) { |
| 505 | chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */ |
| 506 | r = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl)); |
| 507 | } |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 508 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 509 | /* create the new start line */ |
| 510 | sl->info.res.status = strl2ui(status.ptr, status.len); |
Christopher Faulet | bde2c4c | 2020-08-31 16:43:34 +0200 | [diff] [blame] | 511 | return http_replace_stline(htx, vsn, status, r); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /* Replace the response reason in the HTX message <htx> by <reason>. It returns |
| 515 | * 1 on success, otherwise 0. |
| 516 | */ |
| 517 | int http_replace_res_reason(struct htx *htx, const struct ist reason) |
| 518 | { |
| 519 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 520 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 521 | struct ist vsn, status; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 522 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 523 | if (!sl) |
| 524 | return 0; |
| 525 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 526 | /* Start by copying old uri and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 527 | chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */ |
| 528 | vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 529 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 530 | chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */ |
| 531 | status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 532 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 533 | /* create the new start line */ |
| 534 | return http_replace_stline(htx, vsn, status, reason); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 537 | /* Replaces a part of a header value referenced in the context <ctx> by |
| 538 | * <data>. It returns 1 on success, otherwise it returns 0. The context is |
| 539 | * updated if necessary. |
| 540 | */ |
| 541 | int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data) |
| 542 | { |
| 543 | struct htx_blk *blk = ctx->blk; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 544 | struct htx_sl *sl; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 545 | char *start; |
| 546 | struct ist v; |
| 547 | uint32_t len, off; |
| 548 | |
| 549 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 550 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 551 | |
| 552 | v = htx_get_blk_value(htx, blk); |
| 553 | start = ctx->value.ptr - ctx->lws_before; |
| 554 | len = ctx->lws_before + ctx->value.len + ctx->lws_after; |
| 555 | off = start - v.ptr; |
| 556 | |
| 557 | blk = htx_replace_blk_value(htx, blk, ist2(start, len), data); |
| 558 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 559 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 560 | |
| 561 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 562 | |
| 563 | sl = http_get_stline(htx); |
| 564 | if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) { |
| 565 | struct ist n = htx_get_blk_name(htx, blk); |
| 566 | |
| 567 | if (isteq(n, ist("host"))) { |
| 568 | if (!http_update_authority(htx, sl, v)) |
| 569 | goto fail; |
| 570 | ctx->blk = NULL; |
| 571 | http_find_header(htx, ist("host"), ctx, 1); |
| 572 | blk = ctx->blk; |
| 573 | v = htx_get_blk_value(htx, blk); |
| 574 | } |
| 575 | } |
| 576 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 577 | ctx->blk = blk; |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 578 | ctx->value = ist2(v.ptr + off, data.len); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 579 | ctx->lws_before = ctx->lws_after = 0; |
| 580 | |
| 581 | return 1; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 582 | fail: |
| 583 | return 0; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* Fully replaces a header referenced in the context <ctx> by the name <name> |
| 587 | * with the value <value>. It returns 1 on success, otherwise it returns 0. The |
| 588 | * context is updated if necessary. |
| 589 | */ |
| 590 | int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx, |
| 591 | const struct ist name, const struct ist value) |
| 592 | { |
| 593 | struct htx_blk *blk = ctx->blk; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 594 | struct htx_sl *sl; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 595 | |
| 596 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 597 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 598 | |
| 599 | blk = htx_replace_header(htx, blk, name, value); |
| 600 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 601 | goto fail; |
| 602 | |
| 603 | sl = http_get_stline(htx); |
Christopher Faulet | 3e1f7f4 | 2020-02-28 09:47:07 +0100 | [diff] [blame] | 604 | if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteqi(name, ist("host"))) { |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 605 | if (!http_update_authority(htx, sl, value)) |
| 606 | goto fail; |
| 607 | ctx->blk = NULL; |
| 608 | http_find_header(htx, ist("host"), ctx, 1); |
| 609 | blk = ctx->blk; |
| 610 | } |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 611 | |
| 612 | ctx->blk = blk; |
| 613 | ctx->value = ist(NULL); |
| 614 | ctx->lws_before = ctx->lws_after = 0; |
| 615 | |
| 616 | return 1; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 617 | fail: |
| 618 | return 0; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /* Remove one value of a header. This only works on a <ctx> returned by |
| 622 | * http_find_header function. The value is removed, as well as surrounding commas |
| 623 | * if any. If the removed value was alone, the whole header is removed. The |
| 624 | * <ctx> is always updated accordingly, as well as the HTX message <htx>. It |
| 625 | * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a |
| 626 | * form that can be handled by http_find_header() to find next occurrence. |
| 627 | */ |
| 628 | int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx) |
| 629 | { |
| 630 | struct htx_blk *blk = ctx->blk; |
| 631 | char *start; |
| 632 | struct ist v; |
| 633 | uint32_t len; |
| 634 | |
| 635 | if (!blk) |
| 636 | return 0; |
| 637 | |
| 638 | start = ctx->value.ptr - ctx->lws_before; |
| 639 | len = ctx->lws_before + ctx->value.len + ctx->lws_after; |
| 640 | |
| 641 | v = htx_get_blk_value(htx, blk); |
| 642 | if (len == v.len) { |
| 643 | blk = htx_remove_blk(htx, blk); |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 644 | if (blk || htx_is_empty(htx)) { |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 645 | ctx->blk = blk; |
Tim Duesterhus | 241e29e | 2020-03-05 17:56:30 +0100 | [diff] [blame] | 646 | ctx->value = IST_NULL; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 647 | ctx->lws_before = ctx->lws_after = 0; |
| 648 | } |
| 649 | else { |
| 650 | ctx->blk = htx_get_blk(htx, htx->tail); |
| 651 | ctx->value = htx_get_blk_value(htx, ctx->blk); |
| 652 | ctx->lws_before = ctx->lws_after = 0; |
| 653 | } |
| 654 | return 1; |
| 655 | } |
| 656 | |
| 657 | /* This was not the only value of this header. We have to remove the |
| 658 | * part pointed by ctx->value. If it is the last entry of the list, we |
| 659 | * remove the last separator. |
| 660 | */ |
| 661 | if (start == v.ptr) { |
| 662 | /* It's the first header part but not the only one. So remove |
| 663 | * the comma after it. */ |
| 664 | len++; |
| 665 | } |
| 666 | else { |
| 667 | /* There is at least one header part before the removed one. So |
| 668 | * remove the comma between them. */ |
| 669 | start--; |
| 670 | len++; |
| 671 | } |
| 672 | /* Update the block content and its len */ |
| 673 | memmove(start, start+len, v.len-len); |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 674 | htx_change_blk_value_len(htx, blk, v.len-len); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 675 | |
| 676 | /* Finally update the ctx */ |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 677 | ctx->value = ist2(start, 0); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 678 | ctx->lws_before = ctx->lws_after = 0; |
| 679 | |
| 680 | return 1; |
| 681 | } |
Christopher Faulet | 7ff1cea | 2018-10-24 10:39:35 +0200 | [diff] [blame] | 682 | |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 683 | /* Updates the authority part of the uri with the value <host>. It happens when |
| 684 | * the header host is modified. It returns 0 on failure and 1 on success. It is |
| 685 | * the caller responsibility to provide the start-line and to be sure the uri |
| 686 | * contains an authority. Thus, if no authority is found in the uri, an error is |
| 687 | * returned. |
| 688 | */ |
Christopher Faulet | 1543d44 | 2020-04-28 19:57:29 +0200 | [diff] [blame] | 689 | int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 690 | { |
| 691 | struct buffer *temp = get_trash_chunk(); |
| 692 | struct ist meth, vsn, uri, authority; |
Amaury Denoyelle | 69294b2 | 2021-07-06 11:02:22 +0200 | [diff] [blame] | 693 | struct http_uri_parser parser; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 694 | |
| 695 | uri = htx_sl_req_uri(sl); |
Amaury Denoyelle | 69294b2 | 2021-07-06 11:02:22 +0200 | [diff] [blame] | 696 | parser = http_uri_parser_init(uri); |
| 697 | authority = http_parse_authority(&parser, 1); |
Christopher Faulet | 34b18e4 | 2020-02-18 11:02:21 +0100 | [diff] [blame] | 698 | if (!authority.len) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 699 | return 0; |
| 700 | |
Christopher Faulet | 34b18e4 | 2020-02-18 11:02:21 +0100 | [diff] [blame] | 701 | /* Don't update the uri if there is no change */ |
| 702 | if (isteq(host, authority)) |
| 703 | return 1; |
| 704 | |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 705 | /* Start by copying old method and version */ |
| 706 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 707 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
| 708 | |
| 709 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 710 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
| 711 | |
| 712 | chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr); |
Tim Duesterhus | 9f7ed8a | 2021-11-08 09:05:04 +0100 | [diff] [blame] | 713 | chunk_istcat(temp, host); |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 714 | chunk_memcat(temp, istend(authority), istend(uri) - istend(authority)); |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 715 | uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */ |
| 716 | |
| 717 | return http_replace_stline(htx, meth, uri, vsn); |
| 718 | |
| 719 | } |
| 720 | |
| 721 | /* Update the header host by extracting the authority of the uri <uri>. flags of |
| 722 | * the start-line are also updated accordingly. For orgin-form and asterisk-form |
| 723 | * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is |
| 724 | * removed from the flags of the start-line. Otherwise, this flag is set and the |
| 725 | * authority is used to set the value of the header host. This function returns |
| 726 | * 0 on failure and 1 on success. |
| 727 | */ |
Christopher Faulet | 1543d44 | 2020-04-28 19:57:29 +0200 | [diff] [blame] | 728 | int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 729 | { |
| 730 | struct ist authority; |
| 731 | struct http_hdr_ctx ctx; |
Amaury Denoyelle | 69294b2 | 2021-07-06 11:02:22 +0200 | [diff] [blame] | 732 | struct http_uri_parser parser = http_uri_parser_init(uri); |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 733 | |
Amaury Denoyelle | 69294b2 | 2021-07-06 11:02:22 +0200 | [diff] [blame] | 734 | if (parser.format == URI_PARSER_FORMAT_EMPTY || |
| 735 | parser.format == URI_PARSER_FORMAT_ASTERISK || |
| 736 | parser.format == URI_PARSER_FORMAT_ABSPATH) { |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 737 | sl->flags &= ~HTX_SL_F_HAS_AUTHORITY; |
| 738 | } |
| 739 | else { |
| 740 | sl->flags |= HTX_SL_F_HAS_AUTHORITY; |
| 741 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 742 | // absolute-form (RFC7320 #5.3.2) |
| 743 | sl->flags |= HTX_SL_F_HAS_SCHM; |
| 744 | if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h') |
| 745 | sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS); |
| 746 | |
Amaury Denoyelle | 69294b2 | 2021-07-06 11:02:22 +0200 | [diff] [blame] | 747 | authority = http_parse_authority(&parser, 1); |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 748 | if (!authority.len) |
| 749 | goto fail; |
| 750 | } |
| 751 | else { |
| 752 | // authority-form (RFC7320 #5.3.3) |
| 753 | authority = uri; |
| 754 | } |
| 755 | |
| 756 | /* Replace header host value */ |
| 757 | ctx.blk = NULL; |
| 758 | while (http_find_header(htx, ist("host"), &ctx, 1)) { |
| 759 | if (!http_replace_header_value(htx, &ctx, authority)) |
| 760 | goto fail; |
| 761 | } |
| 762 | |
| 763 | } |
| 764 | return 1; |
| 765 | fail: |
| 766 | return 0; |
| 767 | } |
Christopher Faulet | 7ff1cea | 2018-10-24 10:39:35 +0200 | [diff] [blame] | 768 | |
| 769 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 770 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 771 | * performed over the whole headers. Otherwise it must contain a valid header |
| 772 | * context, initialised with ctx->blk=NULL for the first lookup in a series. If |
| 773 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 774 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 775 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 776 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 777 | * list headers. |
| 778 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 779 | */ |
| 780 | unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr, |
| 781 | int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen) |
| 782 | { |
| 783 | struct http_hdr_ctx local_ctx; |
| 784 | struct ist val_hist[MAX_HDR_HISTORY]; |
| 785 | unsigned int hist_idx; |
| 786 | int found; |
| 787 | |
| 788 | if (!ctx) { |
| 789 | local_ctx.blk = NULL; |
| 790 | ctx = &local_ctx; |
| 791 | } |
| 792 | |
| 793 | if (occ >= 0) { |
| 794 | /* search from the beginning */ |
| 795 | while (http_find_header(htx, hdr, ctx, 0)) { |
| 796 | occ--; |
| 797 | if (occ <= 0) { |
| 798 | *vptr = ctx->value.ptr; |
| 799 | *vlen = ctx->value.len; |
| 800 | return 1; |
| 801 | } |
| 802 | } |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | /* negative occurrence, we scan all the list then walk back */ |
| 807 | if (-occ > MAX_HDR_HISTORY) |
| 808 | return 0; |
| 809 | |
| 810 | found = hist_idx = 0; |
| 811 | while (http_find_header(htx, hdr, ctx, 0)) { |
| 812 | val_hist[hist_idx] = ctx->value; |
| 813 | if (++hist_idx >= MAX_HDR_HISTORY) |
| 814 | hist_idx = 0; |
| 815 | found++; |
| 816 | } |
| 817 | if (-occ > found) |
| 818 | return 0; |
| 819 | |
| 820 | /* OK now we have the last occurrence in [hist_idx-1], and we need to |
| 821 | * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have |
| 822 | * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ] |
| 823 | * to remain in the 0..9 range. |
| 824 | */ |
| 825 | hist_idx += occ + MAX_HDR_HISTORY; |
| 826 | if (hist_idx >= MAX_HDR_HISTORY) |
| 827 | hist_idx -= MAX_HDR_HISTORY; |
| 828 | *vptr = val_hist[hist_idx].ptr; |
| 829 | *vlen = val_hist[hist_idx].len; |
| 830 | return 1; |
| 831 | } |
| 832 | |
| 833 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 834 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 835 | * performed over the whole headers. Otherwise it must contain a valid header |
| 836 | * context, initialised with ctx->blk=NULL for the first lookup in a series. If |
| 837 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 838 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 839 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 840 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 841 | * line header values and does not stop at commas. |
| 842 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 843 | */ |
| 844 | unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr, |
| 845 | int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen) |
| 846 | { |
| 847 | struct http_hdr_ctx local_ctx; |
| 848 | struct ist val_hist[MAX_HDR_HISTORY]; |
| 849 | unsigned int hist_idx; |
| 850 | int found; |
| 851 | |
| 852 | if (!ctx) { |
| 853 | local_ctx.blk = NULL; |
| 854 | ctx = &local_ctx; |
| 855 | } |
| 856 | |
| 857 | if (occ >= 0) { |
| 858 | /* search from the beginning */ |
| 859 | while (http_find_header(htx, hdr, ctx, 1)) { |
| 860 | occ--; |
| 861 | if (occ <= 0) { |
| 862 | *vptr = ctx->value.ptr; |
| 863 | *vlen = ctx->value.len; |
| 864 | return 1; |
| 865 | } |
| 866 | } |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | /* negative occurrence, we scan all the list then walk back */ |
| 871 | if (-occ > MAX_HDR_HISTORY) |
| 872 | return 0; |
| 873 | |
| 874 | found = hist_idx = 0; |
| 875 | while (http_find_header(htx, hdr, ctx, 1)) { |
| 876 | val_hist[hist_idx] = ctx->value; |
| 877 | if (++hist_idx >= MAX_HDR_HISTORY) |
| 878 | hist_idx = 0; |
| 879 | found++; |
| 880 | } |
| 881 | if (-occ > found) |
| 882 | return 0; |
| 883 | |
| 884 | /* OK now we have the last occurrence in [hist_idx-1], and we need to |
| 885 | * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have |
| 886 | * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ] |
| 887 | * to remain in the 0..9 range. |
| 888 | */ |
| 889 | hist_idx += occ + MAX_HDR_HISTORY; |
| 890 | if (hist_idx >= MAX_HDR_HISTORY) |
| 891 | hist_idx -= MAX_HDR_HISTORY; |
| 892 | *vptr = val_hist[hist_idx].ptr; |
| 893 | *vlen = val_hist[hist_idx].len; |
| 894 | return 1; |
| 895 | } |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 896 | |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 897 | int http_str_to_htx(struct buffer *buf, struct ist raw, char **errmsg) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 898 | { |
| 899 | struct htx *htx; |
| 900 | struct htx_sl *sl; |
| 901 | struct h1m h1m; |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 902 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 903 | union h1_sl h1sl; |
| 904 | unsigned int flags = HTX_SL_F_IS_RESP; |
| 905 | int ret = 0; |
| 906 | |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 907 | b_reset(buf); |
| 908 | if (!raw.len) { |
| 909 | buf->size = 0; |
Christopher Faulet | 1cdc028 | 2021-02-05 10:29:29 +0100 | [diff] [blame] | 910 | buf->area = NULL; |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 911 | return 1; |
| 912 | } |
| 913 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 914 | buf->size = global.tune.bufsize; |
Tim Duesterhus | 403fd72 | 2021-04-08 20:05:23 +0200 | [diff] [blame] | 915 | buf->area = malloc(buf->size); |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 916 | if (!buf->area) |
| 917 | goto error; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 918 | |
| 919 | h1m_init_res(&h1m); |
| 920 | h1m.flags |= H1_MF_NO_PHDR; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 921 | ret = h1_headers_to_hdr_list(raw.ptr, istend(raw), |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 922 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl); |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 923 | if (ret <= 0) { |
| 924 | memprintf(errmsg, "unabled to parse headers (error offset: %d)", h1m.err_pos); |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 925 | goto error; |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 926 | } |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 927 | |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 928 | if (unlikely(h1sl.st.v.len != 8)) { |
| 929 | memprintf(errmsg, "invalid http version (%.*s)", (int)h1sl.st.v.len, h1sl.st.v.ptr); |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 930 | goto error; |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 931 | } |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 932 | if ((*(h1sl.st.v.ptr + 5) > '1') || |
| 933 | ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1'))) |
| 934 | h1m.flags |= H1_MF_VER_11; |
| 935 | |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 936 | if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102)) { |
| 937 | memprintf(errmsg, "invalid http status code for an error message (%u)", |
| 938 | h1sl.st.status); |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 939 | goto error; |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 940 | } |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 941 | |
Christopher Faulet | b8d148a | 2020-10-09 08:50:26 +0200 | [diff] [blame] | 942 | if (h1sl.st.status == 204 || h1sl.st.status == 304) { |
| 943 | /* Responses known to have no body. */ |
| 944 | h1m.flags &= ~(H1_MF_CLEN|H1_MF_CHNK); |
| 945 | h1m.flags |= H1_MF_XFER_LEN; |
| 946 | h1m.curr_len = h1m.body_len = 0; |
| 947 | } |
| 948 | else if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 949 | h1m.flags |= H1_MF_XFER_LEN; |
| 950 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 951 | if (h1m.flags & H1_MF_VER_11) |
| 952 | flags |= HTX_SL_F_VER_11; |
| 953 | if (h1m.flags & H1_MF_XFER_ENC) |
| 954 | flags |= HTX_SL_F_XFER_ENC; |
Christopher Faulet | b8d148a | 2020-10-09 08:50:26 +0200 | [diff] [blame] | 955 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 956 | flags |= HTX_SL_F_XFER_LEN; |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 957 | if (h1m.flags & H1_MF_CHNK) { |
| 958 | memprintf(errmsg, "chunk-encoded payload not supported"); |
| 959 | goto error; |
| 960 | } |
Christopher Faulet | b8d148a | 2020-10-09 08:50:26 +0200 | [diff] [blame] | 961 | else if (h1m.flags & H1_MF_CLEN) { |
| 962 | flags |= HTX_SL_F_CLEN; |
| 963 | if (h1m.body_len == 0) |
| 964 | flags |= HTX_SL_F_BODYLESS; |
| 965 | } |
| 966 | else |
Christopher Faulet | 0d4ce93 | 2019-10-16 09:09:04 +0200 | [diff] [blame] | 967 | flags |= HTX_SL_F_BODYLESS; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 968 | } |
Christopher Faulet | b8d148a | 2020-10-09 08:50:26 +0200 | [diff] [blame] | 969 | |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 970 | if ((flags & HTX_SL_F_BODYLESS) && raw.len > ret) { |
| 971 | memprintf(errmsg, "message payload not expected"); |
| 972 | goto error; |
| 973 | } |
| 974 | if ((flags & HTX_SL_F_CLEN) && h1m.body_len != (raw.len - ret)) { |
| 975 | memprintf(errmsg, "payload size does not match the announced content-length (%lu != %lu)", |
Willy Tarreau | 431a12c | 2020-11-06 14:24:02 +0100 | [diff] [blame] | 976 | (unsigned long)(raw.len - ret), (unsigned long)h1m.body_len); |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 977 | goto error; |
| 978 | } |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 979 | |
| 980 | htx = htx_from_buf(buf); |
| 981 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r); |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 982 | if (!sl || !htx_add_all_headers(htx, hdrs)) { |
| 983 | memprintf(errmsg, "unable to add headers into the HTX message"); |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 984 | goto error; |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 985 | } |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 986 | sl->info.res.status = h1sl.st.status; |
| 987 | |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 988 | while (raw.len > ret) { |
| 989 | int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret)); |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 990 | if (!sent) { |
| 991 | memprintf(errmsg, "unable to add payload into the HTX message"); |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 992 | goto error; |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 993 | } |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 994 | ret += sent; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 995 | } |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 996 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 997 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 998 | |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 999 | return 1; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 1000 | |
| 1001 | error: |
| 1002 | if (buf->size) |
| 1003 | free(buf->area); |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 1004 | return 0; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 1005 | } |
| 1006 | |
Christopher Faulet | 1863064 | 2020-05-12 18:57:28 +0200 | [diff] [blame] | 1007 | void release_http_reply(struct http_reply *http_reply) |
| 1008 | { |
| 1009 | struct logformat_node *lf, *lfb; |
| 1010 | struct http_reply_hdr *hdr, *hdrb; |
| 1011 | |
| 1012 | if (!http_reply) |
| 1013 | return; |
| 1014 | |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 1015 | ha_free(&http_reply->ctype); |
Christopher Faulet | 1863064 | 2020-05-12 18:57:28 +0200 | [diff] [blame] | 1016 | list_for_each_entry_safe(hdr, hdrb, &http_reply->hdrs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1017 | LIST_DELETE(&hdr->list); |
Christopher Faulet | 1863064 | 2020-05-12 18:57:28 +0200 | [diff] [blame] | 1018 | list_for_each_entry_safe(lf, lfb, &hdr->value, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1019 | LIST_DELETE(&lf->list); |
Christopher Faulet | 1863064 | 2020-05-12 18:57:28 +0200 | [diff] [blame] | 1020 | release_sample_expr(lf->expr); |
| 1021 | free(lf->arg); |
| 1022 | free(lf); |
| 1023 | } |
| 1024 | istfree(&hdr->name); |
| 1025 | free(hdr); |
| 1026 | } |
| 1027 | |
| 1028 | if (http_reply->type == HTTP_REPLY_ERRFILES) { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 1029 | ha_free(&http_reply->body.http_errors); |
Christopher Faulet | 1863064 | 2020-05-12 18:57:28 +0200 | [diff] [blame] | 1030 | } |
| 1031 | else if (http_reply->type == HTTP_REPLY_RAW) |
| 1032 | chunk_destroy(&http_reply->body.obj); |
| 1033 | else if (http_reply->type == HTTP_REPLY_LOGFMT) { |
| 1034 | list_for_each_entry_safe(lf, lfb, &http_reply->body.fmt, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1035 | LIST_DELETE(&lf->list); |
Christopher Faulet | 1863064 | 2020-05-12 18:57:28 +0200 | [diff] [blame] | 1036 | release_sample_expr(lf->expr); |
| 1037 | free(lf->arg); |
| 1038 | free(lf); |
| 1039 | } |
| 1040 | } |
Christopher Faulet | 63d4824 | 2020-05-21 09:59:22 +0200 | [diff] [blame] | 1041 | free(http_reply); |
Christopher Faulet | 1863064 | 2020-05-12 18:57:28 +0200 | [diff] [blame] | 1042 | } |
| 1043 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 1044 | static int http_htx_init(void) |
| 1045 | { |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 1046 | struct buffer chk; |
| 1047 | struct ist raw; |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 1048 | char *errmsg = NULL; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 1049 | int rc; |
| 1050 | int err_code = 0; |
| 1051 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 1052 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 1053 | if (!http_err_msgs[rc]) { |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 1054 | ha_alert("Internal error: no default message defined for HTTP return code %d", rc); |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 1055 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1056 | continue; |
| 1057 | } |
| 1058 | |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 1059 | raw = ist(http_err_msgs[rc]); |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 1060 | if (!http_str_to_htx(&chk, raw, &errmsg)) { |
| 1061 | ha_alert("Internal error: invalid default message for HTTP return code %d: %s.\n", |
| 1062 | http_err_codes[rc], errmsg); |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 1063 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1064 | } |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 1065 | else if (errmsg) { |
| 1066 | ha_warning("invalid default message for HTTP return code %d: %s.\n", http_err_codes[rc], errmsg); |
| 1067 | err_code |= ERR_WARN; |
| 1068 | } |
| 1069 | |
| 1070 | /* Reset errmsg */ |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 1071 | ha_free(&errmsg); |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 1072 | |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 1073 | http_err_chunks[rc] = chk; |
Christopher Faulet | 1b13eca | 2020-05-14 09:54:26 +0200 | [diff] [blame] | 1074 | http_err_replies[rc].type = HTTP_REPLY_ERRMSG; |
| 1075 | http_err_replies[rc].status = http_err_codes[rc]; |
| 1076 | http_err_replies[rc].ctype = NULL; |
| 1077 | LIST_INIT(&http_err_replies[rc].hdrs); |
| 1078 | http_err_replies[rc].body.errmsg = &http_err_chunks[rc]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 1079 | } |
| 1080 | end: |
| 1081 | return err_code; |
| 1082 | } |
| 1083 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1084 | static void http_htx_deinit(void) |
| 1085 | { |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 1086 | struct http_errors *http_errs, *http_errsb; |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 1087 | struct http_reply *http_rep, *http_repb; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1088 | struct ebpt_node *node, *next; |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1089 | struct http_error_msg *http_errmsg; |
Christopher Faulet | de30bb7 | 2020-05-14 10:03:55 +0200 | [diff] [blame] | 1090 | int rc; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1091 | |
| 1092 | node = ebpt_first(&http_error_messages); |
| 1093 | while (node) { |
| 1094 | next = ebpt_next(node); |
| 1095 | ebpt_delete(node); |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1096 | http_errmsg = container_of(node, typeof(*http_errmsg), node); |
| 1097 | chunk_destroy(&http_errmsg->msg); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1098 | free(node->key); |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1099 | free(http_errmsg); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1100 | node = next; |
| 1101 | } |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 1102 | |
| 1103 | list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) { |
| 1104 | free(http_errs->conf.file); |
| 1105 | free(http_errs->id); |
Christopher Faulet | de30bb7 | 2020-05-14 10:03:55 +0200 | [diff] [blame] | 1106 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) |
| 1107 | release_http_reply(http_errs->replies[rc]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1108 | LIST_DELETE(&http_errs->list); |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 1109 | free(http_errs); |
| 1110 | } |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 1111 | |
| 1112 | list_for_each_entry_safe(http_rep, http_repb, &http_replies_list, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1113 | LIST_DELETE(&http_rep->list); |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 1114 | release_http_reply(http_rep); |
| 1115 | } |
Tim Duesterhus | 2b7fa9d | 2022-04-26 23:35:07 +0200 | [diff] [blame] | 1116 | |
| 1117 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) |
| 1118 | chunk_destroy(&http_err_chunks[rc]); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 1121 | REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1122 | REGISTER_POST_DEINIT(http_htx_deinit); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 1123 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1124 | /* Reads content of the error file <file> and convert it into an HTX message. On |
| 1125 | * success, the HTX message is returned. On error, NULL is returned and an error |
| 1126 | * message is written into the <errmsg> buffer. |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1127 | */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1128 | struct buffer *http_load_errorfile(const char *file, char **errmsg) |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1129 | { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1130 | struct buffer *buf = NULL; |
| 1131 | struct buffer chk; |
| 1132 | struct ebpt_node *node; |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1133 | struct http_error_msg *http_errmsg; |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1134 | struct stat stat; |
| 1135 | char *err = NULL; |
| 1136 | int errnum, errlen; |
| 1137 | int fd = -1; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1138 | |
| 1139 | /* already loaded */ |
| 1140 | node = ebis_lookup_len(&http_error_messages, file, strlen(file)); |
| 1141 | if (node) { |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1142 | http_errmsg = container_of(node, typeof(*http_errmsg), node); |
| 1143 | buf = &http_errmsg->msg; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1144 | goto out; |
| 1145 | } |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1146 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1147 | /* Read the error file content */ |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1148 | fd = open(file, O_RDONLY); |
| 1149 | if ((fd < 0) || (fstat(fd, &stat) < 0)) { |
| 1150 | memprintf(errmsg, "error opening file '%s'.", file); |
| 1151 | goto out; |
| 1152 | } |
| 1153 | |
| 1154 | if (stat.st_size <= global.tune.bufsize) |
| 1155 | errlen = stat.st_size; |
| 1156 | else { |
| 1157 | ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n", |
| 1158 | file, global.tune.bufsize); |
| 1159 | errlen = global.tune.bufsize; |
| 1160 | } |
| 1161 | |
| 1162 | err = malloc(errlen); |
| 1163 | if (!err) { |
| 1164 | memprintf(errmsg, "out of memory."); |
| 1165 | goto out; |
| 1166 | } |
| 1167 | |
| 1168 | errnum = read(fd, err, errlen); |
| 1169 | if (errnum != errlen) { |
| 1170 | memprintf(errmsg, "error reading file '%s'.", file); |
| 1171 | goto out; |
| 1172 | } |
| 1173 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1174 | /* Create the node corresponding to the error file */ |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1175 | http_errmsg = calloc(1, sizeof(*http_errmsg)); |
| 1176 | if (!http_errmsg) { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1177 | memprintf(errmsg, "out of memory."); |
| 1178 | goto out; |
| 1179 | } |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1180 | http_errmsg->node.key = strdup(file); |
| 1181 | if (!http_errmsg->node.key) { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1182 | memprintf(errmsg, "out of memory."); |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1183 | free(http_errmsg); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1184 | goto out; |
| 1185 | } |
| 1186 | |
| 1187 | /* Convert the error file into an HTX message */ |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 1188 | if (!http_str_to_htx(&chk, ist2(err, errlen), errmsg)) { |
| 1189 | memprintf(errmsg, "'%s': %s", file, *errmsg); |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1190 | free(http_errmsg->node.key); |
| 1191 | free(http_errmsg); |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1192 | goto out; |
| 1193 | } |
| 1194 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1195 | /* Insert the node in the tree and return the HTX message */ |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1196 | http_errmsg->msg = chk; |
| 1197 | ebis_insert(&http_error_messages, &http_errmsg->node); |
| 1198 | buf = &http_errmsg->msg; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1199 | |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1200 | out: |
| 1201 | if (fd >= 0) |
| 1202 | close(fd); |
| 1203 | free(err); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1204 | return buf; |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1205 | } |
| 1206 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 1207 | /* Convert the raw http message <msg> into an HTX message. On success, the HTX |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1208 | * message is returned. On error, NULL is returned and an error message is |
| 1209 | * written into the <errmsg> buffer. |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1210 | */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1211 | struct buffer *http_load_errormsg(const char *key, const struct ist msg, char **errmsg) |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1212 | { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1213 | struct buffer *buf = NULL; |
| 1214 | struct buffer chk; |
| 1215 | struct ebpt_node *node; |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1216 | struct http_error_msg *http_errmsg; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1217 | |
| 1218 | /* already loaded */ |
| 1219 | node = ebis_lookup_len(&http_error_messages, key, strlen(key)); |
| 1220 | if (node) { |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1221 | http_errmsg = container_of(node, typeof(*http_errmsg), node); |
| 1222 | buf = &http_errmsg->msg; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1223 | goto out; |
| 1224 | } |
| 1225 | /* Create the node corresponding to the error file */ |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1226 | http_errmsg = calloc(1, sizeof(*http_errmsg)); |
| 1227 | if (!http_errmsg) { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1228 | memprintf(errmsg, "out of memory."); |
| 1229 | goto out; |
| 1230 | } |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1231 | http_errmsg->node.key = strdup(key); |
| 1232 | if (!http_errmsg->node.key) { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1233 | memprintf(errmsg, "out of memory."); |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1234 | free(http_errmsg); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1235 | goto out; |
| 1236 | } |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1237 | |
| 1238 | /* Convert the error file into an HTX message */ |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 1239 | if (!http_str_to_htx(&chk, msg, errmsg)) { |
| 1240 | memprintf(errmsg, "invalid error message: %s", *errmsg); |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1241 | free(http_errmsg->node.key); |
| 1242 | free(http_errmsg); |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1243 | goto out; |
| 1244 | } |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1245 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1246 | /* Insert the node in the tree and return the HTX message */ |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 1247 | http_errmsg->msg = chk; |
| 1248 | ebis_insert(&http_error_messages, &http_errmsg->node); |
| 1249 | buf = &http_errmsg->msg; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1250 | out: |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1251 | return buf; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1252 | } |
| 1253 | |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1254 | /* This function parses the raw HTTP error file <file> for the status code |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1255 | * <status>. It returns NULL if there is any error, otherwise it return the |
| 1256 | * corresponding HTX message. |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1257 | */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1258 | struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg) |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1259 | { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1260 | struct buffer *buf = NULL; |
| 1261 | int rc; |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1262 | |
| 1263 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 1264 | if (http_err_codes[rc] == status) { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1265 | buf = http_load_errorfile(file, errmsg); |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1266 | break; |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | if (rc >= HTTP_ERR_SIZE) |
| 1271 | memprintf(errmsg, "status code '%d' not handled.", status); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1272 | return buf; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* This function creates HTX error message corresponding to a redirect message |
| 1276 | * for the status code <status>. <url> is used as location url for the |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1277 | * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It |
| 1278 | * returns NULL if there is any error, otherwise it return the corresponding HTX |
| 1279 | * message. |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1280 | */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1281 | struct buffer *http_parse_errorloc(int errloc, int status, const char *url, char **errmsg) |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1282 | { |
Christopher Faulet | 0bac4cd | 2020-05-27 10:11:59 +0200 | [diff] [blame] | 1283 | static const char *HTTP_302 = |
| 1284 | "HTTP/1.1 302 Found\r\n" |
| 1285 | "Cache-Control: no-cache\r\n" |
| 1286 | "Content-length: 0\r\n" |
| 1287 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 1288 | static const char *HTTP_303 = |
| 1289 | "HTTP/1.1 303 See Other\r\n" |
| 1290 | "Cache-Control: no-cache\r\n" |
| 1291 | "Content-length: 0\r\n" |
| 1292 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 1293 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1294 | struct buffer *buf = NULL; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1295 | const char *msg; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1296 | char *key = NULL, *err = NULL; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1297 | int rc, errlen; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1298 | |
| 1299 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 1300 | if (http_err_codes[rc] == status) { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1301 | /* Create the error key */ |
| 1302 | if (!memprintf(&key, "errorloc%d %s", errloc, url)) { |
| 1303 | memprintf(errmsg, "out of memory."); |
| 1304 | goto out; |
| 1305 | } |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1306 | /* Create the error message */ |
| 1307 | msg = (errloc == 302 ? HTTP_302 : HTTP_303); |
| 1308 | errlen = strlen(msg) + strlen(url) + 5; |
| 1309 | err = malloc(errlen); |
| 1310 | if (!err) { |
| 1311 | memprintf(errmsg, "out of memory."); |
| 1312 | goto out; |
| 1313 | } |
| 1314 | errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url); |
| 1315 | |
| 1316 | /* Load it */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1317 | buf = http_load_errormsg(key, ist2(err, errlen), errmsg); |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1318 | break; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | if (rc >= HTTP_ERR_SIZE) |
| 1323 | memprintf(errmsg, "status code '%d' not handled.", status); |
| 1324 | out: |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1325 | free(key); |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1326 | free(err); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1327 | return buf; |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1328 | } |
| 1329 | |
Christopher Faulet | 7eea241 | 2020-05-13 15:02:59 +0200 | [diff] [blame] | 1330 | /* Check an "http reply" and, for replies referencing an http-errors section, |
| 1331 | * try to find the right section and the right error message in this section. If |
| 1332 | * found, the reply is updated. If the http-errors section exists but the error |
| 1333 | * message is not found, no error message is set to fallback on the default |
| 1334 | * ones. Otherwise (unknown section) an error is returned. |
| 1335 | * |
| 1336 | * The function returns 1 in success case, otherwise, it returns 0 and errmsg is |
| 1337 | * filled. |
| 1338 | */ |
| 1339 | int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **errmsg) |
| 1340 | { |
| 1341 | struct http_errors *http_errs; |
| 1342 | int ret = 1; |
| 1343 | |
| 1344 | if (reply->type != HTTP_REPLY_ERRFILES) |
| 1345 | goto end; |
| 1346 | |
| 1347 | list_for_each_entry(http_errs, &http_errors_list, list) { |
| 1348 | if (strcmp(http_errs->id, reply->body.http_errors) == 0) { |
Christopher Faulet | e29a97e | 2020-05-14 14:49:25 +0200 | [diff] [blame] | 1349 | reply->type = HTTP_REPLY_INDIRECT; |
Christopher Faulet | 7eea241 | 2020-05-13 15:02:59 +0200 | [diff] [blame] | 1350 | free(reply->body.http_errors); |
Christopher Faulet | e29a97e | 2020-05-14 14:49:25 +0200 | [diff] [blame] | 1351 | reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)]; |
| 1352 | if (!reply->body.reply) |
Christopher Faulet | 7eea241 | 2020-05-13 15:02:59 +0200 | [diff] [blame] | 1353 | ha_warning("Proxy '%s': status '%d' referenced by an http reply " |
| 1354 | "not declared in http-errors section '%s'.\n", |
| 1355 | px->id, reply->status, http_errs->id); |
| 1356 | break; |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | if (&http_errs->list == &http_errors_list) { |
| 1361 | memprintf(errmsg, "unknown http-errors section '%s' referenced by an http reply ", |
| 1362 | reply->body.http_errors); |
| 1363 | ret = 0; |
| 1364 | } |
| 1365 | |
| 1366 | end: |
| 1367 | return ret; |
| 1368 | } |
| 1369 | |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1370 | /* Parse an "http reply". It returns the reply on success or NULL on error. This |
| 1371 | * function creates one of the following http replies : |
| 1372 | * |
| 1373 | * - HTTP_REPLY_EMPTY : dummy response, no payload |
| 1374 | * - HTTP_REPLY_ERRMSG : implicit error message depending on the status code or explicit one |
| 1375 | * - HTTP_REPLY_ERRFILES : points on an http-errors section (resolved during post-parsing) |
| 1376 | * - HTTP_REPLY_RAW : explicit file object ('file' argument) |
| 1377 | * - HTTP_REPLY_LOGFMT : explicit log-format string ('content' argument) |
| 1378 | * |
| 1379 | * The content-type must be defined for non-empty payload. It is ignored for |
| 1380 | * error messages (implicit or explicit). When an http-errors section is |
| 1381 | * referenced (HTTP_REPLY_ERRFILES), the real error message should be resolved |
| 1382 | * during the configuration validity check or dynamically. It is the caller |
| 1383 | * responsibility to choose. If no status code is configured, <default_status> |
| 1384 | * is set. |
| 1385 | */ |
| 1386 | struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struct proxy *px, |
| 1387 | int default_status, char **errmsg) |
| 1388 | { |
| 1389 | struct logformat_node *lf, *lfb; |
| 1390 | struct http_reply *reply = NULL; |
| 1391 | struct http_reply_hdr *hdr, *hdrb; |
| 1392 | struct stat stat; |
| 1393 | const char *act_arg = NULL; |
| 1394 | char *obj = NULL; |
Christopher Faulet | 7a06ffb | 2021-10-13 17:22:17 +0200 | [diff] [blame] | 1395 | int cur_arg, cap = 0, objlen = 0, fd = -1; |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1396 | |
| 1397 | |
| 1398 | reply = calloc(1, sizeof(*reply)); |
| 1399 | if (!reply) { |
| 1400 | memprintf(errmsg, "out of memory"); |
| 1401 | goto error; |
| 1402 | } |
| 1403 | LIST_INIT(&reply->hdrs); |
| 1404 | reply->type = HTTP_REPLY_EMPTY; |
| 1405 | reply->status = default_status; |
| 1406 | |
Christopher Faulet | 3b967c1 | 2020-05-15 15:47:44 +0200 | [diff] [blame] | 1407 | if (px->conf.args.ctx == ARGC_HERR) |
| 1408 | cap = (SMP_VAL_REQUEST | SMP_VAL_RESPONSE); |
Christopher Faulet | 7a06ffb | 2021-10-13 17:22:17 +0200 | [diff] [blame] | 1409 | else { |
| 1410 | if (px->cap & PR_CAP_FE) |
| 1411 | cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR); |
| 1412 | if (px->cap & PR_CAP_BE) |
Willy Tarreau | b39e47a | 2021-10-16 14:41:09 +0200 | [diff] [blame] | 1413 | cap |= ((px->conf.args.ctx == ARGC_HRQ) ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR); |
Christopher Faulet | 7a06ffb | 2021-10-13 17:22:17 +0200 | [diff] [blame] | 1414 | } |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1415 | |
| 1416 | cur_arg = *orig_arg; |
| 1417 | while (*args[cur_arg]) { |
| 1418 | if (strcmp(args[cur_arg], "status") == 0) { |
| 1419 | cur_arg++; |
| 1420 | if (!*args[cur_arg]) { |
| 1421 | memprintf(errmsg, "'%s' expects <status_code> as argument", args[cur_arg-1]); |
| 1422 | goto error; |
| 1423 | } |
| 1424 | reply->status = atol(args[cur_arg]); |
| 1425 | if (reply->status < 200 || reply->status > 599) { |
| 1426 | memprintf(errmsg, "Unexpected status code '%d'", reply->status); |
| 1427 | goto error; |
| 1428 | } |
| 1429 | cur_arg++; |
| 1430 | } |
| 1431 | else if (strcmp(args[cur_arg], "content-type") == 0) { |
| 1432 | cur_arg++; |
| 1433 | if (!*args[cur_arg]) { |
| 1434 | memprintf(errmsg, "'%s' expects <ctype> as argument", args[cur_arg-1]); |
| 1435 | goto error; |
| 1436 | } |
| 1437 | free(reply->ctype); |
| 1438 | reply->ctype = strdup(args[cur_arg]); |
| 1439 | cur_arg++; |
| 1440 | } |
| 1441 | else if (strcmp(args[cur_arg], "errorfiles") == 0) { |
| 1442 | if (reply->type != HTTP_REPLY_EMPTY) { |
| 1443 | memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg); |
| 1444 | goto error; |
| 1445 | } |
| 1446 | act_arg = args[cur_arg]; |
| 1447 | cur_arg++; |
| 1448 | if (!*args[cur_arg]) { |
| 1449 | memprintf(errmsg, "'%s' expects <name> as argument", args[cur_arg-1]); |
| 1450 | goto error; |
| 1451 | } |
| 1452 | reply->body.http_errors = strdup(args[cur_arg]); |
| 1453 | if (!reply->body.http_errors) { |
| 1454 | memprintf(errmsg, "out of memory"); |
| 1455 | goto error; |
| 1456 | } |
| 1457 | reply->type = HTTP_REPLY_ERRFILES; |
| 1458 | cur_arg++; |
| 1459 | } |
| 1460 | else if (strcmp(args[cur_arg], "default-errorfiles") == 0) { |
| 1461 | if (reply->type != HTTP_REPLY_EMPTY) { |
| 1462 | memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg); |
| 1463 | goto error; |
| 1464 | } |
| 1465 | act_arg = args[cur_arg]; |
| 1466 | reply->type = HTTP_REPLY_ERRMSG; |
| 1467 | cur_arg++; |
| 1468 | } |
| 1469 | else if (strcmp(args[cur_arg], "errorfile") == 0) { |
| 1470 | if (reply->type != HTTP_REPLY_EMPTY) { |
| 1471 | memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg); |
| 1472 | goto error; |
| 1473 | } |
| 1474 | act_arg = args[cur_arg]; |
| 1475 | cur_arg++; |
| 1476 | if (!*args[cur_arg]) { |
| 1477 | memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]); |
| 1478 | goto error; |
| 1479 | } |
| 1480 | reply->body.errmsg = http_load_errorfile(args[cur_arg], errmsg); |
| 1481 | if (!reply->body.errmsg) { |
| 1482 | goto error; |
| 1483 | } |
| 1484 | reply->type = HTTP_REPLY_ERRMSG; |
| 1485 | cur_arg++; |
| 1486 | } |
| 1487 | else if (strcmp(args[cur_arg], "file") == 0) { |
| 1488 | if (reply->type != HTTP_REPLY_EMPTY) { |
| 1489 | memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg); |
| 1490 | goto error; |
| 1491 | } |
| 1492 | act_arg = args[cur_arg]; |
| 1493 | cur_arg++; |
| 1494 | if (!*args[cur_arg]) { |
| 1495 | memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]); |
| 1496 | goto error; |
| 1497 | } |
| 1498 | fd = open(args[cur_arg], O_RDONLY); |
| 1499 | if ((fd < 0) || (fstat(fd, &stat) < 0)) { |
| 1500 | memprintf(errmsg, "error opening file '%s'", args[cur_arg]); |
| 1501 | goto error; |
| 1502 | } |
| 1503 | if (stat.st_size > global.tune.bufsize) { |
| 1504 | memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)", |
| 1505 | args[cur_arg], (long long)stat.st_size, global.tune.bufsize); |
| 1506 | goto error; |
| 1507 | } |
| 1508 | objlen = stat.st_size; |
| 1509 | obj = malloc(objlen); |
| 1510 | if (!obj || read(fd, obj, objlen) != objlen) { |
| 1511 | memprintf(errmsg, "error reading file '%s'", args[cur_arg]); |
| 1512 | goto error; |
| 1513 | } |
| 1514 | close(fd); |
| 1515 | fd = -1; |
| 1516 | reply->type = HTTP_REPLY_RAW; |
| 1517 | chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen); |
| 1518 | obj = NULL; |
| 1519 | cur_arg++; |
| 1520 | } |
| 1521 | else if (strcmp(args[cur_arg], "string") == 0) { |
| 1522 | if (reply->type != HTTP_REPLY_EMPTY) { |
| 1523 | memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg); |
| 1524 | goto error; |
| 1525 | } |
| 1526 | act_arg = args[cur_arg]; |
| 1527 | cur_arg++; |
| 1528 | if (!*args[cur_arg]) { |
| 1529 | memprintf(errmsg, "'%s' expects <str> as argument", args[cur_arg-1]); |
| 1530 | goto error; |
| 1531 | } |
| 1532 | obj = strdup(args[cur_arg]); |
| 1533 | objlen = strlen(args[cur_arg]); |
| 1534 | if (!obj) { |
| 1535 | memprintf(errmsg, "out of memory"); |
| 1536 | goto error; |
| 1537 | } |
| 1538 | reply->type = HTTP_REPLY_RAW; |
| 1539 | chunk_initlen(&reply->body.obj, obj, global.tune.bufsize, objlen); |
| 1540 | obj = NULL; |
| 1541 | cur_arg++; |
| 1542 | } |
| 1543 | else if (strcmp(args[cur_arg], "lf-file") == 0) { |
| 1544 | if (reply->type != HTTP_REPLY_EMPTY) { |
| 1545 | memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg); |
| 1546 | goto error; |
| 1547 | } |
| 1548 | act_arg = args[cur_arg]; |
| 1549 | cur_arg++; |
| 1550 | if (!*args[cur_arg]) { |
| 1551 | memprintf(errmsg, "'%s' expects <file> as argument", args[cur_arg-1]); |
| 1552 | goto error; |
| 1553 | } |
| 1554 | fd = open(args[cur_arg], O_RDONLY); |
| 1555 | if ((fd < 0) || (fstat(fd, &stat) < 0)) { |
| 1556 | memprintf(errmsg, "error opening file '%s'", args[cur_arg]); |
| 1557 | goto error; |
| 1558 | } |
| 1559 | if (stat.st_size > global.tune.bufsize) { |
| 1560 | memprintf(errmsg, "file '%s' exceeds the buffer size (%lld > %d)", |
| 1561 | args[cur_arg], (long long)stat.st_size, global.tune.bufsize); |
| 1562 | goto error; |
| 1563 | } |
| 1564 | objlen = stat.st_size; |
| 1565 | obj = malloc(objlen + 1); |
| 1566 | if (!obj || read(fd, obj, objlen) != objlen) { |
| 1567 | memprintf(errmsg, "error reading file '%s'", args[cur_arg]); |
| 1568 | goto error; |
| 1569 | } |
| 1570 | close(fd); |
| 1571 | fd = -1; |
| 1572 | obj[objlen] = '\0'; |
| 1573 | reply->type = HTTP_REPLY_LOGFMT; |
Christopher Faulet | 5a3d9a7 | 2022-11-14 08:49:28 +0100 | [diff] [blame] | 1574 | LIST_INIT(&reply->body.fmt); |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1575 | cur_arg++; |
| 1576 | } |
| 1577 | else if (strcmp(args[cur_arg], "lf-string") == 0) { |
| 1578 | if (reply->type != HTTP_REPLY_EMPTY) { |
| 1579 | memprintf(errmsg, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg); |
| 1580 | goto error; |
| 1581 | } |
| 1582 | act_arg = args[cur_arg]; |
| 1583 | cur_arg++; |
| 1584 | if (!*args[cur_arg]) { |
| 1585 | memprintf(errmsg, "'%s' expects <fmt> as argument", args[cur_arg-1]); |
| 1586 | goto error; |
| 1587 | } |
| 1588 | obj = strdup(args[cur_arg]); |
| 1589 | objlen = strlen(args[cur_arg]); |
| 1590 | reply->type = HTTP_REPLY_LOGFMT; |
Christopher Faulet | 5a3d9a7 | 2022-11-14 08:49:28 +0100 | [diff] [blame] | 1591 | LIST_INIT(&reply->body.fmt); |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1592 | cur_arg++; |
| 1593 | } |
| 1594 | else if (strcmp(args[cur_arg], "hdr") == 0) { |
| 1595 | cur_arg++; |
| 1596 | if (!*args[cur_arg] || !*args[cur_arg+1]) { |
| 1597 | memprintf(errmsg, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]); |
| 1598 | goto error; |
| 1599 | } |
| 1600 | if (strcasecmp(args[cur_arg], "content-length") == 0 || |
| 1601 | strcasecmp(args[cur_arg], "transfer-encoding") == 0 || |
| 1602 | strcasecmp(args[cur_arg], "content-type") == 0) { |
| 1603 | ha_warning("parsing [%s:%d] : header '%s' always ignored by the http reply.\n", |
| 1604 | px->conf.args.file, px->conf.args.line, args[cur_arg]); |
| 1605 | cur_arg += 2; |
| 1606 | continue; |
| 1607 | } |
| 1608 | hdr = calloc(1, sizeof(*hdr)); |
| 1609 | if (!hdr) { |
| 1610 | memprintf(errmsg, "'%s' : out of memory", args[cur_arg-1]); |
| 1611 | goto error; |
| 1612 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1613 | LIST_APPEND(&reply->hdrs, &hdr->list); |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1614 | LIST_INIT(&hdr->value); |
| 1615 | hdr->name = ist(strdup(args[cur_arg])); |
| 1616 | if (!isttest(hdr->name)) { |
| 1617 | memprintf(errmsg, "out of memory"); |
| 1618 | goto error; |
| 1619 | } |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1620 | if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, errmsg)) |
| 1621 | goto error; |
| 1622 | |
| 1623 | free(px->conf.lfs_file); |
| 1624 | px->conf.lfs_file = strdup(px->conf.args.file); |
| 1625 | px->conf.lfs_line = px->conf.args.line; |
| 1626 | cur_arg += 2; |
| 1627 | } |
| 1628 | else |
| 1629 | break; |
| 1630 | } |
| 1631 | |
| 1632 | if (reply->type == HTTP_REPLY_EMPTY) { /* no payload */ |
| 1633 | if (reply->ctype) { |
| 1634 | ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply because" |
| 1635 | " neither errorfile nor payload defined.\n", |
| 1636 | px->conf.args.file, px->conf.args.line, reply->ctype); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 1637 | ha_free(&reply->ctype); |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1638 | } |
| 1639 | } |
| 1640 | else if (reply->type == HTTP_REPLY_ERRFILES || reply->type == HTTP_REPLY_ERRMSG) { /* errorfiles or errorfile */ |
| 1641 | |
| 1642 | if (reply->type != HTTP_REPLY_ERRMSG || !reply->body.errmsg) { |
| 1643 | /* default errorfile or errorfiles: check the status */ |
| 1644 | int rc; |
| 1645 | |
| 1646 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 1647 | if (http_err_codes[rc] == reply->status) |
| 1648 | break; |
| 1649 | } |
| 1650 | |
| 1651 | if (rc >= HTTP_ERR_SIZE) { |
| 1652 | memprintf(errmsg, "status code '%d' not handled by default with '%s' argument.", |
| 1653 | reply->status, act_arg); |
| 1654 | goto error; |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | if (reply->ctype) { |
| 1659 | ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used " |
| 1660 | "with an erorrfile.\n", |
| 1661 | px->conf.args.file, px->conf.args.line, reply->ctype); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 1662 | ha_free(&reply->ctype); |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1663 | } |
| 1664 | if (!LIST_ISEMPTY(&reply->hdrs)) { |
| 1665 | ha_warning("parsing [%s:%d] : hdr parameters ignored by the http reply when used " |
| 1666 | "with an erorrfile.\n", |
| 1667 | px->conf.args.file, px->conf.args.line); |
| 1668 | list_for_each_entry_safe(hdr, hdrb, &reply->hdrs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1669 | LIST_DELETE(&hdr->list); |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1670 | list_for_each_entry_safe(lf, lfb, &hdr->value, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1671 | LIST_DELETE(&lf->list); |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1672 | release_sample_expr(lf->expr); |
| 1673 | free(lf->arg); |
| 1674 | free(lf); |
| 1675 | } |
| 1676 | istfree(&hdr->name); |
| 1677 | free(hdr); |
| 1678 | } |
| 1679 | } |
| 1680 | } |
| 1681 | else if (reply->type == HTTP_REPLY_RAW) { /* explicit parameter using 'file' parameter*/ |
Christopher Faulet | b8d148a | 2020-10-09 08:50:26 +0200 | [diff] [blame] | 1682 | if ((reply->status == 204 || reply->status == 304) && objlen) { |
| 1683 | memprintf(errmsg, "No body expected for %d responses", reply->status); |
| 1684 | goto error; |
| 1685 | } |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1686 | if (!reply->ctype && objlen) { |
| 1687 | memprintf(errmsg, "a content type must be defined when non-empty payload is configured"); |
| 1688 | goto error; |
| 1689 | } |
| 1690 | if (reply->ctype && !b_data(&reply->body.obj)) { |
| 1691 | ha_warning("parsing [%s:%d] : content-type '%s' ignored by the http reply when used " |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 1692 | "with an empty payload.\n", |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1693 | px->conf.args.file, px->conf.args.line, reply->ctype); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 1694 | ha_free(&reply->ctype); |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1695 | } |
| 1696 | if (b_room(&reply->body.obj) < global.tune.maxrewrite) { |
| 1697 | ha_warning("parsing [%s:%d] : http reply payload runs over the buffer space reserved to headers rewriting." |
| 1698 | " It may lead to internal errors if strict rewriting mode is enabled.\n", |
| 1699 | px->conf.args.file, px->conf.args.line); |
| 1700 | } |
| 1701 | } |
| 1702 | else if (reply->type == HTTP_REPLY_LOGFMT) { /* log-format payload using 'lf-file' of 'lf-string' parameter */ |
| 1703 | LIST_INIT(&reply->body.fmt); |
Christopher Faulet | b8d148a | 2020-10-09 08:50:26 +0200 | [diff] [blame] | 1704 | if ((reply->status == 204 || reply->status == 304)) { |
| 1705 | memprintf(errmsg, "No body expected for %d responses", reply->status); |
| 1706 | goto error; |
| 1707 | } |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 1708 | if (!reply->ctype) { |
| 1709 | memprintf(errmsg, "a content type must be defined with a log-format payload"); |
| 1710 | goto error; |
| 1711 | } |
| 1712 | if (!parse_logformat_string(obj, px, &reply->body.fmt, LOG_OPT_HTTP, cap, errmsg)) |
| 1713 | goto error; |
| 1714 | |
| 1715 | free(px->conf.lfs_file); |
| 1716 | px->conf.lfs_file = strdup(px->conf.args.file); |
| 1717 | px->conf.lfs_line = px->conf.args.line; |
| 1718 | } |
| 1719 | |
| 1720 | free(obj); |
| 1721 | *orig_arg = cur_arg; |
| 1722 | return reply; |
| 1723 | |
| 1724 | error: |
| 1725 | free(obj); |
| 1726 | if (fd >= 0) |
| 1727 | close(fd); |
| 1728 | release_http_reply(reply); |
| 1729 | return NULL; |
| 1730 | } |
| 1731 | |
Amaury Denoyelle | 4c0882b | 2021-07-07 10:49:26 +0200 | [diff] [blame] | 1732 | /* Apply schemed-based normalization as described on rfc3986 on section 6.3.2. |
| 1733 | * Returns 0 if no error has been found else non-zero. |
| 1734 | * |
| 1735 | * The normalization is processed on the target-uri at the condition that it is |
| 1736 | * in absolute-form. In the case where the target-uri was normalized, every |
| 1737 | * host headers values found are also replaced by the normalized hostname. This |
| 1738 | * assumes that the target-uri and host headers were properly identify as |
| 1739 | * similar before calling this function. |
| 1740 | */ |
| 1741 | int http_scheme_based_normalize(struct htx *htx) |
| 1742 | { |
| 1743 | struct http_hdr_ctx ctx; |
| 1744 | struct htx_sl *sl; |
| 1745 | struct ist uri, scheme, authority, host, port; |
Amaury Denoyelle | 8ac8cbf | 2021-07-06 10:52:58 +0200 | [diff] [blame] | 1746 | struct http_uri_parser parser; |
Amaury Denoyelle | 4c0882b | 2021-07-07 10:49:26 +0200 | [diff] [blame] | 1747 | |
| 1748 | sl = http_get_stline(htx); |
| 1749 | |
| 1750 | if (!sl || !(sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_HAS_AUTHORITY))) |
| 1751 | return 0; |
| 1752 | |
| 1753 | uri = htx_sl_req_uri(sl); |
| 1754 | |
Amaury Denoyelle | 8ac8cbf | 2021-07-06 10:52:58 +0200 | [diff] [blame] | 1755 | parser = http_uri_parser_init(uri); |
| 1756 | scheme = http_parse_scheme(&parser); |
Amaury Denoyelle | 4c0882b | 2021-07-07 10:49:26 +0200 | [diff] [blame] | 1757 | /* if no scheme found, no normalization to proceed */ |
| 1758 | if (!isttest(scheme)) |
| 1759 | return 0; |
| 1760 | |
Christopher Faulet | d1d983f | 2022-07-05 10:24:52 +0200 | [diff] [blame] | 1761 | /* Extract the port if present in authority */ |
| 1762 | authority = http_parse_authority(&parser, 1); |
| 1763 | port = http_get_host_port(authority); |
| 1764 | if (!isttest(port)) { |
| 1765 | /* if no port found, no normalization to proceed */ |
Amaury Denoyelle | 4c0882b | 2021-07-07 10:49:26 +0200 | [diff] [blame] | 1766 | return 0; |
Christopher Faulet | d1d983f | 2022-07-05 10:24:52 +0200 | [diff] [blame] | 1767 | } |
| 1768 | host = isttrim(authority, istlen(authority) - istlen(port) - 1); |
Amaury Denoyelle | 4c0882b | 2021-07-07 10:49:26 +0200 | [diff] [blame] | 1769 | |
Christopher Faulet | d1d983f | 2022-07-05 10:24:52 +0200 | [diff] [blame] | 1770 | if (istlen(port) && http_is_default_port(scheme, port)) { |
Amaury Denoyelle | 4c0882b | 2021-07-07 10:49:26 +0200 | [diff] [blame] | 1771 | /* reconstruct the uri with removal of the port */ |
| 1772 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 0eab050 | 2022-07-06 17:41:31 +0200 | [diff] [blame] | 1773 | struct ist meth, vsn; |
Amaury Denoyelle | 4c0882b | 2021-07-07 10:49:26 +0200 | [diff] [blame] | 1774 | |
| 1775 | /* meth */ |
| 1776 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 1777 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
| 1778 | |
| 1779 | /* vsn */ |
| 1780 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 1781 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
| 1782 | |
| 1783 | /* reconstruct uri without port */ |
Christopher Faulet | 0eab050 | 2022-07-06 17:41:31 +0200 | [diff] [blame] | 1784 | chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr); |
Amaury Denoyelle | 4c0882b | 2021-07-07 10:49:26 +0200 | [diff] [blame] | 1785 | chunk_istcat(temp, host); |
Christopher Faulet | 0eab050 | 2022-07-06 17:41:31 +0200 | [diff] [blame] | 1786 | chunk_memcat(temp, istend(authority), istend(uri) - istend(authority)); |
| 1787 | uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */ |
Amaury Denoyelle | 4c0882b | 2021-07-07 10:49:26 +0200 | [diff] [blame] | 1788 | |
| 1789 | http_replace_stline(htx, meth, uri, vsn); |
| 1790 | |
| 1791 | /* replace every host headers values by the normalized host */ |
| 1792 | ctx.blk = NULL; |
| 1793 | while (http_find_header(htx, ist("host"), &ctx, 0)) { |
| 1794 | if (!http_replace_header_value(htx, &ctx, host)) |
| 1795 | goto fail; |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | return 0; |
| 1800 | |
| 1801 | fail: |
| 1802 | return 1; |
| 1803 | } |
| 1804 | |
Amaury Denoyelle | 2c5a7ee | 2022-08-17 16:33:53 +0200 | [diff] [blame] | 1805 | /* First step function to merge multiple cookie headers in a single entry. |
| 1806 | * |
| 1807 | * Use it for each cookie header at <idx> index over HTTP headers in <list>. |
| 1808 | * <first> and <last> are state variables used internally and must be |
| 1809 | * initialized to -1 before the first invocation. |
| 1810 | */ |
| 1811 | void http_cookie_register(struct http_hdr *list, int idx, int *first, int *last) |
| 1812 | { |
| 1813 | /* Build a linked list of cookie headers. Use header length to point to |
| 1814 | * the next one. The last entry will contains -1. |
| 1815 | */ |
| 1816 | |
| 1817 | /* Mark the current end of cookie linked list. */ |
| 1818 | list[idx].n.len = -1; |
| 1819 | if (*first < 0) { |
| 1820 | /* Save first found cookie for http_cookie_merge call. */ |
| 1821 | *first = idx; |
| 1822 | } |
| 1823 | else { |
| 1824 | /* Update linked list of cookies. */ |
| 1825 | list[*last].n.len = idx; |
| 1826 | } |
| 1827 | |
| 1828 | *last = idx; |
| 1829 | } |
| 1830 | |
| 1831 | /* Second step to merge multiple cookie headers in a single entry. |
| 1832 | * |
| 1833 | * Use it when looping over HTTP headers is done and <htx> message is built. |
| 1834 | * This will concatenate each cookie headers present from <list> directly into |
| 1835 | * <htx> message. <first> is reused from previous http_cookie_register |
| 1836 | * invocation. |
| 1837 | * |
| 1838 | * Returns 0 on success else non-zero. |
| 1839 | */ |
| 1840 | int http_cookie_merge(struct htx *htx, struct http_hdr *list, int first) |
| 1841 | { |
| 1842 | uint32_t fs; /* free space */ |
| 1843 | uint32_t bs; /* block size */ |
| 1844 | uint32_t vl; /* value len */ |
| 1845 | uint32_t tl; /* total length */ |
| 1846 | struct htx_blk *blk; |
| 1847 | |
| 1848 | if (first < 0) |
| 1849 | return 0; |
| 1850 | |
| 1851 | blk = htx_add_header(htx, ist("cookie"), list[first].v); |
| 1852 | if (!blk) |
| 1853 | return 1; |
| 1854 | |
| 1855 | tl = list[first].v.len; |
| 1856 | fs = htx_free_data_space(htx); |
| 1857 | bs = htx_get_blksz(blk); |
| 1858 | |
| 1859 | /* for each extra cookie, we'll extend the cookie's value and insert |
| 1860 | * ";" before the new value. |
| 1861 | */ |
| 1862 | fs += tl; /* first one is already counted */ |
| 1863 | |
| 1864 | /* Loop over cookies linked list built from http_cookie_register. */ |
| 1865 | while ((first = list[first].n.len) >= 0) { |
| 1866 | vl = list[first].v.len; |
| 1867 | tl += vl + 2; |
| 1868 | if (tl > fs) |
| 1869 | return 1; |
| 1870 | |
| 1871 | htx_change_blk_value_len(htx, blk, tl); |
| 1872 | *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';'; |
| 1873 | *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' '; |
| 1874 | memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, |
| 1875 | list[first].v.ptr, vl); |
| 1876 | bs += vl + 2; |
| 1877 | } |
| 1878 | |
| 1879 | return 0; |
| 1880 | } |
| 1881 | |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1882 | /* Parses the "errorloc[302|303]" proxy keyword */ |
| 1883 | static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 1884 | const struct proxy *defpx, const char *file, int line, |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1885 | char **errmsg) |
| 1886 | { |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1887 | struct conf_errors *conf_err; |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 1888 | struct http_reply *reply; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1889 | struct buffer *msg; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1890 | int errloc, status; |
| 1891 | int ret = 0; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1892 | |
| 1893 | if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) { |
| 1894 | ret = 1; |
| 1895 | goto out; |
| 1896 | } |
| 1897 | |
| 1898 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 1899 | memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]); |
| 1900 | ret = -1; |
| 1901 | goto out; |
| 1902 | } |
| 1903 | |
| 1904 | status = atol(args[1]); |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1905 | errloc = (strcmp(args[0], "errorloc303") == 0 ? 303 : 302); |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1906 | msg = http_parse_errorloc(errloc, status, args[2], errmsg); |
| 1907 | if (!msg) { |
| 1908 | memprintf(errmsg, "%s : %s", args[0], *errmsg); |
| 1909 | ret = -1; |
| 1910 | goto out; |
| 1911 | } |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 1912 | |
| 1913 | reply = calloc(1, sizeof(*reply)); |
| 1914 | if (!reply) { |
| 1915 | memprintf(errmsg, "%s : out of memory.", args[0]); |
| 1916 | ret = -1; |
| 1917 | goto out; |
| 1918 | } |
| 1919 | reply->type = HTTP_REPLY_ERRMSG; |
| 1920 | reply->status = status; |
| 1921 | reply->ctype = NULL; |
| 1922 | LIST_INIT(&reply->hdrs); |
| 1923 | reply->body.errmsg = msg; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1924 | LIST_APPEND(&http_replies_list, &reply->list); |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1925 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1926 | conf_err = calloc(1, sizeof(*conf_err)); |
| 1927 | if (!conf_err) { |
| 1928 | memprintf(errmsg, "%s : out of memory.", args[0]); |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 1929 | free(reply); |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1930 | ret = -1; |
| 1931 | goto out; |
| 1932 | } |
| 1933 | conf_err->type = 1; |
| 1934 | conf_err->info.errorfile.status = status; |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 1935 | conf_err->info.errorfile.reply = reply; |
| 1936 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1937 | conf_err->file = strdup(file); |
| 1938 | conf_err->line = line; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1939 | LIST_APPEND(&curpx->conf.errors, &conf_err->list); |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1940 | |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 1941 | /* handle warning message */ |
| 1942 | if (*errmsg) |
| 1943 | ret = 1; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1944 | out: |
| 1945 | return ret; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1946 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1947 | } |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1948 | |
| 1949 | /* Parses the "errorfile" proxy keyword */ |
| 1950 | static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 1951 | const struct proxy *defpx, const char *file, int line, |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1952 | char **errmsg) |
| 1953 | { |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1954 | struct conf_errors *conf_err; |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 1955 | struct http_reply *reply; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1956 | struct buffer *msg; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1957 | int status; |
| 1958 | int ret = 0; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1959 | |
| 1960 | if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) { |
| 1961 | ret = 1; |
| 1962 | goto out; |
| 1963 | } |
| 1964 | |
| 1965 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 1966 | memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]); |
| 1967 | ret = -1; |
| 1968 | goto out; |
| 1969 | } |
| 1970 | |
| 1971 | status = atol(args[1]); |
| 1972 | msg = http_parse_errorfile(status, args[2], errmsg); |
| 1973 | if (!msg) { |
| 1974 | memprintf(errmsg, "%s : %s", args[0], *errmsg); |
| 1975 | ret = -1; |
| 1976 | goto out; |
| 1977 | } |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1978 | |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 1979 | reply = calloc(1, sizeof(*reply)); |
| 1980 | if (!reply) { |
| 1981 | memprintf(errmsg, "%s : out of memory.", args[0]); |
| 1982 | ret = -1; |
| 1983 | goto out; |
| 1984 | } |
| 1985 | reply->type = HTTP_REPLY_ERRMSG; |
| 1986 | reply->status = status; |
| 1987 | reply->ctype = NULL; |
| 1988 | LIST_INIT(&reply->hdrs); |
| 1989 | reply->body.errmsg = msg; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1990 | LIST_APPEND(&http_replies_list, &reply->list); |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 1991 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1992 | conf_err = calloc(1, sizeof(*conf_err)); |
| 1993 | if (!conf_err) { |
| 1994 | memprintf(errmsg, "%s : out of memory.", args[0]); |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 1995 | free(reply); |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1996 | ret = -1; |
| 1997 | goto out; |
| 1998 | } |
| 1999 | conf_err->type = 1; |
| 2000 | conf_err->info.errorfile.status = status; |
Christopher Faulet | 5809e10 | 2020-05-14 17:31:52 +0200 | [diff] [blame] | 2001 | conf_err->info.errorfile.reply = reply; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2002 | conf_err->file = strdup(file); |
| 2003 | conf_err->line = line; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2004 | LIST_APPEND(&curpx->conf.errors, &conf_err->list); |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2005 | |
Christopher Faulet | a66adf4 | 2020-11-05 22:43:41 +0100 | [diff] [blame] | 2006 | /* handle warning message */ |
| 2007 | if (*errmsg) |
| 2008 | ret = 1; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2009 | out: |
| 2010 | return ret; |
| 2011 | |
| 2012 | } |
| 2013 | |
| 2014 | /* Parses the "errorfiles" proxy keyword */ |
| 2015 | static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 2016 | const struct proxy *defpx, const char *file, int line, |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2017 | char **err) |
| 2018 | { |
| 2019 | struct conf_errors *conf_err = NULL; |
| 2020 | char *name = NULL; |
| 2021 | int rc, ret = 0; |
| 2022 | |
| 2023 | if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) { |
| 2024 | ret = 1; |
| 2025 | goto out; |
| 2026 | } |
| 2027 | |
| 2028 | if (!*(args[1])) { |
| 2029 | memprintf(err, "%s : expects <name> as argument.", args[0]); |
| 2030 | ret = -1; |
| 2031 | goto out; |
| 2032 | } |
| 2033 | |
| 2034 | name = strdup(args[1]); |
| 2035 | conf_err = calloc(1, sizeof(*conf_err)); |
| 2036 | if (!name || !conf_err) { |
| 2037 | memprintf(err, "%s : out of memory.", args[0]); |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2038 | goto error; |
| 2039 | } |
| 2040 | conf_err->type = 0; |
| 2041 | |
| 2042 | conf_err->info.errorfiles.name = name; |
| 2043 | if (!*(args[2])) { |
| 2044 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) |
| 2045 | conf_err->info.errorfiles.status[rc] = 1; |
| 2046 | } |
| 2047 | else { |
| 2048 | int cur_arg, status; |
| 2049 | for (cur_arg = 2; *(args[cur_arg]); cur_arg++) { |
| 2050 | status = atol(args[cur_arg]); |
| 2051 | |
| 2052 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 2053 | if (http_err_codes[rc] == status) { |
| 2054 | conf_err->info.errorfiles.status[rc] = 2; |
| 2055 | break; |
| 2056 | } |
| 2057 | } |
| 2058 | if (rc >= HTTP_ERR_SIZE) { |
| 2059 | memprintf(err, "%s : status code '%d' not handled.", args[0], status); |
Christopher Faulet | 7cde96c | 2020-01-21 10:10:11 +0100 | [diff] [blame] | 2060 | goto error; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | conf_err->file = strdup(file); |
| 2065 | conf_err->line = line; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2066 | LIST_APPEND(&curpx->conf.errors, &conf_err->list); |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2067 | out: |
| 2068 | return ret; |
| 2069 | |
| 2070 | error: |
| 2071 | free(name); |
| 2072 | free(conf_err); |
Christopher Faulet | 7cde96c | 2020-01-21 10:10:11 +0100 | [diff] [blame] | 2073 | ret = -1; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2074 | goto out; |
| 2075 | } |
| 2076 | |
Christopher Faulet | 3b967c1 | 2020-05-15 15:47:44 +0200 | [diff] [blame] | 2077 | /* Parses the "http-error" proxy keyword */ |
| 2078 | static int proxy_parse_http_error(char **args, int section, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 2079 | const struct proxy *defpx, const char *file, int line, |
Christopher Faulet | 3b967c1 | 2020-05-15 15:47:44 +0200 | [diff] [blame] | 2080 | char **errmsg) |
| 2081 | { |
| 2082 | struct conf_errors *conf_err; |
| 2083 | struct http_reply *reply = NULL; |
| 2084 | int rc, cur_arg, ret = 0; |
| 2085 | |
| 2086 | if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) { |
| 2087 | ret = 1; |
| 2088 | goto out; |
| 2089 | } |
| 2090 | |
| 2091 | cur_arg = 1; |
| 2092 | curpx->conf.args.ctx = ARGC_HERR; |
| 2093 | reply = http_parse_http_reply((const char **)args, &cur_arg, curpx, 0, errmsg); |
| 2094 | if (!reply) { |
| 2095 | memprintf(errmsg, "%s : %s", args[0], *errmsg); |
| 2096 | goto error; |
| 2097 | } |
| 2098 | else if (!reply->status) { |
| 2099 | memprintf(errmsg, "%s : expects at least a <status> as arguments.\n", args[0]); |
| 2100 | goto error; |
| 2101 | } |
| 2102 | |
| 2103 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 2104 | if (http_err_codes[rc] == reply->status) |
| 2105 | break; |
| 2106 | } |
| 2107 | |
| 2108 | if (rc >= HTTP_ERR_SIZE) { |
| 2109 | memprintf(errmsg, "%s: status code '%d' not handled.", args[0], reply->status); |
| 2110 | goto error; |
| 2111 | } |
| 2112 | if (*args[cur_arg]) { |
| 2113 | memprintf(errmsg, "%s : unknown keyword '%s'.", args[0], args[cur_arg]); |
| 2114 | goto error; |
| 2115 | } |
| 2116 | |
| 2117 | conf_err = calloc(1, sizeof(*conf_err)); |
| 2118 | if (!conf_err) { |
| 2119 | memprintf(errmsg, "%s : out of memory.", args[0]); |
| 2120 | goto error; |
| 2121 | } |
| 2122 | if (reply->type == HTTP_REPLY_ERRFILES) { |
| 2123 | int rc = http_get_status_idx(reply->status); |
| 2124 | |
| 2125 | conf_err->type = 2; |
| 2126 | conf_err->info.errorfiles.name = reply->body.http_errors; |
| 2127 | conf_err->info.errorfiles.status[rc] = 2; |
| 2128 | reply->body.http_errors = NULL; |
| 2129 | release_http_reply(reply); |
| 2130 | } |
| 2131 | else { |
| 2132 | conf_err->type = 1; |
| 2133 | conf_err->info.errorfile.status = reply->status; |
| 2134 | conf_err->info.errorfile.reply = reply; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2135 | LIST_APPEND(&http_replies_list, &reply->list); |
Christopher Faulet | 3b967c1 | 2020-05-15 15:47:44 +0200 | [diff] [blame] | 2136 | } |
| 2137 | conf_err->file = strdup(file); |
| 2138 | conf_err->line = line; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2139 | LIST_APPEND(&curpx->conf.errors, &conf_err->list); |
Christopher Faulet | 3b967c1 | 2020-05-15 15:47:44 +0200 | [diff] [blame] | 2140 | |
Christopher Faulet | 3005d28 | 2020-11-13 10:58:01 +0100 | [diff] [blame] | 2141 | /* handle warning message */ |
| 2142 | if (*errmsg) |
| 2143 | ret = 1; |
Christopher Faulet | 3b967c1 | 2020-05-15 15:47:44 +0200 | [diff] [blame] | 2144 | out: |
| 2145 | return ret; |
| 2146 | |
| 2147 | error: |
| 2148 | release_http_reply(reply); |
| 2149 | ret = -1; |
| 2150 | goto out; |
| 2151 | |
| 2152 | } |
| 2153 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2154 | /* Check "errorfiles" proxy keyword */ |
| 2155 | static int proxy_check_errors(struct proxy *px) |
| 2156 | { |
| 2157 | struct conf_errors *conf_err, *conf_err_back; |
| 2158 | struct http_errors *http_errs; |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 2159 | int rc, err = ERR_NONE; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2160 | |
| 2161 | list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) { |
| 2162 | if (conf_err->type == 1) { |
| 2163 | /* errorfile */ |
| 2164 | rc = http_get_status_idx(conf_err->info.errorfile.status); |
Christopher Faulet | 40e8569 | 2020-05-14 17:34:31 +0200 | [diff] [blame] | 2165 | px->replies[rc] = conf_err->info.errorfile.reply; |
Christopher Faulet | 3b967c1 | 2020-05-15 15:47:44 +0200 | [diff] [blame] | 2166 | |
| 2167 | /* For proxy, to rely on default replies, just don't reference a reply */ |
| 2168 | if (px->replies[rc]->type == HTTP_REPLY_ERRMSG && !px->replies[rc]->body.errmsg) |
| 2169 | px->replies[rc] = NULL; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2170 | } |
| 2171 | else { |
| 2172 | /* errorfiles */ |
| 2173 | list_for_each_entry(http_errs, &http_errors_list, list) { |
| 2174 | if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0) |
| 2175 | break; |
| 2176 | } |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 2177 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2178 | /* unknown http-errors section */ |
| 2179 | if (&http_errs->list == &http_errors_list) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 2180 | ha_alert("proxy '%s': unknown http-errors section '%s' (at %s:%d).\n", |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2181 | px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line); |
| 2182 | err |= ERR_ALERT | ERR_FATAL; |
| 2183 | free(conf_err->info.errorfiles.name); |
| 2184 | goto next; |
| 2185 | } |
| 2186 | |
| 2187 | free(conf_err->info.errorfiles.name); |
| 2188 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 2189 | if (conf_err->info.errorfiles.status[rc] > 0) { |
Christopher Faulet | f1fedc3 | 2020-05-15 14:30:32 +0200 | [diff] [blame] | 2190 | if (http_errs->replies[rc]) |
Christopher Faulet | 40e8569 | 2020-05-14 17:34:31 +0200 | [diff] [blame] | 2191 | px->replies[rc] = http_errs->replies[rc]; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2192 | else if (conf_err->info.errorfiles.status[rc] == 2) |
| 2193 | ha_warning("config: proxy '%s' : status '%d' not declared in" |
| 2194 | " http-errors section '%s' (at %s:%d).\n", |
| 2195 | px->id, http_err_codes[rc], http_errs->id, |
| 2196 | conf_err->file, conf_err->line); |
| 2197 | } |
| 2198 | } |
| 2199 | } |
| 2200 | next: |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2201 | LIST_DELETE(&conf_err->list); |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2202 | free(conf_err->file); |
| 2203 | free(conf_err); |
| 2204 | } |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 2205 | |
| 2206 | out: |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2207 | return err; |
| 2208 | } |
| 2209 | |
Christopher Faulet | 0a589fd | 2020-01-22 14:47:04 +0100 | [diff] [blame] | 2210 | static int post_check_errors() |
| 2211 | { |
| 2212 | struct ebpt_node *node; |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 2213 | struct http_error_msg *http_errmsg; |
Christopher Faulet | 0a589fd | 2020-01-22 14:47:04 +0100 | [diff] [blame] | 2214 | struct htx *htx; |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 2215 | int err_code = ERR_NONE; |
Christopher Faulet | 0a589fd | 2020-01-22 14:47:04 +0100 | [diff] [blame] | 2216 | |
| 2217 | node = ebpt_first(&http_error_messages); |
| 2218 | while (node) { |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 2219 | http_errmsg = container_of(node, typeof(*http_errmsg), node); |
| 2220 | if (b_is_null(&http_errmsg->msg)) |
Christopher Faulet | 0a589fd | 2020-01-22 14:47:04 +0100 | [diff] [blame] | 2221 | goto next; |
Christopher Faulet | b6ea17c | 2020-05-13 21:45:22 +0200 | [diff] [blame] | 2222 | htx = htxbuf(&http_errmsg->msg); |
Christopher Faulet | 0a589fd | 2020-01-22 14:47:04 +0100 | [diff] [blame] | 2223 | if (htx_free_data_space(htx) < global.tune.maxrewrite) { |
| 2224 | ha_warning("config: errorfile '%s' runs over the buffer space" |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 2225 | " reserved to headers rewriting. It may lead to internal errors if " |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2226 | " http-after-response rules are evaluated on this message.\n", |
Christopher Faulet | 0a589fd | 2020-01-22 14:47:04 +0100 | [diff] [blame] | 2227 | (char *)node->key); |
| 2228 | err_code |= ERR_WARN; |
| 2229 | } |
| 2230 | next: |
| 2231 | node = ebpt_next(node); |
| 2232 | } |
| 2233 | |
| 2234 | return err_code; |
| 2235 | } |
| 2236 | |
Willy Tarreau | 016255a | 2021-02-12 08:40:29 +0100 | [diff] [blame] | 2237 | int proxy_dup_default_conf_errors(struct proxy *curpx, const struct proxy *defpx, char **errmsg) |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2238 | { |
| 2239 | struct conf_errors *conf_err, *new_conf_err = NULL; |
| 2240 | int ret = 0; |
| 2241 | |
| 2242 | list_for_each_entry(conf_err, &defpx->conf.errors, list) { |
| 2243 | new_conf_err = calloc(1, sizeof(*new_conf_err)); |
| 2244 | if (!new_conf_err) { |
| 2245 | memprintf(errmsg, "unable to duplicate default errors (out of memory)."); |
| 2246 | goto out; |
| 2247 | } |
| 2248 | new_conf_err->type = conf_err->type; |
| 2249 | if (conf_err->type == 1) { |
| 2250 | new_conf_err->info.errorfile.status = conf_err->info.errorfile.status; |
Christopher Faulet | 40e8569 | 2020-05-14 17:34:31 +0200 | [diff] [blame] | 2251 | new_conf_err->info.errorfile.reply = conf_err->info.errorfile.reply; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2252 | } |
| 2253 | else { |
| 2254 | new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name); |
| 2255 | if (!new_conf_err->info.errorfiles.name) { |
| 2256 | memprintf(errmsg, "unable to duplicate default errors (out of memory)."); |
| 2257 | goto out; |
| 2258 | } |
| 2259 | memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status, |
| 2260 | sizeof(conf_err->info.errorfiles.status)); |
| 2261 | } |
| 2262 | new_conf_err->file = strdup(conf_err->file); |
| 2263 | new_conf_err->line = conf_err->line; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2264 | LIST_APPEND(&curpx->conf.errors, &new_conf_err->list); |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2265 | new_conf_err = NULL; |
| 2266 | } |
| 2267 | ret = 1; |
| 2268 | |
| 2269 | out: |
| 2270 | free(new_conf_err); |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 2271 | return ret; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | void proxy_release_conf_errors(struct proxy *px) |
| 2275 | { |
| 2276 | struct conf_errors *conf_err, *conf_err_back; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 2277 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2278 | list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) { |
| 2279 | if (conf_err->type == 0) |
| 2280 | free(conf_err->info.errorfiles.name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2281 | LIST_DELETE(&conf_err->list); |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2282 | free(conf_err->file); |
| 2283 | free(conf_err); |
| 2284 | } |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | /* |
| 2288 | * Parse an <http-errors> section. |
| 2289 | * Returns the error code, 0 if OK, or any combination of : |
| 2290 | * - ERR_ABORT: must abort ASAP |
| 2291 | * - ERR_FATAL: we can continue parsing but not start the service |
| 2292 | * - ERR_WARN: a warning has been emitted |
| 2293 | * - ERR_ALERT: an alert has been emitted |
| 2294 | * Only the two first ones can stop processing, the two others are just |
| 2295 | * indicators. |
| 2296 | */ |
| 2297 | static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm) |
| 2298 | { |
| 2299 | static struct http_errors *curr_errs = NULL; |
| 2300 | int err_code = 0; |
| 2301 | const char *err; |
| 2302 | char *errmsg = NULL; |
| 2303 | |
| 2304 | if (strcmp(args[0], "http-errors") == 0) { /* new errors section */ |
| 2305 | if (!*args[1]) { |
| 2306 | ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum); |
| 2307 | err_code |= ERR_ALERT | ERR_ABORT; |
| 2308 | goto out; |
| 2309 | } |
| 2310 | |
| 2311 | err = invalid_char(args[1]); |
| 2312 | if (err) { |
| 2313 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 2314 | file, linenum, *err, args[0], args[1]); |
| 2315 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2316 | } |
| 2317 | |
| 2318 | list_for_each_entry(curr_errs, &http_errors_list, list) { |
| 2319 | /* Error if two errors section owns the same name */ |
| 2320 | if (strcmp(curr_errs->id, args[1]) == 0) { |
| 2321 | ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n", |
| 2322 | file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line); |
| 2323 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) { |
| 2328 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 2329 | err_code |= ERR_ALERT | ERR_ABORT; |
| 2330 | goto out; |
| 2331 | } |
| 2332 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2333 | LIST_APPEND(&http_errors_list, &curr_errs->list); |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 2334 | curr_errs->id = strdup(args[1]); |
| 2335 | curr_errs->conf.file = strdup(file); |
| 2336 | curr_errs->conf.line = linenum; |
| 2337 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 2338 | else if (strcmp(args[0], "errorfile") == 0) { /* error message from a file */ |
Christopher Faulet | de30bb7 | 2020-05-14 10:03:55 +0200 | [diff] [blame] | 2339 | struct http_reply *reply; |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 2340 | struct buffer *msg; |
| 2341 | int status, rc; |
| 2342 | |
| 2343 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 2344 | ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n", |
| 2345 | file, linenum, args[0]); |
| 2346 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2347 | goto out; |
| 2348 | } |
| 2349 | |
| 2350 | status = atol(args[1]); |
| 2351 | msg = http_parse_errorfile(status, args[2], &errmsg); |
| 2352 | if (!msg) { |
| 2353 | ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); |
| 2354 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2355 | goto out; |
| 2356 | } |
Christopher Faulet | 3005d28 | 2020-11-13 10:58:01 +0100 | [diff] [blame] | 2357 | if (errmsg) { |
| 2358 | ha_warning("parsing [%s:%d] : %s: %s\n", file, linenum, args[0], errmsg); |
| 2359 | err_code |= ERR_WARN; |
| 2360 | } |
Christopher Faulet | de30bb7 | 2020-05-14 10:03:55 +0200 | [diff] [blame] | 2361 | |
| 2362 | reply = calloc(1, sizeof(*reply)); |
| 2363 | if (!reply) { |
| 2364 | ha_alert("parsing [%s:%d] : %s : out of memory.\n", file, linenum, args[0]); |
| 2365 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2366 | goto out; |
| 2367 | } |
| 2368 | reply->type = HTTP_REPLY_ERRMSG; |
| 2369 | reply->status = status; |
| 2370 | reply->ctype = NULL; |
| 2371 | LIST_INIT(&reply->hdrs); |
| 2372 | reply->body.errmsg = msg; |
| 2373 | |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 2374 | rc = http_get_status_idx(status); |
Christopher Faulet | de30bb7 | 2020-05-14 10:03:55 +0200 | [diff] [blame] | 2375 | curr_errs->replies[rc] = reply; |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 2376 | } |
| 2377 | else if (*args[0] != 0) { |
| 2378 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection); |
| 2379 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2380 | goto out; |
| 2381 | } |
| 2382 | |
| 2383 | out: |
| 2384 | free(errmsg); |
| 2385 | return err_code; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 2389 | { CFG_LISTEN, "errorloc", proxy_parse_errorloc }, |
| 2390 | { CFG_LISTEN, "errorloc302", proxy_parse_errorloc }, |
| 2391 | { CFG_LISTEN, "errorloc303", proxy_parse_errorloc }, |
| 2392 | { CFG_LISTEN, "errorfile", proxy_parse_errorfile }, |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2393 | { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles }, |
Christopher Faulet | 3b967c1 | 2020-05-15 15:47:44 +0200 | [diff] [blame] | 2394 | { CFG_LISTEN, "http-error", proxy_parse_http_error }, |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 2395 | { 0, NULL, NULL }, |
| 2396 | }}; |
| 2397 | |
| 2398 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 2399 | REGISTER_POST_PROXY_CHECK(proxy_check_errors); |
Christopher Faulet | 0a589fd | 2020-01-22 14:47:04 +0100 | [diff] [blame] | 2400 | REGISTER_POST_CHECK(post_check_errors); |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 2401 | |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 2402 | REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL); |
| 2403 | |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2404 | /************************************************************************/ |
| 2405 | /* HTX sample fetches */ |
| 2406 | /************************************************************************/ |
| 2407 | |
| 2408 | /* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */ |
| 2409 | static int |
| 2410 | smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 2411 | { |
| 2412 | if (!smp->strm) |
| 2413 | return 0; |
| 2414 | |
| 2415 | smp->data.u.sint = !!IS_HTX_STRM(smp->strm); |
| 2416 | smp->data.type = SMP_T_BOOL; |
| 2417 | return 1; |
| 2418 | } |
| 2419 | |
| 2420 | /* Returns the number of blocks in an HTX message. The channel is chosen |
| 2421 | * depending on the sample direction. */ |
| 2422 | static int |
| 2423 | smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 2424 | { |
| 2425 | struct channel *chn; |
| 2426 | struct htx *htx; |
| 2427 | |
| 2428 | if (!smp->strm) |
| 2429 | return 0; |
| 2430 | |
| 2431 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 2432 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2433 | if (!htx) |
| 2434 | return 0; |
| 2435 | |
| 2436 | smp->data.u.sint = htx_nbblks(htx); |
| 2437 | smp->data.type = SMP_T_SINT; |
| 2438 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 2439 | return 1; |
| 2440 | } |
| 2441 | |
| 2442 | /* Returns the size of an HTX message. The channel is chosen depending on the |
| 2443 | * sample direction. */ |
| 2444 | static int |
| 2445 | smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 2446 | { |
| 2447 | struct channel *chn; |
| 2448 | struct htx *htx; |
| 2449 | |
| 2450 | if (!smp->strm) |
| 2451 | return 0; |
| 2452 | |
| 2453 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 2454 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2455 | if (!htx) |
| 2456 | return 0; |
| 2457 | |
| 2458 | smp->data.u.sint = htx->size; |
| 2459 | smp->data.type = SMP_T_SINT; |
| 2460 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 2461 | return 1; |
| 2462 | } |
| 2463 | |
| 2464 | /* Returns the data size of an HTX message. The channel is chosen depending on the |
| 2465 | * sample direction. */ |
| 2466 | static int |
| 2467 | smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 2468 | { |
| 2469 | struct channel *chn; |
| 2470 | struct htx *htx; |
| 2471 | |
| 2472 | if (!smp->strm) |
| 2473 | return 0; |
| 2474 | |
| 2475 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 2476 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2477 | if (!htx) |
| 2478 | return 0; |
| 2479 | |
| 2480 | smp->data.u.sint = htx->data; |
| 2481 | smp->data.type = SMP_T_SINT; |
| 2482 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 2483 | return 1; |
| 2484 | } |
| 2485 | |
| 2486 | /* Returns the used space (data+meta) of an HTX message. The channel is chosen |
| 2487 | * depending on the sample direction. */ |
| 2488 | static int |
| 2489 | smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 2490 | { |
| 2491 | struct channel *chn; |
| 2492 | struct htx *htx; |
| 2493 | |
| 2494 | if (!smp->strm) |
| 2495 | return 0; |
| 2496 | |
| 2497 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 2498 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2499 | if (!htx) |
| 2500 | return 0; |
| 2501 | |
| 2502 | smp->data.u.sint = htx_used_space(htx); |
| 2503 | smp->data.type = SMP_T_SINT; |
| 2504 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 2505 | return 1; |
| 2506 | } |
| 2507 | |
| 2508 | /* Returns the free space (size-used) of an HTX message. The channel is chosen |
| 2509 | * depending on the sample direction. */ |
| 2510 | static int |
| 2511 | smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 2512 | { |
| 2513 | struct channel *chn; |
| 2514 | struct htx *htx; |
| 2515 | |
| 2516 | if (!smp->strm) |
| 2517 | return 0; |
| 2518 | |
| 2519 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 2520 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2521 | if (!htx) |
| 2522 | return 0; |
| 2523 | |
| 2524 | smp->data.u.sint = htx_free_space(htx); |
| 2525 | smp->data.type = SMP_T_SINT; |
| 2526 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 2527 | return 1; |
| 2528 | } |
| 2529 | |
| 2530 | /* Returns the free space for data (free-sizeof(blk)) of an HTX message. The |
| 2531 | * channel is chosen depending on the sample direction. */ |
| 2532 | static int |
| 2533 | smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 2534 | { |
| 2535 | struct channel *chn; |
| 2536 | struct htx *htx; |
| 2537 | |
| 2538 | if (!smp->strm) |
| 2539 | return 0; |
| 2540 | |
| 2541 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 2542 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2543 | if (!htx) |
| 2544 | return 0; |
| 2545 | |
| 2546 | smp->data.u.sint = htx_free_data_space(htx); |
| 2547 | smp->data.type = SMP_T_SINT; |
| 2548 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 2549 | return 1; |
| 2550 | } |
| 2551 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2552 | /* Returns 1 if the HTX message contains EOM flag. Otherwise it returns 0. The |
| 2553 | * channel is chosen depending on the sample direction. |
| 2554 | */ |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2555 | static int |
| 2556 | smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 2557 | { |
| 2558 | struct channel *chn; |
| 2559 | struct htx *htx; |
| 2560 | |
| 2561 | if (!smp->strm) |
| 2562 | return 0; |
| 2563 | |
| 2564 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 2565 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2566 | if (!htx) |
| 2567 | return 0; |
| 2568 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2569 | smp->data.u.sint = !!(htx->flags & HTX_FL_EOM); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2570 | smp->data.type = SMP_T_BOOL; |
| 2571 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 2572 | return 1; |
| 2573 | } |
| 2574 | |
| 2575 | /* Returns the type of a specific HTX block, if found in the message. Otherwise |
| 2576 | * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or |
| 2577 | * "head", "tail" or "first". The channel is chosen depending on the sample |
| 2578 | * direction. */ |
| 2579 | static int |
| 2580 | smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 2581 | { |
| 2582 | struct channel *chn; |
| 2583 | struct htx *htx; |
| 2584 | enum htx_blk_type type; |
| 2585 | int32_t pos; |
| 2586 | |
| 2587 | if (!smp->strm || !arg_p) |
| 2588 | return 0; |
| 2589 | |
| 2590 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 2591 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2592 | if (!htx) |
| 2593 | return 0; |
| 2594 | |
| 2595 | pos = arg_p[0].data.sint; |
| 2596 | if (pos == -1) |
| 2597 | type = htx_get_head_type(htx); |
| 2598 | else if (pos == -2) |
| 2599 | type = htx_get_tail_type(htx); |
| 2600 | else if (pos == -3) |
| 2601 | type = htx_get_first_type(htx); |
| 2602 | else |
| 2603 | type = ((pos >= htx->head && pos <= htx->tail) |
| 2604 | ? htx_get_blk_type(htx_get_blk(htx, pos)) |
| 2605 | : HTX_BLK_UNUSED); |
| 2606 | |
| 2607 | chunk_initstr(&smp->data.u.str, htx_blk_type_str(type)); |
| 2608 | smp->data.type = SMP_T_STR; |
| 2609 | smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 2610 | return 1; |
| 2611 | } |
| 2612 | |
| 2613 | /* Returns the size of a specific HTX block, if found in the message. Otherwise |
| 2614 | * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or |
| 2615 | * "first". The channel is chosen depending on the sample direction. */ |
| 2616 | static int |
| 2617 | smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 2618 | { |
| 2619 | struct channel *chn; |
| 2620 | struct htx *htx; |
| 2621 | struct htx_blk *blk; |
| 2622 | int32_t pos; |
| 2623 | |
| 2624 | if (!smp->strm || !arg_p) |
| 2625 | return 0; |
| 2626 | |
| 2627 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 2628 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2629 | if (!htx) |
| 2630 | return 0; |
| 2631 | |
| 2632 | pos = arg_p[0].data.sint; |
| 2633 | if (pos == -1) |
| 2634 | blk = htx_get_head_blk(htx); |
| 2635 | else if (pos == -2) |
| 2636 | blk = htx_get_tail_blk(htx); |
| 2637 | else if (pos == -3) |
| 2638 | blk = htx_get_first_blk(htx); |
| 2639 | else |
| 2640 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 2641 | |
| 2642 | smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0); |
| 2643 | smp->data.type = SMP_T_SINT; |
| 2644 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 2645 | return 1; |
| 2646 | } |
| 2647 | |
| 2648 | /* Returns the start-line if the selected HTX block exists and is a |
| 2649 | * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is |
| 2650 | * supported or "head", "tail" or "first". The channel is chosen depending on |
| 2651 | * the sample direction. */ |
| 2652 | static int |
| 2653 | smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 2654 | { |
| 2655 | struct buffer *temp; |
| 2656 | struct channel *chn; |
| 2657 | struct htx *htx; |
| 2658 | struct htx_blk *blk; |
| 2659 | struct htx_sl *sl; |
| 2660 | int32_t pos; |
| 2661 | |
| 2662 | if (!smp->strm || !arg_p) |
| 2663 | return 0; |
| 2664 | |
| 2665 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 2666 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2667 | if (!htx) |
| 2668 | return 0; |
| 2669 | |
| 2670 | pos = arg_p[0].data.sint; |
| 2671 | if (pos == -1) |
| 2672 | blk = htx_get_head_blk(htx); |
| 2673 | else if (pos == -2) |
| 2674 | blk = htx_get_tail_blk(htx); |
| 2675 | else if (pos == -3) |
| 2676 | blk = htx_get_first_blk(htx); |
| 2677 | else |
| 2678 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 2679 | |
| 2680 | if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) { |
| 2681 | smp->data.u.str.size = 0; |
| 2682 | smp->data.u.str.area = ""; |
| 2683 | smp->data.u.str.data = 0; |
| 2684 | } |
| 2685 | else { |
| 2686 | sl = htx_get_blk_ptr(htx, blk); |
| 2687 | |
| 2688 | temp = get_trash_chunk(); |
| 2689 | chunk_istcat(temp, htx_sl_p1(sl)); |
| 2690 | temp->area[temp->data++] = ' '; |
| 2691 | chunk_istcat(temp, htx_sl_p2(sl)); |
| 2692 | temp->area[temp->data++] = ' '; |
| 2693 | chunk_istcat(temp, htx_sl_p3(sl)); |
| 2694 | |
| 2695 | smp->data.u.str = *temp; |
| 2696 | } |
| 2697 | |
| 2698 | smp->data.type = SMP_T_STR; |
| 2699 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 2700 | return 1; |
| 2701 | } |
| 2702 | |
| 2703 | /* Returns the header name if the selected HTX block exists and is a header or a |
| 2704 | * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is |
| 2705 | * supported or "head", "tail" or "first". The channel is chosen depending on |
| 2706 | * the sample direction. */ |
| 2707 | static int |
| 2708 | smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 2709 | { |
| 2710 | struct channel *chn; |
| 2711 | struct htx *htx; |
| 2712 | struct htx_blk *blk; |
| 2713 | int32_t pos; |
| 2714 | |
| 2715 | if (!smp->strm || !arg_p) |
| 2716 | return 0; |
| 2717 | |
| 2718 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 2719 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2720 | if (!htx) |
| 2721 | return 0; |
| 2722 | |
| 2723 | pos = arg_p[0].data.sint; |
| 2724 | if (pos == -1) |
| 2725 | blk = htx_get_head_blk(htx); |
| 2726 | else if (pos == -2) |
| 2727 | blk = htx_get_tail_blk(htx); |
| 2728 | else if (pos == -3) |
| 2729 | blk = htx_get_first_blk(htx); |
| 2730 | else |
| 2731 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 2732 | |
| 2733 | if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) { |
| 2734 | smp->data.u.str.size = 0; |
| 2735 | smp->data.u.str.area = ""; |
| 2736 | smp->data.u.str.data = 0; |
| 2737 | } |
| 2738 | else { |
| 2739 | struct ist name = htx_get_blk_name(htx, blk); |
| 2740 | |
| 2741 | chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len); |
| 2742 | } |
| 2743 | smp->data.type = SMP_T_STR; |
| 2744 | smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 2745 | return 1; |
| 2746 | } |
| 2747 | |
| 2748 | /* Returns the header value if the selected HTX block exists and is a header or |
| 2749 | * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is |
| 2750 | * supported or "head", "tail" or "first". The channel is chosen depending on |
| 2751 | * the sample direction. */ |
| 2752 | static int |
| 2753 | smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 2754 | { |
| 2755 | struct channel *chn; |
| 2756 | struct htx *htx; |
| 2757 | struct htx_blk *blk; |
| 2758 | int32_t pos; |
| 2759 | |
| 2760 | if (!smp->strm || !arg_p) |
| 2761 | return 0; |
| 2762 | |
| 2763 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 2764 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2765 | if (!htx) |
| 2766 | return 0; |
| 2767 | |
| 2768 | pos = arg_p[0].data.sint; |
| 2769 | if (pos == -1) |
| 2770 | blk = htx_get_head_blk(htx); |
| 2771 | else if (pos == -2) |
| 2772 | blk = htx_get_tail_blk(htx); |
| 2773 | else if (pos == -3) |
| 2774 | blk = htx_get_first_blk(htx); |
| 2775 | else |
| 2776 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 2777 | |
| 2778 | if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) { |
| 2779 | smp->data.u.str.size = 0; |
| 2780 | smp->data.u.str.area = ""; |
| 2781 | smp->data.u.str.data = 0; |
| 2782 | } |
| 2783 | else { |
| 2784 | struct ist val = htx_get_blk_value(htx, blk); |
| 2785 | |
| 2786 | chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len); |
| 2787 | } |
| 2788 | smp->data.type = SMP_T_STR; |
| 2789 | smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 2790 | return 1; |
| 2791 | } |
| 2792 | |
| 2793 | /* Returns the value if the selected HTX block exists and is a data |
| 2794 | * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported |
| 2795 | * or "head", "tail" or "first". The channel is chosen depending on the sample |
| 2796 | * direction. */ |
| 2797 | static int |
Christopher Faulet | c5db14c | 2020-01-08 14:51:03 +0100 | [diff] [blame] | 2798 | smp_fetch_htx_blk_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2799 | { |
| 2800 | struct channel *chn; |
| 2801 | struct htx *htx; |
| 2802 | struct htx_blk *blk; |
| 2803 | int32_t pos; |
| 2804 | |
| 2805 | if (!smp->strm || !arg_p) |
| 2806 | return 0; |
| 2807 | |
| 2808 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 2809 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2810 | if (!htx) |
| 2811 | return 0; |
| 2812 | |
| 2813 | pos = arg_p[0].data.sint; |
| 2814 | if (pos == -1) |
| 2815 | blk = htx_get_head_blk(htx); |
| 2816 | else if (pos == -2) |
| 2817 | blk = htx_get_tail_blk(htx); |
| 2818 | else if (pos == -3) |
| 2819 | blk = htx_get_first_blk(htx); |
| 2820 | else |
| 2821 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 2822 | |
| 2823 | if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) { |
| 2824 | smp->data.u.str.size = 0; |
| 2825 | smp->data.u.str.area = ""; |
| 2826 | smp->data.u.str.data = 0; |
| 2827 | } |
| 2828 | else { |
| 2829 | struct ist val = htx_get_blk_value(htx, blk); |
| 2830 | |
| 2831 | chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len); |
| 2832 | } |
Christopher Faulet | 8178e40 | 2020-01-08 14:38:58 +0100 | [diff] [blame] | 2833 | smp->data.type = SMP_T_BIN; |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2834 | smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 2835 | return 1; |
| 2836 | } |
| 2837 | |
| 2838 | /* This function is used to validate the arguments passed to any "htx_blk" fetch |
| 2839 | * keywords. An argument is expected by these keywords. It must be a positive |
| 2840 | * integer or on of the following strings: "head", "tail" or "first". It returns |
| 2841 | * 0 on error, and a non-zero value if OK. |
| 2842 | */ |
| 2843 | int val_blk_arg(struct arg *arg, char **err_msg) |
| 2844 | { |
| 2845 | if (arg[0].type != ARGT_STR || !arg[0].data.str.data) { |
| 2846 | memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)"); |
| 2847 | return 0; |
| 2848 | } |
| 2849 | if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) { |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 2850 | chunk_destroy(&arg[0].data.str); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2851 | arg[0].type = ARGT_SINT; |
| 2852 | arg[0].data.sint = -1; |
| 2853 | } |
| 2854 | else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) { |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 2855 | chunk_destroy(&arg[0].data.str); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2856 | arg[0].type = ARGT_SINT; |
| 2857 | arg[0].data.sint = -2; |
| 2858 | } |
| 2859 | else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) { |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 2860 | chunk_destroy(&arg[0].data.str); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2861 | arg[0].type = ARGT_SINT; |
| 2862 | arg[0].data.sint = -3; |
| 2863 | } |
| 2864 | else { |
| 2865 | int pos; |
| 2866 | |
| 2867 | for (pos = 0; pos < arg[0].data.str.data; pos++) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 2868 | if (!isdigit((unsigned char)arg[0].data.str.area[pos])) { |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2869 | memprintf(err_msg, "invalid block position"); |
| 2870 | return 0; |
| 2871 | } |
| 2872 | } |
| 2873 | |
| 2874 | pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data); |
| 2875 | if (pos < 0) { |
| 2876 | memprintf(err_msg, "block position must not be negative"); |
| 2877 | return 0; |
| 2878 | } |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 2879 | chunk_destroy(&arg[0].data.str); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2880 | arg[0].type = ARGT_SINT; |
| 2881 | arg[0].data.sint = pos; |
| 2882 | } |
| 2883 | |
| 2884 | return 1; |
| 2885 | } |
| 2886 | |
| 2887 | |
| 2888 | /* Note: must not be declared <const> as its list will be overwritten. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2889 | * Note: htx sample fetches should only used for development purpose. |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2890 | */ |
| 2891 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Christopher Faulet | 2e96194 | 2021-03-25 17:29:38 +0100 | [diff] [blame] | 2892 | { "internal.strm.is_htx", smp_fetch_is_htx, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2893 | |
Christopher Faulet | 01f4445 | 2020-01-08 14:23:40 +0100 | [diff] [blame] | 2894 | { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 2895 | { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 2896 | { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 2897 | { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 2898 | { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 2899 | { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 2900 | { "internal.htx.has_eom", smp_fetch_htx_has_eom, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2901 | |
Christopher Faulet | 01f4445 | 2020-01-08 14:23:40 +0100 | [diff] [blame] | 2902 | { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 2903 | { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 2904 | { "internal.htx_blk.start_line", smp_fetch_htx_blk_stline, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 2905 | { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 2906 | { "internal.htx_blk.hdrval", smp_fetch_htx_blk_hdrval, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
Christopher Faulet | c5db14c | 2020-01-08 14:51:03 +0100 | [diff] [blame] | 2907 | { "internal.htx_blk.data", smp_fetch_htx_blk_data, ARG1(1,STR), val_blk_arg, SMP_T_BIN, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 2908 | |
| 2909 | { /* END */ }, |
| 2910 | }}; |
| 2911 | |
| 2912 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |