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