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