From: Christopher Faulet Date: Mon, 22 Jun 2026 16:36:45 +0000 (+0200) Subject: MINOR: htx: Add a field to save the headers data size X-Git-Tag: v3.5-dev2~21 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/$%7BGITURL%7D/static/gitweb.js?a=commitdiff_plain;h=baec1a9c37a1fa28e769cafaf24cfc90be932f3c;p=haproxy.git MINOR: htx: Add a field to save the headers data size 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. --- diff --git a/include/haproxy/htx-t.h b/include/haproxy/htx-t.h index 0a12ce200..0c484d1b7 100644 --- a/include/haproxy/htx-t.h +++ b/include/haproxy/htx-t.h @@ -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); }; diff --git a/include/haproxy/htx.h b/include/haproxy/htx.h index 336db649f..f6181908c 100644 --- a/include/haproxy/htx.h +++ b/include/haproxy/htx.h @@ -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); diff --git a/src/htx.c b/src/htx.c index cc0b6d1d4..132c2c121 100644 --- 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;