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