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 | |
| 17 | #include <types/global.h> |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 18 | |
| 19 | #include <common/config.h> |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 20 | #include <common/debug.h> |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 21 | #include <common/cfgparse.h> |
Willy Tarreau | afba57a | 2018-12-11 13:44:24 +0100 | [diff] [blame] | 22 | #include <common/h1.h> |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 23 | #include <common/http.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 24 | #include <common/htx.h> |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 25 | |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 26 | #include <proto/arg.h> |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 27 | #include <proto/http_htx.h> |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 28 | #include <proto/http_fetch.h> |
| 29 | #include <proto/sample.h> |
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 | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 32 | struct eb_root http_error_messages = EB_ROOT; |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 33 | struct list http_errors_list = LIST_HEAD_INIT(http_errors_list); |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 34 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 35 | /* The declaration of an errorfiles/errorfile directives. Used during config |
| 36 | * parsing only. */ |
| 37 | struct conf_errors { |
| 38 | char type; /* directive type (0: errorfiles, 1: errorfile) */ |
| 39 | union { |
| 40 | struct { |
| 41 | int status; /* the status code associated to this error */ |
| 42 | struct buffer *msg; /* the HTX error message */ |
| 43 | } errorfile; /* describe an "errorfile" directive */ |
| 44 | struct { |
| 45 | char *name; /* the http-errors section name */ |
| 46 | char status[HTTP_ERR_SIZE]; /* list of status to import (0: ignore, 1: implicit import, 2: explicit import) */ |
| 47 | } errorfiles; /* describe an "errorfiles" directive */ |
| 48 | } info; |
| 49 | |
| 50 | char *file; /* file where the directive appears */ |
| 51 | int line; /* line where the directive appears */ |
| 52 | |
| 53 | struct list list; /* next conf_errors */ |
| 54 | }; |
| 55 | |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 56 | static int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host); |
| 57 | static int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri); |
| 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 | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 93 | /* Finds the first or next occurrence of header <name> in the HTX message <htx> |
| 94 | * using the context <ctx>. This structure holds everything necessary to use the |
| 95 | * header and find next occurrence. If its <blk> member is NULL, the header is |
| 96 | * searched from the beginning. Otherwise, the next occurrence is returned. The |
| 97 | * function returns 1 when it finds a value, and 0 when there is no more. It is |
| 98 | * designed to work with headers defined as comma-separated lists. If <full> is |
| 99 | * set, it works on full-line headers in whose comma is not a delimiter but is |
| 100 | * part of the syntax. A special case, if ctx->value is NULL when searching for |
| 101 | * a new values of a header, the current header is rescanned. This allows |
| 102 | * rescanning after a header deletion. |
| 103 | */ |
| 104 | int http_find_header(const struct htx *htx, const struct ist name, |
| 105 | struct http_hdr_ctx *ctx, int full) |
| 106 | { |
| 107 | struct htx_blk *blk = ctx->blk; |
| 108 | struct ist n, v; |
| 109 | enum htx_blk_type type; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 110 | |
| 111 | if (blk) { |
| 112 | char *p; |
| 113 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 114 | if (!ctx->value.ptr) |
| 115 | goto rescan_hdr; |
| 116 | if (full) |
| 117 | goto next_blk; |
| 118 | v = htx_get_blk_value(htx, blk); |
| 119 | p = ctx->value.ptr + ctx->value.len + ctx->lws_after; |
| 120 | v.len -= (p - v.ptr); |
| 121 | v.ptr = p; |
| 122 | if (!v.len) |
| 123 | goto next_blk; |
| 124 | /* Skip comma */ |
| 125 | if (*(v.ptr) == ',') { |
| 126 | v.ptr++; |
| 127 | v.len--; |
| 128 | } |
| 129 | |
| 130 | goto return_hdr; |
| 131 | } |
| 132 | |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 133 | if (htx_is_empty(htx)) |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 134 | return 0; |
| 135 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 136 | 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] | 137 | rescan_hdr: |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 138 | type = htx_get_blk_type(blk); |
Christopher Faulet | 573fe73 | 2018-11-28 16:55:12 +0100 | [diff] [blame] | 139 | if (type == HTX_BLK_EOH || type == HTX_BLK_EOM) |
| 140 | break; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 141 | if (type != HTX_BLK_HDR) |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 142 | continue; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 143 | if (name.len) { |
| 144 | /* If no name was passed, we want any header. So skip the comparison */ |
| 145 | n = htx_get_blk_name(htx, blk); |
| 146 | if (!isteqi(n, name)) |
| 147 | goto next_blk; |
| 148 | } |
| 149 | v = htx_get_blk_value(htx, blk); |
| 150 | |
| 151 | return_hdr: |
| 152 | ctx->lws_before = 0; |
| 153 | ctx->lws_after = 0; |
| 154 | while (v.len && HTTP_IS_LWS(*v.ptr)) { |
| 155 | v.ptr++; |
| 156 | v.len--; |
| 157 | ctx->lws_before++; |
| 158 | } |
| 159 | if (!full) |
| 160 | v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr; |
| 161 | while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) { |
| 162 | v.len--; |
| 163 | ctx->lws_after++; |
| 164 | } |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 165 | ctx->blk = blk; |
| 166 | ctx->value = v; |
| 167 | return 1; |
| 168 | |
| 169 | next_blk: |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 170 | ; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | ctx->blk = NULL; |
| 174 | ctx->value = ist(""); |
| 175 | ctx->lws_before = ctx->lws_after = 0; |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* Adds a header block int the HTX message <htx>, just before the EOH block. It |
| 180 | * returns 1 on success, otherwise it returns 0. |
| 181 | */ |
| 182 | int http_add_header(struct htx *htx, const struct ist n, const struct ist v) |
| 183 | { |
| 184 | struct htx_blk *blk; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 185 | struct htx_sl *sl; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 186 | enum htx_blk_type type = htx_get_tail_type(htx); |
| 187 | int32_t prev; |
| 188 | |
| 189 | blk = htx_add_header(htx, n, v); |
| 190 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 191 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 192 | |
| 193 | if (unlikely(type < HTX_BLK_EOH)) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 194 | goto end; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 195 | |
| 196 | /* <blk> is the head, swap it iteratively with its predecessor to place |
| 197 | * it just before the end-of-header block. So blocks remains ordered. */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 198 | 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] | 199 | struct htx_blk *pblk = htx_get_blk(htx, prev); |
| 200 | enum htx_blk_type type = htx_get_blk_type(pblk); |
| 201 | |
| 202 | /* Swap .addr and .info fields */ |
| 203 | blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr; |
| 204 | blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info; |
| 205 | |
| 206 | if (blk->addr == pblk->addr) |
| 207 | blk->addr += htx_get_blksz(pblk); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 208 | |
| 209 | /* Stop when end-of-header is reached */ |
| 210 | if (type == HTX_BLK_EOH) |
| 211 | break; |
| 212 | |
| 213 | blk = pblk; |
| 214 | } |
Christopher Faulet | 05aab64 | 2019-04-11 13:43:57 +0200 | [diff] [blame] | 215 | |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 216 | end: |
| 217 | sl = http_get_stline(htx); |
| 218 | if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteq(n, ist("host"))) { |
| 219 | if (!http_update_authority(htx, sl, v)) |
| 220 | goto fail; |
| 221 | } |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 222 | return 1; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 223 | |
| 224 | fail: |
| 225 | return 0; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 228 | /* 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] | 229 | * success, otherwise it returns 0. |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 230 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 231 | 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] | 232 | { |
Christopher Faulet | 7b7d507 | 2019-05-13 15:22:59 +0200 | [diff] [blame] | 233 | struct htx_blk *blk; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 234 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 235 | blk = htx_get_first_blk(htx); |
| 236 | if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3)) |
Christopher Faulet | 7b7d507 | 2019-05-13 15:22:59 +0200 | [diff] [blame] | 237 | return 0; |
| 238 | return 1; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 241 | /* Replace the request method in the HTX message <htx> by <meth>. It returns 1 |
| 242 | * on success, otherwise 0. |
| 243 | */ |
| 244 | int http_replace_req_meth(struct htx *htx, const struct ist meth) |
| 245 | { |
| 246 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 247 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 248 | struct ist uri, vsn; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 249 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 250 | if (!sl) |
| 251 | return 0; |
| 252 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 253 | /* Start by copying old uri and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 254 | chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */ |
| 255 | uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 256 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 257 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 258 | vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 259 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 260 | /* create the new start line */ |
| 261 | sl->info.req.meth = find_http_meth(meth.ptr, meth.len); |
| 262 | return http_replace_stline(htx, meth, uri, vsn); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on |
| 266 | * success, otherwise 0. |
| 267 | */ |
| 268 | int http_replace_req_uri(struct htx *htx, const struct ist uri) |
| 269 | { |
| 270 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 271 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 272 | struct ist meth, vsn; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 273 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 274 | if (!sl) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 275 | goto fail; |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 276 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 277 | /* Start by copying old method and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 278 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 279 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 280 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 281 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 282 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 283 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 284 | /* create the new start line */ |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 285 | if (!http_replace_stline(htx, meth, uri, vsn)) |
| 286 | goto fail; |
| 287 | |
| 288 | sl = http_get_stline(htx); |
| 289 | if (!http_update_host(htx, sl, uri)) |
| 290 | goto fail; |
| 291 | |
| 292 | return 1; |
| 293 | fail: |
| 294 | return 0; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* Replace the request path in the HTX message <htx> by <path>. The host part |
| 298 | * and the query string are preserved. It returns 1 on success, otherwise 0. |
| 299 | */ |
| 300 | int http_replace_req_path(struct htx *htx, const struct ist path) |
| 301 | { |
| 302 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 303 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 304 | struct ist meth, uri, vsn, p; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 305 | size_t plen = 0; |
| 306 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 307 | if (!sl) |
| 308 | return 0; |
| 309 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 310 | uri = htx_sl_req_uri(sl); |
| 311 | p = http_get_path(uri); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 312 | if (!p.ptr) |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 313 | p = uri; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 314 | while (plen < p.len && *(p.ptr + plen) != '?') |
| 315 | plen++; |
| 316 | |
| 317 | /* Start by copying old method and version and create the new uri */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 318 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 319 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 320 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 321 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 322 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
| 323 | |
| 324 | chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */ |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 325 | chunk_memcat(temp, path.ptr, path.len); /* uri: new path */ |
| 326 | chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 327 | 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] | 328 | |
| 329 | /* create the new start line */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 330 | return http_replace_stline(htx, meth, uri, vsn); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* Replace the request query-string in the HTX message <htx> by <query>. The |
| 334 | * host part and the path are preserved. It returns 1 on success, otherwise |
| 335 | * 0. |
| 336 | */ |
| 337 | int http_replace_req_query(struct htx *htx, const struct ist query) |
| 338 | { |
| 339 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 340 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 341 | struct ist meth, uri, vsn, q; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 342 | int offset = 1; |
| 343 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 344 | if (!sl) |
| 345 | return 0; |
| 346 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 347 | uri = htx_sl_req_uri(sl); |
| 348 | q = uri; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 349 | while (q.len > 0 && *(q.ptr) != '?') { |
| 350 | q.ptr++; |
| 351 | q.len--; |
| 352 | } |
| 353 | |
| 354 | /* skip the question mark or indicate that we must insert it |
| 355 | * (but only if the format string is not empty then). |
| 356 | */ |
| 357 | if (q.len) { |
| 358 | q.ptr++; |
| 359 | q.len--; |
| 360 | } |
| 361 | else if (query.len > 1) |
| 362 | offset = 0; |
| 363 | |
| 364 | /* Start by copying old method and version and create the new uri */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 365 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 366 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 367 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 368 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 369 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 370 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 371 | chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */ |
| 372 | chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */ |
| 373 | 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] | 374 | |
| 375 | /* create the new start line */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 376 | return http_replace_stline(htx, meth, uri, vsn); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* Replace the response status in the HTX message <htx> by <status>. It returns |
| 380 | * 1 on success, otherwise 0. |
| 381 | */ |
| 382 | int http_replace_res_status(struct htx *htx, const struct ist status) |
| 383 | { |
| 384 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 385 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 386 | struct ist vsn, reason; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 387 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 388 | if (!sl) |
| 389 | return 0; |
| 390 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 391 | /* Start by copying old uri and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 392 | chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */ |
| 393 | vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 394 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 395 | chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */ |
| 396 | reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 397 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 398 | /* create the new start line */ |
| 399 | sl->info.res.status = strl2ui(status.ptr, status.len); |
| 400 | return http_replace_stline(htx, vsn, status, reason); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* Replace the response reason in the HTX message <htx> by <reason>. It returns |
| 404 | * 1 on success, otherwise 0. |
| 405 | */ |
| 406 | int http_replace_res_reason(struct htx *htx, const struct ist reason) |
| 407 | { |
| 408 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 409 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 410 | struct ist vsn, status; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 411 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 412 | if (!sl) |
| 413 | return 0; |
| 414 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 415 | /* Start by copying old uri and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 416 | chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */ |
| 417 | vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 418 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 419 | chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */ |
| 420 | status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 421 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 422 | /* create the new start line */ |
| 423 | return http_replace_stline(htx, vsn, status, reason); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 426 | /* Replaces a part of a header value referenced in the context <ctx> by |
| 427 | * <data>. It returns 1 on success, otherwise it returns 0. The context is |
| 428 | * updated if necessary. |
| 429 | */ |
| 430 | int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data) |
| 431 | { |
| 432 | struct htx_blk *blk = ctx->blk; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 433 | struct htx_sl *sl; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 434 | char *start; |
| 435 | struct ist v; |
| 436 | uint32_t len, off; |
| 437 | |
| 438 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 439 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 440 | |
| 441 | v = htx_get_blk_value(htx, blk); |
| 442 | start = ctx->value.ptr - ctx->lws_before; |
| 443 | len = ctx->lws_before + ctx->value.len + ctx->lws_after; |
| 444 | off = start - v.ptr; |
| 445 | |
| 446 | blk = htx_replace_blk_value(htx, blk, ist2(start, len), data); |
| 447 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 448 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 449 | |
| 450 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 451 | |
| 452 | sl = http_get_stline(htx); |
| 453 | if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) { |
| 454 | struct ist n = htx_get_blk_name(htx, blk); |
| 455 | |
| 456 | if (isteq(n, ist("host"))) { |
| 457 | if (!http_update_authority(htx, sl, v)) |
| 458 | goto fail; |
| 459 | ctx->blk = NULL; |
| 460 | http_find_header(htx, ist("host"), ctx, 1); |
| 461 | blk = ctx->blk; |
| 462 | v = htx_get_blk_value(htx, blk); |
| 463 | } |
| 464 | } |
| 465 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 466 | ctx->blk = blk; |
| 467 | ctx->value.ptr = v.ptr + off; |
| 468 | ctx->value.len = data.len; |
| 469 | ctx->lws_before = ctx->lws_after = 0; |
| 470 | |
| 471 | return 1; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 472 | fail: |
| 473 | return 0; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* Fully replaces a header referenced in the context <ctx> by the name <name> |
| 477 | * with the value <value>. It returns 1 on success, otherwise it returns 0. The |
| 478 | * context is updated if necessary. |
| 479 | */ |
| 480 | int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx, |
| 481 | const struct ist name, const struct ist value) |
| 482 | { |
| 483 | struct htx_blk *blk = ctx->blk; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 484 | struct htx_sl *sl; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 485 | |
| 486 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 487 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 488 | |
| 489 | blk = htx_replace_header(htx, blk, name, value); |
| 490 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 491 | goto fail; |
| 492 | |
| 493 | sl = http_get_stline(htx); |
| 494 | if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteq(name, ist("host"))) { |
| 495 | if (!http_update_authority(htx, sl, value)) |
| 496 | goto fail; |
| 497 | ctx->blk = NULL; |
| 498 | http_find_header(htx, ist("host"), ctx, 1); |
| 499 | blk = ctx->blk; |
| 500 | } |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 501 | |
| 502 | ctx->blk = blk; |
| 503 | ctx->value = ist(NULL); |
| 504 | ctx->lws_before = ctx->lws_after = 0; |
| 505 | |
| 506 | return 1; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 507 | fail: |
| 508 | return 0; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* Remove one value of a header. This only works on a <ctx> returned by |
| 512 | * http_find_header function. The value is removed, as well as surrounding commas |
| 513 | * if any. If the removed value was alone, the whole header is removed. The |
| 514 | * <ctx> is always updated accordingly, as well as the HTX message <htx>. It |
| 515 | * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a |
| 516 | * form that can be handled by http_find_header() to find next occurrence. |
| 517 | */ |
| 518 | int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx) |
| 519 | { |
| 520 | struct htx_blk *blk = ctx->blk; |
| 521 | char *start; |
| 522 | struct ist v; |
| 523 | uint32_t len; |
| 524 | |
| 525 | if (!blk) |
| 526 | return 0; |
| 527 | |
| 528 | start = ctx->value.ptr - ctx->lws_before; |
| 529 | len = ctx->lws_before + ctx->value.len + ctx->lws_after; |
| 530 | |
| 531 | v = htx_get_blk_value(htx, blk); |
| 532 | if (len == v.len) { |
| 533 | blk = htx_remove_blk(htx, blk); |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 534 | if (blk || htx_is_empty(htx)) { |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 535 | ctx->blk = blk; |
| 536 | ctx->value = ist2(NULL, 0); |
| 537 | ctx->lws_before = ctx->lws_after = 0; |
| 538 | } |
| 539 | else { |
| 540 | ctx->blk = htx_get_blk(htx, htx->tail); |
| 541 | ctx->value = htx_get_blk_value(htx, ctx->blk); |
| 542 | ctx->lws_before = ctx->lws_after = 0; |
| 543 | } |
| 544 | return 1; |
| 545 | } |
| 546 | |
| 547 | /* This was not the only value of this header. We have to remove the |
| 548 | * part pointed by ctx->value. If it is the last entry of the list, we |
| 549 | * remove the last separator. |
| 550 | */ |
| 551 | if (start == v.ptr) { |
| 552 | /* It's the first header part but not the only one. So remove |
| 553 | * the comma after it. */ |
| 554 | len++; |
| 555 | } |
| 556 | else { |
| 557 | /* There is at least one header part before the removed one. So |
| 558 | * remove the comma between them. */ |
| 559 | start--; |
| 560 | len++; |
| 561 | } |
| 562 | /* Update the block content and its len */ |
| 563 | memmove(start, start+len, v.len-len); |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 564 | htx_change_blk_value_len(htx, blk, v.len-len); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 565 | |
| 566 | /* Finally update the ctx */ |
| 567 | ctx->value.ptr = start; |
| 568 | ctx->value.len = 0; |
| 569 | ctx->lws_before = ctx->lws_after = 0; |
| 570 | |
| 571 | return 1; |
| 572 | } |
Christopher Faulet | 7ff1cea | 2018-10-24 10:39:35 +0200 | [diff] [blame] | 573 | |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 574 | /* Updates the authority part of the uri with the value <host>. It happens when |
| 575 | * the header host is modified. It returns 0 on failure and 1 on success. It is |
| 576 | * the caller responsibility to provide the start-line and to be sure the uri |
| 577 | * contains an authority. Thus, if no authority is found in the uri, an error is |
| 578 | * returned. |
| 579 | */ |
| 580 | static int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host) |
| 581 | { |
| 582 | struct buffer *temp = get_trash_chunk(); |
| 583 | struct ist meth, vsn, uri, authority; |
| 584 | |
| 585 | uri = htx_sl_req_uri(sl); |
| 586 | authority = http_get_authority(uri, 1); |
Christopher Faulet | 34b18e4 | 2020-02-18 11:02:21 +0100 | [diff] [blame] | 587 | if (!authority.len) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 588 | return 0; |
| 589 | |
Christopher Faulet | 34b18e4 | 2020-02-18 11:02:21 +0100 | [diff] [blame] | 590 | /* Don't update the uri if there is no change */ |
| 591 | if (isteq(host, authority)) |
| 592 | return 1; |
| 593 | |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 594 | /* Start by copying old method and version */ |
| 595 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 596 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
| 597 | |
| 598 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 599 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
| 600 | |
| 601 | chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr); |
| 602 | chunk_memcat(temp, host.ptr, host.len); |
| 603 | chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len)); |
| 604 | uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */ |
| 605 | |
| 606 | return http_replace_stline(htx, meth, uri, vsn); |
| 607 | |
| 608 | } |
| 609 | |
| 610 | /* Update the header host by extracting the authority of the uri <uri>. flags of |
| 611 | * the start-line are also updated accordingly. For orgin-form and asterisk-form |
| 612 | * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is |
| 613 | * removed from the flags of the start-line. Otherwise, this flag is set and the |
| 614 | * authority is used to set the value of the header host. This function returns |
| 615 | * 0 on failure and 1 on success. |
| 616 | */ |
| 617 | static int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri) |
| 618 | { |
| 619 | struct ist authority; |
| 620 | struct http_hdr_ctx ctx; |
| 621 | |
| 622 | if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') { |
| 623 | // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4) |
| 624 | sl->flags &= ~HTX_SL_F_HAS_AUTHORITY; |
| 625 | } |
| 626 | else { |
| 627 | sl->flags |= HTX_SL_F_HAS_AUTHORITY; |
| 628 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 629 | // absolute-form (RFC7320 #5.3.2) |
| 630 | sl->flags |= HTX_SL_F_HAS_SCHM; |
| 631 | if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h') |
| 632 | sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS); |
| 633 | |
| 634 | authority = http_get_authority(uri, 1); |
| 635 | if (!authority.len) |
| 636 | goto fail; |
| 637 | } |
| 638 | else { |
| 639 | // authority-form (RFC7320 #5.3.3) |
| 640 | authority = uri; |
| 641 | } |
| 642 | |
| 643 | /* Replace header host value */ |
| 644 | ctx.blk = NULL; |
| 645 | while (http_find_header(htx, ist("host"), &ctx, 1)) { |
| 646 | if (!http_replace_header_value(htx, &ctx, authority)) |
| 647 | goto fail; |
| 648 | } |
| 649 | |
| 650 | } |
| 651 | return 1; |
| 652 | fail: |
| 653 | return 0; |
| 654 | } |
Christopher Faulet | 7ff1cea | 2018-10-24 10:39:35 +0200 | [diff] [blame] | 655 | |
| 656 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 657 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 658 | * performed over the whole headers. Otherwise it must contain a valid header |
| 659 | * context, initialised with ctx->blk=NULL for the first lookup in a series. If |
| 660 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 661 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 662 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 663 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 664 | * list headers. |
| 665 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 666 | */ |
| 667 | unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr, |
| 668 | int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen) |
| 669 | { |
| 670 | struct http_hdr_ctx local_ctx; |
| 671 | struct ist val_hist[MAX_HDR_HISTORY]; |
| 672 | unsigned int hist_idx; |
| 673 | int found; |
| 674 | |
| 675 | if (!ctx) { |
| 676 | local_ctx.blk = NULL; |
| 677 | ctx = &local_ctx; |
| 678 | } |
| 679 | |
| 680 | if (occ >= 0) { |
| 681 | /* search from the beginning */ |
| 682 | while (http_find_header(htx, hdr, ctx, 0)) { |
| 683 | occ--; |
| 684 | if (occ <= 0) { |
| 685 | *vptr = ctx->value.ptr; |
| 686 | *vlen = ctx->value.len; |
| 687 | return 1; |
| 688 | } |
| 689 | } |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | /* negative occurrence, we scan all the list then walk back */ |
| 694 | if (-occ > MAX_HDR_HISTORY) |
| 695 | return 0; |
| 696 | |
| 697 | found = hist_idx = 0; |
| 698 | while (http_find_header(htx, hdr, ctx, 0)) { |
| 699 | val_hist[hist_idx] = ctx->value; |
| 700 | if (++hist_idx >= MAX_HDR_HISTORY) |
| 701 | hist_idx = 0; |
| 702 | found++; |
| 703 | } |
| 704 | if (-occ > found) |
| 705 | return 0; |
| 706 | |
| 707 | /* OK now we have the last occurrence in [hist_idx-1], and we need to |
| 708 | * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have |
| 709 | * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ] |
| 710 | * to remain in the 0..9 range. |
| 711 | */ |
| 712 | hist_idx += occ + MAX_HDR_HISTORY; |
| 713 | if (hist_idx >= MAX_HDR_HISTORY) |
| 714 | hist_idx -= MAX_HDR_HISTORY; |
| 715 | *vptr = val_hist[hist_idx].ptr; |
| 716 | *vlen = val_hist[hist_idx].len; |
| 717 | return 1; |
| 718 | } |
| 719 | |
| 720 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 721 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 722 | * performed over the whole headers. Otherwise it must contain a valid header |
| 723 | * context, initialised with ctx->blk=NULL for the first lookup in a series. If |
| 724 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 725 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 726 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 727 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 728 | * line header values and does not stop at commas. |
| 729 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 730 | */ |
| 731 | unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr, |
| 732 | int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen) |
| 733 | { |
| 734 | struct http_hdr_ctx local_ctx; |
| 735 | struct ist val_hist[MAX_HDR_HISTORY]; |
| 736 | unsigned int hist_idx; |
| 737 | int found; |
| 738 | |
| 739 | if (!ctx) { |
| 740 | local_ctx.blk = NULL; |
| 741 | ctx = &local_ctx; |
| 742 | } |
| 743 | |
| 744 | if (occ >= 0) { |
| 745 | /* search from the beginning */ |
| 746 | while (http_find_header(htx, hdr, ctx, 1)) { |
| 747 | occ--; |
| 748 | if (occ <= 0) { |
| 749 | *vptr = ctx->value.ptr; |
| 750 | *vlen = ctx->value.len; |
| 751 | return 1; |
| 752 | } |
| 753 | } |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | /* negative occurrence, we scan all the list then walk back */ |
| 758 | if (-occ > MAX_HDR_HISTORY) |
| 759 | return 0; |
| 760 | |
| 761 | found = hist_idx = 0; |
| 762 | while (http_find_header(htx, hdr, ctx, 1)) { |
| 763 | val_hist[hist_idx] = ctx->value; |
| 764 | if (++hist_idx >= MAX_HDR_HISTORY) |
| 765 | hist_idx = 0; |
| 766 | found++; |
| 767 | } |
| 768 | if (-occ > found) |
| 769 | return 0; |
| 770 | |
| 771 | /* OK now we have the last occurrence in [hist_idx-1], and we need to |
| 772 | * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have |
| 773 | * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ] |
| 774 | * to remain in the 0..9 range. |
| 775 | */ |
| 776 | hist_idx += occ + MAX_HDR_HISTORY; |
| 777 | if (hist_idx >= MAX_HDR_HISTORY) |
| 778 | hist_idx -= MAX_HDR_HISTORY; |
| 779 | *vptr = val_hist[hist_idx].ptr; |
| 780 | *vlen = val_hist[hist_idx].len; |
| 781 | return 1; |
| 782 | } |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 783 | |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 784 | int http_str_to_htx(struct buffer *buf, struct ist raw) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 785 | { |
| 786 | struct htx *htx; |
| 787 | struct htx_sl *sl; |
| 788 | struct h1m h1m; |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 789 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 790 | union h1_sl h1sl; |
| 791 | unsigned int flags = HTX_SL_F_IS_RESP; |
| 792 | int ret = 0; |
| 793 | |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 794 | b_reset(buf); |
| 795 | if (!raw.len) { |
| 796 | buf->size = 0; |
| 797 | buf->area = malloc(raw.len); |
| 798 | return 1; |
| 799 | } |
| 800 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 801 | buf->size = global.tune.bufsize; |
| 802 | buf->area = (char *)malloc(buf->size); |
| 803 | if (!buf->area) |
| 804 | goto error; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 805 | |
| 806 | h1m_init_res(&h1m); |
| 807 | h1m.flags |= H1_MF_NO_PHDR; |
| 808 | ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len, |
| 809 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl); |
| 810 | if (ret <= 0) |
| 811 | goto error; |
| 812 | |
| 813 | if (unlikely(h1sl.st.v.len != 8)) |
| 814 | goto error; |
| 815 | if ((*(h1sl.st.v.ptr + 5) > '1') || |
| 816 | ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1'))) |
| 817 | h1m.flags |= H1_MF_VER_11; |
| 818 | |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 819 | if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102)) |
| 820 | goto error; |
| 821 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 822 | if (h1m.flags & H1_MF_VER_11) |
| 823 | flags |= HTX_SL_F_VER_11; |
| 824 | if (h1m.flags & H1_MF_XFER_ENC) |
| 825 | flags |= HTX_SL_F_XFER_ENC; |
Christopher Faulet | 0d4ce93 | 2019-10-16 09:09:04 +0200 | [diff] [blame] | 826 | if (h1m.flags & H1_MF_CLEN) { |
| 827 | flags |= (HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 828 | if (h1m.body_len == 0) |
| 829 | flags |= HTX_SL_F_BODYLESS; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 830 | } |
Christopher Faulet | 0d4ce93 | 2019-10-16 09:09:04 +0200 | [diff] [blame] | 831 | if (h1m.flags & H1_MF_CHNK) |
| 832 | goto error; /* Unsupported because there is no body parsing */ |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 833 | |
| 834 | htx = htx_from_buf(buf); |
| 835 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r); |
| 836 | if (!sl || !htx_add_all_headers(htx, hdrs)) |
| 837 | goto error; |
| 838 | sl->info.res.status = h1sl.st.status; |
| 839 | |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 840 | while (raw.len > ret) { |
| 841 | int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret)); |
| 842 | if (!sent) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 843 | goto error; |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 844 | ret += sent; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 845 | } |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 846 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 847 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 848 | goto error; |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 849 | |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 850 | return 1; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 851 | |
| 852 | error: |
| 853 | if (buf->size) |
| 854 | free(buf->area); |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 855 | return 0; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | static int http_htx_init(void) |
| 859 | { |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 860 | struct buffer chk; |
| 861 | struct ist raw; |
| 862 | int rc; |
| 863 | int err_code = 0; |
| 864 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 865 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 866 | if (!http_err_msgs[rc]) { |
| 867 | ha_alert("Internal error: no message defined for HTTP return code %d", rc); |
| 868 | err_code |= ERR_ALERT | ERR_FATAL; |
| 869 | continue; |
| 870 | } |
| 871 | |
| 872 | raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])); |
| 873 | if (!http_str_to_htx(&chk, raw)) { |
| 874 | ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n", |
| 875 | http_err_codes[rc]); |
| 876 | err_code |= ERR_ALERT | ERR_FATAL; |
| 877 | } |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 878 | http_err_chunks[rc] = chk; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 879 | } |
| 880 | end: |
| 881 | return err_code; |
| 882 | } |
| 883 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 884 | static void http_htx_deinit(void) |
| 885 | { |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 886 | struct http_errors *http_errs, *http_errsb; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 887 | struct ebpt_node *node, *next; |
| 888 | struct http_error *http_err; |
| 889 | |
| 890 | node = ebpt_first(&http_error_messages); |
| 891 | while (node) { |
| 892 | next = ebpt_next(node); |
| 893 | ebpt_delete(node); |
| 894 | http_err = container_of(node, typeof(*http_err), node); |
| 895 | chunk_destroy(&http_err->msg); |
| 896 | free(node->key); |
| 897 | free(http_err); |
| 898 | node = next; |
| 899 | } |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 900 | |
| 901 | list_for_each_entry_safe(http_errs, http_errsb, &http_errors_list, list) { |
| 902 | free(http_errs->conf.file); |
| 903 | free(http_errs->id); |
| 904 | LIST_DEL(&http_errs->list); |
| 905 | free(http_errs); |
| 906 | } |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 907 | } |
| 908 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 909 | REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 910 | REGISTER_POST_DEINIT(http_htx_deinit); |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 911 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 912 | /* Reads content of the error file <file> and convert it into an HTX message. On |
| 913 | * success, the HTX message is returned. On error, NULL is returned and an error |
| 914 | * message is written into the <errmsg> buffer. |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 915 | */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 916 | struct buffer *http_load_errorfile(const char *file, char **errmsg) |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 917 | { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 918 | struct buffer *buf = NULL; |
| 919 | struct buffer chk; |
| 920 | struct ebpt_node *node; |
| 921 | struct http_error *http_err; |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 922 | struct stat stat; |
| 923 | char *err = NULL; |
| 924 | int errnum, errlen; |
| 925 | int fd = -1; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 926 | |
| 927 | /* already loaded */ |
| 928 | node = ebis_lookup_len(&http_error_messages, file, strlen(file)); |
| 929 | if (node) { |
| 930 | http_err = container_of(node, typeof(*http_err), node); |
| 931 | buf = &http_err->msg; |
| 932 | goto out; |
| 933 | } |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 934 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 935 | /* Read the error file content */ |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 936 | fd = open(file, O_RDONLY); |
| 937 | if ((fd < 0) || (fstat(fd, &stat) < 0)) { |
| 938 | memprintf(errmsg, "error opening file '%s'.", file); |
| 939 | goto out; |
| 940 | } |
| 941 | |
| 942 | if (stat.st_size <= global.tune.bufsize) |
| 943 | errlen = stat.st_size; |
| 944 | else { |
| 945 | ha_warning("custom error message file '%s' larger than %d bytes. Truncating.\n", |
| 946 | file, global.tune.bufsize); |
| 947 | errlen = global.tune.bufsize; |
| 948 | } |
| 949 | |
| 950 | err = malloc(errlen); |
| 951 | if (!err) { |
| 952 | memprintf(errmsg, "out of memory."); |
| 953 | goto out; |
| 954 | } |
| 955 | |
| 956 | errnum = read(fd, err, errlen); |
| 957 | if (errnum != errlen) { |
| 958 | memprintf(errmsg, "error reading file '%s'.", file); |
| 959 | goto out; |
| 960 | } |
| 961 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 962 | /* Create the node corresponding to the error file */ |
| 963 | http_err = calloc(1, sizeof(*http_err)); |
| 964 | if (!http_err) { |
| 965 | memprintf(errmsg, "out of memory."); |
| 966 | goto out; |
| 967 | } |
| 968 | http_err->node.key = strdup(file); |
| 969 | if (!http_err->node.key) { |
| 970 | memprintf(errmsg, "out of memory."); |
Christopher Faulet | 7cde96c | 2020-01-21 10:10:11 +0100 | [diff] [blame] | 971 | free(http_err); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 972 | goto out; |
| 973 | } |
| 974 | |
| 975 | /* Convert the error file into an HTX message */ |
| 976 | if (!http_str_to_htx(&chk, ist2(err, errlen))) { |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 977 | memprintf(errmsg, "unable to convert custom error message file '%s' in HTX.", file); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 978 | free(http_err->node.key); |
| 979 | free(http_err); |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 980 | goto out; |
| 981 | } |
| 982 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 983 | /* Insert the node in the tree and return the HTX message */ |
| 984 | http_err->msg = chk; |
| 985 | ebis_insert(&http_error_messages, &http_err->node); |
| 986 | buf = &http_err->msg; |
| 987 | |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 988 | out: |
| 989 | if (fd >= 0) |
| 990 | close(fd); |
| 991 | free(err); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 992 | return buf; |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 993 | } |
| 994 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 995 | /* Convert the raw http message <msg> into an HTX message. On sucess, the HTX |
| 996 | * message is returned. On error, NULL is returned and an error message is |
| 997 | * written into the <errmsg> buffer. |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 998 | */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 999 | 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] | 1000 | { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1001 | struct buffer *buf = NULL; |
| 1002 | struct buffer chk; |
| 1003 | struct ebpt_node *node; |
| 1004 | struct http_error *http_err; |
| 1005 | |
| 1006 | /* already loaded */ |
| 1007 | node = ebis_lookup_len(&http_error_messages, key, strlen(key)); |
| 1008 | if (node) { |
| 1009 | http_err = container_of(node, typeof(*http_err), node); |
| 1010 | buf = &http_err->msg; |
| 1011 | goto out; |
| 1012 | } |
| 1013 | /* Create the node corresponding to the error file */ |
| 1014 | http_err = calloc(1, sizeof(*http_err)); |
| 1015 | if (!http_err) { |
| 1016 | memprintf(errmsg, "out of memory."); |
| 1017 | goto out; |
| 1018 | } |
| 1019 | http_err->node.key = strdup(key); |
| 1020 | if (!http_err->node.key) { |
| 1021 | memprintf(errmsg, "out of memory."); |
Christopher Faulet | 7cde96c | 2020-01-21 10:10:11 +0100 | [diff] [blame] | 1022 | free(http_err); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1023 | goto out; |
| 1024 | } |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1025 | |
| 1026 | /* Convert the error file into an HTX message */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1027 | if (!http_str_to_htx(&chk, msg)) { |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1028 | memprintf(errmsg, "unable to convert message in HTX."); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1029 | free(http_err->node.key); |
| 1030 | free(http_err); |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1031 | goto out; |
| 1032 | } |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1033 | |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1034 | /* Insert the node in the tree and return the HTX message */ |
| 1035 | http_err->msg = chk; |
| 1036 | ebis_insert(&http_error_messages, &http_err->node); |
| 1037 | buf = &http_err->msg; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1038 | out: |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1039 | return buf; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1040 | } |
| 1041 | |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1042 | /* 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] | 1043 | * <status>. It returns NULL if there is any error, otherwise it return the |
| 1044 | * corresponding HTX message. |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1045 | */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1046 | struct buffer *http_parse_errorfile(int status, const char *file, char **errmsg) |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1047 | { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1048 | struct buffer *buf = NULL; |
| 1049 | int rc; |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1050 | |
| 1051 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 1052 | if (http_err_codes[rc] == status) { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1053 | buf = http_load_errorfile(file, errmsg); |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1054 | break; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | if (rc >= HTTP_ERR_SIZE) |
| 1059 | memprintf(errmsg, "status code '%d' not handled.", status); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1060 | return buf; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | /* This function creates HTX error message corresponding to a redirect message |
| 1064 | * 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] | 1065 | * redirect. <errloc> is used to know if it is a 302 or a 303 redirect. It |
| 1066 | * returns NULL if there is any error, otherwise it return the corresponding HTX |
| 1067 | * message. |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1068 | */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1069 | 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] | 1070 | { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1071 | struct buffer *buf = NULL; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1072 | const char *msg; |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1073 | char *key = NULL, *err = NULL; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1074 | int rc, errlen; |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1075 | |
| 1076 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 1077 | if (http_err_codes[rc] == status) { |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1078 | /* Create the error key */ |
| 1079 | if (!memprintf(&key, "errorloc%d %s", errloc, url)) { |
| 1080 | memprintf(errmsg, "out of memory."); |
| 1081 | goto out; |
| 1082 | } |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1083 | /* Create the error message */ |
| 1084 | msg = (errloc == 302 ? HTTP_302 : HTTP_303); |
| 1085 | errlen = strlen(msg) + strlen(url) + 5; |
| 1086 | err = malloc(errlen); |
| 1087 | if (!err) { |
| 1088 | memprintf(errmsg, "out of memory."); |
| 1089 | goto out; |
| 1090 | } |
| 1091 | errlen = snprintf(err, errlen, "%s%s\r\n\r\n", msg, url); |
| 1092 | |
| 1093 | /* Load it */ |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1094 | buf = http_load_errormsg(key, ist2(err, errlen), errmsg); |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1095 | break; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | if (rc >= HTTP_ERR_SIZE) |
| 1100 | memprintf(errmsg, "status code '%d' not handled.", status); |
| 1101 | out: |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1102 | free(key); |
Christopher Faulet | bdf6526 | 2020-01-16 15:51:59 +0100 | [diff] [blame] | 1103 | free(err); |
Christopher Faulet | 5885775 | 2020-01-15 15:19:50 +0100 | [diff] [blame] | 1104 | return buf; |
Christopher Faulet | 5031ef5 | 2020-01-15 11:22:07 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1107 | /* Parses the "errorloc[302|303]" proxy keyword */ |
| 1108 | static int proxy_parse_errorloc(char **args, int section, struct proxy *curpx, |
| 1109 | struct proxy *defpx, const char *file, int line, |
| 1110 | char **errmsg) |
| 1111 | { |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1112 | struct conf_errors *conf_err; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1113 | struct buffer *msg; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1114 | int errloc, status; |
| 1115 | int ret = 0; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1116 | |
| 1117 | if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) { |
| 1118 | ret = 1; |
| 1119 | goto out; |
| 1120 | } |
| 1121 | |
| 1122 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 1123 | memprintf(errmsg, "%s : expects <status_code> and <url> as arguments.\n", args[0]); |
| 1124 | ret = -1; |
| 1125 | goto out; |
| 1126 | } |
| 1127 | |
| 1128 | status = atol(args[1]); |
| 1129 | errloc = (!strcmp(args[0], "errorloc303") ? 303 : 302); |
| 1130 | msg = http_parse_errorloc(errloc, status, args[2], errmsg); |
| 1131 | if (!msg) { |
| 1132 | memprintf(errmsg, "%s : %s", args[0], *errmsg); |
| 1133 | ret = -1; |
| 1134 | goto out; |
| 1135 | } |
| 1136 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1137 | conf_err = calloc(1, sizeof(*conf_err)); |
| 1138 | if (!conf_err) { |
| 1139 | memprintf(errmsg, "%s : out of memory.", args[0]); |
| 1140 | ret = -1; |
| 1141 | goto out; |
| 1142 | } |
| 1143 | conf_err->type = 1; |
| 1144 | conf_err->info.errorfile.status = status; |
| 1145 | conf_err->info.errorfile.msg = msg; |
| 1146 | conf_err->file = strdup(file); |
| 1147 | conf_err->line = line; |
| 1148 | LIST_ADDQ(&curpx->conf.errors, &conf_err->list); |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1149 | |
| 1150 | out: |
| 1151 | return ret; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1152 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1153 | } |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1154 | |
| 1155 | /* Parses the "errorfile" proxy keyword */ |
| 1156 | static int proxy_parse_errorfile(char **args, int section, struct proxy *curpx, |
| 1157 | struct proxy *defpx, const char *file, int line, |
| 1158 | char **errmsg) |
| 1159 | { |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1160 | struct conf_errors *conf_err; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1161 | struct buffer *msg; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1162 | int status; |
| 1163 | int ret = 0; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1164 | |
| 1165 | if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) { |
| 1166 | ret = 1; |
| 1167 | goto out; |
| 1168 | } |
| 1169 | |
| 1170 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 1171 | memprintf(errmsg, "%s : expects <status_code> and <file> as arguments.\n", args[0]); |
| 1172 | ret = -1; |
| 1173 | goto out; |
| 1174 | } |
| 1175 | |
| 1176 | status = atol(args[1]); |
| 1177 | msg = http_parse_errorfile(status, args[2], errmsg); |
| 1178 | if (!msg) { |
| 1179 | memprintf(errmsg, "%s : %s", args[0], *errmsg); |
| 1180 | ret = -1; |
| 1181 | goto out; |
| 1182 | } |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1183 | |
| 1184 | conf_err = calloc(1, sizeof(*conf_err)); |
| 1185 | if (!conf_err) { |
| 1186 | memprintf(errmsg, "%s : out of memory.", args[0]); |
| 1187 | ret = -1; |
| 1188 | goto out; |
| 1189 | } |
| 1190 | conf_err->type = 1; |
| 1191 | conf_err->info.errorfile.status = status; |
| 1192 | conf_err->info.errorfile.msg = msg; |
| 1193 | conf_err->file = strdup(file); |
| 1194 | conf_err->line = line; |
| 1195 | LIST_ADDQ(&curpx->conf.errors, &conf_err->list); |
| 1196 | |
| 1197 | out: |
| 1198 | return ret; |
| 1199 | |
| 1200 | } |
| 1201 | |
| 1202 | /* Parses the "errorfiles" proxy keyword */ |
| 1203 | static int proxy_parse_errorfiles(char **args, int section, struct proxy *curpx, |
| 1204 | struct proxy *defpx, const char *file, int line, |
| 1205 | char **err) |
| 1206 | { |
| 1207 | struct conf_errors *conf_err = NULL; |
| 1208 | char *name = NULL; |
| 1209 | int rc, ret = 0; |
| 1210 | |
| 1211 | if (warnifnotcap(curpx, PR_CAP_FE | PR_CAP_BE, file, line, args[0], NULL)) { |
| 1212 | ret = 1; |
| 1213 | goto out; |
| 1214 | } |
| 1215 | |
| 1216 | if (!*(args[1])) { |
| 1217 | memprintf(err, "%s : expects <name> as argument.", args[0]); |
| 1218 | ret = -1; |
| 1219 | goto out; |
| 1220 | } |
| 1221 | |
| 1222 | name = strdup(args[1]); |
| 1223 | conf_err = calloc(1, sizeof(*conf_err)); |
| 1224 | if (!name || !conf_err) { |
| 1225 | memprintf(err, "%s : out of memory.", args[0]); |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1226 | goto error; |
| 1227 | } |
| 1228 | conf_err->type = 0; |
| 1229 | |
| 1230 | conf_err->info.errorfiles.name = name; |
| 1231 | if (!*(args[2])) { |
| 1232 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) |
| 1233 | conf_err->info.errorfiles.status[rc] = 1; |
| 1234 | } |
| 1235 | else { |
| 1236 | int cur_arg, status; |
| 1237 | for (cur_arg = 2; *(args[cur_arg]); cur_arg++) { |
| 1238 | status = atol(args[cur_arg]); |
| 1239 | |
| 1240 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 1241 | if (http_err_codes[rc] == status) { |
| 1242 | conf_err->info.errorfiles.status[rc] = 2; |
| 1243 | break; |
| 1244 | } |
| 1245 | } |
| 1246 | if (rc >= HTTP_ERR_SIZE) { |
| 1247 | memprintf(err, "%s : status code '%d' not handled.", args[0], status); |
Christopher Faulet | 7cde96c | 2020-01-21 10:10:11 +0100 | [diff] [blame] | 1248 | goto error; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | conf_err->file = strdup(file); |
| 1253 | conf_err->line = line; |
| 1254 | LIST_ADDQ(&curpx->conf.errors, &conf_err->list); |
| 1255 | out: |
| 1256 | return ret; |
| 1257 | |
| 1258 | error: |
| 1259 | free(name); |
| 1260 | free(conf_err); |
Christopher Faulet | 7cde96c | 2020-01-21 10:10:11 +0100 | [diff] [blame] | 1261 | ret = -1; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1262 | goto out; |
| 1263 | } |
| 1264 | |
| 1265 | /* Check "errorfiles" proxy keyword */ |
| 1266 | static int proxy_check_errors(struct proxy *px) |
| 1267 | { |
| 1268 | struct conf_errors *conf_err, *conf_err_back; |
| 1269 | struct http_errors *http_errs; |
| 1270 | int rc, err = 0; |
| 1271 | |
| 1272 | list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) { |
| 1273 | if (conf_err->type == 1) { |
| 1274 | /* errorfile */ |
| 1275 | rc = http_get_status_idx(conf_err->info.errorfile.status); |
| 1276 | px->errmsg[rc] = conf_err->info.errorfile.msg; |
| 1277 | } |
| 1278 | else { |
| 1279 | /* errorfiles */ |
| 1280 | list_for_each_entry(http_errs, &http_errors_list, list) { |
| 1281 | if (strcmp(http_errs->id, conf_err->info.errorfiles.name) == 0) |
| 1282 | break; |
| 1283 | } |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1284 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1285 | /* unknown http-errors section */ |
| 1286 | if (&http_errs->list == &http_errors_list) { |
| 1287 | ha_alert("config : proxy '%s': unknown http-errors section '%s' (at %s:%d).\n", |
| 1288 | px->id, conf_err->info.errorfiles.name, conf_err->file, conf_err->line); |
| 1289 | err |= ERR_ALERT | ERR_FATAL; |
| 1290 | free(conf_err->info.errorfiles.name); |
| 1291 | goto next; |
| 1292 | } |
| 1293 | |
| 1294 | free(conf_err->info.errorfiles.name); |
| 1295 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 1296 | if (conf_err->info.errorfiles.status[rc] > 0) { |
| 1297 | if (http_errs->errmsg[rc]) |
| 1298 | px->errmsg[rc] = http_errs->errmsg[rc]; |
| 1299 | else if (conf_err->info.errorfiles.status[rc] == 2) |
| 1300 | ha_warning("config: proxy '%s' : status '%d' not declared in" |
| 1301 | " http-errors section '%s' (at %s:%d).\n", |
| 1302 | px->id, http_err_codes[rc], http_errs->id, |
| 1303 | conf_err->file, conf_err->line); |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | next: |
| 1308 | LIST_DEL(&conf_err->list); |
| 1309 | free(conf_err->file); |
| 1310 | free(conf_err); |
| 1311 | } |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1312 | |
| 1313 | out: |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1314 | return err; |
| 1315 | } |
| 1316 | |
Christopher Faulet | 0a589fd | 2020-01-22 14:47:04 +0100 | [diff] [blame] | 1317 | static int post_check_errors() |
| 1318 | { |
| 1319 | struct ebpt_node *node; |
| 1320 | struct http_error *http_err; |
| 1321 | struct htx *htx; |
| 1322 | int err_code = 0; |
| 1323 | |
| 1324 | node = ebpt_first(&http_error_messages); |
| 1325 | while (node) { |
| 1326 | http_err = container_of(node, typeof(*http_err), node); |
| 1327 | if (b_is_null(&http_err->msg)) |
| 1328 | goto next; |
| 1329 | htx = htxbuf(&http_err->msg); |
| 1330 | if (htx_free_data_space(htx) < global.tune.maxrewrite) { |
| 1331 | ha_warning("config: errorfile '%s' runs over the buffer space" |
| 1332 | " reserved to headers rewritting. It may lead to internal errors if " |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 1333 | " http-after-response rules are evaluated on this message.\n", |
Christopher Faulet | 0a589fd | 2020-01-22 14:47:04 +0100 | [diff] [blame] | 1334 | (char *)node->key); |
| 1335 | err_code |= ERR_WARN; |
| 1336 | } |
| 1337 | next: |
| 1338 | node = ebpt_next(node); |
| 1339 | } |
| 1340 | |
| 1341 | return err_code; |
| 1342 | } |
| 1343 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1344 | int proxy_dup_default_conf_errors(struct proxy *curpx, struct proxy *defpx, char **errmsg) |
| 1345 | { |
| 1346 | struct conf_errors *conf_err, *new_conf_err = NULL; |
| 1347 | int ret = 0; |
| 1348 | |
| 1349 | list_for_each_entry(conf_err, &defpx->conf.errors, list) { |
| 1350 | new_conf_err = calloc(1, sizeof(*new_conf_err)); |
| 1351 | if (!new_conf_err) { |
| 1352 | memprintf(errmsg, "unable to duplicate default errors (out of memory)."); |
| 1353 | goto out; |
| 1354 | } |
| 1355 | new_conf_err->type = conf_err->type; |
| 1356 | if (conf_err->type == 1) { |
| 1357 | new_conf_err->info.errorfile.status = conf_err->info.errorfile.status; |
| 1358 | new_conf_err->info.errorfile.msg = conf_err->info.errorfile.msg; |
| 1359 | } |
| 1360 | else { |
| 1361 | new_conf_err->info.errorfiles.name = strdup(conf_err->info.errorfiles.name); |
| 1362 | if (!new_conf_err->info.errorfiles.name) { |
| 1363 | memprintf(errmsg, "unable to duplicate default errors (out of memory)."); |
| 1364 | goto out; |
| 1365 | } |
| 1366 | memcpy(&new_conf_err->info.errorfiles.status, &conf_err->info.errorfiles.status, |
| 1367 | sizeof(conf_err->info.errorfiles.status)); |
| 1368 | } |
| 1369 | new_conf_err->file = strdup(conf_err->file); |
| 1370 | new_conf_err->line = conf_err->line; |
| 1371 | LIST_ADDQ(&curpx->conf.errors, &new_conf_err->list); |
| 1372 | new_conf_err = NULL; |
| 1373 | } |
| 1374 | ret = 1; |
| 1375 | |
| 1376 | out: |
| 1377 | free(new_conf_err); |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1378 | return ret; |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | void proxy_release_conf_errors(struct proxy *px) |
| 1382 | { |
| 1383 | struct conf_errors *conf_err, *conf_err_back; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1384 | |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1385 | list_for_each_entry_safe(conf_err, conf_err_back, &px->conf.errors, list) { |
| 1386 | if (conf_err->type == 0) |
| 1387 | free(conf_err->info.errorfiles.name); |
| 1388 | LIST_DEL(&conf_err->list); |
| 1389 | free(conf_err->file); |
| 1390 | free(conf_err); |
| 1391 | } |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | /* |
| 1395 | * Parse an <http-errors> section. |
| 1396 | * Returns the error code, 0 if OK, or any combination of : |
| 1397 | * - ERR_ABORT: must abort ASAP |
| 1398 | * - ERR_FATAL: we can continue parsing but not start the service |
| 1399 | * - ERR_WARN: a warning has been emitted |
| 1400 | * - ERR_ALERT: an alert has been emitted |
| 1401 | * Only the two first ones can stop processing, the two others are just |
| 1402 | * indicators. |
| 1403 | */ |
| 1404 | static int cfg_parse_http_errors(const char *file, int linenum, char **args, int kwm) |
| 1405 | { |
| 1406 | static struct http_errors *curr_errs = NULL; |
| 1407 | int err_code = 0; |
| 1408 | const char *err; |
| 1409 | char *errmsg = NULL; |
| 1410 | |
| 1411 | if (strcmp(args[0], "http-errors") == 0) { /* new errors section */ |
| 1412 | if (!*args[1]) { |
| 1413 | ha_alert("parsing [%s:%d] : missing name for http-errors section.\n", file, linenum); |
| 1414 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1415 | goto out; |
| 1416 | } |
| 1417 | |
| 1418 | err = invalid_char(args[1]); |
| 1419 | if (err) { |
| 1420 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 1421 | file, linenum, *err, args[0], args[1]); |
| 1422 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1423 | } |
| 1424 | |
| 1425 | list_for_each_entry(curr_errs, &http_errors_list, list) { |
| 1426 | /* Error if two errors section owns the same name */ |
| 1427 | if (strcmp(curr_errs->id, args[1]) == 0) { |
| 1428 | ha_alert("parsing [%s:%d]: http-errors section '%s' already exists (declared at %s:%d).\n", |
| 1429 | file, linenum, args[1], curr_errs->conf.file, curr_errs->conf.line); |
| 1430 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | if ((curr_errs = calloc(1, sizeof(*curr_errs))) == NULL) { |
| 1435 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 1436 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1437 | goto out; |
| 1438 | } |
| 1439 | |
| 1440 | LIST_ADDQ(&http_errors_list, &curr_errs->list); |
| 1441 | curr_errs->id = strdup(args[1]); |
| 1442 | curr_errs->conf.file = strdup(file); |
| 1443 | curr_errs->conf.line = linenum; |
| 1444 | } |
| 1445 | else if (!strcmp(args[0], "errorfile")) { /* error message from a file */ |
| 1446 | struct buffer *msg; |
| 1447 | int status, rc; |
| 1448 | |
| 1449 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 1450 | ha_alert("parsing [%s:%d] : %s: expects <status_code> and <file> as arguments.\n", |
| 1451 | file, linenum, args[0]); |
| 1452 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1453 | goto out; |
| 1454 | } |
| 1455 | |
| 1456 | status = atol(args[1]); |
| 1457 | msg = http_parse_errorfile(status, args[2], &errmsg); |
| 1458 | if (!msg) { |
| 1459 | ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); |
| 1460 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1461 | goto out; |
| 1462 | } |
| 1463 | rc = http_get_status_idx(status); |
| 1464 | curr_errs->errmsg[rc] = msg; |
| 1465 | } |
| 1466 | else if (*args[0] != 0) { |
| 1467 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection); |
| 1468 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1469 | goto out; |
| 1470 | } |
| 1471 | |
| 1472 | out: |
| 1473 | free(errmsg); |
| 1474 | return err_code; |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1478 | { CFG_LISTEN, "errorloc", proxy_parse_errorloc }, |
| 1479 | { CFG_LISTEN, "errorloc302", proxy_parse_errorloc }, |
| 1480 | { CFG_LISTEN, "errorloc303", proxy_parse_errorloc }, |
| 1481 | { CFG_LISTEN, "errorfile", proxy_parse_errorfile }, |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1482 | { CFG_LISTEN, "errorfiles", proxy_parse_errorfiles }, |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1483 | { 0, NULL, NULL }, |
| 1484 | }}; |
| 1485 | |
| 1486 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
Christopher Faulet | 76edc0f | 2020-01-13 15:52:01 +0100 | [diff] [blame] | 1487 | REGISTER_POST_PROXY_CHECK(proxy_check_errors); |
Christopher Faulet | 0a589fd | 2020-01-22 14:47:04 +0100 | [diff] [blame] | 1488 | REGISTER_POST_CHECK(post_check_errors); |
Christopher Faulet | 07f41f7 | 2020-01-16 16:16:06 +0100 | [diff] [blame] | 1489 | |
Christopher Faulet | 35cd81d | 2020-01-15 11:22:56 +0100 | [diff] [blame] | 1490 | REGISTER_CONFIG_SECTION("http-errors", cfg_parse_http_errors, NULL); |
| 1491 | |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 1492 | /************************************************************************/ |
| 1493 | /* HTX sample fetches */ |
| 1494 | /************************************************************************/ |
| 1495 | |
| 1496 | /* Returns 1 if a stream is an HTX stream. Otherwise, it returns 0. */ |
| 1497 | static int |
| 1498 | smp_fetch_is_htx(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1499 | { |
| 1500 | if (!smp->strm) |
| 1501 | return 0; |
| 1502 | |
| 1503 | smp->data.u.sint = !!IS_HTX_STRM(smp->strm); |
| 1504 | smp->data.type = SMP_T_BOOL; |
| 1505 | return 1; |
| 1506 | } |
| 1507 | |
| 1508 | /* Returns the number of blocks in an HTX message. The channel is chosen |
| 1509 | * depending on the sample direction. */ |
| 1510 | static int |
| 1511 | smp_fetch_htx_nbblks(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1512 | { |
| 1513 | struct channel *chn; |
| 1514 | struct htx *htx; |
| 1515 | |
| 1516 | if (!smp->strm) |
| 1517 | return 0; |
| 1518 | |
| 1519 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1520 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1521 | if (!htx) |
| 1522 | return 0; |
| 1523 | |
| 1524 | smp->data.u.sint = htx_nbblks(htx); |
| 1525 | smp->data.type = SMP_T_SINT; |
| 1526 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1527 | return 1; |
| 1528 | } |
| 1529 | |
| 1530 | /* Returns the size of an HTX message. The channel is chosen depending on the |
| 1531 | * sample direction. */ |
| 1532 | static int |
| 1533 | smp_fetch_htx_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1534 | { |
| 1535 | struct channel *chn; |
| 1536 | struct htx *htx; |
| 1537 | |
| 1538 | if (!smp->strm) |
| 1539 | return 0; |
| 1540 | |
| 1541 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1542 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1543 | if (!htx) |
| 1544 | return 0; |
| 1545 | |
| 1546 | smp->data.u.sint = htx->size; |
| 1547 | smp->data.type = SMP_T_SINT; |
| 1548 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1549 | return 1; |
| 1550 | } |
| 1551 | |
| 1552 | /* Returns the data size of an HTX message. The channel is chosen depending on the |
| 1553 | * sample direction. */ |
| 1554 | static int |
| 1555 | smp_fetch_htx_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1556 | { |
| 1557 | struct channel *chn; |
| 1558 | struct htx *htx; |
| 1559 | |
| 1560 | if (!smp->strm) |
| 1561 | return 0; |
| 1562 | |
| 1563 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1564 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1565 | if (!htx) |
| 1566 | return 0; |
| 1567 | |
| 1568 | smp->data.u.sint = htx->data; |
| 1569 | smp->data.type = SMP_T_SINT; |
| 1570 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1571 | return 1; |
| 1572 | } |
| 1573 | |
| 1574 | /* Returns the used space (data+meta) of an HTX message. The channel is chosen |
| 1575 | * depending on the sample direction. */ |
| 1576 | static int |
| 1577 | smp_fetch_htx_used(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1578 | { |
| 1579 | struct channel *chn; |
| 1580 | struct htx *htx; |
| 1581 | |
| 1582 | if (!smp->strm) |
| 1583 | return 0; |
| 1584 | |
| 1585 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1586 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1587 | if (!htx) |
| 1588 | return 0; |
| 1589 | |
| 1590 | smp->data.u.sint = htx_used_space(htx); |
| 1591 | smp->data.type = SMP_T_SINT; |
| 1592 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1593 | return 1; |
| 1594 | } |
| 1595 | |
| 1596 | /* Returns the free space (size-used) of an HTX message. The channel is chosen |
| 1597 | * depending on the sample direction. */ |
| 1598 | static int |
| 1599 | smp_fetch_htx_free(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1600 | { |
| 1601 | struct channel *chn; |
| 1602 | struct htx *htx; |
| 1603 | |
| 1604 | if (!smp->strm) |
| 1605 | return 0; |
| 1606 | |
| 1607 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1608 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1609 | if (!htx) |
| 1610 | return 0; |
| 1611 | |
| 1612 | smp->data.u.sint = htx_free_space(htx); |
| 1613 | smp->data.type = SMP_T_SINT; |
| 1614 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1615 | return 1; |
| 1616 | } |
| 1617 | |
| 1618 | /* Returns the free space for data (free-sizeof(blk)) of an HTX message. The |
| 1619 | * channel is chosen depending on the sample direction. */ |
| 1620 | static int |
| 1621 | smp_fetch_htx_free_data(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1622 | { |
| 1623 | struct channel *chn; |
| 1624 | struct htx *htx; |
| 1625 | |
| 1626 | if (!smp->strm) |
| 1627 | return 0; |
| 1628 | |
| 1629 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1630 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1631 | if (!htx) |
| 1632 | return 0; |
| 1633 | |
| 1634 | smp->data.u.sint = htx_free_data_space(htx); |
| 1635 | smp->data.type = SMP_T_SINT; |
| 1636 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1637 | return 1; |
| 1638 | } |
| 1639 | |
| 1640 | /* Returns 1 if the HTX message contains an EOM block. Otherwise it returns |
| 1641 | * 0. Concretely, it only checks the tail. The channel is chosen depending on |
| 1642 | * the sample direction. */ |
| 1643 | static int |
| 1644 | smp_fetch_htx_has_eom(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1645 | { |
| 1646 | struct channel *chn; |
| 1647 | struct htx *htx; |
| 1648 | |
| 1649 | if (!smp->strm) |
| 1650 | return 0; |
| 1651 | |
| 1652 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1653 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1654 | if (!htx) |
| 1655 | return 0; |
| 1656 | |
| 1657 | smp->data.u.sint = (htx_get_tail_type(htx) == HTX_BLK_EOM); |
| 1658 | smp->data.type = SMP_T_BOOL; |
| 1659 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1660 | return 1; |
| 1661 | } |
| 1662 | |
| 1663 | /* Returns the type of a specific HTX block, if found in the message. Otherwise |
| 1664 | * HTX_BLK_UNUSED is returned. Any positive integer (>= 0) is supported or |
| 1665 | * "head", "tail" or "first". The channel is chosen depending on the sample |
| 1666 | * direction. */ |
| 1667 | static int |
| 1668 | smp_fetch_htx_blk_type(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1669 | { |
| 1670 | struct channel *chn; |
| 1671 | struct htx *htx; |
| 1672 | enum htx_blk_type type; |
| 1673 | int32_t pos; |
| 1674 | |
| 1675 | if (!smp->strm || !arg_p) |
| 1676 | return 0; |
| 1677 | |
| 1678 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1679 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1680 | if (!htx) |
| 1681 | return 0; |
| 1682 | |
| 1683 | pos = arg_p[0].data.sint; |
| 1684 | if (pos == -1) |
| 1685 | type = htx_get_head_type(htx); |
| 1686 | else if (pos == -2) |
| 1687 | type = htx_get_tail_type(htx); |
| 1688 | else if (pos == -3) |
| 1689 | type = htx_get_first_type(htx); |
| 1690 | else |
| 1691 | type = ((pos >= htx->head && pos <= htx->tail) |
| 1692 | ? htx_get_blk_type(htx_get_blk(htx, pos)) |
| 1693 | : HTX_BLK_UNUSED); |
| 1694 | |
| 1695 | chunk_initstr(&smp->data.u.str, htx_blk_type_str(type)); |
| 1696 | smp->data.type = SMP_T_STR; |
| 1697 | smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1698 | return 1; |
| 1699 | } |
| 1700 | |
| 1701 | /* Returns the size of a specific HTX block, if found in the message. Otherwise |
| 1702 | * 0 is returned. Any positive integer (>= 0) is supported or "head", "tail" or |
| 1703 | * "first". The channel is chosen depending on the sample direction. */ |
| 1704 | static int |
| 1705 | smp_fetch_htx_blk_size(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1706 | { |
| 1707 | struct channel *chn; |
| 1708 | struct htx *htx; |
| 1709 | struct htx_blk *blk; |
| 1710 | int32_t pos; |
| 1711 | |
| 1712 | if (!smp->strm || !arg_p) |
| 1713 | return 0; |
| 1714 | |
| 1715 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1716 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1717 | if (!htx) |
| 1718 | return 0; |
| 1719 | |
| 1720 | pos = arg_p[0].data.sint; |
| 1721 | if (pos == -1) |
| 1722 | blk = htx_get_head_blk(htx); |
| 1723 | else if (pos == -2) |
| 1724 | blk = htx_get_tail_blk(htx); |
| 1725 | else if (pos == -3) |
| 1726 | blk = htx_get_first_blk(htx); |
| 1727 | else |
| 1728 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 1729 | |
| 1730 | smp->data.u.sint = (blk ? htx_get_blksz(blk) : 0); |
| 1731 | smp->data.type = SMP_T_SINT; |
| 1732 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1733 | return 1; |
| 1734 | } |
| 1735 | |
| 1736 | /* Returns the start-line if the selected HTX block exists and is a |
| 1737 | * start-line. Otherwise 0 an empty string. Any positive integer (>= 0) is |
| 1738 | * supported or "head", "tail" or "first". The channel is chosen depending on |
| 1739 | * the sample direction. */ |
| 1740 | static int |
| 1741 | smp_fetch_htx_blk_stline(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1742 | { |
| 1743 | struct buffer *temp; |
| 1744 | struct channel *chn; |
| 1745 | struct htx *htx; |
| 1746 | struct htx_blk *blk; |
| 1747 | struct htx_sl *sl; |
| 1748 | int32_t pos; |
| 1749 | |
| 1750 | if (!smp->strm || !arg_p) |
| 1751 | return 0; |
| 1752 | |
| 1753 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1754 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1755 | if (!htx) |
| 1756 | return 0; |
| 1757 | |
| 1758 | pos = arg_p[0].data.sint; |
| 1759 | if (pos == -1) |
| 1760 | blk = htx_get_head_blk(htx); |
| 1761 | else if (pos == -2) |
| 1762 | blk = htx_get_tail_blk(htx); |
| 1763 | else if (pos == -3) |
| 1764 | blk = htx_get_first_blk(htx); |
| 1765 | else |
| 1766 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 1767 | |
| 1768 | if (!blk || (htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL)) { |
| 1769 | smp->data.u.str.size = 0; |
| 1770 | smp->data.u.str.area = ""; |
| 1771 | smp->data.u.str.data = 0; |
| 1772 | } |
| 1773 | else { |
| 1774 | sl = htx_get_blk_ptr(htx, blk); |
| 1775 | |
| 1776 | temp = get_trash_chunk(); |
| 1777 | chunk_istcat(temp, htx_sl_p1(sl)); |
| 1778 | temp->area[temp->data++] = ' '; |
| 1779 | chunk_istcat(temp, htx_sl_p2(sl)); |
| 1780 | temp->area[temp->data++] = ' '; |
| 1781 | chunk_istcat(temp, htx_sl_p3(sl)); |
| 1782 | |
| 1783 | smp->data.u.str = *temp; |
| 1784 | } |
| 1785 | |
| 1786 | smp->data.type = SMP_T_STR; |
| 1787 | smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1788 | return 1; |
| 1789 | } |
| 1790 | |
| 1791 | /* Returns the header name if the selected HTX block exists and is a header or a |
| 1792 | * trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is |
| 1793 | * supported or "head", "tail" or "first". The channel is chosen depending on |
| 1794 | * the sample direction. */ |
| 1795 | static int |
| 1796 | smp_fetch_htx_blk_hdrname(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1797 | { |
| 1798 | struct channel *chn; |
| 1799 | struct htx *htx; |
| 1800 | struct htx_blk *blk; |
| 1801 | int32_t pos; |
| 1802 | |
| 1803 | if (!smp->strm || !arg_p) |
| 1804 | return 0; |
| 1805 | |
| 1806 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1807 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1808 | if (!htx) |
| 1809 | return 0; |
| 1810 | |
| 1811 | pos = arg_p[0].data.sint; |
| 1812 | if (pos == -1) |
| 1813 | blk = htx_get_head_blk(htx); |
| 1814 | else if (pos == -2) |
| 1815 | blk = htx_get_tail_blk(htx); |
| 1816 | else if (pos == -3) |
| 1817 | blk = htx_get_first_blk(htx); |
| 1818 | else |
| 1819 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 1820 | |
| 1821 | if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) { |
| 1822 | smp->data.u.str.size = 0; |
| 1823 | smp->data.u.str.area = ""; |
| 1824 | smp->data.u.str.data = 0; |
| 1825 | } |
| 1826 | else { |
| 1827 | struct ist name = htx_get_blk_name(htx, blk); |
| 1828 | |
| 1829 | chunk_initlen(&smp->data.u.str, name.ptr, name.len, name.len); |
| 1830 | } |
| 1831 | smp->data.type = SMP_T_STR; |
| 1832 | smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1833 | return 1; |
| 1834 | } |
| 1835 | |
| 1836 | /* Returns the header value if the selected HTX block exists and is a header or |
| 1837 | * a trailer. Otherwise 0 an empty string. Any positive integer (>= 0) is |
| 1838 | * supported or "head", "tail" or "first". The channel is chosen depending on |
| 1839 | * the sample direction. */ |
| 1840 | static int |
| 1841 | smp_fetch_htx_blk_hdrval(const struct arg *arg_p, struct sample *smp, const char *kw, void *private) |
| 1842 | { |
| 1843 | struct channel *chn; |
| 1844 | struct htx *htx; |
| 1845 | struct htx_blk *blk; |
| 1846 | int32_t pos; |
| 1847 | |
| 1848 | if (!smp->strm || !arg_p) |
| 1849 | return 0; |
| 1850 | |
| 1851 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1852 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1853 | if (!htx) |
| 1854 | return 0; |
| 1855 | |
| 1856 | pos = arg_p[0].data.sint; |
| 1857 | if (pos == -1) |
| 1858 | blk = htx_get_head_blk(htx); |
| 1859 | else if (pos == -2) |
| 1860 | blk = htx_get_tail_blk(htx); |
| 1861 | else if (pos == -3) |
| 1862 | blk = htx_get_first_blk(htx); |
| 1863 | else |
| 1864 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 1865 | |
| 1866 | if (!blk || (htx_get_blk_type(blk) != HTX_BLK_HDR && htx_get_blk_type(blk) != HTX_BLK_TLR)) { |
| 1867 | smp->data.u.str.size = 0; |
| 1868 | smp->data.u.str.area = ""; |
| 1869 | smp->data.u.str.data = 0; |
| 1870 | } |
| 1871 | else { |
| 1872 | struct ist val = htx_get_blk_value(htx, blk); |
| 1873 | |
| 1874 | chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len); |
| 1875 | } |
| 1876 | smp->data.type = SMP_T_STR; |
| 1877 | smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1878 | return 1; |
| 1879 | } |
| 1880 | |
| 1881 | /* Returns the value if the selected HTX block exists and is a data |
| 1882 | * block. Otherwise 0 an empty string. Any positive integer (>= 0) is supported |
| 1883 | * or "head", "tail" or "first". The channel is chosen depending on the sample |
| 1884 | * direction. */ |
| 1885 | static int |
Christopher Faulet | c5db14c | 2020-01-08 14:51:03 +0100 | [diff] [blame] | 1886 | 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] | 1887 | { |
| 1888 | struct channel *chn; |
| 1889 | struct htx *htx; |
| 1890 | struct htx_blk *blk; |
| 1891 | int32_t pos; |
| 1892 | |
| 1893 | if (!smp->strm || !arg_p) |
| 1894 | return 0; |
| 1895 | |
| 1896 | chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; |
| 1897 | htx = smp_prefetch_htx(smp, chn, 0); |
| 1898 | if (!htx) |
| 1899 | return 0; |
| 1900 | |
| 1901 | pos = arg_p[0].data.sint; |
| 1902 | if (pos == -1) |
| 1903 | blk = htx_get_head_blk(htx); |
| 1904 | else if (pos == -2) |
| 1905 | blk = htx_get_tail_blk(htx); |
| 1906 | else if (pos == -3) |
| 1907 | blk = htx_get_first_blk(htx); |
| 1908 | else |
| 1909 | blk = ((pos >= htx->head && pos <= htx->tail) ? htx_get_blk(htx, pos) : NULL); |
| 1910 | |
| 1911 | if (!blk || htx_get_blk_type(blk) != HTX_BLK_DATA) { |
| 1912 | smp->data.u.str.size = 0; |
| 1913 | smp->data.u.str.area = ""; |
| 1914 | smp->data.u.str.data = 0; |
| 1915 | } |
| 1916 | else { |
| 1917 | struct ist val = htx_get_blk_value(htx, blk); |
| 1918 | |
| 1919 | chunk_initlen(&smp->data.u.str, val.ptr, val.len, val.len); |
| 1920 | } |
Christopher Faulet | 8178e40 | 2020-01-08 14:38:58 +0100 | [diff] [blame] | 1921 | smp->data.type = SMP_T_BIN; |
Christopher Faulet | 29f7284 | 2019-12-11 15:52:32 +0100 | [diff] [blame] | 1922 | smp->flags = SMP_F_CONST | SMP_F_VOLATILE | SMP_F_MAY_CHANGE; |
| 1923 | return 1; |
| 1924 | } |
| 1925 | |
| 1926 | /* This function is used to validate the arguments passed to any "htx_blk" fetch |
| 1927 | * keywords. An argument is expected by these keywords. It must be a positive |
| 1928 | * integer or on of the following strings: "head", "tail" or "first". It returns |
| 1929 | * 0 on error, and a non-zero value if OK. |
| 1930 | */ |
| 1931 | int val_blk_arg(struct arg *arg, char **err_msg) |
| 1932 | { |
| 1933 | if (arg[0].type != ARGT_STR || !arg[0].data.str.data) { |
| 1934 | memprintf(err_msg, "a block position is expected (> 0) or a special block name (head, tail, first)"); |
| 1935 | return 0; |
| 1936 | } |
| 1937 | if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "head", 4)) { |
| 1938 | free(arg[0].data.str.area); |
| 1939 | arg[0].type = ARGT_SINT; |
| 1940 | arg[0].data.sint = -1; |
| 1941 | } |
| 1942 | else if (arg[0].data.str.data == 4 && !strncmp(arg[0].data.str.area, "tail", 4)) { |
| 1943 | free(arg[0].data.str.area); |
| 1944 | arg[0].type = ARGT_SINT; |
| 1945 | arg[0].data.sint = -2; |
| 1946 | } |
| 1947 | else if (arg[0].data.str.data == 5 && !strncmp(arg[0].data.str.area, "first", 5)) { |
| 1948 | free(arg[0].data.str.area); |
| 1949 | arg[0].type = ARGT_SINT; |
| 1950 | arg[0].data.sint = -3; |
| 1951 | } |
| 1952 | else { |
| 1953 | int pos; |
| 1954 | |
| 1955 | for (pos = 0; pos < arg[0].data.str.data; pos++) { |
| 1956 | if (!isdigit(arg[0].data.str.area[pos])) { |
| 1957 | memprintf(err_msg, "invalid block position"); |
| 1958 | return 0; |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | pos = strl2uic(arg[0].data.str.area, arg[0].data.str.data); |
| 1963 | if (pos < 0) { |
| 1964 | memprintf(err_msg, "block position must not be negative"); |
| 1965 | return 0; |
| 1966 | } |
| 1967 | free(arg[0].data.str.area); |
| 1968 | arg[0].type = ARGT_SINT; |
| 1969 | arg[0].data.sint = pos; |
| 1970 | } |
| 1971 | |
| 1972 | return 1; |
| 1973 | } |
| 1974 | |
| 1975 | |
| 1976 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1977 | * Note: htx sample fetches should only used for developpement purpose. |
| 1978 | */ |
| 1979 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Christopher Faulet | 01f4445 | 2020-01-08 14:23:40 +0100 | [diff] [blame] | 1980 | { "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] | 1981 | |
Christopher Faulet | 01f4445 | 2020-01-08 14:23:40 +0100 | [diff] [blame] | 1982 | { "internal.htx.nbblks", smp_fetch_htx_nbblks, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1983 | { "internal.htx.size", smp_fetch_htx_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1984 | { "internal.htx.data", smp_fetch_htx_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1985 | { "internal.htx.used", smp_fetch_htx_used, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1986 | { "internal.htx.free", smp_fetch_htx_free, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1987 | { "internal.htx.free_data", smp_fetch_htx_free_data, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1988 | { "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] | 1989 | |
Christopher Faulet | 01f4445 | 2020-01-08 14:23:40 +0100 | [diff] [blame] | 1990 | { "internal.htx_blk.type", smp_fetch_htx_blk_type, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1991 | { "internal.htx_blk.size", smp_fetch_htx_blk_size, ARG1(1,STR), val_blk_arg, SMP_T_SINT, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1992 | { "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}, |
| 1993 | { "internal.htx_blk.hdrname", smp_fetch_htx_blk_hdrname, ARG1(1,STR), val_blk_arg, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV}, |
| 1994 | { "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] | 1995 | { "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] | 1996 | |
| 1997 | { /* END */ }, |
| 1998 | }}; |
| 1999 | |
| 2000 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |