From 549822f0a174483ecb681325595201bf81ce7f0a Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Mon, 25 Feb 2019 10:23:19 +0100 Subject: [PATCH] MINOR: htx: Add function to drain data from an HTX message The function htx_drain() can now be used to drain data from an HTX message. It will be used by other commits to fix bugs, so it must be backported to 1.9. --- include/common/htx.h | 1 + src/htx.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/common/htx.h b/include/common/htx.h index 79a9a1330..0eabf2400 100644 --- a/include/common/htx.h +++ b/include/common/htx.h @@ -171,6 +171,7 @@ struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk); struct htx_blk *htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz); struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk); void htx_truncate(struct htx *htx, uint32_t offset); +struct htx_ret htx_drain(struct htx *htx, uint32_t max); struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk, const struct ist old, const struct ist new); diff --git a/src/htx.c b/src/htx.c index 28374516d..892bbd6e5 100644 --- a/src/htx.c +++ b/src/htx.c @@ -307,6 +307,43 @@ void htx_truncate(struct htx *htx, uint32_t offset) blk = htx_remove_blk(htx, blk); } +/* Drain bytes from the HTX message . DATA blocks will be cut if + * necessary. Others blocks will be removed at once if is large + * enough. The function returns an htx_ret with the first block remaing in the + * messsage and the amount of data drained. If everything is removed, + * htx_ret.blk is set to NULL. + */ +struct htx_ret htx_drain(struct htx *htx, uint32_t count) +{ + struct htx_blk *blk; + struct htx_ret htxret = { .blk = NULL, .ret = 0 }; + + blk = htx_get_head_blk(htx); + while (count && blk) { + uint32_t sz = htx_get_blksz(blk); + enum htx_blk_type type = htx_get_blk_type(blk); + + /* Ingore unused block */ + if (type == HTX_BLK_UNUSED) + goto next; + + if (sz > count) { + if (type == HTX_BLK_DATA) { + htx_cut_data_blk(htx, blk, count); + htxret.ret += count; + } + break; + } + count -= sz; + htxret.ret += sz; + next: + blk = htx_remove_blk(htx, blk); + } + htxret.blk = blk; + + return htxret; +} + /* Tries to append data to the last inserted block, if the type matches and if * there is enough non-wrapping space. Only DATA and TRAILERS content can be * appended. If the append fails, a new block is inserted. If an error occurred,