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