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