]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: htx: Add the function htx_move_blk_before()
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 11 Jun 2019 08:41:19 +0000 (10:41 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Tue, 11 Jun 2019 12:05:25 +0000 (14:05 +0200)
The function htx_add_data_before() was removed because it was buggy. The
function htx_move_blk_before() may be used if necessary to do something
equivalent, except it just moves blocks. It doesn't handle the adding.

include/common/htx.h
src/htx.c

index df75e3397d27abb9d03341e73a099e5e8b1b39d1..e7d8b80f67a31569ba2d1d9aeefec28208be7870 100644 (file)
@@ -194,6 +194,7 @@ struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type);
 struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data);
 size_t htx_add_data(struct htx *htx, const struct ist data);
 struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data);
+void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref);
 
 int htx_reqline_to_h1(const struct htx_sl *sl, struct buffer *chk);
 int htx_stline_to_h1(const struct htx_sl *sl, struct buffer *chk);
index 24d3ed4c945c5aa6405c765434b630991bc3642c..e83271cb3c74aafa2508fb4c3c119f2fac01d6ef 100644 (file)
--- a/src/htx.c
+++ b/src/htx.c
@@ -1037,6 +1037,29 @@ struct htx_blk *htx_add_last_data(struct htx *htx, struct ist data)
        return blk;
 }
 
+/* Moves the block <blk> just before the block <ref>. Both blocks must be in the
+ * HTX message <htx> and <blk> must be placed after <ref>. pointer to these
+ * blocks are updated to remain valid after the move. */
+void htx_move_blk_before(struct htx *htx, struct htx_blk **blk, struct htx_blk **ref)
+{
+       struct htx_blk *cblk, *pblk;
+
+       cblk = *blk;
+       for (pblk = htx_get_prev_blk(htx, cblk); pblk; pblk = htx_get_prev_blk(htx, pblk)) {
+               /* Swap .addr and .info fields */
+               cblk->addr ^= pblk->addr; pblk->addr ^= cblk->addr; cblk->addr ^= pblk->addr;
+               cblk->info ^= pblk->info; pblk->info ^= cblk->info; cblk->info ^= pblk->info;
+
+               if (cblk->addr == pblk->addr)
+                       cblk->addr += htx_get_blksz(pblk);
+               if (pblk == *ref)
+                       break;
+               cblk = pblk;
+       }
+       *blk = cblk;
+       *ref = pblk;
+}
+
 /* Appends the H1 representation of the request line block <blk> to the
  * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
  * returns 0.