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