From: Christopher Faulet Date: Thu, 3 Dec 2026 20:43:37 +0000 (+0100) Subject: BUG/MINOR: htx: preserve the first block when a message is moved to another buffer X-Git-Url: https://git.kaiwu.me/web/http/$%7BGITURL%7D/static/gitweb.js?a=commitdiff_plain;h=1d5b9dfafd34aeb2cb2d1866a22cdf9bc3956a34;p=haproxy.git BUG/MINOR: htx: preserve the first block when a message is moved to another buffer htx_move_to_large_buffer() and similar functions rely on htx_xfer() to transfer an HTX message to a larger or smaller buffer. But htx_xfer() adds new blocks in the destination message, so the first block to (re)start the analysis from (htx->first) was always the head one. When some blocks were before it in the source message, for instance an interim response not forwarded yet, the wrong block was used as first block after the transfer. For instance, after a "wait-for-body" action moving a response to a large buffer, the next "http-response" rules could be evaluated on a 1xx response instead of the final one. To fix the issue, a new xfer flag is added, HTX_XFER_KEEP_FIRST. When it is set and the destination message is empty, the block copied from the first block of the source message becomes the first block of the destination message. This flag is now used by the functions moving or copying a message to a small or large buffer. This patch must be backported to 3.4. --- diff --git a/include/haproxy/htx.h b/include/haproxy/htx.h index 0d6bd22c6..bc1e7826e 100644 --- a/include/haproxy/htx.h +++ b/include/haproxy/htx.h @@ -67,6 +67,7 @@ struct buffer *htx_copy_to_large_buffer(struct buffer *dst, struct buffer *src); #define HTX_XFER_PARTIAL_HDRS_COPY 0x00000002 /* Allow partial copy of headers and trailers part */ #define HTX_XFER_HDRS_ONLY 0x00000004 /* Only Transfer header blocks (start-line, header and EOH) */ #define HTX_XFER_NO_METADATA 0x00000008 /* don't include meta-data, only payload */ +#define HTX_XFER_KEEP_FIRST 0x00000010 /* Preserve the first block of (htx->first) in */ size_t htx_xfer(struct htx *dst, struct htx *src, size_t count, unsigned int flags); diff --git a/src/htx.c b/src/htx.c index 6e6e41244..9a5493cf3 100644 --- a/src/htx.c +++ b/src/htx.c @@ -864,6 +864,13 @@ struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk, * - HTX_XFER_PARTIAL_HDRS_COPY: partial headers and trailers part can be xferred * - HTX_XFER_HDRS_ONLY: Only the headers part is xferred * - HTX_XFER_NO_METADATA: don't include meta-data, only payload + * - HTX_XFER_KEEP_FIRST: the block in copied from the first block of + * (htx->first) becomes the first block of . + * It is only supported if is empty, otherwise + * it is ignored. If the copy is rolled back, the + * first block of is reset when the block is + * removed, and the next added block will be the + * first one. */ size_t htx_xfer(struct htx *dst, struct htx *src, size_t count, unsigned int flags) { @@ -873,6 +880,7 @@ size_t htx_xfer(struct htx *dst, struct htx *src, size_t count, unsigned int fla uint32_t max, last_dstblk_sz; uint32_t nb_hdrs = 0; int dst_full = 0; + int keep_first = ((flags & HTX_XFER_KEEP_FIRST) && htx_is_empty(dst)); /* Note: no test on the end of the message here. Blocks are added one by * one below, using the safe functions. So invalid additions in a message @@ -952,6 +960,10 @@ size_t htx_xfer(struct htx *dst, struct htx *src, size_t count, unsigned int fla break; } + /* The block was fully copied and is the tail block in */ + if (keep_first && htx_get_blk_pos(src, blk) == src->first) + dst->first = htx_get_tail(dst); + last_dstblk = NULL; /* Reset last_dstblk because it was fully copied */ last_dstblk_sz = 0; } @@ -1458,23 +1470,23 @@ struct buffer *__htx_xfer_to_large_buffer(struct buffer *dst, struct buffer *src /* Move HTX blocks from to . Relies on __htx_xfer_to_small_buffer() */ struct buffer *htx_move_to_small_buffer(struct buffer *dst, struct buffer *src) { - return __htx_xfer_to_small_buffer(dst, src, HTX_XFER_DEFAULT); + return __htx_xfer_to_small_buffer(dst, src, HTX_XFER_KEEP_FIRST); } /* Move HTX blocks from to . Relies on __htx_xfer_to_large_buffer() */ struct buffer *htx_move_to_large_buffer(struct buffer *dst, struct buffer *src) { - return __htx_xfer_to_large_buffer(dst, src, HTX_XFER_DEFAULT); + return __htx_xfer_to_large_buffer(dst, src, HTX_XFER_KEEP_FIRST); } /* Copy HTX blocks from to . Relies on __htx_xfer_to_small_buffer() */ struct buffer *htx_copy_to_small_buffer(struct buffer *dst, struct buffer *src) { - return __htx_xfer_to_small_buffer(dst, src, HTX_XFER_KEEP_SRC_BLKS); + return __htx_xfer_to_small_buffer(dst, src, HTX_XFER_KEEP_SRC_BLKS|HTX_XFER_KEEP_FIRST); } /* Copy HTX blocks from to . Relies on __htx_xfer_to_large_buffer() */ struct buffer *htx_copy_to_large_buffer(struct buffer *dst, struct buffer *src) { - return __htx_xfer_to_large_buffer(dst, src, HTX_XFER_KEEP_SRC_BLKS); + return __htx_xfer_to_large_buffer(dst, src, HTX_XFER_KEEP_SRC_BLKS|HTX_XFER_KEEP_FIRST); }