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