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