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