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