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