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