]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: htx: Add a function to return a block at a specific offset
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 24 Feb 2020 10:41:59 +0000 (11:41 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 6 Mar 2020 13:12:59 +0000 (14:12 +0100)
The htx_find_offset() function may be used to look for a block at a specific
offset in an HTX message, starting from the message head. A compound result is
returned, an htx_ret structure, with the found block and the position of the
offset in the block. If the offset is ouside of the HTX message, the returned
block is NULL.

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

index 11caa59bff4e17b2a9bacc55a39fcf8f131836a5..9818a3f4da7686d1546aa025bbfee94e6bc3e1ac 100644 (file)
@@ -230,6 +230,7 @@ extern struct htx htx_empty;
 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);
+struct htx_ret htx_find_offset(struct htx *htx, uint32_t offset);
 void htx_truncate(struct htx *htx, uint32_t offset);
 struct htx_ret htx_drain(struct htx *htx, uint32_t max);
 
index 41eb47274ecb035528e3ad9340b894c6d1670d8c..1ee60dcbdd23c904090408a397d583c343ac4ba3 100644 (file)
--- a/src/htx.c
+++ b/src/htx.c
@@ -391,6 +391,31 @@ struct htx_blk *htx_remove_blk(struct htx *htx, struct htx_blk *blk)
        return blk;
 }
 
+/* Looks for the HTX block containing the offset <offset>, starting at the HTX
+ * message's head. The function returns an htx_ret with the found HTX block and
+ * the position inside this block where the offset is. If the offset <offset> is
+ * ouside of the HTX message, htx_ret.blk is set to NULL.
+ */
+struct htx_ret htx_find_offset(struct htx *htx, uint32_t offset)
+{
+       struct htx_blk *blk;
+       struct htx_ret htxret = { .blk = NULL, .ret = 0 };
+
+       if (offset >= htx->data)
+               return htxret;
+
+       for (blk = htx_get_head_blk(htx); blk && offset; blk = htx_get_next_blk(htx, blk)) {
+               uint32_t sz = htx_get_blksz(blk);
+
+               if (offset < sz)
+                       break;
+               offset -= sz;
+       }
+       htxret.blk = blk;
+       htxret.ret = offset;
+       return htxret;
+}
+
 /* Removes all blocks after the one containing the offset <offset>. This last
  * one may be truncated if it is a DATA block.
  */