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