]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: htx: Add a field to save the headers data size
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 22 Jun 2026 16:36:45 +0000 (18:36 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 8 Jul 2026 06:52:06 +0000 (08:52 +0200)
The size of headers present in an HTX message are now counted. A dedicated
field was added in the HTX structure to do so. This patch is quite simple
but it will be mandatory to be able to perform some tests on headers. One of
them is to be sure headers never exceed the regular buffer size, even when a
large buffer is used.

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

index 0a12ce200fdca7c350abcf2d489838009120b21c..0c484d1b7c9e21b1e75bd5b75fd5b48d68b921d8 100644 (file)
@@ -258,7 +258,7 @@ struct htx {
        uint32_t data;   /* the data size, in bytes. To known to total size used by all allocated
                          * blocks (blocks and their contents), you need to add size used by blocks,
                          * i.e. [ used * sizeof(struct htx_blk *) ] */
-
+       uint32_t hdrs_data;
        int32_t tail;   /* newest inserted block. -1 if the HTX message is empty */
        int32_t head;   /* oldest inserted block. -1 if the HTX message is empty */
        int32_t first;  /* position of the first block to (re)start the analyse. -1 if unset. */
@@ -269,8 +269,6 @@ struct htx {
 
        uint32_t flags;  /* HTX_FL_* */
 
-       /* XXX 4 bytes unused */
-
        /* Blocks representing the HTTP message itself */
        char blocks[VAR_ARRAY] ALIGNED(8);
 };
index 336db649f5f61c1a20aeb3c302a6fa4ad770d995..f6181908cd5d52df4c7eed0a69a1edd202c7db1c 100644 (file)
@@ -374,6 +374,8 @@ static inline void htx_change_blk_value_len(struct htx *htx, struct htx_blk *blk
 
        /* Update HTTP message */
        delta = (newlen - oldlen);
+       if (type < HTX_BLK_EOH)
+               htx->hdrs_data += delta;
        htx->data += delta;
        if (blk->addr+sz == htx->tail_addr)
                htx->tail_addr += delta;
@@ -687,7 +689,7 @@ static inline int htx_almost_full(const struct htx *htx)
 static inline void htx_reset(struct htx *htx)
 {
        htx->tail = htx->head  = htx->first = -1;
-       htx->data = 0;
+       htx->data = htx->hdrs_data = 0;
        htx->tail_addr = htx->head_addr = htx->end_addr = 0;
        htx->flags = HTX_FL_NONE;
 }
@@ -871,9 +873,9 @@ static inline void htx_dump(struct buffer *chunk, const struct htx *htx, int ful
 {
        int32_t pos;
 
-       chunk_appendf(chunk, " htx=%p(size=%u,data=%u,used=%u,wrap=%s,flags=0x%08x,"
+       chunk_appendf(chunk, " htx=%p(size=%u,data=%u,hdrs=%u,used=%u,wrap=%s,flags=0x%08x,"
                      "first=%d,head=%d,tail=%d,tail_addr=%d,head_addr=%d,end_addr=%d)",
-                     htx, htx->size, htx->data, htx_nbblks(htx), (!htx->head_addr) ? "NO" : "YES",
+                     htx, htx->size, htx->data, htx->hdrs_data, htx_nbblks(htx), (!htx->head_addr) ? "NO" : "YES",
                      htx->flags, htx->first, htx->head, htx->tail,
                      htx->tail_addr, htx->head_addr, htx->end_addr);
 
index cc0b6d1d4df3ef06ea77fbf211c470748475316c..132c2c1216d873ed7832ba55150527c3b0599be2 100644 (file)
--- a/src/htx.c
+++ b/src/htx.c
@@ -347,6 +347,8 @@ struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t bl
        BUG_ON(blk->addr > htx->size);
 
        blk->info = (type << 28);
+       if (type < HTX_BLK_EOH)
+               htx->hdrs_data += blksz;
        return blk;
 }
 
@@ -376,6 +378,8 @@ struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
        addr = blk->addr;
        if (type != HTX_BLK_UNUSED) {
                /* Mark the block as unused, decrement allocated size */
+               if (type < HTX_BLK_EOH)
+                       htx->hdrs_data -= sz;
                htx->data -= htx_get_blksz(blk);
                blk->info = ((uint32_t)HTX_BLK_UNUSED << 28);
        }
@@ -719,6 +723,10 @@ struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
                htx_memcpy(htx_get_blk_ptr(htx, blk), b_orig(chunk), b_data(chunk));
                free_trash_chunk(chunk);
        }
+
+       if (htx_get_blk_type(blk) < HTX_BLK_EOH)
+               htx->hdrs_data += delta;
+
        return blk;
 }
 
@@ -1030,6 +1038,8 @@ struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
                htx->data += delta;
        }
 
+       htx->hdrs_data += delta;
+
        /* Finally, copy data. */
        ptr = htx_get_blk_ptr(htx, blk);
        ist2bin_lc(ptr, name);
@@ -1079,6 +1089,8 @@ struct htx_sl *htx_replace_stline(struct htx *htx, struct htx_blk *blk, const st
                htx->data += delta;
        }
 
+       htx->hdrs_data += delta;
+
        /* Restore start-line info and flags and copy parts of the start-line */
        sl = htx_get_blk_ptr(htx, blk);
        sl->info = tmp.info;