]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: htx: preserve the first block when a message is moved to another buffer
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 3 Dec 2026 20:43:37 +0000 (21:43 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 4 Dec 2026 13:54:03 +0000 (14:54 +0100)
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.

include/haproxy/htx.h
src/htx.c

index 0d6bd22c6c1b188cfbe63f810460c2533c5daa8c..bc1e7826eea1beede5e7555bbd9330912c38c04f 100644 (file)
@@ -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 /* <count> don't include meta-data, only payload */
+#define HTX_XFER_KEEP_FIRST        0x00000010 /* Preserve the first block of <src> (htx->first) in <dst> */
 
 size_t htx_xfer(struct htx *dst, struct htx *src, size_t count, unsigned int flags);
 
index 6e6e41244034bfc9cbd48b02378f723ad6ff1741..9a5493cf349df1fdc44c46094fe99529d1b16195 100644 (file)
--- 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: <count> don't include meta-data, only payload
+ *  - HTX_XFER_KEEP_FIRST: the block in <dst> copied from the first block of
+ *                         <src> (htx->first) becomes the first block of <dst>.
+ *                         It is only supported if <dst> is empty, otherwise
+ *                         it is ignored. If the copy is rolled back, the
+ *                         first block of <dst> 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 <dst> */
+               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 <src> to <dst>. 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 <src> to <dst>. 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 <src> to <dst>. 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 <src> to <dst>. 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);
 }