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