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 | |
| 20 | #include <proto/http_htx.h> |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 21 | |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 22 | struct buffer http_err_chunks[HTTP_ERR_SIZE]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 23 | |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 24 | static int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host); |
| 25 | static int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri); |
| 26 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 27 | /* 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] | 28 | * if the start-line is undefined (first == -1). Otherwise, it returns the |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 29 | * pointer on the htx_sl structure. |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 30 | */ |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 31 | struct htx_sl *http_get_stline(struct htx *htx) |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 32 | { |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 33 | struct htx_blk *blk; |
Christopher Faulet | 573fe73 | 2018-11-28 16:55:12 +0100 | [diff] [blame] | 34 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 35 | BUG_ON(htx->first == -1); |
| 36 | blk = htx_get_first_blk(htx); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 37 | if (!blk) |
| 38 | return NULL; |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 39 | 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] | 40 | return htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* Finds the first or next occurrence of header <name> in the HTX message <htx> |
| 44 | * using the context <ctx>. This structure holds everything necessary to use the |
| 45 | * header and find next occurrence. If its <blk> member is NULL, the header is |
| 46 | * searched from the beginning. Otherwise, the next occurrence is returned. The |
| 47 | * function returns 1 when it finds a value, and 0 when there is no more. It is |
| 48 | * designed to work with headers defined as comma-separated lists. If <full> is |
| 49 | * set, it works on full-line headers in whose comma is not a delimiter but is |
| 50 | * part of the syntax. A special case, if ctx->value is NULL when searching for |
| 51 | * a new values of a header, the current header is rescanned. This allows |
| 52 | * rescanning after a header deletion. |
| 53 | */ |
| 54 | int http_find_header(const struct htx *htx, const struct ist name, |
| 55 | struct http_hdr_ctx *ctx, int full) |
| 56 | { |
| 57 | struct htx_blk *blk = ctx->blk; |
| 58 | struct ist n, v; |
| 59 | enum htx_blk_type type; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 60 | |
| 61 | if (blk) { |
| 62 | char *p; |
| 63 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 64 | if (!ctx->value.ptr) |
| 65 | goto rescan_hdr; |
| 66 | if (full) |
| 67 | goto next_blk; |
| 68 | v = htx_get_blk_value(htx, blk); |
| 69 | p = ctx->value.ptr + ctx->value.len + ctx->lws_after; |
| 70 | v.len -= (p - v.ptr); |
| 71 | v.ptr = p; |
| 72 | if (!v.len) |
| 73 | goto next_blk; |
| 74 | /* Skip comma */ |
| 75 | if (*(v.ptr) == ',') { |
| 76 | v.ptr++; |
| 77 | v.len--; |
| 78 | } |
| 79 | |
| 80 | goto return_hdr; |
| 81 | } |
| 82 | |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 83 | if (htx_is_empty(htx)) |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 84 | return 0; |
| 85 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 86 | 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] | 87 | rescan_hdr: |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 88 | type = htx_get_blk_type(blk); |
Christopher Faulet | 573fe73 | 2018-11-28 16:55:12 +0100 | [diff] [blame] | 89 | if (type == HTX_BLK_EOH || type == HTX_BLK_EOM) |
| 90 | break; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 91 | if (type != HTX_BLK_HDR) |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 92 | continue; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 93 | if (name.len) { |
| 94 | /* If no name was passed, we want any header. So skip the comparison */ |
| 95 | n = htx_get_blk_name(htx, blk); |
| 96 | if (!isteqi(n, name)) |
| 97 | goto next_blk; |
| 98 | } |
| 99 | v = htx_get_blk_value(htx, blk); |
| 100 | |
| 101 | return_hdr: |
| 102 | ctx->lws_before = 0; |
| 103 | ctx->lws_after = 0; |
| 104 | while (v.len && HTTP_IS_LWS(*v.ptr)) { |
| 105 | v.ptr++; |
| 106 | v.len--; |
| 107 | ctx->lws_before++; |
| 108 | } |
| 109 | if (!full) |
| 110 | v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr; |
| 111 | while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) { |
| 112 | v.len--; |
| 113 | ctx->lws_after++; |
| 114 | } |
| 115 | if (!v.len) |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 116 | continue; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 117 | ctx->blk = blk; |
| 118 | ctx->value = v; |
| 119 | return 1; |
| 120 | |
| 121 | next_blk: |
Christopher Faulet | 28f29c7 | 2019-04-30 17:55:45 +0200 | [diff] [blame] | 122 | ; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | ctx->blk = NULL; |
| 126 | ctx->value = ist(""); |
| 127 | ctx->lws_before = ctx->lws_after = 0; |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /* Adds a header block int the HTX message <htx>, just before the EOH block. It |
| 132 | * returns 1 on success, otherwise it returns 0. |
| 133 | */ |
| 134 | int http_add_header(struct htx *htx, const struct ist n, const struct ist v) |
| 135 | { |
| 136 | struct htx_blk *blk; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 137 | struct htx_sl *sl; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 138 | enum htx_blk_type type = htx_get_tail_type(htx); |
| 139 | int32_t prev; |
| 140 | |
| 141 | blk = htx_add_header(htx, n, v); |
| 142 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 143 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 144 | |
| 145 | if (unlikely(type < HTX_BLK_EOH)) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 146 | goto end; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 147 | |
| 148 | /* <blk> is the head, swap it iteratively with its predecessor to place |
| 149 | * it just before the end-of-header block. So blocks remains ordered. */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 150 | 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] | 151 | struct htx_blk *pblk = htx_get_blk(htx, prev); |
| 152 | enum htx_blk_type type = htx_get_blk_type(pblk); |
| 153 | |
| 154 | /* Swap .addr and .info fields */ |
| 155 | blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr; |
| 156 | blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info; |
| 157 | |
| 158 | if (blk->addr == pblk->addr) |
| 159 | blk->addr += htx_get_blksz(pblk); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 160 | |
| 161 | /* Stop when end-of-header is reached */ |
| 162 | if (type == HTX_BLK_EOH) |
| 163 | break; |
| 164 | |
| 165 | blk = pblk; |
| 166 | } |
Christopher Faulet | 05aab64 | 2019-04-11 13:43:57 +0200 | [diff] [blame] | 167 | |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 168 | end: |
| 169 | sl = http_get_stline(htx); |
| 170 | if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteq(n, ist("host"))) { |
| 171 | if (!http_update_authority(htx, sl, v)) |
| 172 | goto fail; |
| 173 | } |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 174 | return 1; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 175 | |
| 176 | fail: |
| 177 | return 0; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 180 | /* 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] | 181 | * success, otherwise it returns 0. |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 182 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 183 | 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] | 184 | { |
Christopher Faulet | 7b7d507 | 2019-05-13 15:22:59 +0200 | [diff] [blame] | 185 | struct htx_blk *blk; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 186 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 187 | blk = htx_get_first_blk(htx); |
| 188 | if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3)) |
Christopher Faulet | 7b7d507 | 2019-05-13 15:22:59 +0200 | [diff] [blame] | 189 | return 0; |
| 190 | return 1; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 193 | /* Replace the request method in the HTX message <htx> by <meth>. It returns 1 |
| 194 | * on success, otherwise 0. |
| 195 | */ |
| 196 | int http_replace_req_meth(struct htx *htx, const struct ist meth) |
| 197 | { |
| 198 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 199 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 200 | struct ist uri, vsn; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 201 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 202 | if (!sl) |
| 203 | return 0; |
| 204 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 205 | /* Start by copying old uri and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 206 | chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */ |
| 207 | uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 208 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 209 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 210 | vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 211 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 212 | /* create the new start line */ |
| 213 | sl->info.req.meth = find_http_meth(meth.ptr, meth.len); |
| 214 | return http_replace_stline(htx, meth, uri, vsn); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on |
| 218 | * success, otherwise 0. |
| 219 | */ |
| 220 | int http_replace_req_uri(struct htx *htx, const struct ist uri) |
| 221 | { |
| 222 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 223 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 224 | struct ist meth, vsn; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 225 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 226 | if (!sl) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 227 | goto fail; |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 228 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 229 | /* Start by copying old method and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 230 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 231 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 232 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 233 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 234 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 235 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 236 | /* create the new start line */ |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 237 | if (!http_replace_stline(htx, meth, uri, vsn)) |
| 238 | goto fail; |
| 239 | |
| 240 | sl = http_get_stline(htx); |
| 241 | if (!http_update_host(htx, sl, uri)) |
| 242 | goto fail; |
| 243 | |
| 244 | return 1; |
| 245 | fail: |
| 246 | return 0; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* Replace the request path in the HTX message <htx> by <path>. The host part |
| 250 | * and the query string are preserved. It returns 1 on success, otherwise 0. |
| 251 | */ |
| 252 | int http_replace_req_path(struct htx *htx, const struct ist path) |
| 253 | { |
| 254 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 255 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 256 | struct ist meth, uri, vsn, p; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 257 | size_t plen = 0; |
| 258 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 259 | if (!sl) |
| 260 | return 0; |
| 261 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 262 | uri = htx_sl_req_uri(sl); |
| 263 | p = http_get_path(uri); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 264 | if (!p.ptr) |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 265 | p = uri; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 266 | while (plen < p.len && *(p.ptr + plen) != '?') |
| 267 | plen++; |
| 268 | |
| 269 | /* Start by copying old method and version and create the new uri */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 270 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 271 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 272 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 273 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 274 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
| 275 | |
| 276 | chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */ |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 277 | chunk_memcat(temp, path.ptr, path.len); /* uri: new path */ |
| 278 | chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 279 | 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] | 280 | |
| 281 | /* create the new start line */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 282 | return http_replace_stline(htx, meth, uri, vsn); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* Replace the request query-string in the HTX message <htx> by <query>. The |
| 286 | * host part and the path are preserved. It returns 1 on success, otherwise |
| 287 | * 0. |
| 288 | */ |
| 289 | int http_replace_req_query(struct htx *htx, const struct ist query) |
| 290 | { |
| 291 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 292 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 293 | struct ist meth, uri, vsn, q; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 294 | int offset = 1; |
| 295 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 296 | if (!sl) |
| 297 | return 0; |
| 298 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 299 | uri = htx_sl_req_uri(sl); |
| 300 | q = uri; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 301 | while (q.len > 0 && *(q.ptr) != '?') { |
| 302 | q.ptr++; |
| 303 | q.len--; |
| 304 | } |
| 305 | |
| 306 | /* skip the question mark or indicate that we must insert it |
| 307 | * (but only if the format string is not empty then). |
| 308 | */ |
| 309 | if (q.len) { |
| 310 | q.ptr++; |
| 311 | q.len--; |
| 312 | } |
| 313 | else if (query.len > 1) |
| 314 | offset = 0; |
| 315 | |
| 316 | /* Start by copying old method and version and create the new uri */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 317 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 318 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 319 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 320 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 321 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 322 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 323 | chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */ |
| 324 | chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */ |
| 325 | 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] | 326 | |
| 327 | /* create the new start line */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 328 | return http_replace_stline(htx, meth, uri, vsn); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* Replace the response status in the HTX message <htx> by <status>. It returns |
| 332 | * 1 on success, otherwise 0. |
| 333 | */ |
| 334 | int http_replace_res_status(struct htx *htx, const struct ist status) |
| 335 | { |
| 336 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 337 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 338 | struct ist vsn, reason; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 339 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 340 | if (!sl) |
| 341 | return 0; |
| 342 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 343 | /* Start by copying old uri and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 344 | chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */ |
| 345 | vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 346 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 347 | chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */ |
| 348 | reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 349 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 350 | /* create the new start line */ |
| 351 | sl->info.res.status = strl2ui(status.ptr, status.len); |
| 352 | return http_replace_stline(htx, vsn, status, reason); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* Replace the response reason in the HTX message <htx> by <reason>. It returns |
| 356 | * 1 on success, otherwise 0. |
| 357 | */ |
| 358 | int http_replace_res_reason(struct htx *htx, const struct ist reason) |
| 359 | { |
| 360 | struct buffer *temp = get_trash_chunk(); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 361 | struct htx_sl *sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 362 | struct ist vsn, status; |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 363 | |
Willy Tarreau | cdce54c | 2019-02-12 12:02:27 +0100 | [diff] [blame] | 364 | if (!sl) |
| 365 | return 0; |
| 366 | |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 367 | /* Start by copying old uri and version */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 368 | chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */ |
| 369 | vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 370 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 371 | chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */ |
| 372 | status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl)); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 373 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 374 | /* create the new start line */ |
| 375 | return http_replace_stline(htx, vsn, status, reason); |
Christopher Faulet | e010c80 | 2018-10-24 10:36:45 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 378 | /* Replaces a part of a header value referenced in the context <ctx> by |
| 379 | * <data>. It returns 1 on success, otherwise it returns 0. The context is |
| 380 | * updated if necessary. |
| 381 | */ |
| 382 | int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data) |
| 383 | { |
| 384 | struct htx_blk *blk = ctx->blk; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 385 | struct htx_sl *sl; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 386 | char *start; |
| 387 | struct ist v; |
| 388 | uint32_t len, off; |
| 389 | |
| 390 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 391 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 392 | |
| 393 | v = htx_get_blk_value(htx, blk); |
| 394 | start = ctx->value.ptr - ctx->lws_before; |
| 395 | len = ctx->lws_before + ctx->value.len + ctx->lws_after; |
| 396 | off = start - v.ptr; |
| 397 | |
| 398 | blk = htx_replace_blk_value(htx, blk, ist2(start, len), data); |
| 399 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 400 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 401 | |
| 402 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 403 | |
| 404 | sl = http_get_stline(htx); |
| 405 | if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY)) { |
| 406 | struct ist n = htx_get_blk_name(htx, blk); |
| 407 | |
| 408 | if (isteq(n, ist("host"))) { |
| 409 | if (!http_update_authority(htx, sl, v)) |
| 410 | goto fail; |
| 411 | ctx->blk = NULL; |
| 412 | http_find_header(htx, ist("host"), ctx, 1); |
| 413 | blk = ctx->blk; |
| 414 | v = htx_get_blk_value(htx, blk); |
| 415 | } |
| 416 | } |
| 417 | |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 418 | ctx->blk = blk; |
| 419 | ctx->value.ptr = v.ptr + off; |
| 420 | ctx->value.len = data.len; |
| 421 | ctx->lws_before = ctx->lws_after = 0; |
| 422 | |
| 423 | return 1; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 424 | fail: |
| 425 | return 0; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /* Fully replaces a header referenced in the context <ctx> by the name <name> |
| 429 | * with the value <value>. It returns 1 on success, otherwise it returns 0. The |
| 430 | * context is updated if necessary. |
| 431 | */ |
| 432 | int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx, |
| 433 | const struct ist name, const struct ist value) |
| 434 | { |
| 435 | struct htx_blk *blk = ctx->blk; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 436 | struct htx_sl *sl; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 437 | |
| 438 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 439 | goto fail; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 440 | |
| 441 | blk = htx_replace_header(htx, blk, name, value); |
| 442 | if (!blk) |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 443 | goto fail; |
| 444 | |
| 445 | sl = http_get_stline(htx); |
| 446 | if (sl && (sl->flags & HTX_SL_F_HAS_AUTHORITY) && isteq(name, ist("host"))) { |
| 447 | if (!http_update_authority(htx, sl, value)) |
| 448 | goto fail; |
| 449 | ctx->blk = NULL; |
| 450 | http_find_header(htx, ist("host"), ctx, 1); |
| 451 | blk = ctx->blk; |
| 452 | } |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 453 | |
| 454 | ctx->blk = blk; |
| 455 | ctx->value = ist(NULL); |
| 456 | ctx->lws_before = ctx->lws_after = 0; |
| 457 | |
| 458 | return 1; |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 459 | fail: |
| 460 | return 0; |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* Remove one value of a header. This only works on a <ctx> returned by |
| 464 | * http_find_header function. The value is removed, as well as surrounding commas |
| 465 | * if any. If the removed value was alone, the whole header is removed. The |
| 466 | * <ctx> is always updated accordingly, as well as the HTX message <htx>. It |
| 467 | * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a |
| 468 | * form that can be handled by http_find_header() to find next occurrence. |
| 469 | */ |
| 470 | int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx) |
| 471 | { |
| 472 | struct htx_blk *blk = ctx->blk; |
| 473 | char *start; |
| 474 | struct ist v; |
| 475 | uint32_t len; |
| 476 | |
| 477 | if (!blk) |
| 478 | return 0; |
| 479 | |
| 480 | start = ctx->value.ptr - ctx->lws_before; |
| 481 | len = ctx->lws_before + ctx->value.len + ctx->lws_after; |
| 482 | |
| 483 | v = htx_get_blk_value(htx, blk); |
| 484 | if (len == v.len) { |
| 485 | blk = htx_remove_blk(htx, blk); |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 486 | if (blk || htx_is_empty(htx)) { |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 487 | ctx->blk = blk; |
| 488 | ctx->value = ist2(NULL, 0); |
| 489 | ctx->lws_before = ctx->lws_after = 0; |
| 490 | } |
| 491 | else { |
| 492 | ctx->blk = htx_get_blk(htx, htx->tail); |
| 493 | ctx->value = htx_get_blk_value(htx, ctx->blk); |
| 494 | ctx->lws_before = ctx->lws_after = 0; |
| 495 | } |
| 496 | return 1; |
| 497 | } |
| 498 | |
| 499 | /* This was not the only value of this header. We have to remove the |
| 500 | * part pointed by ctx->value. If it is the last entry of the list, we |
| 501 | * remove the last separator. |
| 502 | */ |
| 503 | if (start == v.ptr) { |
| 504 | /* It's the first header part but not the only one. So remove |
| 505 | * the comma after it. */ |
| 506 | len++; |
| 507 | } |
| 508 | else { |
| 509 | /* There is at least one header part before the removed one. So |
| 510 | * remove the comma between them. */ |
| 511 | start--; |
| 512 | len++; |
| 513 | } |
| 514 | /* Update the block content and its len */ |
| 515 | memmove(start, start+len, v.len-len); |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 516 | htx_change_blk_value_len(htx, blk, v.len-len); |
Christopher Faulet | 47596d3 | 2018-10-22 09:17:28 +0200 | [diff] [blame] | 517 | |
| 518 | /* Finally update the ctx */ |
| 519 | ctx->value.ptr = start; |
| 520 | ctx->value.len = 0; |
| 521 | ctx->lws_before = ctx->lws_after = 0; |
| 522 | |
| 523 | return 1; |
| 524 | } |
Christopher Faulet | 7ff1cea | 2018-10-24 10:39:35 +0200 | [diff] [blame] | 525 | |
Christopher Faulet | d7b7a1c | 2019-10-08 15:24:52 +0200 | [diff] [blame] | 526 | /* Updates the authority part of the uri with the value <host>. It happens when |
| 527 | * the header host is modified. It returns 0 on failure and 1 on success. It is |
| 528 | * the caller responsibility to provide the start-line and to be sure the uri |
| 529 | * contains an authority. Thus, if no authority is found in the uri, an error is |
| 530 | * returned. |
| 531 | */ |
| 532 | static int http_update_authority(struct htx *htx, struct htx_sl *sl, const struct ist host) |
| 533 | { |
| 534 | struct buffer *temp = get_trash_chunk(); |
| 535 | struct ist meth, vsn, uri, authority; |
| 536 | |
| 537 | uri = htx_sl_req_uri(sl); |
| 538 | authority = http_get_authority(uri, 1); |
| 539 | if (!authority.len || isteq(host, authority)) |
| 540 | return 0; |
| 541 | |
| 542 | /* Start by copying old method and version */ |
| 543 | chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */ |
| 544 | meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl)); |
| 545 | |
| 546 | chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */ |
| 547 | vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl)); |
| 548 | |
| 549 | chunk_memcat(temp, uri.ptr, authority.ptr - uri.ptr); |
| 550 | chunk_memcat(temp, host.ptr, host.len); |
| 551 | chunk_memcat(temp, authority.ptr + authority.len, uri.ptr + uri.len - (authority.ptr + authority.len)); |
| 552 | uri = ist2(temp->area + meth.len + vsn.len, host.len + uri.len - authority.len); /* uri */ |
| 553 | |
| 554 | return http_replace_stline(htx, meth, uri, vsn); |
| 555 | |
| 556 | } |
| 557 | |
| 558 | /* Update the header host by extracting the authority of the uri <uri>. flags of |
| 559 | * the start-line are also updated accordingly. For orgin-form and asterisk-form |
| 560 | * uri, the header host is not changed and the flag HTX_SL_F_HAS_AUTHORITY is |
| 561 | * removed from the flags of the start-line. Otherwise, this flag is set and the |
| 562 | * authority is used to set the value of the header host. This function returns |
| 563 | * 0 on failure and 1 on success. |
| 564 | */ |
| 565 | static int http_update_host(struct htx *htx, struct htx_sl *sl, const struct ist uri) |
| 566 | { |
| 567 | struct ist authority; |
| 568 | struct http_hdr_ctx ctx; |
| 569 | |
| 570 | if (!uri.len || uri.ptr[0] == '/' || uri.ptr[0] == '*') { |
| 571 | // origin-form or a asterisk-form (RFC7320 #5.3.1 and #5.3.4) |
| 572 | sl->flags &= ~HTX_SL_F_HAS_AUTHORITY; |
| 573 | } |
| 574 | else { |
| 575 | sl->flags |= HTX_SL_F_HAS_AUTHORITY; |
| 576 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 577 | // absolute-form (RFC7320 #5.3.2) |
| 578 | sl->flags |= HTX_SL_F_HAS_SCHM; |
| 579 | if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h') |
| 580 | sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS); |
| 581 | |
| 582 | authority = http_get_authority(uri, 1); |
| 583 | if (!authority.len) |
| 584 | goto fail; |
| 585 | } |
| 586 | else { |
| 587 | // authority-form (RFC7320 #5.3.3) |
| 588 | authority = uri; |
| 589 | } |
| 590 | |
| 591 | /* Replace header host value */ |
| 592 | ctx.blk = NULL; |
| 593 | while (http_find_header(htx, ist("host"), &ctx, 1)) { |
| 594 | if (!http_replace_header_value(htx, &ctx, authority)) |
| 595 | goto fail; |
| 596 | } |
| 597 | |
| 598 | } |
| 599 | return 1; |
| 600 | fail: |
| 601 | return 0; |
| 602 | } |
Christopher Faulet | 7ff1cea | 2018-10-24 10:39:35 +0200 | [diff] [blame] | 603 | |
| 604 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 605 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 606 | * performed over the whole headers. Otherwise it must contain a valid header |
| 607 | * context, initialised with ctx->blk=NULL for the first lookup in a series. If |
| 608 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 609 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 610 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 611 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 612 | * list headers. |
| 613 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 614 | */ |
| 615 | unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr, |
| 616 | int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen) |
| 617 | { |
| 618 | struct http_hdr_ctx local_ctx; |
| 619 | struct ist val_hist[MAX_HDR_HISTORY]; |
| 620 | unsigned int hist_idx; |
| 621 | int found; |
| 622 | |
| 623 | if (!ctx) { |
| 624 | local_ctx.blk = NULL; |
| 625 | ctx = &local_ctx; |
| 626 | } |
| 627 | |
| 628 | if (occ >= 0) { |
| 629 | /* search from the beginning */ |
| 630 | while (http_find_header(htx, hdr, ctx, 0)) { |
| 631 | occ--; |
| 632 | if (occ <= 0) { |
| 633 | *vptr = ctx->value.ptr; |
| 634 | *vlen = ctx->value.len; |
| 635 | return 1; |
| 636 | } |
| 637 | } |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | /* negative occurrence, we scan all the list then walk back */ |
| 642 | if (-occ > MAX_HDR_HISTORY) |
| 643 | return 0; |
| 644 | |
| 645 | found = hist_idx = 0; |
| 646 | while (http_find_header(htx, hdr, ctx, 0)) { |
| 647 | val_hist[hist_idx] = ctx->value; |
| 648 | if (++hist_idx >= MAX_HDR_HISTORY) |
| 649 | hist_idx = 0; |
| 650 | found++; |
| 651 | } |
| 652 | if (-occ > found) |
| 653 | return 0; |
| 654 | |
| 655 | /* OK now we have the last occurrence in [hist_idx-1], and we need to |
| 656 | * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have |
| 657 | * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ] |
| 658 | * to remain in the 0..9 range. |
| 659 | */ |
| 660 | hist_idx += occ + MAX_HDR_HISTORY; |
| 661 | if (hist_idx >= MAX_HDR_HISTORY) |
| 662 | hist_idx -= MAX_HDR_HISTORY; |
| 663 | *vptr = val_hist[hist_idx].ptr; |
| 664 | *vlen = val_hist[hist_idx].len; |
| 665 | return 1; |
| 666 | } |
| 667 | |
| 668 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 669 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 670 | * performed over the whole headers. Otherwise it must contain a valid header |
| 671 | * context, initialised with ctx->blk=NULL for the first lookup in a series. If |
| 672 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 673 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 674 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 675 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 676 | * line header values and does not stop at commas. |
| 677 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 678 | */ |
| 679 | unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr, |
| 680 | int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen) |
| 681 | { |
| 682 | struct http_hdr_ctx local_ctx; |
| 683 | struct ist val_hist[MAX_HDR_HISTORY]; |
| 684 | unsigned int hist_idx; |
| 685 | int found; |
| 686 | |
| 687 | if (!ctx) { |
| 688 | local_ctx.blk = NULL; |
| 689 | ctx = &local_ctx; |
| 690 | } |
| 691 | |
| 692 | if (occ >= 0) { |
| 693 | /* search from the beginning */ |
| 694 | while (http_find_header(htx, hdr, ctx, 1)) { |
| 695 | occ--; |
| 696 | if (occ <= 0) { |
| 697 | *vptr = ctx->value.ptr; |
| 698 | *vlen = ctx->value.len; |
| 699 | return 1; |
| 700 | } |
| 701 | } |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | /* negative occurrence, we scan all the list then walk back */ |
| 706 | if (-occ > MAX_HDR_HISTORY) |
| 707 | return 0; |
| 708 | |
| 709 | found = hist_idx = 0; |
| 710 | while (http_find_header(htx, hdr, ctx, 1)) { |
| 711 | val_hist[hist_idx] = ctx->value; |
| 712 | if (++hist_idx >= MAX_HDR_HISTORY) |
| 713 | hist_idx = 0; |
| 714 | found++; |
| 715 | } |
| 716 | if (-occ > found) |
| 717 | return 0; |
| 718 | |
| 719 | /* OK now we have the last occurrence in [hist_idx-1], and we need to |
| 720 | * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have |
| 721 | * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ] |
| 722 | * to remain in the 0..9 range. |
| 723 | */ |
| 724 | hist_idx += occ + MAX_HDR_HISTORY; |
| 725 | if (hist_idx >= MAX_HDR_HISTORY) |
| 726 | hist_idx -= MAX_HDR_HISTORY; |
| 727 | *vptr = val_hist[hist_idx].ptr; |
| 728 | *vlen = val_hist[hist_idx].len; |
| 729 | return 1; |
| 730 | } |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 731 | |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 732 | int http_str_to_htx(struct buffer *buf, struct ist raw) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 733 | { |
| 734 | struct htx *htx; |
| 735 | struct htx_sl *sl; |
| 736 | struct h1m h1m; |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 737 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 738 | union h1_sl h1sl; |
| 739 | unsigned int flags = HTX_SL_F_IS_RESP; |
| 740 | int ret = 0; |
| 741 | |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 742 | b_reset(buf); |
| 743 | if (!raw.len) { |
| 744 | buf->size = 0; |
| 745 | buf->area = malloc(raw.len); |
| 746 | return 1; |
| 747 | } |
| 748 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 749 | buf->size = global.tune.bufsize; |
| 750 | buf->area = (char *)malloc(buf->size); |
| 751 | if (!buf->area) |
| 752 | goto error; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 753 | |
| 754 | h1m_init_res(&h1m); |
| 755 | h1m.flags |= H1_MF_NO_PHDR; |
| 756 | ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len, |
| 757 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl); |
| 758 | if (ret <= 0) |
| 759 | goto error; |
| 760 | |
| 761 | if (unlikely(h1sl.st.v.len != 8)) |
| 762 | goto error; |
| 763 | if ((*(h1sl.st.v.ptr + 5) > '1') || |
| 764 | ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1'))) |
| 765 | h1m.flags |= H1_MF_VER_11; |
| 766 | |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 767 | if (h1sl.st.status < 200 && (h1sl.st.status == 100 || h1sl.st.status >= 102)) |
| 768 | goto error; |
| 769 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 770 | if (h1m.flags & H1_MF_VER_11) |
| 771 | flags |= HTX_SL_F_VER_11; |
| 772 | if (h1m.flags & H1_MF_XFER_ENC) |
| 773 | flags |= HTX_SL_F_XFER_ENC; |
| 774 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 775 | flags |= HTX_SL_F_XFER_LEN; |
| 776 | if (h1m.flags & H1_MF_CHNK) |
| 777 | goto error; /* Unsupported because there is no body parsing */ |
| 778 | else if (h1m.flags & H1_MF_CLEN) { |
| 779 | flags |= HTX_SL_F_CLEN; |
| 780 | if (h1m.body_len == 0) |
| 781 | flags |= HTX_SL_F_BODYLESS; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | htx = htx_from_buf(buf); |
| 786 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r); |
| 787 | if (!sl || !htx_add_all_headers(htx, hdrs)) |
| 788 | goto error; |
| 789 | sl->info.res.status = h1sl.st.status; |
| 790 | |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 791 | while (raw.len > ret) { |
| 792 | int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret)); |
| 793 | if (!sent) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 794 | goto error; |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 795 | ret += sent; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 796 | } |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 797 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 798 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 799 | goto error; |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 800 | |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 801 | return 1; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 802 | |
| 803 | error: |
| 804 | if (buf->size) |
| 805 | free(buf->area); |
Christopher Faulet | 90cc481 | 2019-07-22 16:49:30 +0200 | [diff] [blame] | 806 | return 0; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | static int http_htx_init(void) |
| 810 | { |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 811 | struct buffer chk; |
| 812 | struct ist raw; |
| 813 | int rc; |
| 814 | int err_code = 0; |
| 815 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 816 | for (rc = 0; rc < HTTP_ERR_SIZE; rc++) { |
| 817 | if (!http_err_msgs[rc]) { |
| 818 | ha_alert("Internal error: no message defined for HTTP return code %d", rc); |
| 819 | err_code |= ERR_ALERT | ERR_FATAL; |
| 820 | continue; |
| 821 | } |
| 822 | |
| 823 | raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc])); |
| 824 | if (!http_str_to_htx(&chk, raw)) { |
| 825 | ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n", |
| 826 | http_err_codes[rc]); |
| 827 | err_code |= ERR_ALERT | ERR_FATAL; |
| 828 | } |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 829 | http_err_chunks[rc] = chk; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 830 | } |
| 831 | end: |
| 832 | return err_code; |
| 833 | } |
| 834 | |
| 835 | REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init); |