From 24ed835129e09fc547af2b77fefbee71ef20ab5d Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Thu, 22 Nov 2018 11:20:43 +0100 Subject: [PATCH] MINOR: htx: Add function to add an HTX block just before another one The function htx_add_data_before() can be used to add an HTX block before another one. For instance, it could be used to add some data before the end-of-message marker. --- include/proto/htx.h | 1 + src/htx.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/proto/htx.h b/include/proto/htx.h index 2f7aaf742..7db2059c2 100644 --- a/include/proto/htx.h +++ b/include/proto/htx.h @@ -57,6 +57,7 @@ struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type); struct htx_blk *htx_add_data(struct htx *htx, const struct ist data); struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist tlr); struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob); +struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref, const struct ist data); int htx_reqline_to_str(const union htx_sl *sl, struct buffer *chk); int htx_stline_to_str(const union htx_sl *sl, struct buffer *chk); diff --git a/src/htx.c b/src/htx.c index f04214318..0c9646739 100644 --- a/src/htx.c +++ b/src/htx.c @@ -788,6 +788,37 @@ struct htx_blk *htx_add_oob(struct htx *htx, const struct ist oob) return blk; } +struct htx_blk *htx_add_data_before(struct htx *htx, const struct htx_blk *ref, + const struct ist data) +{ + struct htx_blk *blk; + int32_t prev; + + /* FIXME: check data.len (< 256MB) */ + blk = htx_add_blk(htx, HTX_BLK_DATA, data.len); + if (!blk) + return NULL; + + blk->info += data.len; + memcpy(htx_get_blk_ptr(htx, blk), data.ptr, data.len); + + for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) { + struct htx_blk *pblk = htx_get_blk(htx, prev); + + /* Swap .addr and .info fields */ + blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr; + blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info; + + if (blk->addr == pblk->addr) + blk->addr += htx_get_blksz(pblk); + htx->front = prev; + + if (pblk == ref) + break; + blk = pblk; + } + return blk; +} /* Appends the string representation of the request line block to the * chunk . It returns 1 if data are successfully appended, otherwise it