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