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