]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: htx: Add function to add an HTX block just before another one
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 22 Nov 2018 10:20:43 +0000 (11:20 +0100)
committerWilly Tarreau <w@1wt.eu>
Sat, 1 Dec 2018 16:37:27 +0000 (17:37 +0100)
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
src/htx.c

index 2f7aaf74274e30ee366e6c48a6a72a61787098ad..7db2059c272cd4a5d4f3b2e37bdadb510795ce38 100644 (file)
@@ -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);
index f042143185fa666348376679c931cbd5d340def2..0c9646739d67f55fa92bd51ac0338cf165436e2c 100644 (file)
--- 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 <blk> to the
  * chunk <chk>. It returns 1 if data are successfully appended, otherwise it