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