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